at master 2.8 kB view raw
1#! /usr/bin/env bash 2 3set -euo pipefail 4 5script_dir="$(dirname $(readlink -f $0))" 6nixpkgs_root="$script_dir/../../../.." 7export NIX_PATH="nixpkgs=$nixpkgs_root" 8 9cat - <<EOF 10This script will locally build a NixOS image and upload it as a Custom Image 11using oci-cli. Make sure that an API key for the tenancy administrator has been 12added to '~/.oci'. 13For more info about configuring oci-cli, please visit 14https://docs.cloud.oracle.com/iaas/Content/API/Concepts/apisigningkey.htm#Required_Keys_and_OCIDs 15 16EOF 17 18qcow="oci-image/nixos.qcow2" 19if [ ! -f "$qcow" ]; then 20 echo "OCI image $qcow does not exist" 21 echo "Building image with create-image.sh for 'x86_64-linux'" 22 "$script_dir/create-image.sh" x86_64-linux 23 [ -f "$qcow" ] || { echo "Build failed: image not present after build"; exit 1; } 24else 25 echo "Using prebuilt image $qcow" 26fi 27 28cli="$( 29 nix-build '<nixpkgs>' \ 30 --no-out-link \ 31 -A oci-cli 32)" 33 34PATH="$cli/bin:$PATH" 35bucket="_TEMP_NIXOS_IMAGES_$RANDOM" 36 37echo "Creating a temporary bucket" 38root_ocid="$( 39 oci iam compartment list \ 40 --all \ 41 --compartment-id-in-subtree true \ 42 --access-level ACCESSIBLE \ 43 --include-root \ 44 --raw-output \ 45 --query "data[?contains(\"id\",'tenancy')].id | [0]" 46)" 47bucket_ocid=$( 48 oci os bucket create \ 49 -c "$root_ocid" \ 50 --name "$bucket" \ 51 --raw-output \ 52 --query "data.id" 53) 54# Clean up bucket on script termination 55trap 'echo Removing temporary bucket; oci os bucket delete --force --name "$bucket"' INT TERM EXIT 56 57echo "Uploading image to temporary bucket" 58oci os object put -bn "$bucket" --file "$qcow" 59 60echo "Importing image as a Custom Image" 61bucket_ns="$(oci os ns get --query "data" --raw-output)" 62image_id="$( 63 oci compute image import from-object \ 64 -c "$root_ocid" \ 65 --namespace "$bucket_ns" \ 66 --bucket-name "$bucket" \ 67 --name nixos.qcow2 \ 68 --operating-system NixOS \ 69 --source-image-type QCOW2 \ 70 --launch-mode PARAVIRTUALIZED \ 71 --display-name NixOS \ 72 --raw-output \ 73 --query "data.id" 74)" 75 76cat - <<EOF 77Image created! Please mark all available shapes as compatible with this image by 78visiting the following link and by selecting the 'Edit Details' button on: 79https://cloud.oracle.com/compute/images/$image_id 80EOF 81 82# Workaround until https://github.com/oracle/oci-cli/issues/399 is addressed 83echo "Sleeping for 15 minutes before cleaning up files in the temporary bucket" 84sleep $((15 * 60)) 85 86echo "Deleting image from bucket" 87par_id="$( 88 oci os preauth-request list \ 89 --bucket-name "$bucket" \ 90 --raw-output \ 91 --query "data[0].id" 92)" 93 94if [[ -n $par_id ]]; then 95 oci os preauth-request delete \ 96 --bucket-name "$bucket" \ 97 --par-id "$par_id" 98fi 99 100oci os object delete -bn "$bucket" --object-name nixos.qcow2 --force