Debian Codegraph @

launcher/image/test/scripts/test_ssh_manual.sh

raw
1 #!/bin/bash
2 set -euxo pipefail
3
4 print_usage() {
5 echo "usage: test_ssh_manual.sh [-i imageName] [-p imageProject]"
6 echo " -i <imageName>: which image name to use for the VM"
7 echo " -p <imageProject>: which image project to use for the VM"
8 exit 1
9 }
10
11 run_ssh_test() {
12 BUILD_ID=$(date +%s)
13 HOME_DIR=$(echo ~)
14 VM_NAME="cs-ssh-test-$BUILD_ID"
15 WORKLOAD_IMAGE='us-west1-docker.pkg.dev/confidential-space-images-dev/cs-integ-test-images/basic-test:latest'
16 ZONE="us-central1-a"
17
18 ACCOUNT_NAME=$(gcloud config list account --format "value(core.account)" | tr @. _)
19 PROJECT_NAME=$(gcloud config get-value project)
20
21 # Create a new VM
22 source create_vm.sh -n $VM_NAME -i $IMAGE_NAME -p $IMAGE_PROJECT -m tee-image-reference=$WORKLOAD_IMAGE,tee-container-log-redirect=true,enable-osconfig=TRUE -z $ZONE
23
24 # Add an SSH public key to an OS Login profile
25 gcloud compute os-login ssh-keys add --key-file=$HOME_DIR/.ssh/google_compute_engine.pub || true
26
27 echo "Sleeping so settings have time to propagate."
28 sleep 30
29
30 # SSH into VM with script
31 if [[ $IMAGE_NAME == *"debug"* ]]; then
32 if ssh -i ~/.ssh/google_compute_engine -o StrictHostKeyChecking=no $ACCOUNT_NAME@nic0.$VM_NAME.$ZONE.c.$PROJECT_NAME.internal.gcpnode.com "echo 'SSHABLE'; exit" ; then
33 echo "Success: SSH to host was successful"
34 sed -i '$ d' ~/.ssh/known_hosts
35 else
36 echo "TEST FAILED: SSH to host was ussuccessful"
37 fi
38 else
39 if ssh -i ~/.ssh/google_compute_engine -o StrictHostKeyChecking=no $ACCOUNT_NAME@nic0.$VM_NAME.$ZONE.c.$PROJECT_NAME.internal.gcpnode.com "echo 'SSHABLE'; exit" ; then
40 echo "TEST FAILED: SSH to host was successful"
41 sed -i '$ d' ~/.ssh/known_hosts
42 else
43 echo "Success: SSH to host was ussuccessful"
44 fi
45 fi
46
47
48 # Clean up
49 CLEANUP=true
50 source cleanup.sh $VM_NAME $ZONE
51 }
52
53 IMAGE_NAME=''
54 IMAGE_PROJECT=''
55
56 # In getopts, a ':' following a letter means that that flag takes an argument.
57 # For example, i: means -i takes an additional argument.
58 while getopts 'i:p:' flag; do
59 case "${flag}" in
60 i) IMAGE_NAME=${OPTARG} ;;
61 p) IMAGE_PROJECT=${OPTARG} ;;
62 *) print_usage ;;
63 esac
64 done
65
66 if [ -z "$IMAGE_NAME" ]; then
67 echo "Empty image name supplied."
68 exit 1
69 fi
70
71 if [ -z "$IMAGE_PROJECT" ]; then
72 echo "Empty image project supplied."
73 exit 1
74 fi
75
76 run_ssh_test