| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
""" |
| 11 |
This module adds shared functions for the FlashArray modules |
| 12 |
""" |
| 13 |
|
| 14 |
import platform |
| 15 |
import os |
| 16 |
import re |
| 17 |
|
| 18 |
from ansible.module_utils.common.process import get_bin_path |
| 19 |
from ansible.module_utils.facts.utils import get_file_content |
| 20 |
|
| 21 |
|
| 22 |
def _findstr(text, match): |
| 23 |
for line in text.splitlines(): |
| 24 |
if match in line: |
| 25 |
found = line |
| 26 |
return found |
| 27 |
|
| 28 |
|
| 29 |
def human_to_bytes(size): |
| 30 |
"""Given a human-readable byte string (e.g. 2G, 30M), |
| 31 |
return the number of bytes. Will return 0 if the argument has |
| 32 |
unexpected form. |
| 33 |
""" |
| 34 |
bytes = size[:-1] |
| 35 |
unit = size[-1].upper() |
| 36 |
if bytes.isdigit(): |
| 37 |
bytes = int(bytes) |
| 38 |
if unit == "P": |
| 39 |
bytes *= 1125899906842624 |
| 40 |
elif unit == "T": |
| 41 |
bytes *= 1099511627776 |
| 42 |
elif unit == "G": |
| 43 |
bytes *= 1073741824 |
| 44 |
elif unit == "M": |
| 45 |
bytes *= 1048576 |
| 46 |
elif unit == "K": |
| 47 |
bytes *= 1024 |
| 48 |
elif unit == "B": |
| 49 |
bytes *= 1 |
| 50 |
else: |
| 51 |
bytes = 0 |
| 52 |
else: |
| 53 |
bytes = 0 |
| 54 |
return bytes |
| 55 |
|
| 56 |
|
| 57 |
def human_to_real(iops): |
| 58 |
"""Given a human-readable string (e.g. 2K, 30M IOPs), |
| 59 |
return the real number. Will return 0 if the argument has |
| 60 |
unexpected form. |
| 61 |
""" |
| 62 |
digit = iops[:-1] |
| 63 |
unit = iops[-1].upper() |
| 64 |
if unit.isdigit(): |
| 65 |
digit = iops |
| 66 |
elif digit.isdigit(): |
| 67 |
digit = int(digit) |
| 68 |
if unit == "M": |
| 69 |
digit *= 1000000 |
| 70 |
elif unit == "K": |
| 71 |
digit *= 1000 |
| 72 |
else: |
| 73 |
digit = 0 |
| 74 |
else: |
| 75 |
digit = 0 |
| 76 |
return digit |
| 77 |
|
| 78 |
|
| 79 |
def convert_to_millisecs(hour): |
| 80 |
"""Convert a 12-hour clock to milliseconds from |
| 81 |
midnight""" |
| 82 |
if hour[-2:].upper() == "AM" and hour[:2] == "12": |
| 83 |
return 0 |
| 84 |
elif hour[-2:].upper() == "AM": |
| 85 |
return int(hour[:-2]) * 3600000 |
| 86 |
elif hour[-2:].upper() == "PM" and hour[:2] == "12": |
| 87 |
return 43200000 |
| 88 |
return (int(hour[:-2]) + 12) * 3600000 |
| 89 |
|
| 90 |
|
| 91 |
def convert_time_to_millisecs(time): |
| 92 |
"""Convert a time period in milliseconds""" |
| 93 |
if time[-1:].lower() not in ["w", "d", "h", "m", "s"]: |
| 94 |
return 0 |
| 95 |
try: |
| 96 |
if time[-1:].lower() == "w": |
| 97 |
return int(time[:-1]) * 7 * 86400000 |
| 98 |
elif time[-1:].lower() == "d": |
| 99 |
return int(time[:-1]) * 86400000 |
| 100 |
elif time[-1:].lower() == "h": |
| 101 |
return int(time[:-1]) * 3600000 |
| 102 |
elif time[-1:].lower() == "m": |
| 103 |
return int(time[:-1]) * 60000 |
| 104 |
elif time[-1:].lower() == "s": |
| 105 |
return int(time[:-1]) * 1000 |
| 106 |
except Exception: |
| 107 |
return 0 |
| 108 |
|
| 109 |
|
| 110 |
def get_local_tz(module, timezone="UTC"): |
| 111 |
""" |
| 112 |
We will attempt to get the local timezone of the server running the module and use that. |
| 113 |
If we can't get the timezone then we will set the default to be UTC |
| 114 |
|
| 115 |
Linnux has been tested and other opersting systems should be OK. |
| 116 |
Failures cause assumption of UTC |
| 117 |
|
| 118 |
Windows is not supported and will assume UTC |
| 119 |
""" |
| 120 |
if platform.system() == "Linux": |
| 121 |
timedatectl = get_bin_path("timedatectl") |
| 122 |
if timedatectl is not None: |
| 123 |
rcode, stdout, stderr = module.run_command(timedatectl) |
| 124 |
if rcode == 0 and stdout: |
| 125 |
line = _findstr(stdout, "Time zone") |
| 126 |
full_tz = line.split(":", 1)[1].rstrip() |
| 127 |
timezone = full_tz.split()[0] |
| 128 |
return timezone |
| 129 |
else: |
| 130 |
module.warn("Incorrect timedatectl output. Timezone will be set to UTC") |
| 131 |
else: |
| 132 |
if os.path.exists("/etc/timezone"): |
| 133 |
timezone = get_file_content("/etc/timezone") |
| 134 |
else: |
| 135 |
module.warn("Could not find /etc/timezone. Assuming UTC") |
| 136 |
|
| 137 |
elif platform.system() == "SunOS": |
| 138 |
if os.path.exists("/etc/default/init"): |
| 139 |
for line in get_file_content("/etc/default/init", "").splitlines(): |
| 140 |
if line.startswith("TZ="): |
| 141 |
timezone = line.split("=", 1)[1] |
| 142 |
return timezone |
| 143 |
else: |
| 144 |
module.warn("Could not find /etc/default/init. Assuming UTC") |
| 145 |
|
| 146 |
elif re.match("^Darwin", platform.platform()): |
| 147 |
systemsetup = get_bin_path("systemsetup") |
| 148 |
if systemsetup is not None: |
| 149 |
rcode, stdout, stderr = module.execute(systemsetup, "-gettimezone") |
| 150 |
if rcode == 0 and stdout: |
| 151 |
timezone = stdout.split(":", 1)[1].lstrip() |
| 152 |
else: |
| 153 |
module.warn("Could not run systemsetup. Assuming UTC") |
| 154 |
else: |
| 155 |
module.warn("Could not find systemsetup. Assuming UTC") |
| 156 |
|
| 157 |
elif re.match("^(Free|Net|Open)BSD", platform.platform()): |
| 158 |
if os.path.exists("/etc/timezone"): |
| 159 |
timezone = get_file_content("/etc/timezone") |
| 160 |
else: |
| 161 |
module.warn("Could not find /etc/timezone. Assuming UTC") |
| 162 |
|
| 163 |
elif platform.system() == "AIX": |
| 164 |
aix_oslevel = int(platform.version() + platform.release()) |
| 165 |
if aix_oslevel >= 61: |
| 166 |
if os.path.exists("/etc/environment"): |
| 167 |
for line in get_file_content("/etc/environment", "").splitlines(): |
| 168 |
if line.startswith("TZ="): |
| 169 |
timezone = line.split("=", 1)[1] |
| 170 |
return timezone |
| 171 |
else: |
| 172 |
module.warn("Could not find /etc/environment. Assuming UTC") |
| 173 |
else: |
| 174 |
module.warn( |
| 175 |
"Cannot determine timezone when AIX os level < 61. Assuming UTC" |
| 176 |
) |
| 177 |
else: |
| 178 |
module.warn("Could not find /etc/timezone. Assuming UTC") |
| 179 |
return timezone |