@@ -174,6 +174,51 @@ def latest_aws_image(owner, name, arch='x86_64'):
174174 set_pinned (pin , image )
175175 return image
176176
177+ # latest_gce_image returns the latest GCE image for the given project, family, and arch
178+ # If the image is pinned, it returns the pinned image
179+ # Otherwise, it fetches the latest image from the specified family
180+ def latest_gce_image (project , family , arch = "X86_64" ):
181+ pin = f"gce://images/{ project } /{ family } :{ arch } "
182+ image = get_pinned (pin )
183+ if image :
184+ return image
185+
186+ import googleapiclient .discovery # pylint: disable=import-error, import-outside-toplevel
187+
188+ compute = googleapiclient .discovery .build ("compute" , "v1" , cache_discovery = False )
189+ try :
190+ # Get the latest image from the specified family
191+ # pylint: disable=no-member
192+ image_response = (
193+ compute .images ().getFromFamily (project = project , family = family ).execute ()
194+ )
195+ # pylint: enable=no-member
196+ except Exception as e :
197+ raise RuntimeError (
198+ f"Failed to get image from family { family } in project { project } : { str (e )} "
199+ )
200+
201+ # Verify architecture matches
202+ if "architecture" not in image_response :
203+ raise RuntimeError (
204+ f"Image { image_response ['name' ]} has no architecture specified"
205+ )
206+
207+ if arch not in image_response ["architecture" ]:
208+ raise RuntimeError (
209+ f"Image family { family } has architecture { image_response ['architecture' ]} "
210+ f"which doesn't match requested { arch } "
211+ )
212+
213+ image_name = image_response ["name" ]
214+ set_pinned (pin , image_name )
215+ return f"{ project } /{ image_name } "
216+
217+ # Get latest images from some public images families
218+ gce_distro_images = {
219+ "u2204" : latest_gce_image ("ubuntu-os-cloud" , "ubuntu-2204-lts" ),
220+ }
221+
177222distro_images = {
178223 'al2023' : latest_aws_image ('137112412989' , 'al2023-ami-2*-kernel-6.1-x86_64' ),
179224 'amzn2' : latest_aws_image ('137112412989' , 'amzn2-ami-kernel-5.10-hvm-*-x86_64-gp2' ),
0 commit comments