| 1 |
from __future__ import absolute_import, division, print_function |
| 2 |
__metaclass__ = type |
| 3 |
|
| 4 |
import re |
| 5 |
import json |
| 6 |
import os |
| 7 |
|
| 8 |
try: |
| 9 |
from shlex import quote |
| 10 |
except ImportError: |
| 11 |
from pipes import quote |
| 12 |
|
| 13 |
|
| 14 |
def escape_param(param): |
| 15 |
''' |
| 16 |
Escapes the given parameter |
| 17 |
@param - The parameter to escape |
| 18 |
''' |
| 19 |
return quote(param) |
| 20 |
|
| 21 |
|
| 22 |
def add_arg_to_cmd(cmd_list, param_name, param_value, is_bool=False, omit=None): |
| 23 |
""" |
| 24 |
@cmd_list - List of cmd args. |
| 25 |
@param_name - Param name / flag. |
| 26 |
@param_value - Value of the parameter. |
| 27 |
@is_bool - Flag is a boolean and has no value. |
| 28 |
@omit - List of parameter to omit from the command line. |
| 29 |
""" |
| 30 |
if param_name.replace('-', '') not in omit: |
| 31 |
if is_bool is False and param_value is not None: |
| 32 |
cmd_list.append(param_name) |
| 33 |
if param_name == "--eval": |
| 34 |
cmd_list.append("{0}".format(escape_param(param_value))) |
| 35 |
else: |
| 36 |
cmd_list.append(param_value) |
| 37 |
elif is_bool is True: |
| 38 |
cmd_list.append(param_name) |
| 39 |
return cmd_list |
| 40 |
|
| 41 |
|
| 42 |
def extract_json_document(output): |
| 43 |
""" |
| 44 |
This is for specific type of mongo shell return data in the format SomeText() |
| 45 |
https://github.com/ansible-collections/community.mongodb/issues/436 |
| 46 |
i.e. |
| 47 |
|
| 48 |
""" |
| 49 |
output = output.strip() |
| 50 |
if re.match(r"^[a-zA-Z].*\(", output) and output.endswith(')'): |
| 51 |
first_bracket = output.find('{') |
| 52 |
last_bracket = output.rfind('}') |
| 53 |
if first_bracket > 0 and last_bracket > 0: |
| 54 |
tmp = output[first_bracket:last_bracket + 1] |
| 55 |
tmp = tmp.replace('\n', '') |
| 56 |
tmp = tmp.replace('\t', '') |
| 57 |
if tmp is not None: |
| 58 |
output = tmp |
| 59 |
return output |
| 60 |
|
| 61 |
|
| 62 |
def transform_output(output, transform_type, split_char): |
| 63 |
output = extract_json_document(output) |
| 64 |
if transform_type == "auto": # determine what transform_type to perform |
| 65 |
if output.strip().startswith("{") or output.strip().startswith("["): |
| 66 |
transform_type = "json" |
| 67 |
elif isinstance(output.strip().split(None), list): # Splits on whitespace |
| 68 |
transform_type = "split" |
| 69 |
split_char = None |
| 70 |
elif isinstance(output.strip().split(","), list): |
| 71 |
transform_type = "split" |
| 72 |
split_char = "," |
| 73 |
elif isinstance(output.strip().split(" "), list): |
| 74 |
transform_type = "split" |
| 75 |
split_char = " " |
| 76 |
elif isinstance(output.strip().split("|"), list): |
| 77 |
transform_type = "split" |
| 78 |
split_char = "|" |
| 79 |
elif isinstance(output.strip().split("\t"), list): |
| 80 |
transform_type = "split" |
| 81 |
split_char = "\t" |
| 82 |
else: |
| 83 |
transform_type = "raw" |
| 84 |
if transform_type == "json": |
| 85 |
try: |
| 86 |
output = json.loads(output) |
| 87 |
except json.decoder.JSONDecodeError: |
| 88 |
# Strip Extended JSON stuff like: |
| 89 |
# "_id": ObjectId("58f56171ee9d4bd5e610d6b7"), |
| 90 |
# "count": NumberLong(999), |
| 91 |
output = re.sub(r'\:\s*\S+\s*\(\s*(\S+)\s*\)', r':\1', output) |
| 92 |
try: |
| 93 |
output = json.dumps(output, separators=(',', ':')) |
| 94 |
doc = json.loads(output) |
| 95 |
except json.decoder.JSONDecodeError as excep: |
| 96 |
raise excep |
| 97 |
elif transform_type == "split": |
| 98 |
output = output.strip().split(split_char) |
| 99 |
elif transform_type == "raw": |
| 100 |
output = output.strip() |
| 101 |
return output |
| 102 |
|
| 103 |
|
| 104 |
def get_hash_value(module): |
| 105 |
''' |
| 106 |
Returns the hash value of either the provided file or eval command |
| 107 |
''' |
| 108 |
hash_value = None |
| 109 |
try: |
| 110 |
import hashlib |
| 111 |
except ImportError as excep: |
| 112 |
module.fail_json(msg="Unable to import hashlib: {0}".format(excep.message)) |
| 113 |
if module.params['file'] is not None: |
| 114 |
hash_value = hashlib.md5(module.params['file'].encode('utf-8')).hexdigest() |
| 115 |
else: |
| 116 |
hash_value = hashlib.md5(module.params['eval'].encode('utf-8')).hexdigest() |
| 117 |
return hash_value |
| 118 |
|
| 119 |
|
| 120 |
def touch(fname, times=None): |
| 121 |
with open(fname, 'a'): |
| 122 |
os.utime(fname, times) |
| 123 |
|
| 124 |
|
| 125 |
def detect_if_cmd_exist(cmd="mongosh"): |
| 126 |
path = os.getenv('PATH') |
| 127 |
for folder in path.split(os.path.pathsep): |
| 128 |
mongoCmd = os.path.join(folder, cmd) |
| 129 |
if os.path.exists(mongoCmd) and os.access(mongoCmd, os.X_OK): |
| 130 |
return True |
| 131 |
return False |