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 13, 2018
1 parent
9e17453
commit 80d3543
Showing
2 changed files
with
159 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://localhost:8080/ovirt-engine/api | ||
username=admin@internal | ||
password=12 | ||
ca=/etc/pki/ovirt-engine/ca.pem | ||
output_file=/var/lib/ovirt-ansible-disaster-recovery/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,153 @@ | ||
#!/usr/bin/python | ||
try: | ||
import configparser | ||
except ImportError: | ||
import ConfigParser as configparser | ||
import os.path | ||
import shlex | ||
import subprocess | ||
import sys | ||
|
||
from subprocess import call | ||
|
||
|
||
class GenerateMappingFile(): | ||
prefix = "[Generate Mapping File] " | ||
ansible_play = "../examples/dr_generate_var.yml" | ||
dr_tag = "generate_mapping" | ||
dr_conf_file = "dr.conf" | ||
|
||
def run(self): | ||
print("\n%sStart Generate Variable Mapping File " | ||
"For oVirt Ansible Disaster Recovery" % self.prefix) | ||
site, username, password, ca, output_file = self._init_vars() | ||
print("\n%ssite address: %s \n" | ||
"%susername: %s \n" | ||
"%spassword: \n" | ||
"%sca file location: %s \n" | ||
"%soutput file location: %s \n" | ||
% (self.prefix, | ||
site, | ||
self.prefix, | ||
username, | ||
self.prefix, | ||
self.prefix, | ||
ca, | ||
self.prefix, | ||
output_file)) | ||
|
||
external_vars = "site={} username={} password={} ca={} var_file={}" \ | ||
.format(site, username, password, ca, output_file) | ||
command = "ansible-playbook {} -t {} -e '{}'" \ | ||
.format(self.ansible_play, self.dr_tag, external_vars) | ||
call(command, shell=True) | ||
print("\n%sFinished generating variable mapping file " | ||
"for oVirt ansible disaster recovery" % self.prefix) | ||
print("\n%sVar file is: '%s'" % (self.prefix, output_file)) | ||
|
||
def _validate_output_file_exists(self, fname): | ||
_dir = os.path.dirname(fname) | ||
if not os.path.exists(_dir): | ||
print("%sPath %s does not exists. Create folder" | ||
% (self.prefix, _dir)) | ||
os.makedirs(_dir) | ||
if os.path.isfile(fname): | ||
valid = {"yes": True, "y": True, "ye": True, | ||
"no": False, "n": False} | ||
ans = raw_input(self.prefix + "The output file '" + fname + | ||
"' already exists, " | ||
"would you like to override it (y,n)? ") | ||
ans.lower | ||
while True: | ||
if ans in valid: | ||
if not valid[ans]: | ||
print("\n\n%sExit procedure. " | ||
"file could not be overriden.\n" | ||
% (self.prefix)) | ||
sys.exit(0) | ||
break | ||
else: | ||
ans = raw_input("%sPlease respond with 'yes' or 'no' " | ||
"(or 'y' or 'n'): " % (self.prefix)) | ||
try: | ||
os.remove(fname) | ||
except OSError: | ||
print("\n\n%sFile %s could not be replaced.\n" | ||
% (self.prefix, fname)) | ||
sys.exit(0) | ||
|
||
def _init_vars(self): | ||
site, username, password, ca, output_file = '', '', '', '', '' | ||
settings = configparser.ConfigParser() | ||
settings._interpolation = configparser.ExtendedInterpolation() | ||
settings.read(self.dr_conf_file) | ||
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. Please 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): | ||
username = raw_input(self.prefix + "USERNAME is not initialized. " | ||
"Please provide username" | ||
" (admin@internal) : ") or "admin@internal" | ||
while (not password): | ||
password = raw_input(self.prefix + "PASSWORD is " | ||
"not initialized. Please provide the " | ||
"password for username '" + username + "': ") | ||
while (not ca): | ||
ca = raw_input(self.prefix + "CA file location is not " | ||
"initialized. " | ||
"Please provide the ca file " | ||
"location (/etc/pki/ovirt-engine/ca.pem) : ") \ | ||
or "/etc/pki/ovirt-engine/ca.pem" | ||
while (not output_file): | ||
default_file = "/var/lib/ovirt-ansible-disaster-" \ | ||
"recovery/mapping_vars.yml" | ||
output_file = raw_input(self.prefix + "OUTPUT file is not " | ||
"initialized. Please provide the " | ||
"output file location for the " | ||
"mapping var file (" + default_file + ")" | ||
": ") or default_file | ||
self._validate_output_file_exists(output_file) | ||
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() |