def collect_pack_content(output_path): """ Copy pack contents to the output path. :param output_path: Path where pack contents will be copied to. :type output_path: ``str`` """ LOG.debug('Including content') packs_base_paths = get_packs_base_paths() for index, packs_base_path in enumerate(packs_base_paths, 1): dst = os.path.join(output_path, 'dir-%s' % index) try: shutil.copytree(src=packs_base_path, dst=dst) except IOError: continue base_pack_dirs = get_dirs_in_path(file_path=output_path) for base_pack_dir in base_pack_dirs: pack_dirs = get_dirs_in_path(file_path=base_pack_dir) for pack_dir in pack_dirs: process_content_pack_dir(pack_dir=pack_dir)
def create_archive(include_logs, include_configs, include_content, include_system_info, debug=False): """ Create an archive with debugging information. :return: Path to the generated archive. :rtype: ``str`` """ date = date_utils.get_datetime_utc_now().strftime('%Y-%m-%d-%H:%M:%S') values = {'hostname': socket.gethostname(), 'date': date} output_file_name = OUTPUT_FILENAME_TEMPLATE % values output_file_path = os.path.join('/tmp', output_file_name) # 1. Create temporary directory with the final directory structure where we will move files # which will be processed and included in the tarball temp_dir_path = tempfile.mkdtemp() output_paths = { 'logs': os.path.join(temp_dir_path, 'logs/'), 'configs': os.path.join(temp_dir_path, 'configs/'), 'content': os.path.join(temp_dir_path, 'content/'), 'system_info': os.path.join(temp_dir_path, 'system_info.yaml') } for directory_name in DIRECTORY_STRUCTURE: full_path = os.path.join(temp_dir_path, directory_name) os.mkdir(full_path) # 2. Moves all the files to the temporary directory LOG.info('Collecting files...') # Logs if include_logs: LOG.debug('Including log files') for file_path_glob in LOG_FILE_PATHS: log_file_list = get_full_file_list(file_path_glob=file_path_glob) copy_files(file_paths=log_file_list, destination=output_paths['logs']) # Config files if include_configs: LOG.debug('Including config files') copy_files(file_paths=CONFIG_FILE_PATHS, destination=output_paths['configs']) # Content if include_content: LOG.debug('Including content') packs_base_paths = get_packs_base_paths() for index, packs_base_path in enumerate(packs_base_paths, 1): dst = os.path.join(output_paths['content'], 'dir-%s' % (index)) try: shutil.copytree(src=packs_base_path, dst=dst) except IOError: continue # System information if include_system_info: LOG.debug('Including system info') system_information = get_system_information() system_information = yaml.dump(system_information, default_flow_style=False) with open(output_paths['system_info'], 'w') as fp: fp.write(system_information) # Configs st2_config_path = os.path.join(output_paths['configs'], ST2_CONFIG_FILE_NAME) process_st2_config(config_path=st2_config_path) mistral_config_path = os.path.join(output_paths['configs'], MISTRAL_CONFIG_FILE_NAME) process_mistral_config(config_path=mistral_config_path) # Content base_pack_dirs = get_dirs_in_path(file_path=output_paths['content']) for base_pack_dir in base_pack_dirs: pack_dirs = get_dirs_in_path(file_path=base_pack_dir) for pack_dir in pack_dirs: process_content_pack_dir(pack_dir=pack_dir) # 4. Create a tarball LOG.info('Creating tarball...') with tarfile.open(output_file_path, 'w:gz') as tar: for file_path in output_paths.values(): file_path = os.path.normpath(file_path) source_dir = file_path if '.' in file_path: arcname = os.path.basename(file_path) else: arcname = os.path.split(file_path)[-1] tar.add(source_dir, arcname=arcname) return output_file_path