nixos-config/scripts/generate-and-upload-gce-image
2022-11-27 00:52:16 +08:00

59 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env nix-shell
#! nix-shell -i bash -p google-cloud-sdk
#
# This is a modified script `nixos/maintainers/scripts/gce/create-gce.sh' from
# `nixpkgs'.
#
# Licensed under MIT license
# Copyright (c) 2003-2022 Eelco Dolstra and the Nixpkgs/NixOS contributors
# 2022 Gabriel Arazas
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
BUCKET_NAME="${BUCKET_NAME:-foodogsquared-operating-system-images-dumping-ground}"
TIMESTAMP="$(date +%Y%m%d%H%M)"
PACKAGE=plover-gce
DIRECTORY=gce-image
export TIMESTAMP
nix build ".#$PACKAGE" -o "$DIRECTORY"
img_path=$(echo "$DIRECTORY"/*.tar.gz)
img_name=${IMAGE_NAME:-$(basename "$img_path")}
img_id=$(echo "$img_name" | sed 's|.raw.tar.gz$||;s|\.|-|g;s|_|-|g')
img_family=$(echo "$img_id" | cut -d - -f1-4)
if ! gsutil ls "gs://${BUCKET_NAME}/$img_name"; then
gsutil cp "$img_path" "gs://${BUCKET_NAME}/$img_name"
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'
fi
# vi:ft=bash