1111# ANY KIND, either express or implied. See the License for the specific
1212# language governing permissions and limitations under the License.
1313
14+ import os
1415import sys
1516
16- from awscli .customizations .commands import BasicCommand
1717from awscli .customizations .codedeploy .systems import DEFAULT_CONFIG_FILE
18- from awscli .customizations .codedeploy .utils import \
19- validate_region , validate_instance_name , validate_tags , \
20- validate_iam_user_arn , INSTANCE_NAME_ARG , IAM_USER_ARN_ARG
18+ from awscli .customizations .codedeploy .utils import (
19+ IAM_USER_ARN_ARG ,
20+ INSTANCE_NAME_ARG ,
21+ validate_iam_user_arn ,
22+ validate_instance_name ,
23+ validate_region ,
24+ validate_tags ,
25+ )
26+ from awscli .customizations .commands import BasicCommand
2127from awscli .utils import create_nested_client
2228
2329
@@ -39,15 +45,15 @@ class Register(BasicCommand):
3945 "Key" : {
4046 "description" : "The tag key." ,
4147 "type" : "string" ,
42- "required" : True
48+ "required" : True ,
4349 },
4450 "Value" : {
4551 "description" : "The tag value." ,
4652 "type" : "string" ,
47- "required" : True
48- }
49- }
50- }
53+ "required" : True ,
54+ },
55+ },
56+ },
5157 }
5258
5359 ARG_TABLE = [
@@ -61,9 +67,9 @@ class Register(BasicCommand):
6167 'help_text' : (
6268 'Optional. The list of key/value pairs to tag the on-premises '
6369 'instance.'
64- )
70+ ),
6571 },
66- IAM_USER_ARN_ARG
72+ IAM_USER_ARN_ARG ,
6773 ]
6874
6975 def _run_main (self , parsed_args , parsed_globals ):
@@ -79,12 +85,10 @@ def _run_main(self, parsed_args, parsed_globals):
7985 'codedeploy' ,
8086 region_name = params .region ,
8187 endpoint_url = parsed_globals .endpoint_url ,
82- verify = parsed_globals .verify_ssl
88+ verify = parsed_globals .verify_ssl ,
8389 )
8490 self .iam = create_nested_client (
85- self ._session ,
86- 'iam' ,
87- region_name = params .region
91+ self ._session , 'iam' , region_name = params .region
8892 )
8993
9094 try :
@@ -97,54 +101,41 @@ def _run_main(self, parsed_args, parsed_globals):
97101 if params .tags :
98102 self ._add_tags (params )
99103 sys .stdout .write (
100- 'Copy the on-premises configuration file named {0 } to the '
104+ f 'Copy the on-premises configuration file named { DEFAULT_CONFIG_FILE } to the '
101105 'on-premises instance, and run the following command on the '
102106 'on-premises instance to install and configure the AWS '
103107 'CodeDeploy Agent:\n '
104- 'aws deploy install --config-file {0}\n ' .format (
105- DEFAULT_CONFIG_FILE
106- )
108+ f'aws deploy install --config-file { DEFAULT_CONFIG_FILE } \n '
107109 )
108110 except Exception as e :
109111 sys .stdout .flush ()
110112 sys .stderr .write (
111113 'ERROR\n '
112- '{0 }\n '
114+ f' { e } \n '
113115 'Register the on-premises instance by following the '
114116 'instructions in "Configure Existing On-Premises Instances by '
115117 'Using AWS CodeDeploy" in the AWS CodeDeploy User '
116- 'Guide.\n ' . format ( e )
118+ 'Guide.\n '
117119 )
118120
119121 def _create_iam_user (self , params ):
120122 sys .stdout .write ('Creating the IAM user... ' )
121123 params .user_name = params .instance_name
122124 response = self .iam .create_user (
123- Path = '/AWS/CodeDeploy/' ,
124- UserName = params .user_name
125+ Path = '/AWS/CodeDeploy/' , UserName = params .user_name
125126 )
126127 params .iam_user_arn = response ['User' ]['Arn' ]
127- sys .stdout .write (
128- 'DONE\n '
129- 'IamUserArn: {0}\n ' .format (
130- params .iam_user_arn
131- )
132- )
128+ sys .stdout .write ('DONE\n ' f'IamUserArn: { params .iam_user_arn } \n ' )
133129
134130 def _create_access_key (self , params ):
135131 sys .stdout .write ('Creating the IAM user access key... ' )
136- response = self .iam .create_access_key (
137- UserName = params .user_name
138- )
132+ response = self .iam .create_access_key (UserName = params .user_name )
139133 params .access_key_id = response ['AccessKey' ]['AccessKeyId' ]
140134 params .secret_access_key = response ['AccessKey' ]['SecretAccessKey' ]
141135 sys .stdout .write (
142136 'DONE\n '
143- 'AccessKeyId: {0}\n '
144- 'SecretAccessKey: {1}\n ' .format (
145- params .access_key_id ,
146- params .secret_access_key
147- )
137+ f'AccessKeyId: { params .access_key_id } \n '
138+ f'SecretAccessKey: { params .secret_access_key } \n '
148139 )
149140
150141 def _create_user_policy (self , params ):
@@ -163,49 +154,50 @@ def _create_user_policy(self, params):
163154 self .iam .put_user_policy (
164155 UserName = params .user_name ,
165156 PolicyName = params .policy_name ,
166- PolicyDocument = params .policy_document
157+ PolicyDocument = params .policy_document ,
167158 )
168159 sys .stdout .write (
169160 'DONE\n '
170- 'PolicyName: {0}\n '
171- 'PolicyDocument: {1}\n ' .format (
172- params .policy_name ,
173- params .policy_document
174- )
161+ f'PolicyName: { params .policy_name } \n '
162+ f'PolicyDocument: { params .policy_document } \n '
175163 )
176164
177165 def _create_config (self , params ):
178166 sys .stdout .write (
179- 'Creating the on-premises instance configuration file named {0 }'
180- '...' . format ( DEFAULT_CONFIG_FILE )
167+ f 'Creating the on-premises instance configuration file named { DEFAULT_CONFIG_FILE } '
168+ '...'
181169 )
182- with open (DEFAULT_CONFIG_FILE , 'w' ) as f :
183- f .write (
184- '---\n '
185- 'region: {0}\n '
186- 'iam_user_arn: {1}\n '
187- 'aws_access_key_id: {2}\n '
188- 'aws_secret_access_key: {3}\n ' .format (
189- params .region ,
190- params .iam_user_arn ,
191- params .access_key_id ,
192- params .secret_access_key
170+ try :
171+ fd = os .open (
172+ DEFAULT_CONFIG_FILE ,
173+ os .O_WRONLY | os .O_CREAT | os .O_TRUNC ,
174+ 0o600 ,
175+ )
176+ with os .fdopen (fd , 'w' ) as f :
177+ os .chmod (DEFAULT_CONFIG_FILE , 0o600 )
178+ f .write (
179+ '---\n '
180+ f'region: { params .region } \n '
181+ f'iam_user_arn: { params .iam_user_arn } \n '
182+ f'aws_access_key_id: { params .access_key_id } \n '
183+ f'aws_secret_access_key: { params .secret_access_key } \n '
193184 )
185+ except OSError as e :
186+ raise RuntimeError (
187+ f'Failed to create config file { DEFAULT_CONFIG_FILE } : { e } '
194188 )
195189 sys .stdout .write ('DONE\n ' )
196190
197191 def _register_instance (self , params ):
198192 sys .stdout .write ('Registering the on-premises instance... ' )
199193 self .codedeploy .register_on_premises_instance (
200- instanceName = params .instance_name ,
201- iamUserArn = params .iam_user_arn
194+ instanceName = params .instance_name , iamUserArn = params .iam_user_arn
202195 )
203196 sys .stdout .write ('DONE\n ' )
204197
205198 def _add_tags (self , params ):
206199 sys .stdout .write ('Adding tags to the on-premises instance... ' )
207200 self .codedeploy .add_tags_to_on_premises_instances (
208- tags = params .tags ,
209- instanceNames = [params .instance_name ]
201+ tags = params .tags , instanceNames = [params .instance_name ]
210202 )
211203 sys .stdout .write ('DONE\n ' )
0 commit comments