cloudflare/GCS-Logshare-Setup-Script

Public

mirrored fromhttps://github.com/cloudflare/GCS-Logshare-Setup-Script

CodeCommitsIssuesPull requestsActionsInsightsSecurity
master

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS
SSH

Download ZIP

main.sh

83lines · modecode

1#! /bin/bash
2
3## Checking for local dependencies
4# curl
5if ! curl -V > /dev/null; then
6 echo "Curl, Python and JQ are required."
7 exit 1
8fi
9
10# python
11if ! python -V > /dev/null; then
12 echo "Curl, Python and JQ are required."
13 exit 1
14fi
15
16# jq
17if ! jq -V > /dev/null; then
18 echo "Curl, Python and JQ are required."
19 exit 1
20fi
21
22# copied version of config.default.json to config.json
23if [ ! -e config.json ]; then
24 echo -e "Remember to copy and configure config.default.json to config.json\n\n\tRun:\tmv config.default.json config.json"
25 exit 1
26fi
27
28## Setting up the Google Cloud SDK
29if gcloud --version 2>&1 | grep "Updates are available for some Cloud SDK components." -c; then # Checking for Updates
30 gcloud components update --quiet
31elif gcloud --version > /dev/null; then # skipping if already installed.
32 echo "GCloud SDK already Installing. Skipping init configuration."
33else # install gcloud sdk
34 curl https://sdk.cloud.google.com > cloudsdk.sh
35 chmod +x cloudsdk.sh
36 ./cloudsdk.sh --disable-prompts
37 rm ./cloudsdk.sh
38
39 echo -e "export PATH=\$PATH:~/google-cloud-sdk/bin" >> ~/.bashrc
40 export PATH=$PATH:~/google-cloud-sdk/bin
41
42 gcloud init
43fi
44
45## Authenticate if required
46if gcloud auth list 2>&1 | grep "No credentialed accounts." -c; then
47 gcloud auth login
48fi
49
50## Grab project Id from the config file
51PROJECTID=$(jq -rc '[ .gcs_project_id ] | .[]' config.json)
52gcloud config set project $PROJECTID
53gcloud projects list | grep $PROJECTID -c > /dev/null
54if [[ $? -eq 0 ]]; then
55 gcloud projects create $PROJECTID 2>&1 | grep ERROR -c > /dev/null # Check for errors
56 if [[ $? -ne 0 ]]; then
57 echo "\"gcs_project_id\":\"$PROJECTID\" in config.json must be globally unique"
58 exit 1
59 fi
60fi
61
62## Create a random ID for this project
63RANDOMVALUE=$RANDOM
64
65## Define the GCS Bucket Name
66VMBUCKETVALUE=cf-els-vm-setupfiles-$RANDOMVALUE
67VMBUCKET="gs://$VMBUCKETVALUE"
68
69## Make bucket for the VM setup files
70gsutil mb -c regional -l us-central1 -p $PROJECTID $VMBUCKET
71
72## Copy config.json and gcs-initialize.sh to Google Cloud Storage
73gsutil cp config.json gcs-initialize.sh $VMBUCKET
74
75## Output newline
76echo -e "Creating VM...\n"
77
78## Create the VM
79gcloud compute instances create "logshare-cli-cron-$RANDOMVALUE" --zone "us-central1-a" --machine-type "f1-micro" --image "ubuntu-1604-xenial-v20170919" --subnet "default" --metadata "startup-script-url=$VMBUCKET/gcs-initialize.sh,CONFIGBUCKET=$VMBUCKETVALUE,RANDOMVALUE=$RANDOMVALUE" --image-project "ubuntu-os-cloud" --boot-disk-size "10" --boot-disk-type "pd-standard" --scopes "https://www.googleapis.com/auth/cloud-platform" --project $PROJECTID --verbosity="error"
80
81## Output instructions
82echo -e "\nSuccessfully kicked off the VM provisioning steps. The VM may take up to 10 minutes to fully provision.\n\nIf you are seeing any issues, please share them by submitting an issue to the repository.\n\nYou can login to the VM via ssh with: gcloud compute ssh logshare-cli-cron-$RANDOMVALUE --zone us-central1-a\n\nAnd then you can view the VM's startup script progress by tailing the syslog file:\n\ttail -f /var/log/syslog\n\nEnjoy!"