Skip to content

Commit

Permalink
add script to create sa and copy to clipboard (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
samos123 authored Jul 3, 2024
1 parent 0cff677 commit 55f1122
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions deploy/create-gcp-sa.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash

set -e
set -u

copy_to_clipboard() {
local file="$1"
case "$(uname -s)" in
Linux)
# Linux generally uses xclip or xsel. xclip is used here.
if command -v xclip >/dev/null 2>&1; then
xclip -selection clipboard < "$file"
else
echo "xclip is not installed. Please install it to use this function."
return 1
fi
;;
Darwin)
# macOS uses pbcopy
pbcopy < "$file"
;;
CYGWIN*|MINGW32*|MSYS*|MINGW*)
# Windows with Git Bash or similar environment uses /dev/clipboard
cat "$file" > /dev/clipboard
;;
*)
echo "Unsupported operating system."
return 1
;;
esac
}

export PROJECT_ID=${PROJECT_ID:-$(gcloud config get-value project)}
echo "Working in project $PROJECT_ID"

export SA_NAME=${SA_NAME:-"substratus-control-plane"}
set -x

# Create a service account if it doesn't exist
if gcloud iam service-accounts list | grep -q $SA_NAME; then
echo "Service account $SA_NAME already exists"
else
gcloud iam service-accounts create substratus-control-plane --display-name "Service account used by Substratus Control Plane to manage AI infrastructure"
fi

# Assign privleges to the service account
# Assign compute admin
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$SA_NAME@$PROJECT_ID.iam.gserviceaccount.com \
--role roles/compute.admin

# Create a keyfile if it doesn't exist
keyfile_path="/tmp/substratus-sa.keyfile.json"
if [ -f $keyfile_path ]; then
echo "Keyfile $keyfile_path already exists"
else
gcloud iam service-accounts keys create $keyfile_path \
--iam-account $SA_NAME@$PROJECT_ID.iam.gserviceaccount.com
fi

echo "Your service account keyfile is at $keyfile_path."
echo "The contents have been copied to your clipboard."
copy_to_clipboard "$keyfile_path"

0 comments on commit 55f1122

Please sign in to comment.