pydsigner

Untitled

Sep 17th, 2021
815
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2. set -o errexit
  3. set -o pipefail
  4.  
  5. readonly ENVIRONMENT="${1:-}"
  6. readonly USE_IAP="${2:-false}"
  7. readonly PROJECT="domain-${ENVIRONMENT}"
  8.  
  9. append() {
  10.     touch ${SCRIPTDIR}/inventory.yml
  11.     printf "%s\n" "${1}" >> ${SCRIPTDIR}/inventory.yml
  12. }
  13.  
  14. generateHeader() {
  15.     append '---'
  16. }
  17. generateJumphostEntry() {
  18.     append 'group_jumphost:'
  19.     append '  hosts:'
  20.     append '    jumphost:'
  21.     append "      ansible_user: ${SSH_USER}"
  22.     append "      ansible_host: jumphost.${ENVIRONMENT}.domain.com"
  23.     append "      ansible_ssh_private_key_file: ${SSH_KEY_FILE}"
  24.     append '      ansible_python_interpreter: "/usr/bin/python3"'
  25.     append ''
  26. }
  27. generateHostEntry() {
  28.     local prior_group=${1:-}
  29.     local entry_group=${2:-}
  30.     local entry_host=${3:-}
  31.     local header_group=${4:-true}
  32.  
  33.   if [[ "$prior_group" != "$entry_group" ]]; then
  34.     append "$entry_group:"
  35.     append '  hosts:'
  36.   fi
  37.     append "    $entry_host:"
  38.     append '      ansible_python_interpreter: "/usr/bin/python3"'
  39.   if [ ${USE_IAP} == "false" ]; then
  40.     local address_host=$(gcloud compute instances list --filter="name=($entry_host)" --format="value(INTERNAL_IP)")
  41.     append "      ansible_user: ${SSH_USER}"
  42.     append "      ansible_host: $address_host"
  43.     append "      ansible_ssh_private_key_file: ${SSH_KEY_FILE}"
  44.     append "      ansible_ssh_common_args: ${SSH_JUMPHOST_SETTINGS}"
  45.   fi
  46.     append ''
  47. }
  48.  
  49. main() {
  50.     gcloud config set project ${PROJECT}
  51.  
  52.     if [ "${CI}" == "true" ]; then
  53.         declare SCRIPTDIR=$GITHUB_WORKSPACE
  54.         readonly SSH_USER="deploy"
  55.         readonly SSH_KEY_FILE="/home/runner/.ssh/ssh-priv-key"
  56.     else
  57.         declare SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )
  58.         read -p "Enter SSH_USER ($USER if empty): " SSH_USER; SSH_USER=${SSH_USER:-$USER}
  59.         read -ep "Enter absolute path of SSH_KEY_FILE: " SSH_KEY_FILE # has tab completion, much wow
  60.     fi
  61.  
  62.     declare SSH_JUMPHOST_CON_STR="${SSH_USER}@jumphost.${ENVIRONMENT}.domain.com"
  63.     declare SSH_JUMPHOST_SETTINGS='-o ProxyCommand="ssh -W %h:%p -q '${SSH_JUMPHOST_CON_STR}'"'
  64.  
  65.     rm -f ${SCRIPTDIR}/inventory.yml
  66.     generateHeader
  67.     [ ${USE_IAP} == "false" ] && generateJumphostEntry
  68.  
  69.     last_group=
  70.     declare header_group
  71.     declare -a HOSTS=$(gcloud compute instances list --format='value(name)')
  72.     for host in ${HOSTS}; do
  73.         [ $(echo $host | cut -f1 -d "-") == "gke" ] && continue # exclude legacy, testing and gke infra
  74.         [ $(echo $host | cut -f1 -d "-") == "staging" ] && continue
  75.         [ $(echo $host | cut -f1 -d "-") == "stage" ] && continue
  76.         [ $(echo $host | cut -f1 -d "-") == "test" ] && continue
  77.         [ $(echo $host | cut -f1 -d "-") == "jumphost" ] && continue
  78.         group=group_$(echo $host | cut -f1 -d "-")
  79.  
  80.         generateHostEntry "$last_group" "$group" "$host" "$header_group"
  81.         last_group="$group"
  82.     done
  83.  
  84.     cat ${SCRIPTDIR}/inventory.yml
  85. }
  86. time main
  87.  
RAW Paste Data