Google Cloud SDK User Script Docker


Recommended Posts

The enclosed script is my own creation for using a Dockerhub Google Cloud SDK image in order to run API commands for DNS updates.  This script starts a Docker container, runs the command, and discards it.  The persistent storage is in the appdata directory and should be mounted each time a new project is initialized or command is run. This is intended to be run via the User Scripts plugin.  Simply tweak the script to your liking and fill in the variables at the top.

 

Please do not ask for support regarding the Google API commands as I am no expert on all the details of what that entails, its simply provided as a starting point for those looking to run the GCloud SDK on Unraid without dedicating a VM to the task.  Before the script is run you will need to run the gcloud auth login and gcloud workspace commands with the persistent storage attached.  Directly from command line that would look something like this:

 

docker run --rm -e CLOUDSDK_CORE_PROJECT=$PROJECT_NAME -v /mnt/user/appdata/gcloud:/root/.config/gcloud google/cloud-sdk gcloud auth login



I use the LinuxServer.io Letsencrypt (SWAG) container with Google DNS authentication for a wildcard web certificate.  There does not seem to be an easier method of implementing dynamic DNS via Google Cloud (Google Domains does not have DNS authentication for wildcard certificates).

 

#!/bin/bash
DNS_ZONE=
DNS_RECORD_NAME=
DNS_RECORD_TYPE=A
DNS_RECORD_TTL=300
PATH_TRANSACTION=/root/.config/gcloud
PROJECT_NAME=

DNS_RECORD_OLDDATA=$(docker run --rm -e CLOUDSDK_CORE_PROJECT=$PROJECT_NAME -v /mnt/user/appdata/gcloud:/root/.config/gcloud google/cloud-sdk gcloud dns record-sets list --zone=$DNS_ZONE --name=$DNS_RECORD_NAME --type=$DNS_RECORD_TYPE | grep $DNS_RECORD_NAME | xargs | cut -d\  -f4)
DNS_RECORD_NEWDATA=$(curl ifconfig.me)

if [[ $DNS_RECORD_OLDDATA != $DNS_RECORD_NEWDATA ]]; then
  DEFAULT="--zone=$DNS_ZONE --transaction-file=$PATH_TRANSACTION/gcloud-dns-transaction-$(date +%s).yaml"
  docker run --rm -e CLOUDSDK_CORE_PROJECT=$PROJECT_NAME -v /mnt/user/appdata/gcloud:/root/.config/gcloud google/cloud-sdk gcloud dns record-sets transaction start $DEFAULT
  docker run --rm -e CLOUDSDK_CORE_PROJECT=$PROJECT_NAME -v /mnt/user/appdata/gcloud:/root/.config/gcloud google/cloud-sdk gcloud dns record-sets transaction remove $DEFAULT --name=$DNS_RECORD_NAME --type=$DNS_RECORD_TYPE --ttl=$DNS_RECORD_TTL $DNS_RECORD_OLDDATA
  docker run --rm -e CLOUDSDK_CORE_PROJECT=$PROJECT_NAME -v /mnt/user/appdata/gcloud:/root/.config/gcloud google/cloud-sdk gcloud dns record-sets transaction add $DEFAULT --name=$DNS_RECORD_NAME --type=$DNS_RECORD_TYPE --ttl=$DNS_RECORD_TTL $DNS_RECORD_NEWDATA
  docker run --rm -e CLOUDSDK_CORE_PROJECT=$PROJECT_NAME -v /mnt/user/appdata/gcloud:/root/.config/gcloud google/cloud-sdk gcloud dns record-sets transaction execute $DEFAULT
fi

 

Anyways, thats my rabbit hole for today... cheers.

dnsupdate.sh

Edited by slashmach1
discovered code function in forum posting
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.