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