def separate_file(folder, filename): Log.info("Separating file {}".format(filename)) ps_path = 'powershell' separator_path = os.path.join(MODULES_DIR, 'separator.ps1') file = os.path.join(MAIN_REPOSITORY_PATH, folder, filename) command = ps_path + " -Command " + separator_path + " " + file result = subprocess.check_output(command).decode('cp866').rstrip() return result, 200
def remove_old_result_files(): Log.info("Checking RESULTS_DIR for old files") week = 604800 # seconds in 1 week res = list() for file in __listdirattr(RESULTS_DIR): if file['created'] < get_timestamp() - week: Log.info("File {} is older than 1 week. Deleting...".format( file['name'])) delete_file(RESULTS_DIR, file['name']) return res, 200
def connect(dns): Log.info("Connecting to {}".format(dns)) try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(dns, username='******', key_filename='C:\\Users\\IDidyk\\.ssh\\id_rsa.ppk') except TimeoutError as error: Log.error(error) return str(error), 410 else: return ssh
def delete_file(folder, filename): file = os.path.join(MAIN_REPOSITORY_PATH, folder, filename) if os.path.exists(file): os.remove(file) if not os.path.exists(file): Log.info("File {} is deleted".format(filename)) return "File {} removed".format(filename), 200 else: return "Something went wrong", 510 else: return "Not found", 404
def move_file(old_folder, new_folder, filename): Log.info("Try to move {} from {} to {}".format(filename, old_folder, new_folder)) old_path = os.path.join(MAIN_REPOSITORY_PATH, old_folder, filename) new_path = os.path.join(MAIN_REPOSITORY_PATH, new_folder, filename) if os.path.exists(old_path): if old_path == new_path: return "File is already in this folder", 400 os.rename(old_path, new_path) if os.path.exists(new_path): return "File moved to /" + new_folder, 200 else: return "Something went wrong", 510 else: return "Not found", 404
def collect(data): # Preparation Log.info("Command preparation...") config = get_config() component = config[data['component']] file_name = get_result_file_name(data) ssh = connect(component['dns']) cat_command = get_command(ssh, data, config) # CAT operation Log.info("Command executing...") channel = ssh.get_transport().open_session() channel.exec_command(cat_command) while not channel.exit_status_ready(): time.sleep(1) # Copying from remote to local Log.info("Try to copy remote file...") sftp = ssh.open_sftp() remote_path = "/home/ivan.didyk/{}".format(file_name) local_path = os.path.join(RESULTS_DIR, file_name) sftp.get(remote_path, local_path) # Remove remote file Log.info("Try to remove remote file...") sftp.remove(remote_path) sftp.close() Log.info("SFTP connection closed...") ssh.close() Log.info("SSH connection closed...") return get_result_file_name(data), 200