From ac9c0b791beac615761582a2bf9e8541239dd695 Mon Sep 17 00:00:00 2001 From: Maor Lipchuk Date: Tue, 6 Mar 2018 12:56:09 +0200 Subject: [PATCH] Add python scripts for generate_mapping failover and failback --- files/dr.conf | 6 +++ files/generate_vars.py | 115 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 files/dr.conf create mode 100755 files/generate_vars.py diff --git a/files/dr.conf b/files/dr.conf new file mode 100644 index 0000000..b0e6d07 --- /dev/null +++ b/files/dr.conf @@ -0,0 +1,6 @@ +[generate_vars] +site=http://10.35.0.140:8080/ovirt-engine/api +username=admin@internal +#password= +ca=/tmp/ca.pem +output_file=mapping_vars.yml diff --git a/files/generate_vars.py b/files/generate_vars.py new file mode 100755 index 0000000..9f0b877 --- /dev/null +++ b/files/generate_vars.py @@ -0,0 +1,115 @@ +#!/usr/bin/python +import configparser +import subprocess +import shlex + + +class GenerateMappingFile(): + prefix = "[Generate Mapping File] " + + def run(self): + print("\n%sStart Generate Varialbe Mapping File " + "For oVirt Ansible Disaster Recovery\n" % self.prefix) + site, username, password, ca, output_file = self._init_vars() + print("%ssite address: %s \n" + "%susername: %s \n" + "%spassword: %s \n" + "%sca file location: %s \n" + "%soutput file location: %s \n" + % (self.prefix, + site, + self.prefix, + username, + self.prefix, + password, + self.prefix, + ca, + self.prefix, + output_file)) + + ansible_play = "../examples/dr_generate_var.yml" + dr_tag = "generate_mapping" + external_vars = "site={} username={} password={} ca={} var_file={}" \ + .format(site, username, password, ca, output_file) + command = "ansible-playbook {} -t {} -e '{}'" \ + .format(ansible_play, dr_tag, external_vars) + process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, + stdout=subprocess.PIPE) + out, err = process.communicate(command) + print(out) + + def _init_vars(self): + site, username, password, ca, output_file = '', '', '', '', '' + settings = configparser.ConfigParser() + settings._interpolation = configparser.ExtendedInterpolation() + settings.read('dr.conf') + site = settings.get('generate_vars', 'site', + vars=DefaultOption(settings, + 'generate_vars', + site=None)) + username = settings.get('generate_vars', 'username', + vars=DefaultOption(settings, + 'generate_vars', + username=None)) + password = settings.get('generate_vars', 'password', + vars=DefaultOption(settings, + 'generate_vars', + password=None)) + ca = settings.get('generate_vars', 'ca', + vars=DefaultOption(settings, + 'generate_vars', + ca=None)) + output_file = settings.get('generate_vars', 'output_file', + vars=DefaultOption(settings, + 'generate_vars', + output_file=None)) + if (not site): + site = raw_input(self.prefix + "SITE address is not " + "initialized.\nPlease provide a site URL " + "(http://127.0.0.1:8080/ovirt-engine/api) : ") \ + or "http://127.0.0.1:8080/ovirt-engine/api" + if (not username): + print("\n") + username = raw_input(self.prefix + "USERNAME is not initialized.\n" + "Please provide username" + " (admin@internal) : ") or "admin@internal" + while (not password): + print("\n") + password = raw_input(self.prefix + "PASSWORD is " + "not initialized.\nPlease provide the " + "password for username '" + username + "': ") + while (not ca): + print("\n") + ca = raw_input(self.prefix + "CA file location is not " + "initialized.\n" + "Please provide the ca file location: ") + while (not output_file): + default_file = "../files/disaster_recovery_vars.yml" + print("\n") + output_file = raw_input(self.prefix + "OUPTUT file is not " + "initialized.\nPlease provide the " + "output file location for the generated " + "mapping var file: ") or default_file + print("\n") + return (site, username, password, ca, output_file) + + +class DefaultOption(dict): + + def __init__(self, config, section, **kv): + self._config = config + self._section = section + dict.__init__(self, **kv) + + def items(self): + _items = [] + for option in self: + if not self._config.has_option(self._section, option): + _items.append((option, self[option])) + else: + value_in_config = self._config.get(self._section, option) + _items.append((option, value_in_config)) + return _items + +if __name__ == "__main__": + GenerateMappingFile().run()