blob: b1df64239cd667cd3a84fdeb88af88737b5f72a8 [file] [log] [blame]
# Copyright 2021 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Recipe to roll base/source_images for Packer.
"""
from PB.recipes.fuchsia.contrib.salt_packer_base_image_roller import InputProperties
DEPS = [
"fuchsia/auto_roller",
"fuchsia/buildbucket_util",
"fuchsia/gcloud",
"fuchsia/git",
"fuchsia/git_checkout",
"recipe_engine/context",
"recipe_engine/file",
"recipe_engine/json",
"recipe_engine/properties",
"recipe_engine/raw_io",
"recipe_engine/step",
]
PROPERTIES = InputProperties
def RunSteps(api, props):
salt_path, revision = api.git_checkout(props.repo)
# Get a short revision, image names must be < 64 characters
with api.context(cwd=salt_path):
revision = api.git(
"git rev-parse",
"rev-parse",
"--short",
revision,
stdout=api.raw_io.output_text(),
).stdout.rstrip()
json_path = salt_path / "starlark" / "packer" / "packer-source-image.json"
source_image = api.file.read_json(
name="load packer source image json", source=json_path
)
commit_message = "Rolling Salt Packer Base Images:\n\n"
for family, config in source_image.items():
project = config["project"]
old_image = config["image"]
result = api.gcloud(
"compute",
"images",
"describe-from-family",
f"{family}",
f"--project={project}",
"--format=json",
ok_ret="any",
stdout=api.json.output(),
step_name=f"get latest image for {project}/{family}",
)
if result.retcode != 0 or "name" not in result.stdout:
raise api.step.StepFailure(f"Unable to find image for {family}")
new_image = result.stdout["name"]
if old_image != new_image:
commit_message += f"{family}: {old_image} -> {new_image}\n"
source_image[family]["image"] = new_image
api.file.write_json(
name="update packer source image template",
dest=json_path,
data=source_image,
indent=4,
)
api.step("regen starlark", [salt_path / "gen.sh"])
api.auto_roller.attempt_roll(
api.auto_roller.Options(
remote=props.repo,
dry_run=props.dry_run,
),
repo_dir=salt_path,
commit_message=commit_message,
)
def GenTests(api):
repo = "https://fuchsia.googlesource.com/infra/salt"
yield (
api.buildbucket_util.test("update", git_repo=repo)
+ api.properties(repo=repo)
+ api.step_data(
"load packer source image json",
api.file.read_json(
json_content={"bar": {"image": "old", "project": "foo"}}
),
)
+ api.step_data(
"get latest image for foo/bar",
stdout=api.json.output({"name": "new"}),
retcode=0,
)
+ api.auto_roller.success()
)
yield (
api.buildbucket_util.test("get_latest_failure", status="FAILURE", git_repo=repo)
+ api.properties(repo=repo)
+ api.step_data(
"load packer source image json",
api.file.read_json(
json_content={"bar": {"image": "old", "project": "foo"}}
),
)
+ api.step_data(
"get latest image for foo/bar", stdout=api.json.output({}), retcode=1
)
)