2017-04-13 11:42:28 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#! nix-shell -i bash -p google-cloud-sdk
|
2014-04-14 12:38:44 +00:00
|
|
|
|
2017-04-13 11:42:28 +00:00
|
|
|
set -euo pipefail
|
|
|
|
|
2018-04-06 08:56:56 +00:00
|
|
|
BUCKET_NAME="${BUCKET_NAME:-nixos-cloud-images}"
|
2017-04-13 11:42:28 +00:00
|
|
|
TIMESTAMP="$(date +%Y%m%d%H%M)"
|
|
|
|
export TIMESTAMP
|
2014-04-14 12:38:44 +00:00
|
|
|
|
2018-11-26 14:51:00 +00:00
|
|
|
nix-build '<nixpkgs/nixos/lib/eval-config.nix>' \
|
2017-04-13 11:42:28 +00:00
|
|
|
-A config.system.build.googleComputeImage \
|
2018-11-26 14:51:00 +00:00
|
|
|
--arg modules "[ <nixpkgs/nixos/modules/virtualisation/google-compute-image.nix> ]" \
|
2017-04-13 11:42:28 +00:00
|
|
|
--argstr system x86_64-linux \
|
|
|
|
-o gce \
|
|
|
|
-j 10
|
2014-04-14 12:38:44 +00:00
|
|
|
|
2017-04-13 11:42:28 +00:00
|
|
|
img_path=$(echo gce/*.tar.gz)
|
2019-10-24 08:53:12 +00:00
|
|
|
img_name=${IMAGE_NAME:-$(basename "$img_path")}
|
2017-04-13 11:42:28 +00:00
|
|
|
img_id=$(echo "$img_name" | sed 's|.raw.tar.gz$||;s|\.|-|g;s|_|-|g')
|
2021-03-20 10:59:13 +00:00
|
|
|
img_family=$(echo "$img_id" | cut -d - -f1-4)
|
|
|
|
|
2017-04-13 11:42:28 +00:00
|
|
|
if ! gsutil ls "gs://${BUCKET_NAME}/$img_name"; then
|
|
|
|
gsutil cp "$img_path" "gs://${BUCKET_NAME}/$img_name"
|
2018-04-06 08:56:56 +00:00
|
|
|
gsutil acl ch -u AllUsers:R "gs://${BUCKET_NAME}/$img_name"
|
2021-03-20 10:59:13 +00:00
|
|
|
|
|
|
|
gcloud compute images create \
|
|
|
|
"$img_id" \
|
|
|
|
--source-uri "gs://${BUCKET_NAME}/$img_name" \
|
|
|
|
--family="$img_family"
|
|
|
|
|
|
|
|
gcloud compute images add-iam-policy-binding \
|
|
|
|
"$img_id" \
|
|
|
|
--member='allAuthenticatedUsers' \
|
|
|
|
--role='roles/compute.imageUser'
|
2014-04-14 12:38:44 +00:00
|
|
|
fi
|