def asm_run(self, mod_filename): """Compare original asm output with your version""" file_base = mod_filename[:-2] orig_cor = file_base + ".orig_cor" cor = file_base + ".cor" orig_ret, orig_out = helpers.run_command("{} {}".format( self.db.get('true_asm'), mod_filename)) if orig_out.startswith(self.file_compiled_prefix): # Copy the original *.cor file, so it is not over-written for later comparison helpers.rename_file(cor, orig_cor) mod_ret, mod_out = helpers.run_command("{} {}".format( self.db.get('my_asm'), mod_filename)) if os.path.exists(cor) and os.path.exists( orig_cor): # save *.cor and *.orig_cor files for binaries diff self.cor_files.append((cor, orig_cor)) return { 'ret': orig_ret, 'out': orig_out, 'file': orig_cor }, { 'ret': mod_ret, 'out': mod_out, 'file': cor }
def profile(): import helpers import os utilDir = helpers.path('util') if not os.path.exists(utilDir + '/profiles/profile.py'): snippet = '''{\n\t"settings" : {\n\n\t\t}\n}''' helpers.run_command('mkdir {}/profiles'.format(utilDir), False) helpers.write_file(utilDir + '/profiles/profile.py', snippet) print("\nprofile added!\n") msg.done
def set_config(self, key, val): if key[0] == '#': run_command( f'/usr/bin/sudo /bin/sed -i "s|^#*{key[1:]}=.*|{key}={val}|" {self.hostapd_conf}' ) else: run_command( f'/usr/bin/sudo /bin/sed -i "s|^#*{key}=.*|{key}={val}|" {self.hostapd_conf}' ) self.config = read_config(self.hostapd_conf)
def do_backup(username, database, path, dump_name): db_exists = run_command( "su - postgres -c \"psql -lt\" | grep %s | awk '{print $1}' | grep -xE %s" % (username, database) ) if db_exists: run_command( 'su - postgres -c "/usr/bin/pg_dump %s --format=c --file=%s%s-%s.dump"' % (database, path, database, dump_name) )
def cor_run(self, filename): """Run corewar binaries""" orig_command = "{} {}".format(self.db.get('true_cw'), filename) my_command = "{} {}".format(self.db.get('my_cw'), filename) orig_ret, orig_out = helpers.run_command(orig_command) mod_ret, mod_out = helpers.run_command(my_command) return { 'ret': orig_ret, 'out': orig_out }, { 'ret': mod_ret, 'out': mod_out }
def iterate_users(user_list): for user in user_list: try: if os.path.isfile(os.path.join(pil.dl_path, user + '.lock')): logger.warn( "Lock file is already present for '{:s}', there is probably another download " "ongoing!".format(user)) logger.warn( "If this is not the case, manually delete the file '{:s}' and try again." .format(user + '.lock')) else: logger.info( "Launching daemon process for '{:s}'.".format(user)) start_result = helpers.run_command( "pyinstalive -d {:s} -cp '{:s}' -dp '{:s}' {:s} {:s} {:s}". format(user, pil.config_path, pil.dl_path, '--no-lives' if not pil.dl_lives else '', '--no-replays' if not pil.dl_replays else '', '--no-heartbeat' if not pil.do_heartbeat else '')) if start_result: logger.warn("Could not start process: {:s}".format( str(start_result))) else: logger.info("Process started successfully.") logger.separator() time.sleep(2) except Exception as e: logger.warn("Could not start process: {:s}".format(str(e))) except KeyboardInterrupt: logger.binfo('The process launching has been aborted by the user.') logger.separator() break
def exec_wpa_cli(self, cmd, statusOnly=True, quiet=True): if not quiet: print(cmd) wpa_cli_out = run_command(cmd) if statusOnly: return wpa_cli_out[0] if wpa_cli_out else None else: return wpa_cli_out
def deploy_terraform(args): """ Deploys Terraform for the given environment. """ env_dir = helpers.ENVS_DIR + '/' + args.env_name helpers.log('Rolling out Terraform for %s ' % args.env_name, as_banner=True, bold=True) cmd = 'terragrunt plan -state=%s/terraform.tfstate' % env_dir env = os.environ.copy() env['TF_VAR_fingerprint'] = args.fingerprint env['TF_VAR_private_key_path'] = args.private_key_file env['TF_VAR_user_ocid'] = args.user_ocid (stdout, stderr, returncode) = helpers.run_command(cmd=cmd, env=env, cwd=env_dir, verbose=True) if returncode != 0: raise Exception('Terraform plan failed') if not '0 to destroy' in stdout: raise Exception( 'Terraform plan indicated changes, too scared to continue!') # Generate destroy script for unmanaged environments for easy cleanup if not args.managed: generate_destroy_env_script(args) cmd = 'terragrunt apply --terragrunt-non-interactive --state=%s/terraform.tfstate' % env_dir (stdout, stderr, returncode) = helpers.run_command(cmd=cmd, env=env, cwd=env_dir, verbose=True) if returncode != 0: raise Exception('Terraform deployment failed') # Generate file indicating the SHA at which Terraform was deployed f = open(env_dir + '/' + TERRAFORM_SHA_FILE, 'w') cmd = 'git rev-parse --verify HEAD' (stdout, _, returncode) = helpers.run_command(cmd=cmd, cwd=helpers.PROJECT_ROOT_DIR, silent=True, verbose=False) if returncode != 0: raise Exception('Failed to get Git SHA') f.write(stdout)
def kubectl(action, kubeconfig, exit_on_error=True, verbose=True, silent=False): (stdout, _, returncode) = helpers.run_command( 'kubectl --kubeconfig %s %s' % (kubeconfig, action), verbose=verbose, silent=silent, verify_return_code=True) return stdout
def get_status(self): output = run_command(self.captive_portal_status_cmd) if output: parsed = '' for line in output: if parsed.startswith('ndsctl:'): return None if parsed and parsed[-1] == "}" and line == "{": break parsed += line return parsed else: return None
def fire(self, action, params=''): query_action = str(params) if action == "startCountdown" else "default" # gpios, states, funcs gpio_actions = self.actions[action]['gpio'].get(query_action, [[], [], []]) # actions, colors, brightness, args ledpanel_actions = self.actions[action]['ledpanel'].get(query_action, []) # remotes, payloads remote_actions = self.actions[action]['remote'].get(query_action, []) if gpio_actions[0]: cmd = f"{GPIO_CMD} {action} {','.join(gpio_actions[0])} {','.join(gpio_actions[1])} {','.join(gpio_actions[2])} {params}" run_command(cmd, wait=False) for slot in ledpanel_actions: args_dict = slot['args'] if slot['args'] != '' else {} args_dict['color'] = slot['color'] args_dict['iterations'] = 1 self.ledpanel.send(slot['action'], False, float(slot['brightness']), args_dict, None) for slot in remote_actions: cmd = f"{REMOTE_CMD} -m {slot['func']}{slot['state']} -t photobooth/remote/{slot['remoteuid']}" run_command(cmd, wait=False) self.log.debug(f'Trigger {action} fired successfully.')
def commit_changes(args): """ Prepares files for checkin for the given environment, including encrypting and creating a Git commit. """ # Nothing to do for unmanaged environments if not args.managed: return env_dir = helpers.ENVS_DIR + '/' + args.env_name helpers.log('Preparing environment files for check-in', as_banner=True, bold=True) helpers.log('Encrypting sensitive files', as_banner=True) cmd = 'ansible-vault encrypt %s/terraform.tfstate' % env_dir (_, _, returncode) = helpers.run_command(cmd=cmd, verbose=True) if returncode != 0: raise Exception('Failed to encrypt Terraform state') if not args.skip_branch: helpers.log('Creating branch', as_banner=True) cmd = 'git checkout -b %s' % get_git_branch_name(args.env_name) (_, _, returncode) = helpers.run_command(cmd=cmd, verbose=True) if returncode != 0: raise Exception('Failed to create branch') helpers.log('Staging environment files', as_banner=True) cmd = 'git add %s' % env_dir (_, _, returncode) = helpers.run_command(cmd=cmd, verbose=True) if returncode != 0: raise Exception('Failed to stage environment files') helpers.log('Creating commit for new environment files', as_banner=True) cmd = 'git commit -m "Environment files for %s"' % args.env_name (_, _, returncode) = helpers.run_command(cmd=cmd, verbose=True) if returncode != 0: raise Exception('Failed to commit environment files')
def deploy_ansible(args): """ Deploys Ansible for the given environment. """ env_dir = helpers.ENVS_DIR + '/' + args.env_name helpers.log('Rolling out Ansible for %s ' % args.env_name, as_banner=True, bold=True) helpers.log('Populating dynamic files for %s ' % args.env_name, as_banner=True) helpers.populate_env(args.env_name) cmd = 'ansible-playbook -i %s/hosts -vv site.yml' % env_dir (stdout, stderr, returncode) = helpers.run_command(cmd=cmd, verbose=True) if returncode != 0: raise Exception('Ansible deployment failed')
def connect(self, ssid, psk, key_mgmt='WPA-PSK', quiet=True): network = 0 if ssid == next(iter(self.config.networks())) else 1 if network: connect_status = self.set_network(ssid, psk, key_mgmt, quiet) else: connect_status = (0, 'Using primary network') if connect_status[0] > 0: return connect_status wifi_connect_out = run_command( f'{self.wifi_connect} {self.iface} {network}') wifi_connect_status = wifi_connect_out[-1] status = self.status().get('wpa_state') if wifi_connect_status == 'success' and status == 'COMPLETED': connect_status = (0, status) else: connect_status = (11, 'ERROR') return connect_status
def check_cor_files(self): """Compare all *.cor file pairs of format (*.orig_cor, *.mod_cor)""" i = 0 for mod, orig in self.cor_files: orig_xxd, mod_xxd = orig[:-3] + 'xxd', mod[:-3] + 'xxd' diff_xxd = mod[:-3] + 'xxd_diff' cmd = "xxd -g 2 {0} > {1}; xxd -g 2 {2} > {3}; diff {1} {3} > {4}; rm -rf {1} {3}".format( orig, orig_xxd, mod, mod_xxd, diff_xxd) ret, out = helpers.run_command(cmd) if ret != 0: self.log("Compiled version differs for {}".format(mod), False) else: self.log("Compiled binaries match", True) path, file = os.path.split(mod) shutil.rmtree(path) if i > 0 and i % 80 == 0 and not self.args['v']: print('') i += 1
def get_trusted(self): output = run_command(self.captive_portal_status_cmd) if output: parsed = '' trusted = [] parse = False for line in output: if parsed.startswith('ndsctl:'): return [] if parsed and parsed[-1] == "}" and line == "{": break if line.startswith('"trusted":['): parse = True continue if not parse: continue else: if line == "]": break trusted += [line.replace('"', '').replace(',', '').strip()] parsed += line return trusted else: return []
# Populate the environment try: helpers.populate_env(env_name) except Exception, e: logger.error('Failure: ' + str(e) + ", exception: " + traceback.format_exc().replace('\n', ' | ')) raise Exception('[%s] Populate failed' % env_name) try: # Deploy Ansible helpers.log('[%s] Deploying Ansible' % env_name, as_banner=True) cmd = 'ansible-playbook -i envs/%s/hosts -vv %s' % (env_name, args.playbook) if len(args.tags) > 0: cmd += ' --tags ' + args.tags if len(args.extra_vars) > 0: cmd += ' --extra-vars "' + args.extra_vars + '"' (stdout, stderr, returncode) = helpers.run_command(cmd=cmd, verbose=True, logger=logger) if returncode != 0: logger.error('[%s] Deployment failed' % env_name) raise Exception('[%s] Deployment failed' % env_name) else: logger.info('[%s] Deployment succeeded' % env_name) # Health Check if args.healthcheck: try: helpers.log('[%s] Checking health' % env_name, as_banner=True) health.health('envs/%s/files/health.json' % env_name, 'all', logger) logger.info('[%s] Health Check succeeded' % env_name) except Exception, e: logger.error('[%s] Health Check failed.' % env_name) raise Exception('[%s] Health Check failed' % env_name)
if __name__ == "__main__": # if not make_checks(): # exit() dump_name, json = parse_args() for environment, values in json.items(): username = values[0]['username'] backup_dir = values[0]['system_path'] + 'backups/' databases = values[0]['databases'] try: if not run_command('id %s' % username): raise Exception check_path(backup_dir) run_command('/bin/chown postgres:postgres %s' % backup_dir) except: continue try: if isinstance(databases, unicode) and databases == 'all': databases = run_command( "su - postgres -c \"psql -lt\" | grep %s | awk '{print $1}'" % (username) ).split() if isinstance(databases, list): for database in databases: do_backup(username, database, backup_dir, dump_name)
def get_inet_passthrough(self): output = run_command(self.read_inet_passthrough_cmd) if output: return output[0] else: return None
def set_inet_passthrough(self, state): run_command(f'{self.write_inet_passthrough_cmd}={state}')
def getMode(self): mode = 0 for out in run_command(self.lsusb_cmd): if out.lower().find('edimax') != -1: mode += 1 return mode
def check_control_request(self): try: if not self.api_token: print('PhotomateurAPI ERROR: api_token missing') else: response = self.post('device/control/status') response_msg = response.get('message', None) if response_msg != None: pass # print(f'Control request response: {response_msg}') else: print(f'PhotomateurAPI ERROR - control: {response}') unit = Unit(b'ngrok@ssh\\x20http.service') unit.load() status = (unit.Unit.ActiveState).decode() if response_msg == 1 and status != 'active': print('Initiating remote control...') cmd = 'systemctl start ngrok@"ssh\\x20http".service' run_command(cmd) retries = 10 while status != 'active' and retries > 0: time.sleep(1) retries -= 1 status = (unit.Unit.ActiveState).decode() if status == 'active': tunnels_detailed = requests.get( 'http://127.0.0.1:4040/api/tunnels').json( )['tunnels'] tunnels = dict([(t['public_url'].split(":")[0], t['public_url']) for t in tunnels_detailed]) self.post('device/control/callback', { 'status': 'up', 'tunnels': json.dumps(tunnels) }) else: self.post('device/control/callback', { 'status': 'error', 'tunnels': json.dumps({}) }) elif response_msg == 1 and status == 'active': tunnels_detailed = requests.get( 'http://127.0.0.1:4040/api/tunnels').json()['tunnels'] tunnels = dict([(t['public_url'].split(":")[0], t['public_url']) for t in tunnels_detailed]) self.post('device/control/callback', { 'status': 'up', 'tunnels': json.dumps(tunnels) }) elif response_msg == 0 and status == 'active': print('Exiting remote control...') cmd = 'systemctl stop ngrok@"ssh\\x20http".service' run_command(cmd) self.post('device/control/callback', { 'status': 'down', 'tunnels': json.dumps({}) }) elif response_msg == 0 and status != 'active': pass except Exception as err: print(f'PhotomateurAPI ERROR: {err}')
def discover(self): if self.available_log.exists(): os.remove(self.available_log) run_command(self.discover_cmd) time.sleep(3) self.available = self.read_available()
def set_trusted(self, trusted): for mac in trusted: run_command(f'{self.captive_portal_set_trusted_cmd} {mac}')
def actionlist(): import helpers helpers.run_command('code {}/action-list.json'.format( helpers.path('util')))
def parse_args(): """ Parses and verifies arguments and checks prereqs. """ # # Parse command line args # parser = argparse.ArgumentParser( description='Create New Managed Environment') parser.add_argument('env_name', type=str, help='Name of the environment') # Load all possible params from params file, preserving ordering from params file in help output, for readability f = open(PARAMS_JSON, 'r') params = json.load(f, object_pairs_hook=OrderedDict) for param in params: # We don't actually use argparse "defaults" here, to give us the chance to interactively prompt # for certain unspecified values below if params[param]['type'] == "boolean": parser.add_argument('--' + param, help=params[param]['help'], type=helpers.str2bool, nargs='?', const=True) else: parser.add_argument('--' + param, help=params[param]['help'], type=str, required=False) args = parser.parse_args() # # Short-circuit the prompting if we're resuming a previously started environment # env_dir = helpers.ENVS_DIR + '/' + args.env_name if args.resume: resumable_file = env_dir + '/' + RESUMABLE_FILE_NAME if not os.path.exists(resumable_file): raise Exception( 'The last execution of the given environment was not resumable' ) helpers.load_attributes_from_file(args, resumable_file, params, RESUME_SECTION) os.remove(resumable_file) return args if os.path.isdir(env_dir): raise Exception( 'Directory %s for the specified environment already exists' % env_dir) # # Load arguments from preferences file, if specified # args.prefs = PREFS_FILE_DEFAULT if args.prefs == None else args.prefs if os.path.exists(args.prefs): helpers.logger.info('Loading preferences from %s...' % args.prefs) helpers.load_attributes_from_file(args, args.prefs, params, K8S_SECTION, overwrite=False) # Process any remaining args that haven't been specified yet for param in params: if getattr(args, param) is None: if params[param]['prompt']: if params[param]['type'] == 'boolean': setattr(args, param, helpers.yes_or_no(params[param]['help'])) else: setattr( args, param, helpers.prompt_for_value(params[param]['help'], params[param]['default'])) else: setattr(args, param, params[param]['default']) # Ensure all expected boolean types have boolean values (no Nones) for param in params: if params[param]['type'] == 'boolean': setattr(args, param, bool(getattr(args, param))) # # Handle prereqs that differ between managed/unmanaged environments. # if args.managed: # Ensure ansible-vault password set if not 'ANSIBLE_VAULT_PASSWORD_FILE' in os.environ: raise Exception( 'ANSIBLE_VAULT_PASSWORD_FILE must be set as an environment variable ' 'in order to create managed environments') # Only process the file if it's been encrypted - otherwise just return cmd = ('ansible-vault decrypt --output=- %s' % ANSIBLE_VAULT_CHALLENGE_FILE) (_, stderr, returncode) = helpers.run_command(cmd=cmd, verbose=False, silent=True) if returncode != 0: raise Exception( 'Looks like you have the wrong Ansible Vault password - please take your grubby hands ' 'off of our managed environments') # Creating a managed environment involves creating a Git commit, so ensure that # the current environment doesn't have any staged files to begin with cmd = 'git diff --cached' (stdout, _, _) = helpers.run_command(cmd=cmd, verbose=False, silent=True) if stdout.strip() != '': raise Exception( 'Can\'t create a managed environment with existing staged Git files' ) else: if args.env_name in helpers.MANAGED_ENVS: raise Exception( 'Can\'t create an unmanaged environment using one of the names reserved for ' ' managed environments: %s' % helpers.MANAGED_ENVS) # # Addition validation of certain params # if sum(get_num_per_ad_list(args.k8s_masters)) <= 0: raise Exception('At least one K8S master must be specified') return args
def restart(self): trusted = self.get_trusted() run_command(self.restart_cmd) time.sleep(1) self.set_trusted(trusted)