This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add python scripts for generate_mapping failover and failback
- Loading branch information
Maor Lipchuk
committed
Mar 7, 2018
1 parent
5805e7f
commit ac9c0b7
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |