#!/bin/bash set -euo pipefail display_help() { local app app=$(basename "$0") echo "PXConfig Deployer" echo " " echo "$app [options]" echo " " echo "Required options:" echo "-s, --service-id=SERVICE_ID specify a service to deploy the Config store" echo "-a, --appid=APPID specify an appid" echo "-c, --cookie_secret=SECRET specify a cookie_secret" echo "-t, --auth_token=TOKEN specify an auth_token" } if ! OPTS=$(getopt -o hs:v:a:c:t:b: -l help,service-id:,appid:,cookie_secret:,auth_token: -- "$@") then exit 1 fi eval set -- "$OPTS" opt_help="no" SERVICE_ID="" APPID="" COOKIE_SECRET="" AUTH_TOKEN="" CF="PXConfig" while [ $# -gt 0 ] do case $1 in -h|--help) opt_help="yes" ;; -s|--service-id) shift; SERVICE_ID="$1" ;; -a|--appid) shift; APPID="$1" ;; -c|--cookie_secret) shift; COOKIE_SECRET="$1" ;; -t|--auth_token) shift; AUTH_TOKEN="$1" ;; (--) shift; break;; (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;; (*) break;; esac done if ! hash jq 2>/dev/null; then echo "'jq' command is not found. Please install 'jq' and re-run this script" exit 1 fi if [[ -z $SERVICE_ID || -z $APPID || -z $COOKIE_SECRET || -z $AUTH_TOKEN ]]; then echo "Error: One or more required variables are not provided" echo display_help exit 1 fi store_id="$(fastly config-store list -j | jq -r '.[] | select(.name=="'$CF'").id')" if [[ -n $store_id ]]; then echo "Error: Config store ${CF} already exists! Stopping the script." exit 1 fi echo "Creating Config store ${CF}" fastly config-store create --name=${CF} # minimal PX configuration declare -A ditems ditems[px_app_id]="$APPID" ditems[px_cookie_secret]="$COOKIE_SECRET" ditems[px_auth_token]="$AUTH_TOKEN" ditems[px_debug]=false ditems[px_module_enabled]=true ditems[px_module_mode]="monitor" echo "Adding Config store Items..." CF_ID="$(fastly config-store list -j | jq -r '.[] | select(.name=="'$CF'").id')" if [[ -z $CF_ID ]]; then echo "Error: Failed to get ${CF} Config store ID." exit 1 fi echo "Config store ID: ${CF_ID}" for key in "${!ditems[@]}"; do fastly config-store-entry create --store-id="${CF_ID}" --key="$key" --value="${ditems[$key]}" done echo " " echo "All done! Let's fight some bots! ༼ つ ▀_▀ ༽つ"