-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp-init.sh
executable file
·147 lines (121 loc) · 3.19 KB
/
gcp-init.sh
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /bin/bash
set -e
trap 'catch $?' EXIT
function catch() {
if [ "$1" != "0" ]; then
echo "An error occurred"
exit 1
fi
}
function usage() {
if [[ $* != "" ]]; then
echo "Error: $*"
fi
cat <<EOF
Usage: $PROGNAME [OPTION ...]
Create GCP project and terraform state bucket
Options:
--help display this usage message and exit
--account GCP account email
--gcpproject GCP project name
--region GCP region
EOF
exit 0
}
function life_cycle_policy() {
filename=$(mktemp)
cat <<EOF >"$filename"
{
"lifecycle": {
"rule": [
{
"action": { "type": "Delete" },
"condition": {
"numNewerVersions": 2,
"isLive": false
}
}
]
}
}
EOF
echo "${filename}"
}
function bucket_name() {
local gcpproject=$1
echo "${gcpproject}-terraform-state"
}
function create_project_and_bucket() {
local account=$1
local gcpproject=$2
local region=$3
bucket_name="$(bucket_name "${gcpproject}")"
# Project
gcloud projects create "${gcpproject}" --enable-cloud-apis
# Billing
billing_account="$(gcloud beta billing accounts list | awk 'NR==2 { printf $1 }')"
gcloud beta billing projects link "${gcpproject}" --billing-account "${billing_account}"
# Enable API's
gcloud services enable iam.googleapis.com --project "${gcpproject}"
gcloud services enable billingbudgets.googleapis.com --project "${gcpproject}"
gcloud services enable cloudbilling.googleapis.com --project "${gcpproject}"
gcloud services enable cloudresourcemanager.googleapis.com --project "${gcpproject}"
# Add Terraform admin role
permissions="serviceusage.services.use"
permissions="${permissions},resourcemanager.projects.get"
permissions="${permissions},storage.objects.create"
permissions="${permissions},storage.objects.delete"
permissions="${permissions},storage.objects.get"
permissions="${permissions},storage.objects.getIamPolicy"
permissions="${permissions},storage.objects.list"
gcloud iam roles create "terraform.admin" --title "Terraform Admin" --project "${gcpproject}" --permissions "${permissions}" --stage="GA"
gcloud projects add-iam-policy-binding "${gcpproject}" --member="user:${account}" --role=projects/"${gcpproject}"/roles/terraform.admin
# Bucket
gsutil mb -p "${gcpproject}" -l "${region}" gs://"${bucket_name}"
# Uniform bucket access
gsutil ubla set on gs://"${bucket_name}"
# Prevent public access
gsutil pap set enforced gs://"${bucket_name}"
# Versioning
gsutil versioning set on gs://"${bucket_name}"
gsutil lifecycle set "$(life_cycle_policy)" gs://"${bucket_name}"
}
##
## Script
##
account=""
gcpproject=""
region=""
while [[ $# -gt 0 ]]; do
case "$1" in
--help)
usage
;;
--account)
account="$2"
shift
;;
--gcpproject)
gcpproject="$2"
shift
;;
--region)
region="$2"
shift
;;
*)
usage "Unknown option '$1'"
;;
esac
shift
done
if [ "${account}" == "" ]; then
usage "Account email must be provided"
fi
if [ "${gcpproject}" == "" ]; then
usage "GCP project name must be provided"
fi
if [ "${region}" == "" ]; then
usage "Region must be provided"
fi
create_project_and_bucket "${account}" "${gcpproject}" "${region}"