<s> import sys <EOL> from awscli . customizations . commands import BasicCommand <EOL> from awscli . customizations . codedeploy . systems import DEFAULT_CONFIG_FILE <EOL> from awscli . customizations . codedeploy . utils import validate_region , validate_instance_name , validate_tags , validate_iam_user_arn , INSTANCE_NAME_ARG , IAM_USER_ARN_ARG <EOL> class Register ( BasicCommand ) : <EOL> NAME = '<STR_LIT>' <EOL> DESCRIPTION = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> TAGS_SCHEMA = { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : True <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : True <EOL> } <EOL> } <EOL> } <EOL> } <EOL> ARG_TABLE = [ <EOL> INSTANCE_NAME_ARG , <EOL> { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT:+>' , <EOL> '<STR_LIT>' : TAGS_SCHEMA , <EOL> '<STR_LIT>' : ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> } , <EOL> IAM_USER_ARN_ARG <EOL> ] <EOL> def _run_main ( self , parsed_args , parsed_globals ) : <EOL> params = parsed_args <EOL> params . session = self . _session <EOL> validate_region ( params , parsed_globals ) <EOL> validate_instance_name ( params ) <EOL> validate_tags ( params ) <EOL> validate_iam_user_arn ( params ) <EOL> self . codedeploy = self . _session . create_client ( <EOL> '<STR_LIT>' , <EOL> region_name = params . region , <EOL> endpoint_url = parsed_globals . endpoint_url , <EOL> verify = parsed_globals . verify_ssl <EOL> ) <EOL> self . iam = self . _session . create_client ( <EOL> '<STR_LIT>' , <EOL> region_name = params . region <EOL> ) <EOL> try : <EOL> if not params . iam_user_arn : <EOL> self . _create_iam_user ( params ) <EOL> self . _create_access_key ( params ) <EOL> self . _create_user_policy ( params ) <EOL> self . _create_config ( params ) <EOL> self . _register_instance ( params ) <EOL> if params . tags : <EOL> self . _add_tags ( params ) <EOL> sys . stdout . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> DEFAULT_CONFIG_FILE <EOL> ) <EOL> ) <EOL> except Exception as e : <EOL> sys . stdout . flush ( ) <EOL> sys . stderr . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( e ) <EOL> ) <EOL> def _create_iam_user ( self , params ) : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> params . user_name = params . instance_name <EOL> response = self . iam . create_user ( <EOL> Path = '<STR_LIT>' , <EOL> UserName = params . user_name <EOL> ) <EOL> params . iam_user_arn = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> sys . stdout . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> params . iam_user_arn <EOL> ) <EOL> ) <EOL> def _create_access_key ( self , params ) : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> response = self . iam . create_access_key ( <EOL> UserName = params . user_name <EOL> ) <EOL> params . access_key_id = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> params . secret_access_key = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> sys . stdout . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> params . access_key_id , <EOL> params . secret_access_key <EOL> ) <EOL> ) <EOL> def _create_user_policy ( self , params ) : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> params . policy_name = '<STR_LIT>' <EOL> params . policy_document = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:}>' <EOL> ) <EOL> self . iam . put_user_policy ( <EOL> UserName = params . user_name , <EOL> PolicyName = params . policy_name , <EOL> PolicyDocument = params . policy_document <EOL> ) <EOL> sys . stdout . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> params . policy_name , <EOL> params . policy_document <EOL> ) <EOL> ) <EOL> def _create_config ( self , params ) : <EOL> sys . stdout . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( DEFAULT_CONFIG_FILE ) <EOL> ) <EOL> with open ( DEFAULT_CONFIG_FILE , '<STR_LIT:w>' ) as f : <EOL> f . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> params . region , <EOL> params . iam_user_arn , <EOL> params . access_key_id , <EOL> params . secret_access_key <EOL> ) <EOL> ) <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> def _register_instance ( self , params ) : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> self . codedeploy . register_on_premises_instance ( <EOL> instanceName = params . instance_name , <EOL> iamUserArn = params . iam_user_arn <EOL> ) <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> def _add_tags ( self , params ) : <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> self . codedeploy . add_tags_to_on_premises_instances ( <EOL> tags = params . tags , <EOL> instanceNames = [ params . instance_name ] <EOL> ) <EOL> sys . stdout . write ( '<STR_LIT>' ) </s>
