<s> from __future__ import absolute_import , print_function <EOL> import os <EOL> import sys <EOL> import subprocess <EOL> import textwrap <EOL> import fabric <EOL> import fabric . api as fab <EOL> from testmill import ( login , cache , keypair , manifest , application , <EOL> error , util , console , inflect ) <EOL> usage = textwrap . dedent ( """<STR_LIT>""" ) <EOL> description = textwrap . dedent ( """<STR_LIT>""" ) <EOL> def add_args ( parser ) : <EOL> parser . usage = usage <EOL> parser . description = description <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:?>' , default = '<STR_LIT>' ) <EOL> def do_ssh ( args , env ) : <EOL> """<STR_LIT>""" <EOL> with env . let ( quiet = True ) : <EOL> login . default_login ( ) <EOL> keypair . default_keypair ( ) <EOL> if manifest . manifest_exists ( ) : <EOL> with env . let ( quiet = True ) : <EOL> manif = manifest . default_manifest ( ) <EOL> else : <EOL> manif = None <EOL> parts = args . application . split ( '<STR_LIT::>' ) <EOL> if len ( parts ) in ( <NUM_LIT:1> , <NUM_LIT:2> ) and manif is None : <EOL> error . raise_error ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> manifest . manifest_name ( ) ) <EOL> if len ( parts ) in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> project = manif [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] <EOL> console . info ( '<STR_LIT>' , project ) <EOL> defname = parts [ <NUM_LIT:0> ] <EOL> instance = parts [ <NUM_LIT:1> ] if len ( parts ) == <NUM_LIT:2> else None <EOL> elif len ( parts ) == <NUM_LIT:3> : <EOL> project , defname , instance = parts <EOL> else : <EOL> error . raise_error ( '<STR_LIT>' , appname ) <EOL> apps = cache . find_applications ( project , defname , instance ) <EOL> if len ( apps ) == <NUM_LIT:0> : <EOL> error . raise_error ( '<STR_LIT>' , <EOL> defname ) <EOL> elif len ( apps ) > <NUM_LIT:1> : <EOL> error . raise_error ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> defname ) <EOL> app = cache . get_application ( apps [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] ) <EOL> appname = app [ '<STR_LIT:name>' ] <EOL> _ , _ , instance = appname . split ( '<STR_LIT::>' ) <EOL> vmname = args . vm <EOL> vm = application . get_vm ( app , vmname ) <EOL> if vm is None : <EOL> error . raise_error ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> defname , instance , vmname ) <EOL> console . info ( "<STR_LIT>" , <EOL> vmname , defname , instance ) <EOL> state = application . get_application_state ( app ) <EOL> if state not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> error . raise_error ( "<STR_LIT>" , vmname ) <EOL> userdata = vm . get ( '<STR_LIT>' , { } ) <EOL> vmkey = userdata . get ( '<STR_LIT>' , { } ) <EOL> if vmkey . get ( '<STR_LIT:id>' ) != env . public_key [ '<STR_LIT:id>' ] : <EOL> error . raise_error ( "<STR_LIT>" , <EOL> vmkey . get ( '<STR_LIT:name>' ) ) <EOL> application . start_application ( app ) <EOL> application . wait_for_application ( app , [ vmname ] ) <EOL> host = '<STR_LIT>' . format ( vm [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> command = '<STR_LIT>' . format ( args . testid ) <EOL> openssh = util . find_openssh ( ) <EOL> interactive = os . isatty ( sys . stdin . fileno ( ) ) <EOL> if interactive and openssh : <EOL> if not sys . platform . startswith ( '<STR_LIT>' ) : <EOL> argv = [ '<STR_LIT>' , '<STR_LIT>' , env . private_key_file , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , host , command ] <EOL> console . debug ( '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( argv ) ) <EOL> os . execve ( openssh , argv , os . environ ) <EOL> else : <EOL> command = [ openssh , '<STR_LIT>' , env . private_key_file , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , host , command ] <EOL> ssh = subprocess . Popen ( command ) <EOL> ret = ssh . wait ( ) <EOL> error . exit ( ret ) <EOL> console . info ( textwrap . dedent ( """<STR_LIT>""" ) ) <EOL> fab . env . host_string = host <EOL> fab . env . key_filename = env . private_key_file <EOL> fab . env . disable_known_hosts = True <EOL> fab . env . remote_interrupt = True <EOL> fab . env . output_prefix = None <EOL> fabric . state . output . running = None <EOL> fabric . state . output . status = None <EOL> ret = fab . run ( command , warn_only = True ) <EOL> return ret . return_code </s>
