cloudflare/GCS-Logshare-Setup-Script
Publicmirrored fromhttps://github.com/cloudflare/GCS-Logshare-Setup-Script
main.sh
83lines · modecode
Updated logshare-cli cmd line option to match spec. Updated a few things in the readme such as removing /received because it's the default endpoint now. Updated main.sh script output73222c3
8 years ago
| 1 | #! /bin/bash |
| 2 | |
| 3 | ## Checking for local dependencies |
| 4 | # curl |
| 5 | if ! curl -V > /dev/null; then |
| 6 | echo "Curl, Python and JQ are required." |
| 7 | exit 1 |
| 8 | fi |
| 9 | |
| 10 | # python |
| 11 | if ! python -V > /dev/null; then |
| 12 | echo "Curl, Python and JQ are required." |
| 13 | exit 1 |
| 14 | fi |
| 15 | |
| 16 | # jq |
| 17 | if ! jq -V > /dev/null; then |
| 18 | echo "Curl, Python and JQ are required." |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | # copied version of config.default.json to config.json |
| 23 | if [ ! -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 |
| 26 | fi |
| 27 | |
| 28 | ## Setting up the Google Cloud SDK |
| 29 | if gcloud --version 2>&1 | grep "Updates are available for some Cloud SDK components." -c; then # Checking for Updates |
| 30 | gcloud components update --quiet |
| 31 | elif gcloud --version > /dev/null; then # skipping if already installed. |
| 32 | echo "GCloud SDK already Installing. Skipping init configuration." |
| 33 | else # 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 |
| 43 | fi |
| 44 | |
| 45 | ## Authenticate if required |
| 46 | if gcloud auth list 2>&1 | grep "No credentialed accounts." -c; then |
| 47 | gcloud auth login |
| 48 | fi |
| 49 | |
| 50 | ## Grab project Id from the config file |
| 51 | PROJECTID=$(jq -rc '[ .gcs_project_id ] | .[]' config.json) |
| 52 | gcloud config set project $PROJECTID |
| 53 | gcloud projects list | grep $PROJECTID -c > /dev/null |
| 54 | if [[ $? -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 |
| 60 | fi |
| 61 | |
| 62 | ## Create a random ID for this project |
| 63 | RANDOMVALUE=$RANDOM |
| 64 | |
| 65 | ## Define the GCS Bucket Name |
| 66 | VMBUCKETVALUE=cf-els-vm-setupfiles-$RANDOMVALUE |
| 67 | VMBUCKET="gs://$VMBUCKETVALUE" |
| 68 | |
| 69 | ## Make bucket for the VM setup files |
| 70 | gsutil mb -c regional -l us-central1 -p $PROJECTID $VMBUCKET |
| 71 | |
| 72 | ## Copy config.json and gcs-initialize.sh to Google Cloud Storage |
| 73 | gsutil cp config.json gcs-initialize.sh $VMBUCKET |
| 74 | |
| 75 | ## Output newline |
| 76 | echo -e "Creating VM...\n" |
| 77 | |
| 78 | ## Create the VM |
| 79 | gcloud 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 |
| 82 | echo -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!" |