<s> import argparse <EOL> import os <EOL> import re <EOL> import sys <EOL> import tempfile <EOL> import time <EOL> from spinnaker . fetch import fetch <EOL> from spinnaker . fetch import GOOGLE_INSTANCE_METADATA_URL <EOL> from spinnaker . run import run_quick <EOL> from spinnaker . run import check_run_quick <EOL> from spinnaker . yaml_util import YamlBindings <EOL> __NEXT_STEP_INSTRUCTIONS = """<STR_LIT>""" <EOL> def get_project ( options ) : <EOL> """<STR_LIT>""" <EOL> if not options . project : <EOL> result = check_run_quick ( '<STR_LIT>' , echo = False ) <EOL> options . project = re . search ( '<STR_LIT>' , result . stdout ) . group ( <NUM_LIT:1> ) <EOL> return options . project <EOL> def get_zone ( options ) : <EOL> """<STR_LIT>""" <EOL> if not options . zone : <EOL> result = fetch ( os . path . join ( GOOGLE_INSTANCE_METADATA_URL , '<STR_LIT>' ) , <EOL> google = True ) <EOL> if result . ok ( ) : <EOL> options . zone = os . path . basename ( result . content ) <EOL> else : <EOL> options . zone = '<STR_LIT>' <EOL> return options . zone <EOL> def init_argument_parser ( parser ) : <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> default = '<STR_LIT>' . format ( user = os . environ [ '<STR_LIT>' ] ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = True , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def try_until_ready ( command ) : <EOL> while True : <EOL> result = run_quick ( command , echo = False ) <EOL> if not result . returncode : <EOL> break <EOL> msg = result . stderr or result . stdout <EOL> if msg . find ( '<STR_LIT>' ) > <NUM_LIT:0> : <EOL> print '<STR_LIT>' <EOL> else : <EOL> print msg . strip ( ) <EOL> print '<STR_LIT>' <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> def make_remote_directories ( options ) : <EOL> all = [ ] <EOL> if options . copy_personal_files : <EOL> all . append ( '<STR_LIT>' ) <EOL> if options . aws_credentials : <EOL> all . append ( '<STR_LIT>' ) <EOL> if options . master_yml : <EOL> all . append ( '<STR_LIT>' ) <EOL> if options . copy_gcloud_config : <EOL> all . append ( '<STR_LIT>' ) <EOL> if all : <EOL> command = '<STR_LIT:U+0020>' . join ( [ <EOL> '<STR_LIT>' , <EOL> options . instance , <EOL> '<STR_LIT>' , get_project ( options ) , <EOL> '<STR_LIT>' , get_zone ( options ) , <EOL> '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( all ) ) ] ) <EOL> try_until_ready ( command ) <EOL> def copy_custom_file ( options , source , target ) : <EOL> command = '<STR_LIT:U+0020>' . join ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , get_project ( options ) , <EOL> '<STR_LIT>' , get_zone ( options ) , <EOL> source , <EOL> '<STR_LIT>' . format ( instance = options . instance , <EOL> target = target ) ] ) <EOL> try_until_ready ( command ) <EOL> def copy_home_file_list ( options , type , base_dir , sources ) : <EOL> have = [ ] <EOL> for file in sources : <EOL> full_path = os . path . abspath ( <EOL> os . path . join ( os . environ [ '<STR_LIT>' ] , base_dir , file ) ) <EOL> if os . path . exists ( full_path ) : <EOL> have . append ( '<STR_LIT>' . format ( full_path ) ) <EOL> if have : <EOL> print '<STR_LIT>' . format ( type = type ) <EOL> source_list = '<STR_LIT:U+0020>' . join ( have ) <EOL> copy_custom_file ( options , source_list , base_dir ) <EOL> else : <EOL> print '<STR_LIT>' . format ( type = type ) <EOL> def maybe_inform ( type , test_path , option_to_enable ) : <EOL> if os . path . exists ( os . path . join ( os . environ [ '<STR_LIT>' ] , test_path ) ) : <EOL> print '<STR_LIT>' . format ( <EOL> type = type , option = option_to_enable ) <EOL> def maybe_copy_aws_credentials ( options ) : <EOL> if options . aws_credentials : <EOL> print '<STR_LIT>' <EOL> copy_custom_file ( options , options . aws_credentials , '<STR_LIT>' ) <EOL> else : <EOL> maybe_inform ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def maybe_copy_gcloud_config ( options ) : <EOL> if options . copy_gcloud_config : <EOL> copy_home_file_list ( options , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> else : <EOL> maybe_inform ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def maybe_copy_git_credentials ( options ) : <EOL> if options . copy_git_credentials : <EOL> copy_home_file_list ( options , '<STR_LIT>' , <EOL> '<STR_LIT:.>' , [ '<STR_LIT>' ] ) <EOL> else : <EOL> maybe_inform ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def maybe_copy_ssh_credentials ( options ) : <EOL> if options . copy_ssh_credentials : <EOL> copy_home_file_list ( options , '<STR_LIT>' , <EOL> '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> else : <EOL> maybe_inform ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def copy_personal_files ( options ) : <EOL> copy_home_file_list ( options , '<STR_LIT>' , <EOL> '<STR_LIT:.>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> copy_home_file_list ( options , '<STR_LIT>' , <EOL> '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> def create_instance ( options ) : <EOL> """<STR_LIT>""" <EOL> project = get_project ( options ) <EOL> print '<STR_LIT>' . format ( <EOL> project = project , zone = get_zone ( options ) , instance = options . instance ) <EOL> print ( '<STR_LIT>' <EOL> . format ( type = options . machine_type , disk_size = options . disk_size ) ) <EOL> google_dev_dir = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> dev_dir = os . path . dirname ( __file__ ) <EOL> project_dir = os . path . join ( dev_dir , '<STR_LIT:..>' ) <EOL> install_dir = '<STR_LIT>' . format ( dir = dev_dir ) <EOL> startup_command = [ '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> fd , temp_startup = tempfile . mkstemp ( ) <EOL> os . write ( fd , '<STR_LIT:;>' . join ( startup_command ) ) <EOL> os . close ( fd ) <EOL> metadata_files = [ <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( google_dev_dir = google_dev_dir , <EOL> dev_dir = dev_dir , <EOL> project_dir = project_dir , <EOL> temp_startup = temp_startup ) ] <EOL> metadata = '<STR_LIT:U+002C>' . join ( [ <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ] ) <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> options . instance , <EOL> '<STR_LIT>' , get_project ( options ) , <EOL> '<STR_LIT>' , get_zone ( options ) , <EOL> '<STR_LIT>' , options . machine_type , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' . format ( size = options . disk_size ) , <EOL> '<STR_LIT>' . format ( type = options . disk_type ) , <EOL> '<STR_LIT>' , metadata , <EOL> '<STR_LIT>' . format ( <EOL> files = '<STR_LIT:U+002C>' . join ( metadata_files ) ) ] <EOL> if options . address : <EOL> command . extend ( [ '<STR_LIT>' , options . address ] ) <EOL> check_run_quick ( '<STR_LIT:U+0020>' . join ( command ) , echo = False ) <EOL> def maybe_copy_master_yml ( options ) : <EOL> """<STR_LIT>""" <EOL> if not options . master_yml : <EOL> maybe_inform ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> return <EOL> bindings = YamlBindings ( ) <EOL> bindings . import_path ( options . master_yml ) <EOL> try : <EOL> json_credential_path = bindings . get ( <EOL> '<STR_LIT>' ) <EOL> except KeyError : <EOL> json_credential_path = None <EOL> gcp_home = os . path . join ( '<STR_LIT>' , os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> gcp_credential_path = os . path . join ( gcp_home , '<STR_LIT>' ) <EOL> with open ( options . master_yml , '<STR_LIT:r>' ) as f : <EOL> content = f . read ( ) <EOL> if json_credential_path : <EOL> if not os . path . exists ( json_credential_path ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( options . master_yml , <EOL> json_credential_path ) ) <EOL> content = content . replace ( json_credential_path , gcp_credential_path ) <EOL> fd , temp_path = tempfile . mkstemp ( ) <EOL> os . fchmod ( fd , os . stat ( options . master_yml ) . st_mode ) <EOL> os . write ( fd , content ) <EOL> os . close ( fd ) <EOL> actual_path = temp_path <EOL> copy_custom_file ( options , actual_path , '<STR_LIT>' ) <EOL> if json_credential_path : <EOL> copy_custom_file ( options , json_credential_path , <EOL> '<STR_LIT>' ) <EOL> if temp_path : <EOL> os . remove ( temp_path ) <EOL> def check_gcloud ( ) : <EOL> result = run_quick ( '<STR_LIT>' , echo = False ) <EOL> if not result . returncode : <EOL> return <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> def check_args ( options ) : <EOL> """<STR_LIT>""" <EOL> for path in [ options . aws_credentials , options . master_yml ] : <EOL> if path and not os . path . exists ( path ) : <EOL> sys . stderr . write ( '<STR_LIT>' . format ( path = path ) ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> check_gcloud ( ) <EOL> parser = argparse . ArgumentParser ( ) <EOL> init_argument_parser ( parser ) <EOL> options = parser . parse_args ( ) <EOL> if options . copy_private_files : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> check_args ( options ) <EOL> create_instance ( options ) <EOL> make_remote_directories ( options ) <EOL> if options . copy_personal_files : <EOL> copy_personal_files ( options ) <EOL> maybe_copy_ssh_credentials ( options ) <EOL> maybe_copy_git_credentials ( options ) <EOL> maybe_copy_aws_credentials ( options ) <EOL> maybe_copy_gcloud_config ( options ) <EOL> maybe_copy_master_yml ( options ) <EOL> print __NEXT_STEP_INSTRUCTIONS . format ( <EOL> project = get_project ( options ) , <EOL> zone = get_zone ( options ) , <EOL> instance = options . instance ) </s>
