<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import shutil <EOL> import socket <EOL> import logging <EOL> import tarfile <EOL> import argparse <EOL> import platform <EOL> import tempfile <EOL> import httplib <EOL> import six <EOL> import yaml <EOL> import gnupg <EOL> import requests <EOL> from distutils . spawn import find_executable <EOL> import st2common <EOL> from st2common . content . utils import get_packs_base_paths <EOL> from st2common import __version__ as st2_version <EOL> from st2common import config <EOL> from st2common . util import date as date_utils <EOL> from st2common . util . shell import run_command <EOL> from st2debug . constants import GPG_KEY <EOL> from st2debug . constants import GPG_KEY_FINGERPRINT <EOL> from st2debug . constants import S3_BUCKET_URL <EOL> from st2debug . constants import COMPANY_NAME <EOL> from st2debug . constants import ARG_NAMES <EOL> from st2debug . utils . fs import copy_files <EOL> from st2debug . utils . fs import get_full_file_list <EOL> from st2debug . utils . fs import get_dirs_in_path <EOL> from st2debug . utils . fs import remove_file <EOL> from st2debug . utils . system_info import get_cpu_info <EOL> from st2debug . utils . system_info import get_memory_info <EOL> from st2debug . utils . system_info import get_package_list <EOL> from st2debug . utils . git_utils import get_repo_latest_revision_hash <EOL> from st2debug . processors import process_st2_config <EOL> from st2debug . processors import process_mistral_config <EOL> from st2debug . processors import process_content_pack_dir <EOL> LOG = logging . getLogger ( __name__ ) <EOL> GPG_INSTALLED = find_executable ( '<STR_LIT>' ) is not None <EOL> LOG_FILE_PATHS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> ST2_CONFIG_FILE_PATH = '<STR_LIT>' <EOL> MISTRAL_CONFIG_FILE_PATH = '<STR_LIT>' <EOL> SHELL_COMMANDS = [ ] <EOL> DIRECTORY_STRUCTURE = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> OUTPUT_PATHS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ST2_CONF_OPTIONS_TO_REMOVE = { <EOL> '<STR_LIT>' : [ '<STR_LIT:username>' , '<STR_LIT:password>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:url>' ] <EOL> } <EOL> REMOVE_VALUE_NAME = '<STR_LIT>' <EOL> OUTPUT_FILENAME_TEMPLATE = '<STR_LIT>' <EOL> DATE_FORMAT = '<STR_LIT>' <EOL> try : <EOL> config . parse_args ( args = [ ] ) <EOL> except Exception : <EOL> pass <EOL> def setup_logging ( ) : <EOL> root = LOG <EOL> root . setLevel ( logging . INFO ) <EOL> ch = logging . StreamHandler ( sys . stdout ) <EOL> ch . setLevel ( logging . DEBUG ) <EOL> formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> ch . setFormatter ( formatter ) <EOL> root . addHandler ( ch ) <EOL> class DebugInfoCollector ( object ) : <EOL> def __init__ ( self , include_logs , include_configs , include_content , include_system_info , <EOL> include_shell_commands = False , user_info = None , debug = False , config_file = None , <EOL> output_path = None ) : <EOL> """<STR_LIT>""" <EOL> self . include_logs = include_logs <EOL> self . include_configs = include_configs <EOL> self . include_content = include_content <EOL> self . include_system_info = include_system_info <EOL> self . include_shell_commands = include_shell_commands <EOL> self . user_info = user_info <EOL> self . debug = debug <EOL> self . output_path = output_path <EOL> config_file = config_file or { } <EOL> self . st2_config_file_path = config_file . get ( '<STR_LIT>' , ST2_CONFIG_FILE_PATH ) <EOL> self . mistral_config_file_path = config_file . get ( '<STR_LIT>' , <EOL> MISTRAL_CONFIG_FILE_PATH ) <EOL> self . log_file_paths = config_file . get ( '<STR_LIT>' , LOG_FILE_PATHS [ : ] ) <EOL> self . gpg_key = config_file . get ( '<STR_LIT>' , GPG_KEY ) <EOL> self . gpg_key_fingerprint = config_file . get ( '<STR_LIT>' , GPG_KEY_FINGERPRINT ) <EOL> self . s3_bucket_url = config_file . get ( '<STR_LIT>' , S3_BUCKET_URL ) <EOL> self . company_name = config_file . get ( '<STR_LIT>' , COMPANY_NAME ) <EOL> self . shell_commands = config_file . get ( '<STR_LIT>' , SHELL_COMMANDS ) <EOL> self . st2_config_file_name = os . path . basename ( self . st2_config_file_path ) <EOL> self . mistral_config_file_name = os . path . basename ( self . mistral_config_file_path ) <EOL> self . config_file_paths = [ <EOL> self . st2_config_file_path , <EOL> self . mistral_config_file_path <EOL> ] <EOL> def run ( self , encrypt = False , upload = False , existing_file = None ) : <EOL> """<STR_LIT>""" <EOL> temp_files = [ ] <EOL> try : <EOL> if existing_file : <EOL> working_file = existing_file <EOL> else : <EOL> working_file = self . create_archive ( ) <EOL> if not encrypt and not upload : <EOL> LOG . info ( '<STR_LIT>' <EOL> '<STR_LIT>' % working_file ) <EOL> else : <EOL> temp_files . append ( working_file ) <EOL> if encrypt : <EOL> working_file = self . encrypt_archive ( archive_file_path = working_file ) <EOL> if not upload : <EOL> LOG . info ( '<STR_LIT>' % <EOL> working_file ) <EOL> else : <EOL> temp_files . append ( working_file ) <EOL> if upload : <EOL> self . upload_archive ( archive_file_path = working_file ) <EOL> tarball_name = os . path . basename ( working_file ) <EOL> LOG . info ( '<STR_LIT>' % <EOL> ( self . company_name , tarball_name ) ) <EOL> LOG . info ( '<STR_LIT>' <EOL> '<STR_LIT>' % tarball_name ) <EOL> finally : <EOL> for temp_file in temp_files : <EOL> assert temp_file . startswith ( '<STR_LIT>' ) <EOL> remove_file ( file_path = temp_file ) <EOL> def create_archive ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> temp_dir_path = self . create_temp_directories ( ) <EOL> output_paths = { } <EOL> for key , path in OUTPUT_PATHS . iteritems ( ) : <EOL> output_paths [ key ] = os . path . join ( temp_dir_path , path ) <EOL> LOG . info ( '<STR_LIT>' ) <EOL> if self . include_logs : <EOL> self . collect_logs ( output_paths [ '<STR_LIT>' ] ) <EOL> if self . include_configs : <EOL> self . collect_config_files ( output_paths [ '<STR_LIT>' ] ) <EOL> if self . include_content : <EOL> self . collect_pack_content ( output_paths [ '<STR_LIT:content>' ] ) <EOL> if self . include_system_info : <EOL> self . add_system_information ( output_paths [ '<STR_LIT>' ] ) <EOL> if self . user_info : <EOL> self . add_user_info ( output_paths [ '<STR_LIT>' ] ) <EOL> if self . include_shell_commands : <EOL> self . add_shell_command_output ( output_paths [ '<STR_LIT>' ] ) <EOL> return self . create_tarball ( temp_dir_path ) <EOL> except Exception as e : <EOL> LOG . exception ( '<STR_LIT>' , exc_info = True ) <EOL> raise e <EOL> def encrypt_archive ( self , archive_file_path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> assert archive_file_path . endswith ( '<STR_LIT>' ) <EOL> LOG . info ( '<STR_LIT>' ) <EOL> gpg = gnupg . GPG ( verbose = self . debug ) <EOL> import_result = gpg . import_keys ( self . gpg_key ) <EOL> assert import_result . count == <NUM_LIT:1> <EOL> encrypted_archive_output_file_name = os . path . basename ( archive_file_path ) + '<STR_LIT>' <EOL> encrypted_archive_output_file_path = os . path . join ( '<STR_LIT>' , <EOL> encrypted_archive_output_file_name ) <EOL> with open ( archive_file_path , '<STR_LIT:rb>' ) as fp : <EOL> gpg . encrypt_file ( file = fp , <EOL> recipients = self . gpg_key_fingerprint , <EOL> always_trust = True , <EOL> output = encrypted_archive_output_file_path ) <EOL> return encrypted_archive_output_file_path <EOL> except Exception as e : <EOL> LOG . exception ( '<STR_LIT>' , exc_info = True ) <EOL> raise e <EOL> def upload_archive ( self , archive_file_path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> assert archive_file_path . endswith ( '<STR_LIT>' ) <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> file_name = os . path . basename ( archive_file_path ) <EOL> url = self . s3_bucket_url + file_name <EOL> assert url . startswith ( '<STR_LIT>' ) <EOL> with open ( archive_file_path , '<STR_LIT:rb>' ) as fp : <EOL> response = requests . put ( url = url , files = { '<STR_LIT:file>' : fp } ) <EOL> assert response . status_code == httplib . OK <EOL> except Exception as e : <EOL> LOG . exception ( '<STR_LIT>' % self . company_name , exc_info = True ) <EOL> raise e <EOL> def collect_logs ( self , output_path ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> for file_path_glob in self . log_file_paths : <EOL> log_file_list = get_full_file_list ( file_path_glob = file_path_glob ) <EOL> copy_files ( file_paths = log_file_list , destination = output_path ) <EOL> def collect_config_files ( self , output_path ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> copy_files ( file_paths = self . config_file_paths , destination = output_path ) <EOL> st2_config_path = os . path . join ( output_path , self . st2_config_file_name ) <EOL> process_st2_config ( config_path = st2_config_path ) <EOL> mistral_config_path = os . path . join ( output_path , self . mistral_config_file_name ) <EOL> process_mistral_config ( config_path = mistral_config_path ) <EOL> @ staticmethod <EOL> def collect_pack_content ( output_path ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> packs_base_paths = get_packs_base_paths ( ) <EOL> for index , packs_base_path in enumerate ( packs_base_paths , <NUM_LIT:1> ) : <EOL> dst = os . path . join ( output_path , '<STR_LIT>' % index ) <EOL> try : <EOL> shutil . copytree ( src = packs_base_path , dst = dst ) <EOL> except IOError : <EOL> continue <EOL> base_pack_dirs = get_dirs_in_path ( file_path = output_path ) <EOL> for base_pack_dir in base_pack_dirs : <EOL> pack_dirs = get_dirs_in_path ( file_path = base_pack_dir ) <EOL> for pack_dir in pack_dirs : <EOL> process_content_pack_dir ( pack_dir = pack_dir ) <EOL> def add_system_information ( self , output_path ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> system_information = yaml . dump ( self . get_system_information ( ) , <EOL> default_flow_style = False ) <EOL> with open ( output_path , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( system_information ) <EOL> def add_user_info ( self , output_path ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> user_info = yaml . dump ( self . user_info , default_flow_style = False ) <EOL> with open ( output_path , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( user_info ) <EOL> def add_shell_command_output ( self , output_path ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> for cmd in self . shell_commands : <EOL> output_file = os . path . join ( output_path , '<STR_LIT>' % self . format_output_filename ( cmd ) ) <EOL> exit_code , stdout , stderr = run_command ( cmd = cmd , shell = True ) <EOL> with open ( output_file , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> fp . write ( stdout ) <EOL> fp . write ( '<STR_LIT>' ) <EOL> fp . write ( '<STR_LIT>' ) <EOL> fp . write ( stderr ) <EOL> fp . write ( '<STR_LIT>' ) <EOL> def create_tarball ( self , temp_dir_path ) : <EOL> """<STR_LIT>""" <EOL> LOG . info ( '<STR_LIT>' ) <EOL> if self . output_path : <EOL> output_file_path = self . output_path <EOL> else : <EOL> date = date_utils . get_datetime_utc_now ( ) . strftime ( DATE_FORMAT ) <EOL> values = { '<STR_LIT>' : socket . gethostname ( ) , '<STR_LIT:date>' : date } <EOL> output_file_name = OUTPUT_FILENAME_TEMPLATE % values <EOL> output_file_path = os . path . join ( '<STR_LIT>' , output_file_name ) <EOL> with tarfile . open ( output_file_path , '<STR_LIT>' ) as tar : <EOL> tar . add ( temp_dir_path , arcname = '<STR_LIT>' ) <EOL> return output_file_path <EOL> @ staticmethod <EOL> def create_temp_directories ( ) : <EOL> """<STR_LIT>""" <EOL> temp_dir_path = tempfile . mkdtemp ( ) <EOL> for directory_name in DIRECTORY_STRUCTURE : <EOL> full_path = os . path . join ( temp_dir_path , directory_name ) <EOL> os . mkdir ( full_path ) <EOL> return temp_dir_path <EOL> @ staticmethod <EOL> def format_output_filename ( cmd ) : <EOL> """<STR_LIT>""" <EOL> return cmd . translate ( None , """<STR_LIT>""" ) <EOL> @ staticmethod <EOL> def get_system_information ( ) : <EOL> """<STR_LIT>""" <EOL> system_information = { <EOL> '<STR_LIT>' : socket . gethostname ( ) , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } <EOL> } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = platform . system ( ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = platform . release ( ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = platform . platform ( ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = platform . system ( ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT:U+0020>' . join ( platform . architecture ( ) ) <EOL> if platform . system ( ) . lower ( ) == '<STR_LIT>' : <EOL> distribution = '<STR_LIT:U+0020>' . join ( platform . linux_distribution ( ) ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = distribution <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT:version>' ] = sys . version . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> cpu_info = get_cpu_info ( ) <EOL> if cpu_info : <EOL> core_count = len ( cpu_info ) <EOL> model = cpu_info [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : core_count , <EOL> '<STR_LIT>' : model <EOL> } <EOL> else : <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> memory_info = get_memory_info ( ) <EOL> if memory_info : <EOL> total = memory_info [ '<STR_LIT>' ] / <NUM_LIT> <EOL> free = memory_info [ '<STR_LIT>' ] / <NUM_LIT> <EOL> used = ( total - free ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : total , <EOL> '<STR_LIT>' : used , <EOL> '<STR_LIT>' : free <EOL> } <EOL> else : <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT:version>' ] = st2_version <EOL> st2common_path = st2common . __file__ <EOL> st2common_path = os . path . dirname ( st2common_path ) <EOL> if '<STR_LIT>' in st2common_path : <EOL> base_install_path = st2common_path . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> revision_hash = get_repo_latest_revision_hash ( repo_path = base_install_path ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT:source>' <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = revision_hash <EOL> else : <EOL> package_list = get_package_list ( name_startswith = '<STR_LIT>' ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = package_list <EOL> repo_path = '<STR_LIT>' <EOL> revision_hash = get_repo_latest_revision_hash ( repo_path = repo_path ) <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT:source>' <EOL> system_information [ '<STR_LIT>' ] [ '<STR_LIT>' ] = revision_hash <EOL> return system_information <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> setup_logging ( ) <EOL> abort = True <EOL> for arg_name in ARG_NAMES : <EOL> abort &= getattr ( args , arg_name , False ) <EOL> if abort : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if args . config : <EOL> try : <EOL> with open ( args . config , '<STR_LIT:r>' ) as yaml_file : <EOL> config_file = yaml . safe_load ( yaml_file ) <EOL> except Exception as e : <EOL> LOG . error ( '<STR_LIT>' % e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not isinstance ( config_file , dict ) : <EOL> LOG . error ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> config_file = { } <EOL> company_name = config_file . get ( '<STR_LIT>' , COMPANY_NAME ) <EOL> encrypt = True <EOL> upload = True <EOL> if args . review : <EOL> encrypt = False <EOL> upload = False <EOL> if encrypt : <EOL> if not GPG_INSTALLED : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise ValueError ( msg ) <EOL> if not args . yes and not args . existing_file and upload : <EOL> submitted_content = [ name . replace ( '<STR_LIT>' , '<STR_LIT>' ) for name in ARG_NAMES if <EOL> not getattr ( args , name , False ) ] <EOL> submitted_content = '<STR_LIT:U+002CU+0020>' . join ( submitted_content ) <EOL> print ( '<STR_LIT>' % ( company_name , <EOL> submitted_content ) ) <EOL> value = six . moves . input ( '<STR_LIT>' ) <EOL> if value . strip ( ) . lower ( ) not in [ '<STR_LIT:y>' , '<STR_LIT:yes>' ] : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> user_info = { } <EOL> if not args . yes and not args . existing_file : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> value = six . moves . input ( '<STR_LIT>' ) <EOL> if value . strip ( ) . lower ( ) in [ '<STR_LIT:y>' , '<STR_LIT:yes>' ] : <EOL> user_info [ '<STR_LIT:name>' ] = six . moves . input ( '<STR_LIT>' ) <EOL> user_info [ '<STR_LIT:email>' ] = six . moves . input ( '<STR_LIT>' ) <EOL> user_info [ '<STR_LIT>' ] = six . moves . input ( '<STR_LIT>' ) <EOL> debug_collector = DebugInfoCollector ( include_logs = not args . exclude_logs , <EOL> include_configs = not args . exclude_configs , <EOL> include_content = not args . exclude_content , <EOL> include_system_info = not args . exclude_system_info , <EOL> include_shell_commands = not args . exclude_shell_commands , <EOL> user_info = user_info , <EOL> debug = args . debug , <EOL> config_file = config_file , <EOL> output_path = args . output ) <EOL> debug_collector . run ( encrypt = encrypt , upload = upload , existing_file = args . existing_file ) </s>
