Log AMI publish step timings

This commit is contained in:
Timothy J. Aveni
2026-06-30 20:08:58 -07:00
parent 458ad8ae85
commit 3f26531f74
+43 -12
View File
@@ -7,6 +7,7 @@ builder_instance_type=""
name="nixos-zfs-ec2" name="nixos-zfs-ec2"
keep_builder=false keep_builder=false
extra_tofu_args=() extra_tofu_args=()
script_start_epoch="$(date +%s)"
usage() { usage() {
cat >&2 <<'EOF' cat >&2 <<'EOF'
@@ -87,6 +88,31 @@ require() {
fi fi
} }
elapsed() {
local start="$1"
local end
end="$(date +%s)"
printf '%ss' "$((end - start))"
}
total_elapsed() {
elapsed "$script_start_epoch"
}
log() {
printf '[%s] %s\n' "$(date -Is)" "$*" >&2
}
step() {
local label="$1"
shift
local start
start="$(date +%s)"
log "starting: ${label}"
"$@"
log "finished: ${label} ($(elapsed "$start"), total $(total_elapsed))"
}
require aws require aws
require git require git
require jq require jq
@@ -117,8 +143,8 @@ if [ -n "$builder_instance_type" ]; then
common_tofu_vars+=(-var "builder_instance_type=$builder_instance_type") common_tofu_vars+=(-var "builder_instance_type=$builder_instance_type")
fi fi
tofu init step "tofu init" tofu init
tofu apply \ step "tofu apply builder resources" tofu apply \
-auto-approve \ -auto-approve \
"${common_tofu_vars[@]}" \ "${common_tofu_vars[@]}" \
"${extra_tofu_args[@]}" "${extra_tofu_args[@]}"
@@ -128,9 +154,10 @@ nix_volume_id="$(tofu output -raw nix_volume_id)"
nix_device_name="$(tofu output -raw nix_device_name)" nix_device_name="$(tofu output -raw nix_device_name)"
nix_pool="$(tofu output -raw nix_pool)" nix_pool="$(tofu output -raw nix_pool)"
echo "Waiting for builder instance to stop: $builder_instance_id" >&2 log "waiting for builder instance to stop: $builder_instance_id"
aws ec2 wait instance-stopped --region "$aws_region" --instance-ids "$builder_instance_id" step "wait for builder instance stop" aws ec2 wait instance-stopped --region "$aws_region" --instance-ids "$builder_instance_id"
log "creating capture image from stopped builder"
capture_image_id="$( capture_image_id="$(
aws ec2 create-image \ aws ec2 create-image \
--region "$aws_region" \ --region "$aws_region" \
@@ -142,8 +169,9 @@ capture_image_id="$(
--query ImageId \ --query ImageId \
--output text --output text
)" )"
log "capture image: $capture_image_id"
aws ec2 wait image-available --region "$aws_region" --image-ids "$capture_image_id" step "wait for capture image" aws ec2 wait image-available --region "$aws_region" --image-ids "$capture_image_id"
capture_image_json="$(aws ec2 describe-images --region "$aws_region" --image-ids "$capture_image_id")" capture_image_json="$(aws ec2 describe-images --region "$aws_region" --image-ids "$capture_image_id")"
root_snapshot_id="$( root_snapshot_id="$(
@@ -222,6 +250,7 @@ block_device_mappings="$(
]' ]'
)" )"
log "registering final AMI"
image_id="$( image_id="$(
aws ec2 register-image \ aws ec2 register-image \
--region "$aws_region" \ --region "$aws_region" \
@@ -235,10 +264,11 @@ image_id="$(
--query ImageId \ --query ImageId \
--output text --output text
)" )"
log "final AMI: $image_id"
aws ec2 wait image-available --region "$aws_region" --image-ids "$image_id" step "wait for final AMI" aws ec2 wait image-available --region "$aws_region" --image-ids "$image_id"
aws ec2 create-tags \ step "tag root snapshot" aws ec2 create-tags \
--region "$aws_region" \ --region "$aws_region" \
--resources "$root_snapshot_id" \ --resources "$root_snapshot_id" \
--tags \ --tags \
@@ -250,7 +280,7 @@ aws ec2 create-tags \
"Key=BuilderGitRev,Value=${builder_git_rev}" \ "Key=BuilderGitRev,Value=${builder_git_rev}" \
"Key=FlakeLockRev,Value=${flake_lock_rev}" "Key=FlakeLockRev,Value=${flake_lock_rev}"
aws ec2 create-tags \ step "tag nix snapshot and volume" aws ec2 create-tags \
--region "$aws_region" \ --region "$aws_region" \
--resources "$nix_snapshot_id" "$nix_volume_id" \ --resources "$nix_snapshot_id" "$nix_volume_id" \
--tags \ --tags \
@@ -263,7 +293,7 @@ aws ec2 create-tags \
"Key=FlakeLockRev,Value=${flake_lock_rev}" \ "Key=FlakeLockRev,Value=${flake_lock_rev}" \
"Key=NixPool,Value=${nix_pool}" "Key=NixPool,Value=${nix_pool}"
aws ec2 create-tags \ step "tag final AMI" aws ec2 create-tags \
--region "$aws_region" \ --region "$aws_region" \
--resources "$image_id" \ --resources "$image_id" \
--tags \ --tags \
@@ -285,7 +315,7 @@ for ssm_ami_parameter_name in \
"${ssm_ami_prefix}/by-builder-rev/${builder_git_rev}" \ "${ssm_ami_prefix}/by-builder-rev/${builder_git_rev}" \
"${ssm_ami_prefix}/by-input-hash/${input_hash}" "${ssm_ami_prefix}/by-input-hash/${input_hash}"
do do
aws ssm put-parameter \ step "publish SSM ${ssm_ami_parameter_name}" aws ssm put-parameter \
--region "$aws_region" \ --region "$aws_region" \
--name "$ssm_ami_parameter_name" \ --name "$ssm_ami_parameter_name" \
--type String \ --type String \
@@ -293,7 +323,7 @@ do
--overwrite >/dev/null --overwrite >/dev/null
done done
aws ec2 deregister-image --region "$aws_region" --image-id "$capture_image_id" >/dev/null step "deregister capture image" aws ec2 deregister-image --region "$aws_region" --image-id "$capture_image_id" >/dev/null
jq -n \ jq -n \
--arg architecture "$architecture" \ --arg architecture "$architecture" \
@@ -324,7 +354,7 @@ jq -n \
}' > artifact.json }' > artifact.json
if [ "$keep_builder" = false ]; then if [ "$keep_builder" = false ]; then
tofu destroy \ step "tofu destroy builder resources" tofu destroy \
-auto-approve \ -auto-approve \
"${common_tofu_vars[@]}" \ "${common_tofu_vars[@]}" \
"${extra_tofu_args[@]}" "${extra_tofu_args[@]}"
@@ -332,3 +362,4 @@ fi
echo "Published AMI: $image_id" echo "Published AMI: $image_id"
echo "Published SSM parameter: ${ssm_ami_prefix}/recommended" echo "Published SSM parameter: ${ssm_ami_prefix}/recommended"
log "total elapsed: $(total_elapsed)"