diff --git a/compute/compute/ingredients/instances/create_instance.py b/compute/compute/ingredients/instances/create_instance.py index f49fa6a981ae..afbbbef330a6 100644 --- a/compute/compute/ingredients/instances/create_instance.py +++ b/compute/compute/ingredients/instances/create_instance.py @@ -19,6 +19,7 @@ import re from typing import List +import warnings from google.cloud import compute_v1 @@ -37,6 +38,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -69,7 +72,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -97,8 +103,10 @@ def create_instance( access.nat_i_p = external_ipv4 network_interface.access_configs = [access] + # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -109,13 +117,18 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn("Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = compute_v1.Scheduling.ProvisioningModel.SPOT.name + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/ingredients/instances/create_instance_from_template_with_overrides.py b/compute/compute/ingredients/instances/create_instance_from_template_with_overrides.py index 5920a06482a1..39432712b92e 100644 --- a/compute/compute/ingredients/instances/create_instance_from_template_with_overrides.py +++ b/compute/compute/ingredients/instances/create_instance_from_template_with_overrides.py @@ -72,7 +72,7 @@ def create_instance_from_template_with_overrides( instance = compute_v1.Instance() instance.name = instance_name instance.machine_type = machine_type - instance.disks = instance_template.properties.disks + instance.disks = list(instance_template.properties.disks) new_disk = compute_v1.AttachedDisk() new_disk.initialize_params.disk_size_gb = 50 diff --git a/compute/compute/ingredients/instances/preemptible/create.py b/compute/compute/ingredients/instances/preemptible/create.py index 46d611a5dcad..145fbc3679fa 100644 --- a/compute/compute/ingredients/instances/preemptible/create.py +++ b/compute/compute/ingredients/instances/preemptible/create.py @@ -12,23 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets -# folder for complete code samples that are ready to be used. -# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. -# flake8: noqa -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - # This is an ingredient file. It is not meant to be run directly. Check the samples/snippets # folder for complete code samples that are ready to be used. # Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. diff --git a/compute/compute/ingredients/instances/spot/create.py b/compute/compute/ingredients/instances/spot/create.py new file mode 100644 index 000000000000..ba87875cf8e5 --- /dev/null +++ b/compute/compute/ingredients/instances/spot/create.py @@ -0,0 +1,43 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets +# folder for complete code samples that are ready to be used. +# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. +# flake8: noqa + +from google.cloud import compute_v1 + + +# +def create_spot_instance(project_id: str, zone: str, instance_name: str) -> compute_v1.Instance: + """ + Create a new Spot VM instance with Debian 10 operating system. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + + Returns: + Instance object. + """ + newest_debian = get_image_from_family( + project="debian-cloud", family="debian-11" + ) + disk_type = f"zones/{zone}/diskTypes/pd-standard" + disks = [disk_from_image(disk_type, 10, True, newest_debian.self_link)] + instance = create_instance(project_id, zone, instance_name, disks, spot=True) + return instance +# diff --git a/compute/compute/ingredients/instances/spot/get.py b/compute/compute/ingredients/instances/spot/get.py new file mode 100644 index 000000000000..3366cf36d591 --- /dev/null +++ b/compute/compute/ingredients/instances/spot/get.py @@ -0,0 +1,39 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets +# folder for complete code samples that are ready to be used. +# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check. +# flake8: noqa +from google.cloud import compute_v1 + + +# +def is_spot_vm(project_id: str, zone: str, instance_name: str) -> bool: + """ + Check if a given instance is Spot VM or not. + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone you want to use. For example: "us-west3-b" + instance_name: name of the virtual machine to check. + Returns: + The Spot VM status of the instance. + """ + instance_client = compute_v1.InstancesClient() + instance = instance_client.get( + project=project_id, zone=zone, instance=instance_name + ) + return instance.scheduling.provisioning_model == compute_v1.Scheduling.ProvisioningModel.SPOT.name +# + diff --git a/compute/compute/recipes/instances/spot/__init__.py b/compute/compute/recipes/instances/spot/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/compute/compute/recipes/instances/spot/create.py b/compute/compute/recipes/instances/spot/create.py new file mode 100644 index 000000000000..fba16e4ce8a9 --- /dev/null +++ b/compute/compute/recipes/instances/spot/create.py @@ -0,0 +1,31 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# flake8: noqa + +# +# + +# + + +# + +# + + +# + + +# +# diff --git a/compute/compute/recipes/instances/spot/is_spot_vm.py b/compute/compute/recipes/instances/spot/is_spot_vm.py new file mode 100644 index 000000000000..ea613650ab07 --- /dev/null +++ b/compute/compute/recipes/instances/spot/is_spot_vm.py @@ -0,0 +1,21 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# flake8: noqa + +# +# + +# + +# diff --git a/compute/compute/snippets/instances/create.py b/compute/compute/snippets/instances/create.py index e538b5b59a26..801c05b16a84 100644 --- a/compute/compute/snippets/instances/create.py +++ b/compute/compute/snippets/instances/create.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/create_start_instance/create_from_custom_image.py b/compute/compute/snippets/instances/create_start_instance/create_from_custom_image.py index 4ddc4138f3da..d78dd8198687 100644 --- a/compute/compute/snippets/instances/create_start_instance/create_from_custom_image.py +++ b/compute/compute/snippets/instances/create_start_instance/create_from_custom_image.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/create_start_instance/create_from_public_image.py b/compute/compute/snippets/instances/create_start_instance/create_from_public_image.py index 439d985410ed..50bf6bffdc9f 100644 --- a/compute/compute/snippets/instances/create_start_instance/create_from_public_image.py +++ b/compute/compute/snippets/instances/create_start_instance/create_from_public_image.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/create_start_instance/create_from_snapshot.py b/compute/compute/snippets/instances/create_start_instance/create_from_snapshot.py index a3dde1bf8986..d71bd4500f92 100644 --- a/compute/compute/snippets/instances/create_start_instance/create_from_snapshot.py +++ b/compute/compute/snippets/instances/create_start_instance/create_from_snapshot.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -125,6 +126,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -157,7 +160,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -187,6 +193,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -197,13 +204,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/create_start_instance/create_windows_instance.py b/compute/compute/snippets/instances/create_start_instance/create_windows_instance.py index 3d6877fc951a..ce0620da2167 100644 --- a/compute/compute/snippets/instances/create_start_instance/create_windows_instance.py +++ b/compute/compute/snippets/instances/create_start_instance/create_windows_instance.py @@ -24,6 +24,7 @@ import re import sys from typing import Any, List, Optional +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -144,6 +145,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -176,7 +179,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -206,6 +212,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -216,13 +223,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/create_start_instance/create_with_additional_disk.py b/compute/compute/snippets/instances/create_start_instance/create_with_additional_disk.py index e923f73624aa..e75aadccd78f 100644 --- a/compute/compute/snippets/instances/create_start_instance/create_with_additional_disk.py +++ b/compute/compute/snippets/instances/create_start_instance/create_with_additional_disk.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -173,6 +174,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -205,7 +208,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -235,6 +241,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -245,13 +252,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/create_start_instance/create_with_existing_disks.py b/compute/compute/snippets/instances/create_start_instance/create_with_existing_disks.py index 22f92da6073c..4fa4cd2e34cc 100644 --- a/compute/compute/snippets/instances/create_start_instance/create_with_existing_disks.py +++ b/compute/compute/snippets/instances/create_start_instance/create_with_existing_disks.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, Iterable, List, NoReturn +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -101,6 +102,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -133,7 +136,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -163,6 +169,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -173,13 +180,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/create_start_instance/create_with_snapshotted_data_disk.py b/compute/compute/snippets/instances/create_start_instance/create_with_snapshotted_data_disk.py index c86108b406f0..057eba548e63 100644 --- a/compute/compute/snippets/instances/create_start_instance/create_with_snapshotted_data_disk.py +++ b/compute/compute/snippets/instances/create_start_instance/create_with_snapshotted_data_disk.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -180,6 +181,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -212,7 +215,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -242,6 +248,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -252,13 +259,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/create_with_subnet.py b/compute/compute/snippets/instances/create_with_subnet.py index 4ab870837ea3..6696dc4a198c 100644 --- a/compute/compute/snippets/instances/create_with_subnet.py +++ b/compute/compute/snippets/instances/create_with_subnet.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/custom_hostname/create.py b/compute/compute/snippets/instances/custom_hostname/create.py index 03498dec9acf..6813bc4dcd59 100644 --- a/compute/compute/snippets/instances/custom_hostname/create.py +++ b/compute/compute/snippets/instances/custom_hostname/create.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/custom_machine_types/create_shared_with_helper.py b/compute/compute/snippets/instances/custom_machine_types/create_shared_with_helper.py index 9d41d09819cc..99dea3ef2da9 100644 --- a/compute/compute/snippets/instances/custom_machine_types/create_shared_with_helper.py +++ b/compute/compute/snippets/instances/custom_machine_types/create_shared_with_helper.py @@ -26,6 +26,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -337,6 +338,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -369,7 +372,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -399,6 +405,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -409,13 +416,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/custom_machine_types/create_with_helper.py b/compute/compute/snippets/instances/custom_machine_types/create_with_helper.py index cdb85464ebe8..18f642549ca7 100644 --- a/compute/compute/snippets/instances/custom_machine_types/create_with_helper.py +++ b/compute/compute/snippets/instances/custom_machine_types/create_with_helper.py @@ -26,6 +26,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -337,6 +338,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -369,7 +372,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -399,6 +405,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -409,13 +416,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/custom_machine_types/create_without_helper.py b/compute/compute/snippets/instances/custom_machine_types/create_without_helper.py index 42da51444cc1..30b2edd9e9a9 100644 --- a/compute/compute/snippets/instances/custom_machine_types/create_without_helper.py +++ b/compute/compute/snippets/instances/custom_machine_types/create_without_helper.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/custom_machine_types/extra_mem_no_helper.py b/compute/compute/snippets/instances/custom_machine_types/extra_mem_no_helper.py index de1c0057362d..de210032724f 100644 --- a/compute/compute/snippets/instances/custom_machine_types/extra_mem_no_helper.py +++ b/compute/compute/snippets/instances/custom_machine_types/extra_mem_no_helper.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/delete_protection/create.py b/compute/compute/snippets/instances/delete_protection/create.py index addf7a9c7753..7d9bbe9bbfa5 100644 --- a/compute/compute/snippets/instances/delete_protection/create.py +++ b/compute/compute/snippets/instances/delete_protection/create.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/from_instance_template/create_from_template_with_overrides.py b/compute/compute/snippets/instances/from_instance_template/create_from_template_with_overrides.py index da62d3ecf879..9ff3ba4af4fb 100644 --- a/compute/compute/snippets/instances/from_instance_template/create_from_template_with_overrides.py +++ b/compute/compute/snippets/instances/from_instance_template/create_from_template_with_overrides.py @@ -125,7 +125,7 @@ def create_instance_from_template_with_overrides( instance = compute_v1.Instance() instance.name = instance_name instance.machine_type = machine_type - instance.disks = instance_template.properties.disks + instance.disks = list(instance_template.properties.disks) new_disk = compute_v1.AttachedDisk() new_disk.initialize_params.disk_size_gb = 50 diff --git a/compute/compute/snippets/instances/preemptible/create_preemptible.py b/compute/compute/snippets/instances/preemptible/create_preemptible.py index a85f01355014..5816d39f4b0c 100644 --- a/compute/compute/snippets/instances/preemptible/create_preemptible.py +++ b/compute/compute/snippets/instances/preemptible/create_preemptible.py @@ -23,6 +23,7 @@ import re import sys from typing import Any, List +import warnings from google.api_core.extended_operation import ExtendedOperation from google.cloud import compute_v1 @@ -143,6 +144,8 @@ def create_instance( external_ipv4: str = None, accelerators: List[compute_v1.AcceleratorConfig] = None, preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", custom_hostname: str = None, delete_protection: bool = False, ) -> compute_v1.Instance: @@ -175,7 +178,10 @@ def create_instance( accelerators: a list of AcceleratorConfig objects describing the accelerators that will be attached to the new instance. preemptible: boolean value indicating if the new instance should be preemptible - or not. + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" custom_hostname: Custom hostname of the new VM instance. Custom hostnames must conform to RFC 1035 requirements for valid hostnames. delete_protection: boolean value indicating if the new virtual machine should be @@ -205,6 +211,7 @@ def create_instance( # Collect information into the Instance object. instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] instance.name = instance_name instance.disks = disks if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): @@ -215,13 +222,22 @@ def create_instance( if accelerators: instance.guest_accelerators = accelerators - instance.network_interfaces = [network_interface] - if preemptible: # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) instance.scheduling = compute_v1.Scheduling() instance.scheduling.preemptible = True + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + if custom_hostname is not None: # Set the custom hostname for the instance instance.hostname = custom_hostname diff --git a/compute/compute/snippets/instances/spot/__init__.py b/compute/compute/snippets/instances/spot/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/compute/compute/snippets/instances/spot/create.py b/compute/compute/snippets/instances/spot/create.py new file mode 100644 index 000000000000..ddf437f6bfcf --- /dev/null +++ b/compute/compute/snippets/instances/spot/create.py @@ -0,0 +1,287 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# flake8: noqa + + +# This file is automatically generated. Please do not modify it directly. +# Find the relevant recipe file in the samples/recipes or samples/ingredients +# directory and apply your changes there. + + +# [START compute_spot_create] +import re +import sys +from typing import Any, List +import warnings + +from google.api_core.extended_operation import ExtendedOperation +from google.cloud import compute_v1 + + +def get_image_from_family(project: str, family: str) -> compute_v1.Image: + """ + Retrieve the newest image that is part of a given family in a project. + + Args: + project: project ID or project number of the Cloud project you want to get image from. + family: name of the image family you want to get image from. + + Returns: + An Image object. + """ + image_client = compute_v1.ImagesClient() + # List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details + newest_image = image_client.get_from_family(project=project, family=family) + return newest_image + + +def disk_from_image( + disk_type: str, + disk_size_gb: int, + boot: bool, + source_image: str, + auto_delete: bool = True, +) -> compute_v1.AttachedDisk: + """ + Create an AttachedDisk object to be used in VM instance creation. Uses an image as the + source for the new disk. + + Args: + disk_type: the type of disk you want to create. This value uses the following format: + "zones/{zone}/diskTypes/(pd-standard|pd-ssd|pd-balanced|pd-extreme)". + For example: "zones/us-west3-b/diskTypes/pd-ssd" + disk_size_gb: size of the new disk in gigabytes + boot: boolean flag indicating whether this disk should be used as a boot disk of an instance + source_image: source image to use when creating this disk. You must have read access to this disk. This can be one + of the publicly available images or an image from one of your projects. + This value uses the following format: "projects/{project_name}/global/images/{image_name}" + auto_delete: boolean flag indicating whether this disk should be deleted with the VM that uses it + + Returns: + AttachedDisk object configured to be created using the specified image. + """ + boot_disk = compute_v1.AttachedDisk() + initialize_params = compute_v1.AttachedDiskInitializeParams() + initialize_params.source_image = source_image + initialize_params.disk_size_gb = disk_size_gb + initialize_params.disk_type = disk_type + boot_disk.initialize_params = initialize_params + # Remember to set auto_delete to True if you want the disk to be deleted when you delete + # your VM instance. + boot_disk.auto_delete = auto_delete + boot_disk.boot = boot + return boot_disk + + +def wait_for_extended_operation( + operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300 +) -> Any: + """ + This method will wait for the extended (long-running) operation to + complete. If the operation is successful, it will return its result. + If the operation ends with an error, an exception will be raised. + If there were any warnings during the execution of the operation + they will be printed to sys.stderr. + + Args: + operation: a long-running operation you want to wait on. + verbose_name: (optional) a more verbose name of the operation, + used only during error and warning reporting. + timeout: how long (in seconds) to wait for operation to finish. + If None, wait indefinitely. + + Returns: + Whatever the operation.result() returns. + + Raises: + This method will raise the exception received from `operation.exception()` + or RuntimeError if there is no exception set, but there is an `error_code` + set for the `operation`. + + In case of an operation taking longer than `timeout` seconds to complete, + a `concurrent.futures.TimeoutError` will be raised. + """ + result = operation.result(timeout=timeout) + + if operation.error_code: + print( + f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}", + file=sys.stderr, + flush=True, + ) + print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True) + raise operation.exception() or RuntimeError(operation.error_message) + + if operation.warnings: + print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True) + for warning in operation.warnings: + print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True) + + return result + + +def create_instance( + project_id: str, + zone: str, + instance_name: str, + disks: List[compute_v1.AttachedDisk], + machine_type: str = "n1-standard-1", + network_link: str = "global/networks/default", + subnetwork_link: str = None, + internal_ip: str = None, + external_access: bool = False, + external_ipv4: str = None, + accelerators: List[compute_v1.AcceleratorConfig] = None, + preemptible: bool = False, + spot: bool = False, + instance_termination_action: str = "STOP", + custom_hostname: str = None, + delete_protection: bool = False, +) -> compute_v1.Instance: + """ + Send an instance creation request to the Compute Engine API and wait for it to complete. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + disks: a list of compute_v1.AttachedDisk objects describing the disks + you want to attach to your new instance. + machine_type: machine type of the VM being created. This value uses the + following format: "zones/{zone}/machineTypes/{type_name}". + For example: "zones/europe-west3-c/machineTypes/f1-micro" + network_link: name of the network you want the new instance to use. + For example: "global/networks/default" represents the network + named "default", which is created automatically for each project. + subnetwork_link: name of the subnetwork you want the new instance to use. + This value uses the following format: + "regions/{region}/subnetworks/{subnetwork_name}" + internal_ip: internal IP address you want to assign to the new instance. + By default, a free address from the pool of available internal IP addresses of + used subnet will be used. + external_access: boolean flag indicating if the instance should have an external IPv4 + address assigned. + external_ipv4: external IPv4 address to be assigned to this instance. If you specify + an external IP address, it must live in the same region as the zone of the instance. + This setting requires `external_access` to be set to True to work. + accelerators: a list of AcceleratorConfig objects describing the accelerators that will + be attached to the new instance. + preemptible: boolean value indicating if the new instance should be preemptible + or not. Preemptible VMs have been deprecated and you should now use Spot VMs. + spot: boolean value indicating if the new instance should be a Spot VM or not. + instance_termination_action: What action should be taken once a Spot VM is terminated. + Possible values: "STOP", "DELETE" + custom_hostname: Custom hostname of the new VM instance. + Custom hostnames must conform to RFC 1035 requirements for valid hostnames. + delete_protection: boolean value indicating if the new virtual machine should be + protected against deletion or not. + Returns: + Instance object. + """ + instance_client = compute_v1.InstancesClient() + + # Use the network interface provided in the network_link argument. + network_interface = compute_v1.NetworkInterface() + network_interface.name = network_link + if subnetwork_link: + network_interface.subnetwork = subnetwork_link + + if internal_ip: + network_interface.network_i_p = internal_ip + + if external_access: + access = compute_v1.AccessConfig() + access.type_ = compute_v1.AccessConfig.Type.ONE_TO_ONE_NAT.name + access.name = "External NAT" + access.network_tier = access.NetworkTier.PREMIUM.name + if external_ipv4: + access.nat_i_p = external_ipv4 + network_interface.access_configs = [access] + + # Collect information into the Instance object. + instance = compute_v1.Instance() + instance.network_interfaces = [network_interface] + instance.name = instance_name + instance.disks = disks + if re.match(r"^zones/[a-z\d\-]+/machineTypes/[a-z\d\-]+$", machine_type): + instance.machine_type = machine_type + else: + instance.machine_type = f"zones/{zone}/machineTypes/{machine_type}" + + if accelerators: + instance.guest_accelerators = accelerators + + if preemptible: + # Set the preemptible setting + warnings.warn( + "Preemptible VMs are being replaced by Spot VMs.", DeprecationWarning + ) + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.preemptible = True + + if spot: + # Set the Spot VM setting + instance.scheduling = compute_v1.Scheduling() + instance.scheduling.provisioning_model = ( + compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + instance.scheduling.instance_termination_action = instance_termination_action + + if custom_hostname is not None: + # Set the custom hostname for the instance + instance.hostname = custom_hostname + + if delete_protection: + # Set the delete protection bit + instance.deletion_protection = True + + # Prepare the request to insert an instance. + request = compute_v1.InsertInstanceRequest() + request.zone = zone + request.project = project_id + request.instance_resource = instance + + # Wait for the create operation to complete. + print(f"Creating the {instance_name} instance in {zone}...") + + operation = instance_client.insert(request=request) + + wait_for_extended_operation(operation, "instance creation") + + print(f"Instance {instance_name} created.") + return instance_client.get(project=project_id, zone=zone, instance=instance_name) + + +def create_spot_instance( + project_id: str, zone: str, instance_name: str +) -> compute_v1.Instance: + """ + Create a new Spot VM instance with Debian 10 operating system. + + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone to create the instance in. For example: "us-west3-b" + instance_name: name of the new virtual machine (VM) instance. + + Returns: + Instance object. + """ + newest_debian = get_image_from_family(project="debian-cloud", family="debian-11") + disk_type = f"zones/{zone}/diskTypes/pd-standard" + disks = [disk_from_image(disk_type, 10, True, newest_debian.self_link)] + instance = create_instance(project_id, zone, instance_name, disks, spot=True) + return instance + + +# [END compute_spot_create] diff --git a/compute/compute/snippets/instances/spot/is_spot_vm.py b/compute/compute/snippets/instances/spot/is_spot_vm.py new file mode 100644 index 000000000000..4cb83657094f --- /dev/null +++ b/compute/compute/snippets/instances/spot/is_spot_vm.py @@ -0,0 +1,46 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# flake8: noqa + + +# This file is automatically generated. Please do not modify it directly. +# Find the relevant recipe file in the samples/recipes or samples/ingredients +# directory and apply your changes there. + + +# [START compute_spot_check] +from google.cloud import compute_v1 + + +def is_spot_vm(project_id: str, zone: str, instance_name: str) -> bool: + """ + Check if a given instance is Spot VM or not. + Args: + project_id: project ID or project number of the Cloud project you want to use. + zone: name of the zone you want to use. For example: "us-west3-b" + instance_name: name of the virtual machine to check. + Returns: + The Spot VM status of the instance. + """ + instance_client = compute_v1.InstancesClient() + instance = instance_client.get( + project=project_id, zone=zone, instance=instance_name + ) + return ( + instance.scheduling.provisioning_model + == compute_v1.Scheduling.ProvisioningModel.SPOT.name + ) + + +# [END compute_spot_check] diff --git a/compute/compute/snippets/tests/test_images.py b/compute/compute/snippets/tests/test_images.py index b6f61b3c821c..1cc7e6bb58fc 100644 --- a/compute/compute/snippets/tests/test_images.py +++ b/compute/compute/snippets/tests/test_images.py @@ -96,7 +96,7 @@ def test_get_image(): image2 = get_image("debian-cloud", image.name) - assert image == image2 + assert image.name == image2.name def test_create_delete_image(test_disk): diff --git a/compute/compute/snippets/tests/test_spot_vms.py b/compute/compute/snippets/tests/test_spot_vms.py new file mode 100644 index 000000000000..5e6114161ca7 --- /dev/null +++ b/compute/compute/snippets/tests/test_spot_vms.py @@ -0,0 +1,42 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import uuid + +import google.auth +import pytest + +from ..instances.delete import delete_instance +from ..instances.spot.create import create_spot_instance +from ..instances.spot.is_spot_vm import is_spot_vm + +PROJECT = google.auth.default()[1] +INSTANCE_ZONE = "europe-central2-c" + + +@pytest.fixture +def autodelete_instance_name(): + instance_name = "i" + uuid.uuid4().hex[:10] + + yield instance_name + + delete_instance(PROJECT, INSTANCE_ZONE, instance_name) + + +def test_preemptible_creation(autodelete_instance_name): + instance = create_spot_instance( + PROJECT, INSTANCE_ZONE, autodelete_instance_name + ) + + assert instance.name == autodelete_instance_name + assert is_spot_vm(PROJECT, INSTANCE_ZONE, instance.name)