23
23
import re
24
24
import sys
25
25
from typing import Any , List
26
+ import warnings
26
27
27
28
from google .api_core .extended_operation import ExtendedOperation
28
29
from google .cloud import compute_v1
@@ -143,6 +144,8 @@ def create_instance(
143
144
external_ipv4 : str = None ,
144
145
accelerators : List [compute_v1 .AcceleratorConfig ] = None ,
145
146
preemptible : bool = False ,
147
+ spot : bool = False ,
148
+ instance_termination_action : str = "STOP" ,
146
149
custom_hostname : str = None ,
147
150
delete_protection : bool = False ,
148
151
) -> compute_v1 .Instance :
@@ -175,7 +178,10 @@ def create_instance(
175
178
accelerators: a list of AcceleratorConfig objects describing the accelerators that will
176
179
be attached to the new instance.
177
180
preemptible: boolean value indicating if the new instance should be preemptible
178
- or not.
181
+ or not. Preemptible VMs have been deprecated and you should now use Spot VMs.
182
+ spot: boolean value indicating if the new instance should be a Spot VM or not.
183
+ instance_termination_action: What action should be taken once a Spot VM is terminated.
184
+ Possible values: "STOP", "DELETE"
179
185
custom_hostname: Custom hostname of the new VM instance.
180
186
Custom hostnames must conform to RFC 1035 requirements for valid hostnames.
181
187
delete_protection: boolean value indicating if the new virtual machine should be
@@ -205,6 +211,7 @@ def create_instance(
205
211
206
212
# Collect information into the Instance object.
207
213
instance = compute_v1 .Instance ()
214
+ instance .network_interfaces = [network_interface ]
208
215
instance .name = instance_name
209
216
instance .disks = disks
210
217
if re .match (r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$" , machine_type ):
@@ -215,13 +222,22 @@ def create_instance(
215
222
if accelerators :
216
223
instance .guest_accelerators = accelerators
217
224
218
- instance .network_interfaces = [network_interface ]
219
-
220
225
if preemptible :
221
226
# Set the preemptible setting
227
+ warnings .warn (
228
+ "Preemptible VMs are being replaced by Spot VMs." , DeprecationWarning
229
+ )
222
230
instance .scheduling = compute_v1 .Scheduling ()
223
231
instance .scheduling .preemptible = True
224
232
233
+ if spot :
234
+ # Set the Spot VM setting
235
+ instance .scheduling = compute_v1 .Scheduling ()
236
+ instance .scheduling .provisioning_model = (
237
+ compute_v1 .Scheduling .ProvisioningModel .SPOT .name
238
+ )
239
+ instance .scheduling .instance_termination_action = instance_termination_action
240
+
225
241
if custom_hostname is not None :
226
242
# Set the custom hostname for the instance
227
243
instance .hostname = custom_hostname
0 commit comments