Repository navigation
Expand file tree
/
Copy pathutils.sh
More file actions
92 lines (80 loc) · 2.79 KB
/
Copy pathutils.sh
File metadata and controls
92 lines (80 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Utilies for working with clusters and google cloud
# Checks that the gsutil CLI exists.
function check_gsutil_exists() {
command -v gsutil >/dev/null 2>&1 || {
echo >&2 "gsutil command is not defined"
echo >&2 "Install Google Cloud SDK first:"
echo >&2 " https://cloud.google.com/sdk/downloads"
exit 1
}
}
# Checks that the gcloud CLI exists.
function check_gcloud_exists() {
command -v gcloud >/dev/null 2>&1 || {
echo >&2 "gcloud command is not defined"
echo >&2 "Install Google Cloud SDK first"
echo >&2 " https://cloud.google.com/sdk/downloads"
exit 1
}
}
# Creates a cloud bucket if it doesn't exist. Recall that cloud buckets are
# a global namespace.
#
# Globals:
# BUCKET_NAME: The name of the cloud bucket
# BUCKET_LOCATION: The location to create the cloud bucket
function create_gcs_bucket() {
check_gsutil_exists
if [[ -z "${BUCKET_NAME}" ]]; then
echo >&2 "BUCKET_NAME is not defined"
return 1
fi
if [[ -z "${BUCKET_LOCATION}" ]]; then
echo >&2 "BUCKET_LOCATION is not defined"
return 1
fi
gsutil ls -b gs://$BUCKET_NAME >/dev/null || {
echo >&2 "Bucket $BUCKET_NAME does not exist. Creating."
gsutil mb -l $BUCKET_LOCATION gs://$BUCKET_NAME
}
}
# Creates a cluster service account if it doesn't exist.
# Globals:
# PROJECT: The cloud project
# SERVICE_ACCOUNT: The service account email to create.
function create_service_account_key() {
check_gcloud_exists
if [[ -z "${PROJECT}" ]]; then
echo >&2 "PROJECT is not defined"
return 1
fi
if [[ -z "${SERVICE_ACCOUNT}" ]]; then
echo >&2 "SERVICE_ACCOUNT is not defined"
return 1
fi
if [[ -z "${SERVICE_ACCOUNT_EMAIL}" ]]; then
echo >&2 "SERVICE_ACCOUNT_EMAIL is not defined"
return 1
fi
if [[ -z "${SERVICE_ACCOUNT_KEY_LOCATION}" ]]; then
echo >&2 "SERVICE_ACCOUNT_KEY_LOCATION is not defined"
return 1
fi
if ! gcloud iam service-accounts list --project=$PROJECT | grep -q ${SERVICE_ACCOUNT}; then
echo >&2 "SERVICE_ACCOUNT doesn't exist: creating"
gcloud iam service-accounts create $SERVICE_ACCOUNT --project=$PROJECT
fi
# Grant it read/write permissions on our bucket. Should be safe to do multiple times
gsutil acl ch -u "${SERVICE_ACCOUNT_EMAIL}":W gs://${BUCKET_NAME}
if [[ ! -f "${SERVICE_ACCOUNT_KEY_LOCATION}" ]]; then
echo >&2 "Service account key file doesn't exist."
echo >&2 "Creating new key file at ${SERVICE_ACCOUNT_KEY_LOCATION}"
gcloud iam service-accounts keys create \
"${SERVICE_ACCOUNT_KEY_LOCATION}" --iam-account "${SERVICE_ACCOUNT_EMAIL}"
# Ensure the service account can write to GCS
gcloud projects add-iam-policy-binding "${PROJECT}" \
--member serviceAccount:"${SERVICE_ACCOUNT_EMAIL}" \
--role roles/storage.objectAdmin \
--project "${PROJECT}"
fi
}