def get_structure(): choices = [] seen_groups = set() for i in self.hosttable.all(): seen_groups.add( (i['group'], i['group_checked'], i['group_uuid'])) seen_groups = sorted(seen_groups, key=lambda x: x[0]) for group, group_checked, group_uuid in seen_groups: choices.append( gutils.color_pick(self.gConfig, "group: " + group, group_checked) + gutils.color_uuid(" id: " + group_uuid)) for host in sorted(self.searchGroupName(group), key=lambda x: x['hostname']): choices.append( gutils.color_pick(self.gConfig, "\t--host: " + host["hostname"], host["host_checked"]) + gutils.color_uuid(" id: " + host['host_uuid'])) if collapse: for install in host["installations"]: if install['installation_name'] != '__NA__': choices.append( gutils.color_pick( self.gConfig, "\t\t--install: " + install["installation_name"], install['installation_checked']) + gutils.color_uuid( " id: " + install['installation_uuid'])) for instance in install["instances"]: if instance['instance_name'] != "__NA__": choices.append( gutils.color_pick( self.gConfig, "\t\t\t--instance: " + instance["instance_name"], instance["instance_checked"]) + gutils.color_uuid( " id: " + instance['instance_uuid'])) for db in instance["databases"]: if db['db_name'] != "__NA__": choices.append( gutils.color_pick( self.gConfig, "\t\t\t\t--db: " + db["db_name"], db["db_checked"]) + gutils.color_uuid( " id: " + db['db_uuid'])) #todo remove choices.append("STOP") return choices
def analyze(condition, result, verify=False): msg = condition.get('msg', None) expect = condition.get('expect', False) failsolve = condition.get('failsolve', [None]) output = result[0].decode() self.gLogging.info("phrases to check: %s" % ",".join(msg)) self.gLogging.info("expected: %s" % expect) if verify is False: self.gLogging.info("trying to fix with: %s" % failsolve) if any(msg in output for msg in msg): if expect is True: self.gLogging.info( gutils.color_pick(self.gConfig, "condition passed", self.gConfig['JSON']['pick_yes'])) return True else: if verify is False: for fixstep in failsolve: self.gLogging.info("running fix step: %s" % fixstep) runUuid(result[6], fixstep) if stopping: self.gLogging.show(" ") self.gLogging.show("--- fixes applied.. ---") self.gLogging.show("--- press Enter to continue.. ---") self.gLogging.show(" ") input() else: pass return False else: if expect is True: if verify is False: for fixstep in failsolve: self.gLogging.info("running fix step (expect): %s" % fixstep) runUuid(result[6], fixstep) if stopping: self.gLogging.show(" ") self.gLogging.show("--- fixes applied.. ---") self.gLogging.show("--- press Enter to continue.. ---") self.gLogging.show(" ") input() else: pass return False else: self.gLogging.info( gutils.color_pick(self.gConfig, "condition passed", self.gConfig['JSON']['pick_yes'])) return True
def __init__(self, config, logger, hostfile=None): """ Construct object Args: config (object): holds instance of GcConfig class logger (object): holds instance of GcLogging class hostfile (str): json file with definitions """ # enable logging and config, add some utils, create a proper objects self.gLogging = logger self.gConfig = config.config #used by _indexHosts, list of 5element tuples contain uuids from every level self.hosts_idx = [] #used to drill down hierarchy of objects such as groups, hosts.. self.pick_drill = "" # if hostfile not provided, use default one from config file # start TinyDB object try: if hostfile is None: hostfile = "{}/{}".format(gutils.gcpath(), self.gConfig['JSON']['hostfile']) self.hostfile = TinyDB(hostfile) self.hosttable = self.hostfile.table("HOSTS") else: self.hostfile = TinyDB(hostfile) self.hosttable = self.hostfile.table("HOSTS") #refresh uuids self._indexHosts() except Exception as e: self.gLogging.critical("hosts definitions cannot be loaded")
def __init__(self, config, logger, credfile=None): """ Construct object Args: config (object): holds instance of GcConfig class logger (object): holds instance of GcLogging class credfile (str): json file with definitions """ # enable logging and config, create a proper objects self.gLogging = logger self.gConfig = config.config #if credfile not provided, use default one from config file #start TinyDB object try: if credfile is None: credfile = "{}/{}".format(gutils.gcpath(), self.gConfig['JSON']['credfile']) self.credfile = TinyDB(credfile) self.credtable = self.credfile.table("CREDS") else: self.credfile = TinyDB(credfile) self.credfile = self.credfile.table("CREDS") except Exception: self.gLogging.critical("credentials definitions cannot be loaded")
def __init__(self, config, logger, command): """ Construct object Args: config (object): holds instance of GcConfig class logger (object): holds instance of GcLogging class command (object): holds instance of GcCommands class """ cmd.Cmd.doc_header = "GlobalConsole: type <help> to see this message" cmd.Cmd.undoc_header = "Commands: type <command> -h to get help (+h for db2 command)" cmd.Cmd.__init__(self) #initialize required objects self.gLogging = logger self.gConfig = config.config self.gCommand = command self.gLogging.info("Session started.") #set cmd module environment self.epilog = "" try: if self.gConfig['CMD']['print_header'] == "YES": self.header() except Exception: self.gLogging.warning("not supported terminal, you may experience a strange behaviour") #check env variable try: len(os.environ[self.gConfig['JSON']['decrypt_key_os_var']]) except Exception: self.gLogging.warning("ENV VARIABLE: {} is not set! this may cause problems if password encryption is or will be enabled! Exit program and run: export GC_KEY=<YourPassword>".format(self.gConfig['JSON']['decrypt_key_os_var'])) #initialize history file try: if os.path.exists("{}/{}".format(gutils.gcpath(), self.gConfig['CMD']['histfile'])) is False: with open("{}/{}".format(gutils.gcpath(), self.gConfig['CMD']['histfile']), "w") as f: f.write("") readline.read_history_file("{}/{}".format(gutils.gcpath(), self.gConfig['CMD']['histfile'])) # default history len is -1 (infinite), which may grow unruly readline.set_history_length(int(self.gConfig['CMD']['histlen'])) except Exception: self.gLogging.error("cannot load history file")
def do_exit(self, args): """ This method saves command history and exit program """ self.gLogging.debug("do_exit invoked") atexit.register(readline.write_history_file, "{}/{}".format(gutils.gcpath(), self.gConfig['CMD']['histfile'])) self.gCommand.exit()
def removeUuid(uuid): detailedInfo = self.gCommand.gHosts.searchByUuid(uuid) self.gLogging.info( gutils.color_pick( self.gConfig, "removing: %s with uuid %s from next step execution" % (detailedInfo[1], uuid), self.gConfig['JSON']['pick_no'])) self.gCommand.gHosts.pickHosts(manual=True, uuids=[uuid], _printing=False)
def __init__(self): self.config = configparser.ConfigParser() try: loaded = self.config.read( glob.glob("{}/{}".format(gutils.gcpath(), self.CONFIG_MASK))) if self.CONFIG_MAIN not in [ gutils.gcfile(file) for file in loaded ]: raise Exception( "main config file: {} cannot be found. exiting..".format( self.CONFIG_MAIN)) if not all(elem in self.config.sections() for elem in self.CONFIG_MAIN_SECTIONS): raise Exception( "main config file: {} seems to be invalid. exiting..". format(self.CONFIG_MAIN)) except Exception as e: print(e) exit(1)
def importCsvAsCreds(self, filename): """ This method imports csv file with credentials. Args: filename (str): path to csv file with credentials definitions **csv file must follow format(no headers):** **credname,username,password,path_to_key,key_password,use_password_or_key*,encrypted_yes_or_no,env_var_used_to_encrypt** *valid options: password | key Examples: format: >>> rootVM,root,MyPass,/path/to/key,key_password,password,no,GC_KEY execution: >>> importCsvAsCreds("creds/creds.csv") """ #todo strip whitespace self.gLogging.debug("importCsvAsCreds invoked") MyCred = Query() try: with open(filename, 'r') as infile: for line in infile: if len(line) > 4: newone = {"credname": line.split(",")[0], "username": line.split(",")[1], "password": line.split(",")[2], "key": line.split(",")[3], "key_password": line.split(",")[4], "use": line.split(",")[5], "encrypted": line.split(",")[6], "secret_env_key": line.split(",")[7].strip("\n")} try: oldone = self.searchCredName(line.split(",")[0])[0] except Exception: oldone = None self.credtable.upsert({"credname": line.split(",")[0], "username": gutils.compare_dicts("username", newone, oldone), "password": gutils.compare_dicts("password", newone, oldone), "key": gutils.compare_dicts("key", newone, oldone), "key_password": gutils.compare_dicts("key_password", newone, oldone), "use": gutils.compare_dicts("use", newone, oldone), "encrypted": gutils.compare_dicts("encrypted", newone, oldone), "secret_env_key": gutils.compare_dicts("secret_env_key", newone, oldone)}, MyCred.credname == line.split(",")[0]) except Exception: self.gLogging.error("cannot open or invalid csv file: " + filename)
def __init__(self, config, logger): """ Construct object Args: config (object): holds instance of GcConfig class logger (object): holds instance of GcLogging class """ # enable logging and config self.gLogging = logger self.gConfig = config.config # try: varsfile = "{}/{}".format(gutils.gcpath(), self.gConfig['JSON']['varfile']) self.varsfile = TinyDB(varsfile) self.varstable = self.varsfile.table("VARS") self.allvars = self.varstable.all() except Exception: self.gLogging.critical("variables definitions cannot be loaded")
def __init__(self, config): """ Construct object Args: config (object): holds instance of GcConfig class """ self.gConfig = config.config try: self.logger = logging.getLogger(self.gConfig['LOGGING']['logger_name']) self.logger.setLevel(self.gConfig['LOGGING']['logging_level']) #file self.handler = RotatingFileHandler("{}/{}".format(gutils.gcpath(), self.gConfig['LOGGING']['log_file']), maxBytes=int(self.gConfig['LOGGING']['log_file_size']), backupCount=int(self.gConfig['LOGGING']['log_file_rotate'])) self.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') self.handler.setFormatter(self.formatter) self.logger.addHandler(self.handler) except Exception as e: print("cannot start logging mechanism. reason: {}. exiting..".format(e)) self.logging_levels = {"CRITICAL": 50, "ERROR": 40, "WARNING": 30, "INFO": 20, "DEBUG": 10, "NOSET": 10} self.logging_level = self.logging_levels[self.gConfig['LOGGING']['logging_level']]
def yamlExecutor(self, source, stopping=True): """ This method executes yaml file step by step. If verification will fail for a current db, instance or host, it will be removed from a next step execution list. Args: source (str): file to execute, local or github (raw) stopping (bool): disable stopping before next step execution Examples: >>> yamlExecutor('/tmp/file.yaml', True) """ try: with urlopen(source) as gitfile: gfile = serv.read().decode() except ValueError: pass try: gfile = open(source, 'r') except FileNotFoundError: self.gLogging.critical("file: %s not found" % source) except Exception: self.gLogging.critical("cannot load source file: %s" % source) try: yfile = yaml.load(gfile, Loader=Loader) except Exception: self.gLogging.critical("cannot parse as valid yaml file: %s" % source) if stopping: self.gLogging.show("--- press Enter to load host file ---") input() try: hosts = [ hosts['hosts'] for hosts in yfile if hosts.get('hosts', None) is not None ][0] self.gCommand.gHosts.importCsvAsHosts(hosts) self.gLogging.info("hosts loaded from a yaml, location: %s" % hosts) except Exception: self.gLogging.info("hosts loaded from a GC") if stopping: self.gLogging.show("--- press Enter to load cred file ---") input() try: creds = [ creds['creds'] for creds in yfile if creds.get('creds', None) is not None ][0] self.gCommand.gCreds.importCsvAsCreds(creds) self.gLogging.info("creds loaded from a yaml, location: %s" % creds) except Exception: self.gLogging.info("creds loaded from a GC") def runcmd(cmd): self.onecmd(cmd) def removeUuid(uuid): detailedInfo = self.gCommand.gHosts.searchByUuid(uuid) self.gLogging.info( gutils.color_pick( self.gConfig, "removing: %s with uuid %s from next step execution" % (detailedInfo[1], uuid), self.gConfig['JSON']['pick_no'])) self.gCommand.gHosts.pickHosts(manual=True, uuids=[uuid], _printing=False) def runUuid(uuid, fixcmd): backuphosts = self.gCommand.gHosts.hosttable.all() backupconnections = self.gCommand.connections ## detailedInfo = self.gCommand.gHosts.searchByUuid(uuid) self.gCommand.connections = [(host, client) for host, client in backupconnections if host == detailedInfo[0][0]['hostname']] self.gCommand.gHosts.pickHosts(reset=True, _printing=False, resetOption='N') if detailedInfo[1] == 'host': detailedInfo[0][0]['host_checked'] = self.gConfig['JSON'][ 'pick_yes'] elif detailedInfo[1] == 'instance': detailedInfo[0][0]['host_checked'] = self.gConfig['JSON'][ 'pick_yes'] for key, value in detailedInfo[0][0].items(): if key == 'installations': for install in value: for ikey, ivalue in install.items(): if ikey == 'instances': for instance in ivalue: if instance['instance_uuid'] == uuid: instance[ 'instance_checked'] = self.gConfig[ 'JSON']['pick_yes'] elif detailedInfo[1] == 'db': detailedInfo[0][0]['host_checked'] = self.gConfig['JSON'][ 'pick_yes'] for key, value in dinfo[0][0].items(): if key == 'installations': for install in value: for ikey, ivalue in install.items(): if ikey == 'instances': for instance in ivalue: for dkey, dvalue in instance.items(): if dkey == 'databases': for db in dvalue: if db['db_uuid'] == uuid: db['db_checked'] = self.gConfig[ 'JSON']['pick_yes'] else: pass self.gCommand.gHosts.hosttable.write_back(detailedInfo[0]) runcmd(fixcmd) self.gCommand.gHosts.hosttable.write_back(backuphosts) self.gCommand.connections = backupconnections def analyze(condition, result, verify=False): msg = condition.get('msg', None) expect = condition.get('expect', False) failsolve = condition.get('failsolve', [None]) output = result[0].decode() self.gLogging.info("phrases to check: %s" % ",".join(msg)) self.gLogging.info("expected: %s" % expect) if verify is False: self.gLogging.info("trying to fix with: %s" % failsolve) if any(msg in output for msg in msg): if expect is True: self.gLogging.info( gutils.color_pick(self.gConfig, "condition passed", self.gConfig['JSON']['pick_yes'])) return True else: if verify is False: for fixstep in failsolve: self.gLogging.info("running fix step: %s" % fixstep) runUuid(result[6], fixstep) if stopping: self.gLogging.show(" ") self.gLogging.show("--- fixes applied.. ---") self.gLogging.show("--- press Enter to continue.. ---") self.gLogging.show(" ") input() else: pass return False else: if expect is True: if verify is False: for fixstep in failsolve: self.gLogging.info("running fix step (expect): %s" % fixstep) runUuid(result[6], fixstep) if stopping: self.gLogging.show(" ") self.gLogging.show("--- fixes applied.. ---") self.gLogging.show("--- press Enter to continue.. ---") self.gLogging.show(" ") input() else: pass return False else: self.gLogging.info( gutils.color_pick(self.gConfig, "condition passed", self.gConfig['JSON']['pick_yes'])) return True steps = [ step['step'] for step in yfile if step.get('step', None) is not None ] if len(steps) == 0: self.gLogging.critical("no steps to run has been found") else: pass if stopping: self.gLogging.show("--- press Enter to connect to hosts ---") input() #do connect to a hosts self.gCommand.close() self.gCommand.connect() # get list of active connections self.gLogging.show("working on: %s hosts" % str(len(self.gCommand.connections))) for step in steps: if stopping: self.gLogging.show(" ") self.gLogging.show("--- press Enter to run a next step.. ---") self.gLogging.show("--- cmd: %s ---" % step['cmd']) self.gLogging.show("--- desc: %s ---" % step.get('desc', '')) self.gLogging.show(" ") input() else: self.gLogging.show("--- cmd: %s ---" % step['cmd']) self.gLogging.show("--- desc: %s ---" % step.get('desc', '')) self.gLogging.show(" ") cmd = step['cmd'] # running command runcmd(cmd) for result in self.gCommand.result: for condition in step.get('fail', [None]): if condition is not None: if analyze(condition, result): pass else: runUuid(result[6], cmd) if analyze(condition, self.gCommand.result[0], verify=True): pass else: self.gLogging.show( gutils.color_pick( self.gConfig, "condition failed", self.gConfig['JSON']['pick_no'])) removeUuid(result[6]) break else: self.gLogging.show( gutils.color_pick(self.gConfig, "no condition to check", self.gConfig['JSON']['pick_yes'])) if self.gCommand.gConfig['PANACEUM']['showpicked'] == 'YES': self.gCommand.gHosts.pickHosts(_printing=True) if stopping: self.gLogging.show("--- press Enter to close active connections ---") input() # do close connections to a hosts self.gCommand.close() try: gfile.close() except Exception: pass
def importCsvAsHosts(self, filename): """ This method imports csv file with definitions of hosts. Args: filename (str): path to csv file with hosts definitions **csv file must follow format(no headers):** **hostname,ip/host,port,credential_name*,group** *credential_name - should correspond to defined credentials Examples: format: >>> myhost2,192.168.56.102,22,rootVM,VM execution: >>> importCsvAsHosts("hosts/hosts.csv") """ self.gLogging.debug("importCsvAsHosts invoked") MyHost = Query() try: with open(filename, 'r') as infile: for line in infile: if len(line) > 4: newone = { "group": line.split(",")[4].strip("\n"), "port": line.split(",")[2], "installations": [], "scanned": "no", "def_cred": line.split(",")[3], "host": line.split(",")[1], "hostname": line.split(",")[0] } try: oldone = self.searchHostName(line.split(",")[0])[0] except Exception: oldone = None self.hosttable.upsert( { "group": gutils.compare_dicts("group", newone, oldone), "group_uuid": gutils.uuid_generator( line.split(",")[4].strip("\n")), "group_checked": gutils.compare_checked( "group_checked", self.gConfig['JSON']['pick_yes'], oldone), "host_checked": gutils.compare_checked( "host_checked", self.gConfig['JSON']['pick_yes'], oldone), "port": gutils.compare_dicts("port", newone, oldone), "installations": gutils.compare_dicts("installations", newone, oldone), "scanned": gutils.compare_dicts("scanned", newone, oldone), "def_cred": gutils.compare_dicts("def_cred", newone, oldone), "host": gutils.compare_dicts("host", newone, oldone), "host_uuid": gutils.uuid_generator( line.split(",")[4].strip("\n") + line.split(",")[0]), "hostname": line.split(",")[0] }, MyHost.hostname == line.split(",")[0]) self._indexHosts() except Exception: self.gLogging.error("cannot open or invalid csv file: " + filename)