|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2024 Cloudera, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import configparser |
| 20 | + |
| 21 | +from ansible.module_utils.common.dict_transformations import recursive_diff |
| 22 | +from ansible.module_utils.common.text.converters import to_native |
| 23 | +from ansible.module_utils.basic import AnsibleModule |
| 24 | + |
| 25 | + |
| 26 | +class AnsibleModuleError(Exception): |
| 27 | + def __init__(self, results): |
| 28 | + self.results = results |
| 29 | + |
| 30 | + |
| 31 | +def main(): |
| 32 | + module = AnsibleModule( |
| 33 | + argument_spec=dict( |
| 34 | + config=dict(default="/etc/cloudera-scm-server/db.properties"), |
| 35 | + script=dict(required=True), |
| 36 | + type=dict(required=True, choices=["postgresql", "oracle", "mysql"]), |
| 37 | + host=dict(required=True), |
| 38 | + port=dict(required=True, type="int"), |
| 39 | + database=dict(required=True), |
| 40 | + username=dict(required=True), |
| 41 | + password=dict(required=True, no_log=True), |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + # Handle parameters |
| 46 | + config = module.params["config"] |
| 47 | + script = module.params["script"] |
| 48 | + type = module.params["type"] |
| 49 | + host = module.params["host"] |
| 50 | + port = module.params["port"] |
| 51 | + database = module.params["database"] |
| 52 | + username = module.params["username"] |
| 53 | + password = module.params["password"] |
| 54 | + |
| 55 | + # Run precheck |
| 56 | + try: |
| 57 | + with open(config, "r") as f: |
| 58 | + contents = "[SCM]\n" + f.read() |
| 59 | + existing = configparser.ConfigParser() |
| 60 | + existing.optionxform = str |
| 61 | + existing.read_string(contents) |
| 62 | + except Exception as e: |
| 63 | + module.fail_json( |
| 64 | + msg="Error parsing Cloudera Manager db.properties", |
| 65 | + error=to_native(e), |
| 66 | + ) |
| 67 | + |
| 68 | + incoming = { |
| 69 | + "com.cloudera.cmf.db.setupType": "EXTERNAL", |
| 70 | + "com.cloudera.cmf.db.type": type, |
| 71 | + "com.cloudera.cmf.db.host": host + ":" + str(port), |
| 72 | + "com.cloudera.cmf.db.name": database, |
| 73 | + "com.cloudera.cmf.db.user": username, |
| 74 | + "com.cloudera.cmf.db.password": password, |
| 75 | + } |
| 76 | + |
| 77 | + diff = recursive_diff(dict(existing["SCM"]), incoming) |
| 78 | + |
| 79 | + # Execute system command |
| 80 | + if diff is not None: |
| 81 | + args = f"{script} --host {host} --port {port} {type} {database} {username} {password}" |
| 82 | + |
| 83 | + (rc, stdout, stderr) = module.run_command(args, use_unsafe_shell=False) |
| 84 | + |
| 85 | + if rc != 0: |
| 86 | + module.fail_json( |
| 87 | + msg="Error preparing database.", |
| 88 | + rc=rc, |
| 89 | + stderr=stderr, |
| 90 | + stdout=stdout, |
| 91 | + stderr_lines=str(stderr).splitlines(), |
| 92 | + stdout_lines=str(stdout).splitlines(), |
| 93 | + ) |
| 94 | + else: |
| 95 | + module.exit_json( |
| 96 | + changed=True, |
| 97 | + msg="Database preparation succeeded.", |
| 98 | + stdout=stdout, |
| 99 | + stdout_lines=str(stdout).splitlines(), |
| 100 | + ) |
| 101 | + else: |
| 102 | + module.exit_json(changed=False) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments