11#! /bin/bash
22#
33# Redeploys an existing GCP VM with a new image while preserving its IP address and metadata.
4- # Usage: ./redeploy_gcp.sh --tdx|--sev-snp <vm-name> <tar.gz-file>
4+ #
5+ # Usage: ./redeploy_gcp.sh <tar.gz-file> [--tdx|--sev-snp] [vm-name] [--zone=ZONE] [--dry-run]
56#
67# The script will:
78# 1. Capture the existing VM's configuration (IP, metadata, machine type, etc.)
1415# - A GCP project selected (gcloud config set project <project>)
1516# - The VM to redeploy exists
1617#
18+ # Arguments:
19+ # tar.gz-file Path to the tar.gz file to deploy (required)
20+ # --tdx Deploy to Intel TDX machine (default)
21+ # --sev-snp Deploy to AMD SEV-SNP machine
22+ # vm-name Name of the VM to redeploy (optional, uses cache or prompts)
23+ # --zone=ZONE Zone where the VM is located (default: us-central1-a)
24+ # --dry-run Show what would be done without making changes
25+ #
1726# Examples:
18- # ./redeploy_gcp.sh --tdx gcp-tdx-demo build/kettle-vm.tar.gz
19- # ./redeploy_gcp.sh --tdx gcp-tdx-demo build/kettle-vm-devtools.tar.gz
20- # ./redeploy_gcp.sh --sev-snp gcp-sev-demo build/kettle-vm.tar.gz
21- # ./redeploy_gcp.sh --tdx gcp-tdx-demo build/kettle-vm.tar.gz --dry-run
27+ # ./redeploy_gcp.sh build/kettle-vm.tar.gz # Prompts for VM name
28+ # ./redeploy_gcp.sh build/kettle-vm.tar.gz my-vm # TDX by default
29+ # ./redeploy_gcp.sh build/kettle-vm.tar.gz --tdx my-vm
30+ # ./redeploy_gcp.sh build/kettle-vm.tar.gz --sev-snp my-vm
31+ # ./redeploy_gcp.sh build/kettle-vm.tar.gz --dry-run # Uses cached VM name
2232#
2333
2434set -euo pipefail
@@ -27,6 +37,7 @@ set -euo pipefail
2737BUCKET_NAME=" " # Will be set based on project ID
2838REGION=" us-central1"
2939DEFAULT_ZONE=" us-central1-a"
40+ VM_NAME_CACHE_FILE=" .vm_name_gcp"
3041
3142# Colors for logging
3243RED=' \033[0;31m'
@@ -88,71 +99,75 @@ cleanup_on_failure() {
8899 exit 1
89100}
90101
91- # Validate arguments
92- if [ $# -lt 3 ]; then
93- echo " Usage: $0 --tdx|--sev-snp <vm-name> <tar.gz-file> [--zone=ZONE]"
94- echo " "
95- echo " Required first argument (TEE type):"
96- echo " --tdx Deploy to Intel TDX machine (c3-standard-4)"
97- echo " --sev-snp Deploy to AMD SEV-SNP machine (n2d-standard-2)"
102+ # Validate arguments - need at least the tar.gz file
103+ if [ $# -lt 1 ]; then
104+ echo " Usage: $0 <tar.gz-file> [--tdx|--sev-snp] [vm-name] [--zone=ZONE] [--dry-run]"
98105 echo " "
99106 echo " Arguments:"
100- echo " vm-name Name of the existing VM to redeploy"
101- echo " tar.gz-file Path to the tar.gz file to deploy"
107+ echo " tar.gz-file Path to the tar.gz file to deploy (required)"
102108 echo " "
103109 echo " Options:"
110+ echo " --tdx Deploy to Intel TDX machine (default)"
111+ echo " --sev-snp Deploy to AMD SEV-SNP machine"
112+ echo " vm-name Name of the existing VM to redeploy (uses cache or prompts if omitted)"
104113 echo " --zone=ZONE Zone where the VM is located (default: $DEFAULT_ZONE )"
105114 echo " --dry-run Show what would be done without making changes"
106115 echo " "
107116 echo " Examples:"
108- echo " $0 --tdx gcp-tdx-demo build/kettle-vm.tar.gz"
109- echo " $0 --tdx gcp-tdx-demo build/kettle-vm.tar.gz --zone=us-central1-b"
110- echo " $0 --sev-snp gcp-sev-demo build/kettle-vm.tar.gz"
111- echo " $0 --sev-snp gcp-sev-demo build/kettle-vm.tar.gz --dry-run"
117+ echo " $0 build/kettle-vm.tar.gz # Prompts for VM name"
118+ echo " $0 build/kettle-vm.tar.gz my-vm # TDX by default"
119+ echo " $0 build/kettle-vm.tar.gz --tdx my-vm"
120+ echo " $0 build/kettle-vm.tar.gz --sev-snp my-vm"
121+ echo " $0 build/kettle-vm.tar.gz --dry-run # Uses cached VM name"
112122 exit 1
113123fi
114124
115- # Parse required TEE type argument
116- TEE_TYPE=" "
117- case " $1 " in
118- --tdx)
119- TEE_TYPE=" tdx"
120- CONFIDENTIAL_TYPE=" TDX"
121- NEW_MACHINE_TYPE=" c3-standard-4"
122- IMAGE_OS_FEATURES=" UEFI_COMPATIBLE,VIRTIO_SCSI_MULTIQUEUE,GVNIC,TDX_CAPABLE"
123- VM_TAG=" tdx-vm"
124- ;;
125- --sev-snp)
126- TEE_TYPE=" sev-snp"
127- CONFIDENTIAL_TYPE=" SEV_SNP"
128- NEW_MACHINE_TYPE=" n2d-standard-2"
129- IMAGE_OS_FEATURES=" UEFI_COMPATIBLE,VIRTIO_SCSI_MULTIQUEUE,GVNIC,SEV_SNP_CAPABLE"
130- VM_TAG=" sev-snp-vm"
131- ;;
132- * )
133- echo " Error: First argument must be --tdx or --sev-snp"
134- echo " "
135- echo " Usage: $0 --tdx|--sev-snp <vm-name> <tar.gz-file> [options]"
136- exit 1
137- ;;
138- esac
125+ # First argument must be the tar.gz file
126+ TAR_FILE=" $1 "
139127shift
140128
141- VM_NAME=" $1 "
142- TAR_FILE=" $2 "
129+ # Initialize defaults
130+ TEE_TYPE=" tdx"
131+ CONFIDENTIAL_TYPE=" TDX"
132+ NEW_MACHINE_TYPE=" c3-standard-4"
133+ IMAGE_OS_FEATURES=" UEFI_COMPATIBLE,VIRTIO_SCSI_MULTIQUEUE,GVNIC,TDX_CAPABLE"
134+ VM_TAG=" tdx-vm"
135+ VM_NAME=" "
143136VM_ZONE=" $DEFAULT_ZONE "
144-
145- # Parse optional arguments
146137DRY_RUN=false
147- shift 2
138+
139+ # Parse remaining arguments
148140for arg in " $@ " ; do
149141 case " $arg " in
142+ --tdx)
143+ TEE_TYPE=" tdx"
144+ CONFIDENTIAL_TYPE=" TDX"
145+ NEW_MACHINE_TYPE=" c3-standard-4"
146+ IMAGE_OS_FEATURES=" UEFI_COMPATIBLE,VIRTIO_SCSI_MULTIQUEUE,GVNIC,TDX_CAPABLE"
147+ VM_TAG=" tdx-vm"
148+ ;;
149+ --sev-snp)
150+ TEE_TYPE=" sev-snp"
151+ CONFIDENTIAL_TYPE=" SEV_SNP"
152+ NEW_MACHINE_TYPE=" n2d-standard-2"
153+ IMAGE_OS_FEATURES=" UEFI_COMPATIBLE,VIRTIO_SCSI_MULTIQUEUE,GVNIC,SEV_SNP_CAPABLE"
154+ VM_TAG=" sev-snp-vm"
155+ ;;
150156 --zone=* )
151157 VM_ZONE=" ${arg#* =} "
152158 ;;
153159 --dry-run)
154160 DRY_RUN=true
155161 ;;
162+ * )
163+ # Assume it's the VM name if not recognized as an option
164+ if [ -z " $VM_NAME " ]; then
165+ VM_NAME=" $arg "
166+ else
167+ log_error " Unknown argument: $arg "
168+ exit 1
169+ fi
170+ ;;
156171 esac
157172done
158173
@@ -166,6 +181,23 @@ if [ ! -f "$TAR_FILE" ]; then
166181 exit 1
167182fi
168183
184+ # Handle VM name: use provided, cached, or prompt
185+ if [ -z " $VM_NAME " ]; then
186+ # Try to read from cache
187+ if [ -f " $VM_NAME_CACHE_FILE " ]; then
188+ VM_NAME=$( cat " $VM_NAME_CACHE_FILE " )
189+ log_info " Using cached VM name: $VM_NAME "
190+ else
191+ # No cache, prompt for VM name
192+ echo -n " Enter the VM name to redeploy: "
193+ read -r VM_NAME
194+ if [ -z " $VM_NAME " ]; then
195+ log_error " VM name is required"
196+ exit 1
197+ fi
198+ fi
199+ fi
200+
169201# Generate unique identifiers for this deployment
170202DEPLOY_HASH=$( openssl rand -hex 4)
171203TAR_BASENAME=$( basename " $TAR_FILE " )
@@ -559,3 +591,7 @@ if [ "$PROMOTED_IP" = true ] && [ -n "$STATIC_IP_NAME" ]; then
559591 echo " gcloud compute addresses delete $STATIC_IP_NAME --region=$IP_REGION --quiet"
560592fi
561593echo " "
594+
595+ # Cache the VM name for future redeployments
596+ echo " $VM_NAME " > " $VM_NAME_CACHE_FILE "
597+ log_info " Cached VM name '$VM_NAME ' for future redeployments"
0 commit comments