Skip to content

Commit

Permalink
Add support to use a specific service account via command line (#54)
Browse files Browse the repository at this point in the history
* Add support to use a specific service account via command line

* Updated Usage

* Fix typo
  • Loading branch information
guestisp authored and jacksegal committed Oct 8, 2018
1 parent 2786c02 commit 46dd37f
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions gcloud-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export PATH=$PATH:/usr/local/bin/:/usr/bin
#

usage() {
echo -e "\nUsage: $0 [-d <days>] [-t <label_name>] [-i <instance_name>] [-z <instance_zone>] [-p <prefix>]" 1>&2
echo -e "\nUsage: $0 [-d <days>] [-t <label_name>] [-i <instance_name>] [-z <instance_zone>] [-p <prefix>] [-a <service_account>]" 1>&2
echo -e "\nOptions:\n"
echo -e " -d Number of days to keep snapshots. Snapshots older than this number deleted."
echo -e " Default if not set: 7 [OPTIONAL]"
Expand All @@ -28,6 +28,7 @@ usage() {
echo -e " host."
echo -e " -z Instance zone. If empty, uses the zone of the calling host."
echo -e " -p Prefix to be used for naming snapshots, default to 'gcs'"
echo -e " -a Service Account to use. If empty, it uses the gcloud default."
echo -e "\n"
exit 1
}
Expand All @@ -39,7 +40,7 @@ usage() {

setScriptOptions()
{
while getopts ":d:t:i:z:p:" o; do
while getopts ":d:t:i:z:p:a:" o; do
case "${o}" in
d)
opt_d=${OPTARG}
Expand All @@ -56,6 +57,9 @@ setScriptOptions()
p)
opt_p=${OPTARG}
;;
a)
opt_a=${OPTARG}
;;
*)
usage
;;
Expand Down Expand Up @@ -92,6 +96,12 @@ setScriptOptions()
else
PREFIX="gcs"
fi

if [[ -n $opt_a ]];then
OPT_INSTANCE_SERVICE_ACCOUNT="--account $opt_a"
else
OPT_INSTANCE_SERVICE_ACCOUNT=""
fi
}


Expand Down Expand Up @@ -122,7 +132,7 @@ getInstanceId()
if [[ -z "$OPT_INSTANCE_NAME" ]];then # no typo: only when querying for the calling machine get the real instance ID
echo -e "$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/id" -H "Metadata-Flavor: Google")"
else
echo -e "$(gcloud -q compute instances describe $OPT_INSTANCE_NAME --zone=$INSTANCE_ZONE --format='value(id)')"
echo -e "$(gcloud $OPT_INSTANCE_SERVICE_ACCOUNT -q compute instances describe $OPT_INSTANCE_NAME --zone=$INSTANCE_ZONE --format='value(id)')"
fi
}

Expand Down Expand Up @@ -152,7 +162,7 @@ getInstanceZone()

getDeviceList()
{
echo -e "$(gcloud compute disks list --filter "users~instances/$1\$ $LABEL_CLAUSE" --format='value(name)')"
echo -e "$(gcloud $OPT_INSTANCE_SERVICE_ACCOUNT compute disks list --filter "users~instances/$1\$ $LABEL_CLAUSE" --format='value(name)')"
}


Expand Down Expand Up @@ -197,7 +207,7 @@ createSnapshotName()

createSnapshot()
{
echo -e "$(gcloud compute disks snapshot $1 --snapshot-names $2 --zone $3)"
echo -e "$(gcloud $OPT_INSTANCE_SERVICE_ACCOUNT compute disks snapshot $1 --snapshot-names $2 --zone $3)"
}


Expand All @@ -214,7 +224,7 @@ getSnapshotsForDeletion()
SNAPSHOTS=()

# get list of snapshots from gcloud for this device
local gcloud_response="$(gcloud compute snapshots list --filter="name~'"$1"' AND creationTimestamp<'$2'" --uri)"
local gcloud_response="$(gcloud $OPT_INSTANCE_SERVICE_ACCOUNT compute snapshots list --filter="name~'"$1"' AND creationTimestamp<'$2'" --uri)"

# loop through and get snapshot name from URI
while read line
Expand Down Expand Up @@ -249,7 +259,7 @@ getSnapshotDeletionDate()

deleteSnapshot()
{
echo -e "$(gcloud compute snapshots delete $1 -q)"
echo -e "$(gcloud $OPT_INSTANCE_SERVICE_ACCOUNT compute snapshots delete $1 -q)"
}


Expand Down

0 comments on commit 46dd37f

Please sign in to comment.