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 validator script for mapping file
- Loading branch information
Maor Lipchuk
committed
Mar 15, 2018
1 parent
6d269da
commit dc50bd3
Showing
2 changed files
with
169 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
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,165 @@ | ||
#!/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 ValidateMappingFile(): | ||
|
||
prefix = "[Validate Mapping File] " | ||
dr_conf_file = "dr.conf" | ||
def_var_file = "/var/lib/ovirt-ansible-disaster-" \ | ||
"recovery/mapping_vars.yml" | ||
storages = '' | ||
clusters = '' | ||
affinity_groups = '' | ||
affinity_labels = '' | ||
domains= '' | ||
roles = '' | ||
networks = '' | ||
luns = '' | ||
keys = ['storages', | ||
'clusters', | ||
'affnity_groups', | ||
'affinity_labels', | ||
'domains', | ||
'roles', | ||
'networks', | ||
'luns'] | ||
scope_list = {key: [] for key in keys} | ||
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%sOutput file location: %s \n" | ||
% (self.prefix, | ||
output_file)) | ||
|
||
external_vars = "site={} username={} password={} ca={} var_file={}" \ | ||
.format(site, username, password, ca, output_file) | ||
self._read_file(output_file) | ||
print("\n%sFinished validating variable mapping file " | ||
"for oVirt ansible disaster recovery" % self.prefix) | ||
|
||
def _read_file(self, fname): | ||
_file_ = open(fname, 'r') | ||
content = _file_.read() | ||
paths = content.split("\n") #split it into lines | ||
path_list = [] | ||
path_dict = {} | ||
scope = 'root' | ||
print paths | ||
for path in paths: | ||
if (path == 'dr_import_storages:'): | ||
scope = 'storages' | ||
elif (path == 'dr_cluster_mappings:'): | ||
scope = 'clusters' | ||
elif (path == 'dr_affinity_group_mappings:'): | ||
scope = 'affinity_groups' | ||
elif (path == 'dr_affinity_label_mappings:'): | ||
scope = 'affinity_labels' | ||
elif (path == 'dr_domain_mappings:'): | ||
scope = 'domains' | ||
elif (path == 'dr_role_mappings:'): | ||
scope = 'role' | ||
elif (path == 'dr_network_mappings:'): | ||
scope = 'networks' | ||
elif (path == 'dr_lun_mappings:'): | ||
scope = 'luns' | ||
|
||
print path | ||
if (scope != 'root'): | ||
if path.startswith('- '): | ||
obj = {} | ||
print "MAORMAOR" | ||
print path | ||
path = path[2:] | ||
print path | ||
print "MAORMAOR" | ||
while (not path.startswith('- ') and path != '' | ||
and not path.startswith('--') | ||
and not path.startswith('#') | ||
and not any(scope_path == path for scope_path | ||
in ['dr_import_storages:', | ||
'dr_cluster_mappings:', | ||
'dr_affinity_group_mappings:', | ||
'dr_affinity_label_mappings:', | ||
'dr_domain_mappings:', | ||
'dr_role_mappings:', | ||
'dr_network_mappings:', | ||
'dr_lun_mappings:'])): | ||
print path | ||
p = path.split(": ") | ||
if len(p) == 2: | ||
path_list.append(p[1].lstrip()) | ||
obj[p[0].lstrip()] = p[1].lstrip() | ||
else: | ||
print ("%s[ERROR] %s is not a legal path in var file" | ||
% (self.prefix, p)) | ||
exit | ||
iter_ = iter(paths) | ||
path = iter_.next() | ||
self.scope_list[scope].append(obj) | ||
print "MAOR" | ||
print self.scope_list | ||
continue | ||
|
||
|
||
p = path.split(": ") | ||
print p | ||
if len(p) == 2: | ||
print p[1] | ||
path_list.append(p[1].lstrip()) | ||
path_dict[p[0].lstrip()] = p[1].lstrip() | ||
path_dict.update(self.scope_list) | ||
print path_dict | ||
|
||
def _validate_output_file_exists(self, fname): | ||
_dir = os.path.dirname(fname) | ||
if not os.path.isfile(fname): | ||
print("\n\n%s[Failure] File %s does not exist.\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) | ||
var_file = settings.get('validate_vars', 'var_file', | ||
vars=DefaultOption(settings, | ||
'validate_vars', | ||
site=self.def_var_file)) | ||
while (not var_file): | ||
var_file = raw_input(self.prefix + "var file is not initialized. " | ||
"Please provide the location of the var file " | ||
"(" + self.def_var_file + ")" \ | ||
or def_var_file) | ||
self._validate_output_file_exists(var_file) | ||
return (site, username, password, ca, var_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__": | ||
ValidateMappingFile().run() |