Skip to content

Commit ec27cab

Browse files
author
Lucas Moura
committed
gce: add support for launching pro instances
1 parent febeb20 commit ec27cab

File tree

2 files changed

+101
-53
lines changed

2 files changed

+101
-53
lines changed

examples/gce.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,11 @@
66
import os
77

88
import pycloudlib
9+
from pycloudlib.cloud import ImageType
910

1011

11-
def demo():
12-
"""Show example of using the GCE library.
13-
14-
Connects to GCE and finds the latest daily image. Then runs
15-
through a number of examples.
16-
"""
17-
gce = pycloudlib.GCE(
18-
tag="examples",
19-
credentials_path="MY-GCE-CREDENTIALS-PATH",
20-
project="PROJECT-ID",
21-
region="us-west2",
22-
zone="a",
23-
)
24-
daily = gce.daily_image("bionic")
25-
12+
def manage_ssh_key(gce):
13+
"""Manage ssh keys for gce instances."""
2614
pub_key_path = "gce-pubkey"
2715
priv_key_path = "gce-privkey"
2816
pub_key, priv_key = gce.create_key_pair()
@@ -38,10 +26,45 @@ def demo():
3826

3927
gce.use_key(public_key_path=pub_key_path, private_key_path=priv_key_path)
4028

29+
30+
def generic(gce):
31+
"""Show example of using the GCE library.
32+
33+
Connects to GCE and finds the latest daily image. Then runs
34+
through a number of examples.
35+
"""
36+
daily = gce.daily_image("bionic")
4137
inst = gce.launch(daily)
4238
print(inst.execute("lsb_release -a"))
39+
inst.delete()
4340

4441

45-
if __name__ == "__main__":
42+
def pro(gce):
43+
"""Show example of running a GCE PRO machine."""
44+
daily = gce.daily_image("bionic", image_type=ImageType.PRO)
45+
inst = gce.launch(daily)
46+
print(inst.execute("sudo ua status --wait"))
47+
inst.delete()
48+
49+
50+
def pro_fips(gce):
51+
"""Show example of running a GCE PRO FIPS machine."""
52+
daily = gce.daily_image("bionic", image_type=ImageType.PRO_FIPS)
53+
inst = gce.launch(daily)
54+
print(inst.execute("sudo ua status --wait"))
55+
inst.delete()
56+
57+
58+
def demo():
59+
"""Show examples of launching GCP instances."""
4660
logging.basicConfig(level=logging.DEBUG)
61+
gce = pycloudlib.GCE(tag="examples")
62+
manage_ssh_key(gce)
63+
64+
generic(gce)
65+
pro(gce)
66+
pro_fips(gce)
67+
68+
69+
if __name__ == "__main__":
4770
demo()

pycloudlib/gce/cloud.py

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
import googleapiclient.discovery
1515

16-
from pycloudlib.cloud import BaseCloud
16+
from pycloudlib.cloud import BaseCloud, ImageType
1717
from pycloudlib.config import ConfigFile
1818
from pycloudlib.gce.instance import GceInstance
1919
from pycloudlib.gce.util import get_credentials, raise_on_error
20-
from pycloudlib.util import subp
20+
from pycloudlib.util import UBUNTU_RELEASE_VERSION_MAP, subp
2121

2222
logging.getLogger("googleapiclient.discovery").setLevel(logging.WARNING)
2323

@@ -107,42 +107,88 @@ def __init__(
107107
self.zone = "%s-%s" % (region, zone)
108108
self.instance_counter = count()
109109
self.service_account_email = service_account_email or self.config.get(
110-
"service_account_meail"
110+
"service_account_email"
111111
)
112112

113-
def _find_image(self, release, daily, arch="amd64"):
114-
images = self._image_list(release, daily, arch)
115-
116-
image_id = images[0]["id"]
117-
118-
return "projects/ubuntu-os-cloud-devel/global/images/%s" % image_id
119-
120-
def released_image(self, release, arch="amd64"):
113+
def released_image(
114+
self, release, image_type: ImageType = ImageType.GENERIC
115+
):
121116
"""ID of the latest released image for a particular release.
122117
123118
Args:
124119
release: The release to look for
125-
arch: string, architecture to use
126120
127121
Returns:
128122
A single string with the latest released image ID for the
129123
specified release.
130124
"""
131-
return self.daily_image(release, arch)
125+
return self.daily_image(release=release, image_type=image_type)
126+
127+
def _get_project(self, image_type: ImageType):
128+
return (
129+
"ubuntu-os-cloud-devel"
130+
if image_type == ImageType.GENERIC
131+
else "ubuntu-os-pro-cloud"
132+
)
133+
134+
def _get_name_filter(self, release: str, image_type: ImageType):
135+
if image_type == ImageType.GENERIC:
136+
return "ubuntu-{}-{}-*".format(
137+
UBUNTU_RELEASE_VERSION_MAP[release].replace(".", ""), release
138+
)
139+
140+
if image_type == ImageType.PRO:
141+
return "ubuntu-pro-{}-{}-*".format(
142+
UBUNTU_RELEASE_VERSION_MAP[release].replace(".", ""), release
143+
)
144+
145+
if image_type == ImageType.PRO_FIPS:
146+
return "ubuntu-pro-fips-{}-{}-*".format(
147+
UBUNTU_RELEASE_VERSION_MAP[release].replace(".", ""), release
148+
)
149+
150+
raise ValueError("Invalid image_type: {}".format(image_type.value))
151+
152+
def _find_latest_image(self, release: str, image_type: ImageType):
153+
project = self._get_project(image_type=image_type)
154+
name_filter = self._get_name_filter(
155+
release=release, image_type=image_type
156+
)
157+
158+
image = (
159+
self.compute.images()
160+
.list(
161+
project=project,
162+
filter="name={}".format(name_filter),
163+
)
164+
.execute()
165+
)
166+
167+
image_list = image.get("items", [])
132168

133-
def daily_image(self, release, arch="amd64"):
169+
if not image_list:
170+
raise Exception(
171+
"Could not find {} image for {} release".format(
172+
image_type.value,
173+
release,
174+
)
175+
)
176+
177+
image = sorted(image_list, key=lambda x: x["creationTimestamp"])[-1]
178+
return "projects/{}/global/images/{}".format(project, image["id"])
179+
180+
def daily_image(self, release, image_type: ImageType = ImageType.GENERIC):
134181
"""Find the id of the latest image for a particular release.
135182
136183
Args:
137184
release: string, Ubuntu release to look for
138-
arch: string, architecture to use
139185
140186
Returns:
141187
string, path to latest daily image
142188
143189
"""
144190
self._log.debug("finding daily Ubuntu image for %s", release)
145-
return self._find_image(release, daily=True, arch=arch)
191+
return self._find_latest_image(release=release, image_type=image_type)
146192

147193
def image_serial(self, image_id):
148194
"""Find the image serial of the latest daily image for a particular release.
@@ -326,27 +372,6 @@ def snapshot(self, instance: GceInstance, clean=True, **kwargs):
326372
self.project, snapshot_name
327373
)
328374

329-
def _image_list(self, release, daily, arch="amd64"):
330-
"""Find list of images with a filter.
331-
332-
Args:
333-
release: string, Ubuntu release to look for
334-
arch: string, architecture to use
335-
336-
Returns:
337-
list of dictionaries of images
338-
339-
"""
340-
filters = [
341-
"arch=%s" % arch,
342-
"endpoint=%s" % "https://www.googleapis.com",
343-
"region=%s" % self.region,
344-
"release=%s" % release,
345-
"virt=kvm",
346-
]
347-
348-
return self._streams_query(filters, daily)
349-
350375
def _wait_for_operation(
351376
self, operation, operation_type="global", sleep_seconds=300
352377
):

0 commit comments

Comments
 (0)