-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_resources.sh
executable file
·117 lines (101 loc) · 4.03 KB
/
create_resources.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
#!/bin/bash
set -exo pipefail
export API_DOMAIN=${API_DOMAIN:-"appscode.com"}
export API_SECRET=${API_SECRET:-}
export GCP_PROJECT=${GCP_PROJECT:-"appscode-testing"}
export GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS:-gcp-cred.json}
export REGION=${REGION:-us-central1}
#variables
BUCKET_NAME=${BUCKET_NAME:-}
PUBLIC_IP=${PUBLIC_IP:-}
RAND=$(head /dev/urandom | tr -dc 'a-z' | head -c 4)
GOOGLE_APPLICATION_CREDENTIALS_STRING=$(cat $GOOGLE_APPLICATION_CREDENTIALS | base64 -w 0)
function ace::gcp::install_gcloud() {
echo "Installing Google Cloud SDK..."
export DEBIAN_FRONTEND="noninteractive"
apt-get -y update
apt upgrade -y
apt-get install jq unzip -y >/dev/null
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
sudo apt-get install apt-transport-https ca-certificates gnupg -y
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
sudo apt-get update && sudo apt-get install google-cloud-sdk -y >/dev/null
}
function ace::gcp::setup_gcloud() {
gcloud auth activate-service-account --key-file="$GOOGLE_APPLICATION_CREDENTIALS"
gcloud config set project "$GCP_PROJECT"
}
function ace::gcp::create_bucket() {
BUCKET_NAME="ace-bucket-$RAND"
echo "Creating bucket: $BUCKET_NAME in project: $GCP_PROJECT and region: ${REGION}"
gsutil mb -p "$GCP_PROJECT" -c STANDARD -l "${REGION}" gs://"$BUCKET_NAME"/
if [ $? -eq 0 ]; then
echo "Bucket $BUCKET_NAME created successfully."
else
echo "Failed to create bucket $BUCKET_NAME."
exit
fi
}
function ace::gcp::create_static_public_ip() {
PUBLIC_IP=$(gcloud compute addresses create "ace-ip-$RAND" --global --ip-version IPV4 --format="get(address)")
}
function ace::gcp::finalize_installer() {
CLUSTER_ID=$(kubectl get ns kube-system -o=jsonpath='{.metadata.uid}')
UTC_TIME=$(date -u +"%Y-%m-%dT%H:%M:%S.%NZ")
resp=$(curl -X POST https://${API_DOMAIN}/marketplace/api/v1/marketplaces/gcp/notification/resource?secret=${API_SECRET} \
-H "Content-Type: application/json" \
-d '{
"eventType": "BIND",
"eventTime": "'${UTC_TIME}'",
"bindingInfo": {
"installerID": "'${INSTALLER_ID}'",
"clusterId": "'${CLUSTER_ID}'",
"options": {
"infra": {
"dns": {
"provider": "none",
"targetIPs": ["'${PUBLIC_IP}'"]
},
"cloudServices": {
"objstore": {
"auth": {
"gcs": {
"GOOGLE_PROJECT_ID": "'${GCP_PROJECT}'",
"GOOGLE_SERVICE_ACCOUNT_JSON_KEY": "'${GOOGLE_APPLICATION_CREDENTIALS_STRING}'"
}
},
"bucket": "gs://'${BUCKET_NAME}'",
"prefix": "ace"
},
"provider": "gcs"
},
"kubestash": {
"backend": {
"gcs": {
"bucket": "gs://'${BUCKET_NAME}'",
"prefix": "ace"
},
"provider": "gcs"
},
"retentionPolicy": "keep-1mo",
"schedule": "0 */2 * * *",
"storageSecret": {
"create": true
}
}
}
}
}
}')
link=$(echo ${resp} | jq -r '.link')
if [ ${link} == "null" ]; then exit; fi
curl -L "${link}" -o "archive.tar.gz"
tar -zxvf archive.tar.gz >/dev/null
}
function ace::gcp::init() {
ace::gcp::install_gcloud
ace::gcp::setup_gcloud
ace::gcp::create_bucket
ace::gcp::create_static_public_ip
ace::gcp::finalize_installer
}