-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): Adding samples for Spot VMs (#285)
* docs(samples): Adding samples for Spot VMs * Fixing tests perhaps * Update samples/snippets/instances/spot/__init__.py Co-authored-by: Leah E. Cole <[email protected]> * Update samples/recipes/instances/spot/__init__.py Co-authored-by: Leah E. Cole <[email protected]> Co-authored-by: Leah E. Cole <[email protected]>
- Loading branch information
1 parent
96ccd63
commit 28c7502
Showing
30 changed files
with
832 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/google-cloud-compute/samples/ingredients/instances/spot/create.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
# <INGREDIENT create_spot_instance> | ||
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 | ||
# </INGREDIENT> |
39 changes: 39 additions & 0 deletions
39
packages/google-cloud-compute/samples/ingredients/instances/spot/get.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
# <INGREDIENT is_spot_vm> | ||
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 | ||
# </INGREDIENT> | ||
|
Empty file.
31 changes: 31 additions & 0 deletions
31
packages/google-cloud-compute/samples/recipes/instances/spot/create.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
# <REGION compute_spot_create> | ||
# <IMPORTS/> | ||
|
||
# <INGREDIENT get_image_from_family /> | ||
|
||
|
||
# <INGREDIENT disk_from_image /> | ||
|
||
# <INGREDIENT wait_for_extended_operation /> | ||
|
||
|
||
# <INGREDIENT create_instance /> | ||
|
||
|
||
# <INGREDIENT create_spot_instance /> | ||
# </REGION compute_spot_create> |
21 changes: 21 additions & 0 deletions
21
packages/google-cloud-compute/samples/recipes/instances/spot/is_spot_vm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
# <REGION compute_spot_check> | ||
# <IMPORTS/> | ||
|
||
# <INGREDIENT is_spot_vm /> | ||
|
||
# </REGION compute_spot_check> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.