|
13 | 13 |
|
14 | 14 | import googleapiclient.discovery |
15 | 15 |
|
16 | | -from pycloudlib.cloud import BaseCloud |
| 16 | +from pycloudlib.cloud import BaseCloud, ImageType |
17 | 17 | from pycloudlib.config import ConfigFile |
18 | 18 | from pycloudlib.gce.instance import GceInstance |
19 | 19 | 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 |
21 | 21 |
|
22 | 22 | logging.getLogger("googleapiclient.discovery").setLevel(logging.WARNING) |
23 | 23 |
|
@@ -107,42 +107,88 @@ def __init__( |
107 | 107 | self.zone = "%s-%s" % (region, zone) |
108 | 108 | self.instance_counter = count() |
109 | 109 | self.service_account_email = service_account_email or self.config.get( |
110 | | - "service_account_meail" |
| 110 | + "service_account_email" |
111 | 111 | ) |
112 | 112 |
|
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 | + ): |
121 | 116 | """ID of the latest released image for a particular release. |
122 | 117 |
|
123 | 118 | Args: |
124 | 119 | release: The release to look for |
125 | | - arch: string, architecture to use |
126 | 120 |
|
127 | 121 | Returns: |
128 | 122 | A single string with the latest released image ID for the |
129 | 123 | specified release. |
130 | 124 | """ |
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", []) |
132 | 168 |
|
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): |
134 | 181 | """Find the id of the latest image for a particular release. |
135 | 182 |
|
136 | 183 | Args: |
137 | 184 | release: string, Ubuntu release to look for |
138 | | - arch: string, architecture to use |
139 | 185 |
|
140 | 186 | Returns: |
141 | 187 | string, path to latest daily image |
142 | 188 |
|
143 | 189 | """ |
144 | 190 | 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) |
146 | 192 |
|
147 | 193 | def image_serial(self, image_id): |
148 | 194 | """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): |
326 | 372 | self.project, snapshot_name |
327 | 373 | ) |
328 | 374 |
|
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 | | - |
350 | 375 | def _wait_for_operation( |
351 | 376 | self, operation, operation_type="global", sleep_seconds=300 |
352 | 377 | ): |
|
0 commit comments