|
1 | 1 | # This file is part of pycloudlib. See LICENSE file for license information. |
2 | 2 | """AWS EC2 Cloud type.""" |
| 3 | +import re |
| 4 | + |
3 | 5 | import botocore |
4 | 6 |
|
5 | 7 | from pycloudlib.cloud import BaseCloud, ImageType |
@@ -111,13 +113,11 @@ def _get_name_for_image_type( |
111 | 113 | ) |
112 | 114 | if release in LTS_RELEASES: |
113 | 115 | return "{}/ubuntu-{}{}-*-server-*".format( |
114 | | - base_location, release, |
115 | | - "-daily" if daily else "" |
| 116 | + base_location, release, "-daily" if daily else "" |
116 | 117 | ) |
117 | 118 |
|
118 | 119 | return "{}/ubuntu-{}{}-*".format( |
119 | | - base_location, release, |
120 | | - "-daily" if daily else "" |
| 120 | + base_location, release, "-daily" if daily else "" |
121 | 121 | ) |
122 | 122 |
|
123 | 123 | if image_type == ImageType.PRO: |
@@ -196,6 +196,36 @@ def daily_image( |
196 | 196 | ) |
197 | 197 | return image["ImageId"] |
198 | 198 |
|
| 199 | + def _find_image_serial( |
| 200 | + self, image_id, image_type: ImageType = ImageType.GENERIC |
| 201 | + ): |
| 202 | + owner = self._get_owner(image_type=image_type) |
| 203 | + filters = [ |
| 204 | + { |
| 205 | + "Name": "image-id", |
| 206 | + "Values": (image_id,), |
| 207 | + } |
| 208 | + ] |
| 209 | + |
| 210 | + images = self.client.describe_images( |
| 211 | + Owners=[owner], |
| 212 | + Filters=filters, |
| 213 | + ) |
| 214 | + |
| 215 | + if not images.get("Images"): |
| 216 | + raise Exception("Could not find image: {}".format(image_id)) |
| 217 | + |
| 218 | + image_name = images["Images"][0].get("Name", "") |
| 219 | + serial_regex = r"ubuntu/.*/.*/.*-(?P<serial>\d+)$" |
| 220 | + serial_match = re.match(serial_regex, image_name) |
| 221 | + |
| 222 | + if not serial_match: |
| 223 | + raise Exception( |
| 224 | + "Could not find image serial for image: {}".format(image_id) |
| 225 | + ) |
| 226 | + |
| 227 | + return serial_match.groupdict().get("serial") |
| 228 | + |
199 | 229 | def image_serial(self, image_id): |
200 | 230 | """Find the image serial of a given EC2 image ID. |
201 | 231 |
|
|
0 commit comments