<s> import subprocess <EOL> import dcoscli <EOL> import docopt <EOL> from dcos import cmds , emitting , marathon , mesos , util <EOL> from dcos . errors import DCOSException , DefaultError <EOL> from dcoscli import log , tables <EOL> from dcoscli . subcommand import default_command_info , default_doc <EOL> from dcoscli . util import decorate_docopt_usage <EOL> logger = util . get_logger ( __name__ ) <EOL> emitter = emitting . FlatEmitter ( ) <EOL> def main ( argv ) : <EOL> try : <EOL> return _main ( argv ) <EOL> except DCOSException as e : <EOL> emitter . publish ( e ) <EOL> return <NUM_LIT:1> <EOL> @ decorate_docopt_usage <EOL> def _main ( argv ) : <EOL> args = docopt . docopt ( <EOL> default_doc ( "<STR_LIT>" ) , <EOL> argv = argv , <EOL> version = "<STR_LIT>" . format ( dcoscli . version ) ) <EOL> return cmds . execute ( _cmds ( ) , args ) <EOL> def _cmds ( ) : <EOL> """<STR_LIT>""" <EOL> return [ <EOL> cmds . Command ( <EOL> hierarchy = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> arg_keys = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> function = _log ) , <EOL> cmds . Command ( <EOL> hierarchy = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> arg_keys = [ '<STR_LIT>' ] , <EOL> function = _shutdown ) , <EOL> cmds . Command ( <EOL> hierarchy = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> arg_keys = [ ] , <EOL> function = _info ) , <EOL> cmds . Command ( <EOL> hierarchy = [ '<STR_LIT>' ] , <EOL> arg_keys = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> function = _service ) , <EOL> ] <EOL> def _info ( ) : <EOL> """<STR_LIT>""" <EOL> emitter . publish ( default_command_info ( "<STR_LIT>" ) ) <EOL> return <NUM_LIT:0> <EOL> def _service ( inactive , completed , is_json ) : <EOL> """<STR_LIT>""" <EOL> services = mesos . get_master ( ) . frameworks ( <EOL> inactive = inactive , <EOL> completed = completed ) <EOL> if is_json : <EOL> emitter . publish ( [ service . dict ( ) for service in services ] ) <EOL> else : <EOL> table = tables . service_table ( services ) <EOL> output = str ( table ) <EOL> if output : <EOL> emitter . publish ( output ) <EOL> return <NUM_LIT:0> <EOL> def _shutdown ( service_id ) : <EOL> """<STR_LIT>""" <EOL> mesos . DCOSClient ( ) . shutdown_framework ( service_id ) <EOL> return <NUM_LIT:0> <EOL> def _log ( follow , lines , ssh_config_file , service , file_ ) : <EOL> """<STR_LIT>""" <EOL> if lines is None : <EOL> lines = <NUM_LIT:10> <EOL> lines = util . parse_int ( lines ) <EOL> if service == '<STR_LIT>' : <EOL> if file_ : <EOL> raise DCOSException ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return _log_marathon ( follow , lines , ssh_config_file ) <EOL> else : <EOL> if ssh_config_file : <EOL> raise DCOSException ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return _log_service ( follow , lines , service , file_ ) <EOL> def _log_service ( follow , lines , service , file_ ) : <EOL> """<STR_LIT>""" <EOL> if file_ is None : <EOL> file_ = '<STR_LIT>' <EOL> task = _get_service_task ( service ) <EOL> return _log_task ( task [ '<STR_LIT:id>' ] , follow , lines , file_ ) <EOL> def _log_task ( task_id , follow , lines , file_ ) : <EOL> """<STR_LIT>""" <EOL> dcos_client = mesos . DCOSClient ( ) <EOL> task = mesos . get_master ( dcos_client ) . task ( task_id ) <EOL> mesos_file = mesos . MesosFile ( file_ , task = task , dcos_client = dcos_client ) <EOL> return log . log_files ( [ mesos_file ] , follow , lines ) <EOL> def _get_service_task ( service_name ) : <EOL> """<STR_LIT>""" <EOL> marathon_client = marathon . create_client ( ) <EOL> app = _get_service_app ( marathon_client , service_name ) <EOL> tasks = marathon_client . get_app ( app [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] <EOL> if len ( tasks ) != <NUM_LIT:1> : <EOL> raise DCOSException ( <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) . format ( app [ '<STR_LIT:id>' ] , len ( tasks ) ) ) <EOL> return tasks [ <NUM_LIT:0> ] <EOL> def _get_service_app ( marathon_client , service_name ) : <EOL> """<STR_LIT>""" <EOL> apps = marathon_client . get_apps_for_framework ( service_name ) <EOL> if len ( apps ) > <NUM_LIT:1> : <EOL> raise DCOSException ( <EOL> '<STR_LIT>' . format ( <EOL> service_name , <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' . format ( app [ '<STR_LIT:id>' ] ) for app in apps ) ) ) <EOL> elif len ( apps ) == <NUM_LIT:0> : <EOL> raise DCOSException ( <EOL> '<STR_LIT>' . format ( service_name ) ) <EOL> else : <EOL> return apps [ <NUM_LIT:0> ] <EOL> def _log_marathon ( follow , lines , ssh_config_file ) : <EOL> """<STR_LIT>""" <EOL> ssh_options = util . get_ssh_options ( ssh_config_file , [ ] ) <EOL> journalctl_args = '<STR_LIT>' <EOL> if follow : <EOL> journalctl_args += '<STR_LIT>' <EOL> if lines : <EOL> journalctl_args += '<STR_LIT>' . format ( lines ) <EOL> leader_ip = marathon . create_client ( ) . get_leader ( ) . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> user_string = '<STR_LIT>' <EOL> if ssh_config_file : <EOL> user_string = '<STR_LIT>' <EOL> cmd = ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) . format ( <EOL> ssh_options , <EOL> user_string , <EOL> leader_ip , <EOL> journalctl_args ) <EOL> emitter . publish ( DefaultError ( "<STR_LIT>" . format ( cmd ) ) ) <EOL> return subprocess . call ( cmd , shell = True ) </s>
