def return_id(taskid): output = db.db_completed(taskid) logging.debug(output) if output == None: logging.debug('[X] Processing') return response_build(202,{'info':'processing has not finished'}) else: return response_build(200,{'output':output})
def sort_params(params):#deals with param dics command = '' for item in params: keys= list(item.keys()) values= list(item.values()) logging.debug(keys) logging.debug(values) command+=keys[0]+' '+values[0]+' ' return command
def run_job(): job = request.json try: if check_auth(job['auth']) == 'Error': #check auth token, if it errs throw a 403 and end return response_build(403,{'error':'Authorization incorrect'}) logging.debug(job) del job['auth'] #remove auth so sent data passes data check logging.debug('passed auth') db_playbook = db.db_lookup(job['name']) #data check logging.debug('passed name lookup') if db_playbook != 'Error': # if no error continue ans_command, password = dict_mgm.make_play(job, db_playbook, location) #parse dict into command and return command and password if ans_command != 'Error': #if make_play did not fail continue items = db.db_outputid() db.db_stdoutinput('No current stdout') task = run_command.delay(ans_command, password, items) return response_build(202,{'taskid':items}) else: #handles datacheck error logging.info('[X] Data submitted does not match corresponding blueprint in db') return response_build(400,{'error':'Data submitted does not match corresponding blueprint in db'}) else: logging.info('[X] No playbook by that name in the database') return response_build(400,{'error':'No playbook by that name in the database'}) except KeyError: #missing name or auth field logging.info('[X] No name or auth contained in request') return response_build(400,{'error':' No name or auth contained in request'}) except IndexError: #empty dict in request logging.info('[X] Empty dict in params section of request') return response_build(400,{'error':'empty dict in params section of request'})
def make_play(data,db_data,location): if dict_mgm.data_check(data, db_data) == 'OK': command = 'ansible-playbook {location}'.format(location=location) #did and incredi bad if else thing logging.debug(data.keys()) command+=data['name']+' ' if 'params' in data.keys(): command+= dict_mgm.sort_params(data['params']) if 'args' in data.keys(): command+= dict_mgm.sort_args(data['args']) if 'password' in data.keys(): password = data['password'] else: password = None logging.debug(command) logging.debug(password) return command, password else: return 'Error', None
def run_command( command, password, taskid, *args ): #runs built command and determines if become password needs to be used #rules = ['-k' in command, # '--ask-sudo-pass' in command, # '-K' in command, # '--ask-pass' in command] logging.debug(command) logging.debug(password) #if any(rules): #try except fall through try: process = pexpect.spawnu(command, timeout=None) logging.debug('enter password') process.expect('password') process.sendline(password) pexpect_readout(password, taskid) except pexpect.exceptions.EOF: process = pexpect.spawnu(command, timeout=None) pexpect_readout(process, taskid)
def data_check(data,db_data): logging.debug(data) logging.debug(db_data) if len(data) != len(db_data): logging.debug('triggered 1') return 'Error' if data.keys() != db_data.keys(): logging.debug('triggered 2') return 'Error' if len(data.values()) != len(db_data.values()): logging.debug('triggered 3') return 'Error' #for playbooks that have no params/args try: if len(data['params']) != len(db_data['params']): logging.debug('triggered 4') return 'Error' except KeyError: pass try: if len(data['args']) != len(db_data['args']): logging.debug('triggered 5') return 'Error' except KeyError: pass logging.debug('OK') return 'OK'
def pexpect_readout(process, taskid): stdout = process.read() logging.debug(stdout + 'stdout') db.db_updateinput(stdout, taskid)