12
12
13
13
from mypy_boto3_ec2 .client import EC2Client
14
14
from mypy_boto3_ec2 .literals import BootModeValuesType
15
- from mypy_boto3_ec2 .type_defs import RegionTypeDef
15
+ from mypy_boto3_ec2 .type_defs import RegionTypeDef , RegisterImageRequestTypeDef
16
16
from mypy_boto3_s3 .client import S3Client
17
17
18
18
from concurrent .futures import ThreadPoolExecutor
@@ -127,6 +127,7 @@ def register_image_if_not_exists(
127
127
image_info : ImageInfo ,
128
128
snapshot_id : str ,
129
129
public : bool ,
130
+ enable_tpm : bool ,
130
131
) -> str :
131
132
"""
132
133
Register image if it doesn't exist yet
@@ -150,19 +151,11 @@ def register_image_if_not_exists(
150
151
else :
151
152
raise Exception ("Unknown system: " + image_info ["system" ])
152
153
153
- logging .info (f"Registering image { image_name } with snapshot { snapshot_id } " )
154
-
155
- # TODO(arianvp): Not all instance types support TPM 2.0 yet. We should
156
- # upload two images, one with and one without TPM 2.0 support.
157
-
158
- # if architecture == "x86_64" and image_info["boot_mode"] == "uefi":
159
- # tpmsupport['TpmSupport'] = "v2.0"
160
-
161
- register_image = ec2 .register_image (
162
- Name = image_name ,
163
- Architecture = architecture ,
164
- BootMode = image_info ["boot_mode" ],
165
- BlockDeviceMappings = [
154
+ register_image_kwargs : RegisterImageRequestTypeDef = {
155
+ "Name" : image_name ,
156
+ "Architecture" : architecture ,
157
+ "BootMode" : image_info ["boot_mode" ],
158
+ "BlockDeviceMappings" : [
166
159
{
167
160
"DeviceName" : "/dev/xvda" ,
168
161
"Ebs" : {
@@ -171,12 +164,12 @@ def register_image_if_not_exists(
171
164
},
172
165
}
173
166
],
174
- RootDeviceName = "/dev/xvda" ,
175
- VirtualizationType = "hvm" ,
176
- EnaSupport = True ,
177
- ImdsSupport = "v2.0" ,
178
- SriovNetSupport = "simple" ,
179
- TagSpecifications = [
167
+ " RootDeviceName" : "/dev/xvda" ,
168
+ " VirtualizationType" : "hvm" ,
169
+ " EnaSupport" : True ,
170
+ " ImdsSupport" : "v2.0" ,
171
+ " SriovNetSupport" : "simple" ,
172
+ " TagSpecifications" : [
180
173
{
181
174
"ResourceType" : "image" ,
182
175
"Tags" : [
@@ -185,7 +178,18 @@ def register_image_if_not_exists(
185
178
],
186
179
}
187
180
],
188
- )
181
+ }
182
+
183
+ if (
184
+ enable_tpm
185
+ and architecture == "x86_64"
186
+ and image_info ["boot_mode" ] == "uefi"
187
+ ):
188
+ register_image_kwargs ["TpmSupport" ] = "v2.0"
189
+
190
+ logging .info (f"Registering image { image_name } with snapshot { snapshot_id } " )
191
+
192
+ register_image = ec2 .register_image (** register_image_kwargs )
189
193
image_id = register_image ["ImageId" ]
190
194
191
195
ec2 .get_waiter ("image_available" ).wait (ImageIds = [image_id ])
@@ -303,6 +307,7 @@ def upload_ami(
303
307
run_id : str ,
304
308
public : bool ,
305
309
dest_regions : list [str ],
310
+ enable_tpm : bool ,
306
311
) -> dict [str , str ]:
307
312
"""
308
313
Upload NixOS AMI to AWS and return the image ids for each region
@@ -324,7 +329,7 @@ def upload_ami(
324
329
)
325
330
326
331
image_id = register_image_if_not_exists (
327
- ec2 , image_name , image_info , snapshot_id , public
332
+ ec2 , image_name , image_info , snapshot_id , public , enable_tpm
328
333
)
329
334
330
335
regions = filter (
@@ -366,6 +371,12 @@ def main() -> None:
366
371
action = "append" ,
367
372
default = [],
368
373
)
374
+ parser .add_argument (
375
+ "--enable-tpm" ,
376
+ action = "store_true" ,
377
+ default = False ,
378
+ help = "Enable TPM 2.0 support for UEFI x86_64 images" ,
379
+ )
369
380
370
381
args = parser .parse_args ()
371
382
@@ -384,6 +395,7 @@ def main() -> None:
384
395
args .run_id ,
385
396
args .public ,
386
397
args .dest_region ,
398
+ args .enable_tpm ,
387
399
)
388
400
print (json .dumps (image_ids ))
389
401
0 commit comments