3-infrastructure-as-code-examples/manage-workload-on-gcp/launch-agent-gcp.sh
#!/bin/bash

if [[ $# -eq 0 ]] ; then
    echo valid hostname is required
    exit 1
fi

echo "Hostname = $1"

gcpagent=${1}
# Create instance from template
gcloud compute instances create ${gcpagent} --project=sso-gcp-dba-ctm4-pub-cc10274 --zone=us-central1-a --machine-type=e2-medium --network-interface=network-tier=PREMIUM,subnet=default --metadata=startup-script=\#\!/bin/bash$'\n'sudo\ echo\ 52.12.7.249\ ip-172-31-50-204.us-west-2.compute.internal\ \>\>\ /etc/hosts$'\n'sudo\ apt-get\ update$'\n'sudo\ apt-get\ install\ -yq\ openjdk-11-jdk\ wget\ psmisc$'\n'wget\ https://\<your\ control-m\ em\ url\>:8443/automation-api/ctm-cli.tgz\ --no-check-certificate$'\n'sudo\ curl\ -sL\ https://deb.nodesource.com/setup_14.x\ \|\ sudo\ -E\ bash\ -$'\n'sudo\ apt-get\ install\ -y\ nodejs$'\n'iid=\`curl\ -s\ \"http://metadata.google.internal/computeMetadata/v1/instance/id\"\ -H\ \"Metadata-Flavor:\ Google\"\`$'\n'iname=\`curl\ -s\ \"http://metadata.google.internal/computeMetadata/v1/instance/name\"\ -H\ \"Metadata-Flavor:\ Google\"\`$'\n'zone=\`curl\ -s\ \"http://metadata.google.internal/computeMetadata/v1/instance/zone\"\ -H\ \"Metadata-Flavor:\ Google\"\`$'\n'CTM_ENV=\`gcloud\ compute\ instances\ describe\ \$\(hostname\)\ --zone=\$\{zone\}\ --format=\"text\(labels\)\"\ \|\ grep\ ctmenvironment\ \|\ cut\ -f\ 2\ -d\ \':\'\ \|\ sed\ s/\[^a-zA-Z0-9_-\]//g\`$'\n'ctmhgroup=\`gcloud\ compute\ instances\ describe\ \$\(hostname\)\ --zone=\$\{zone\}\ --format=\"text\(labels\)\"\ \|\ grep\ ctmhostgroup\ \|\ cut\ -f\ 2\ -d\ \':\'\ \|\ sed\ s/\[^a-zA-Z0-9_-\]//g\`$'\n'envurl=\$\{CTM_ENV\}-url$'\n'ctmurl=\`gcloud\ secrets\ versions\ access\ latest\ --secret=\"\$\{envurl\}\"\`$'\n'envuser=\$\{CTM_ENV\}-user$'\n'ctmuser=\`gcloud\ secrets\ versions\ access\ latest\ --secret=\"\$\{envuser\}\"\`$'\n'envpswd=\$\{CTM_ENV\}-password$'\n'ctmpswd=\`gcloud\ secrets\ versions\ access\ latest\ --secret=\"\$\{envpswd\}\"\`$'\n'envausr=\$\{CTM_ENV\}-agentuser$'\n'agentuser=\`gcloud\ secrets\ versions\ access\ latest\ --secret=\"\$\{envausr\}\"\`$'\n'newhost=\"\$\(hostname\).ctmgcpagents.com\"$'\n'externalIp=\`gcloud\ compute\ instances\ describe\ \$\{iname\}\ --zone=\$\{zone\}\ --format=\'get\(networkInterfaces\[0\].accessConfigs\[0\].natIP\)\'\`$'\n'gcloud\ beta\ dns\ --project=sso-gcp-dba-ctm4-pub-cc10274\ record-sets\ transaction\ start\ --zone=\"ctmgcpagents-com\"\ \&\&\ gcloud\ beta\ dns\ --project=sso-gcp-dba-ctm4-pub-cc10274\ record-sets\ transaction\ add\ \$\{externalIp\}\ --name=\"\$\{newhost\}\"\ --ttl=\"300\"\ --type=\"A\"\ --zone=\"ctmgcpagents-com\"\ \&\&\ gcloud\ beta\ dns\ --project=sso-gcp-dba-ctm4-pub-cc10274\ record-sets\ transaction\ execute\ --zone=\"ctmgcpagents-com\"$'\n'sudo\ useradd\ -d\ /home/\$\{agentuser\}\ -m\ -s\ /bin/bash\ \$\{agentuser\}$'\n'sudo\ wget\ https://smprod.ctmdemo.com:8443/automation-api/ctm-cli.tgz\ --no-check-certificate$'\n'sudo\ npm\ install\ -g\ ctm-cli.tgz$'\n'sudo\ su\ -\ \$\{agentuser\}\ -c\ \"ctm\ env\ add\ ctm\ \$\{ctmurl\}\ \$\{ctmuser\}\ \$\{ctmpswd\}\"$'\n'sudo\ su\ -\ \$\{agentuser\}\ -c\ \"ctm\ provision\ install\ Agent_20.200.Linux\ smprod\ \$\{newhost\}\"$'\n'sudo\ su\ -\ \$\{agentuser\}\ -c\ \"ctm\ config\ server:hostgroup:agent::add\ smprod\ \$\{ctmhgroup\}\ \$\{newhost\}\" --maintenance-policy=MIGRATE --service-account=jogoldbectm@sso-gcp-dba-ctm4-pub-cc10274.iam.gserviceaccount.com --scopes=https://www.googleapis.com/auth/cloud-platform --tags=ctmagent,https-server --create-disk=auto-delete=yes,boot=yes,device-name=dynamic-agent-template,image=projects/debian-cloud/global/images/debian-10-buster-v20220118,mode=rw,size=40,type=projects/sso-gcp-dba-ctm4-pub-cc10274/zones/us-central1-a/diskTypes/pd-balanced --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --labels=ctmhostgroup=gcp-dyn-agents,ctmenvironment=ctmprod --reservation-affinity=any