def run_cmd(host, cmd, path="", func_name=None, is_root=False, show_stdout=True): """Generic function to run single commands""" func_name = func_name if func_name else helpers.get_function_name() ssh_run = SshRun(host.hostname, host.ssh_hostname, path, func_name, is_root) output = ssh_run.run_single(cmd) ssh_run.close_ssh_channel() # Clear stdout if needed updated_output = output if not show_stdout: updated_output = output.copy() updated_output['stdout'] = "" _log_rc(output, ssh_run.function_name, hostname=host.hostname, cmd=cmd, module=COMMAND_MODULE_BUILTIN, output=updated_output) return output
def untar(host, location, is_root): """Copy and untar file on remote host""" _path, tar = os.path.split(host.tar_file) func_name = helpers.get_function_name() outcome = {'rc': 1} tar_cmd = "tar -zxvf %s -C %s" % (tar, location) if copy_to_host(host, "./", host.tar_file, False): ssh_run = SshRun(host.hostname, host.ssh_hostname, host.tar_file, func_name, is_root) # Untar bundle ssh_run.add_cmd(tar_cmd) # Remove old tar file ssh_run.add_cmd("rm -rf ./%s" % tar) outcome = ssh_run.run() _log_rc(outcome, func_name, cmd=tar_cmd, hostname=host.hostname, content=outcome['outputs'][0]['stdout'].split('\r\n') if outcome['rc'] < 1 else "", location=location, module=COMMAND_MODULE_BUILTIN) return True
def get_file_content(host, remote_file, local_file, is_root=False): """Get content of file from remote host""" helpers.create_path(local_file) return run_cmd(host, "cat %s" % remote_file, helpers.get_function_name(), remote_file, is_root, False)
def clear_files(host, app_path, file_regex, is_root=False): """Removes files from server""" if not check_path(app_path): return False results = run_cmd(host, "rm -f %s " % os.path.join(app_path, file_regex), helpers.get_function_name(), app_path, is_root) return results['rc'] < 1
def rotate_logs(host, log_path, retention, is_root=False): """Function to rotate appetite logs""" if not check_path(log_path): return False results = run_cmd( host, "find %s -type f -mtime +%d -delete" % (log_path, retention), helpers.get_function_name(), log_path, is_root) return results['rc'] < 1
def delete(host, remote_object, is_root=False, app_path_check=True): """Delete file/folder on remote host Function limits deleting to only files in the application directory """ if not check_path(remote_object, app_path_check): return False results = run_cmd(host, "rm -rf %s" % remote_object, remote_object, helpers.get_function_name(), is_root) return results['rc'] < 1
def run_command(self, ecommand, host): """Run single stored command""" command = ecommand['command'] # Check to see if host can run command if not command.can_host_use(host): if not command.suppress_limit_to_hosts_warnings: logger.warn("Invalid host for command", command=command.name, hostname=host.hostname, module=COMMAND_MODULE_INIT, allowed_hosts=command.limit_to_hosts) return False # Call root is already taken applied in get_cmd ssh_run = SshRun(host.hostname, host.ssh_hostname, "", helpers.get_function_name(), False) logger.info("SSH Started", state=0, hostname=host.hostname, command=command.name, module=COMMAND_MODULE_CUSTOM) results = ssh_run.run_single(self.get_cmd(ecommand)) ssh_run.close_ssh_channel() _log_rc(results, "SSH Finished", state=1, auth=command.use_auth, app_binary=command.use_app_binary, hostname=host.hostname, command=command.name, cmd=self.get_cmd_clean(ecommand), output=results, module=COMMAND_MODULE_CUSTOM) return True
def copy_to_host(host, remote_file, local_file, is_root=False): """Copy file to remote host Function first copies file to remote user directory and then moving. Final move depends to right to directory. """ _lpath, lfilename = os.path.split(local_file) rpath, rfilename = os.path.split(remote_file) local_filepath = os.path.join('./', lfilename) if len(rfilename) < 1: rfilename = lfilename remote_file = os.path.join(rpath, rfilename) if not os.path.isfile(local_file): logger.error("File to copy does not exist", file=local_file, module=COMMAND_MODULE_CUSTOM) return False ssh_run = SshRun(host.hostname, host.ssh_hostname, local_file, helpers.get_function_name(), is_root) if not ssh_run.create_ssh_channel(): return False success = True if not CREDS.DRY_RUN: retries = 0 success = False while retries < MAX_SCP_RETRIES: # Copies file to remote users directory with SCPClient(ssh_run.ssh.get_transport()) as scp: try: scp.put(local_file, lfilename) success = True break except Exception as e: if _error_check(e.message, remote_file, host.hostname, "copy_to_host"): return retries += 1 time.sleep(2) if not success: logger.error("Problem using scp", hostname=host.hostname, local_file=local_file, module=COMMAND_MODULE_CUSTOM) return False if local_filepath != remote_file: # Only copy file if not the same location ssh_run.add_cmd("mkdir -p %s" % rpath) ssh_run.add_cmd("mv %s %s" % (lfilename, remote_file)) if is_root: # Make sure root permission are set if needed ssh_run.add_cmd("chown root:root %s" % remote_file) ssh_run.add_cmd("restorecon %s" % remote_file) results = ssh_run.run() _log_rc(results, ssh_run.function_name, hostname=host.hostname, remote_file=remote_file, local_file=local_file, outputs=results['outputs'], module=COMMAND_MODULE_BUILTIN) return results['rc'] < 1