Created
October 17, 2018 12:46
-
-
Save andyrepton/e11761163ae9268e521e48d5960b7e91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Admin script for workshops | |
export AWS_ACCESS_KEY_ID=$(grep -A 2 my_aws_profile ~/.aws/credentials | grep aws_access_key_id | awk '{print $3}') | |
export AWS_SECRET_ACCESS_KEY=$(grep -A 2 my_aws_profile ~/.aws/credentials | grep aws_secret_access_key | awk '{print $3}') | |
export KOPS_STATE_STORE="s3://your-bucket-here" | |
deploy() { | |
echo $1 | |
cluster_name=$1 | |
export NAME=${cluster_name}.your-domain.com | |
USERNUM=$(echo $cluster_name | sed 's/user//') | |
if [ "$USERNUM" -ge 1 -a "$USERNUM" -le 5 ]; then | |
AZ=eu-west-1a | |
elif [ "$USERNUM" -ge 6 -a "$USERNUM" -le 10 ]; then | |
AZ=eu-west-2a | |
elif [ "$USERNUM" -ge 11 -a "$USERNUM" -le 15 ]; then | |
AZ=eu-west-3a | |
elif [ "$USERNUM" -ge 16 -a "$USERNUM" -le 20 ]; then | |
AZ=eu-central-1a | |
elif [ "$USERNUM" -ge 21 -a "$USERNUM" -le 25 ]; then | |
AZ=ap-south-1a | |
elif [ "$USERNUM" -ge 26 -a "$USERNUM" -le 30 ]; then | |
AZ=us-east-1a | |
elif [ "$USERNUM" -ge 31 -a "$USERNUM" -le 35 ]; then | |
AZ=us-east-2a | |
elif [ "$USERNUM" -ge 36 -a "$USERNUM" -le 40 ]; then | |
AZ=us-west-1a | |
elif [ "$USERNUM" -ge 41 -a "$USERNUM" -le 45 ]; then | |
AZ=us-west-2a | |
elif [ "$USERNUM" -ge 46 -a "$USERNUM" -le 50 ]; then | |
AZ=eu-west-1b | |
elif [ "$USERNUM" -ge 51 -a "$USERNUM" -le 55 ]; then | |
AZ=eu-central-1b | |
elif [ "$USERNUM" -ge 56 -a "$USERNUM" -le 60 ]; then | |
AZ=eu-west-1b | |
elif [ "$USERNUM" -ge 61 -a "$USERNUM" -le 65 ]; then | |
AZ=eu-central-1b | |
elif [ "$USERNUM" -ge 66 -a "$USERNUM" -le 70 ]; then | |
AZ=eu-central-1c | |
elif [ "$USERNUM" -ge 71 -a "$USERNUM" -le 75 ]; then | |
AZ=eu-west-1b | |
elif [ "$USERNUM" -ge 76 -a "$USERNUM" -le 80 ]; then | |
AZ=eu-west-1c | |
elif [ "$USERNUM" -ge 81 -a "$USERNUM" -le 85 ]; then | |
AZ=eu-central-1a | |
elif [ "$USERNUM" -ge 86 -a "$USERNUM" -le 90 ]; then | |
AZ=eu-west-1a | |
elif [ "$USERNUM" -ge 91 -a "$USERNUM" -le 95 ]; then | |
AZ=eu-central-1c | |
elif [ "$USERNUM" -ge 96 -a "$USERNUM" -le 100 ]; then | |
AZ=eu-west-1a | |
elif [ "$USERNUM" -ge 101 -a "$USERNUM" -le 105 ]; then | |
AZ=eu-central-1a | |
elif [ "$USERNUM" -ge 106 -a "$USERNUM" -le 110 ]; then | |
AZ=eu-west-1b | |
elif [ "$USERNUM" -ge 111 -a "$USERNUM" -le 115 ]; then | |
AZ=eu-central-1b | |
fi | |
kops create secret --name ${cluster_name}.your-domain.com sshpublickey admin -i ~/.ssh/id_rsa.pub | |
kops create cluster --zones ${AZ} --node-size t2.medium --master-size t2.medium $NAME | |
kops update cluster $NAME --yes | |
} | |
export_kubeconfig() { | |
echo $1 | |
export KUBECONFIG=${1}.kubeconfig | |
swap ${1} | |
aws s3 cp ${1}.kubeconfig s3://your-bucket-here-public/ --acl public-read | |
} | |
create_subdomain() { | |
echo $1 | |
cluster_name=$1 | |
echo 'Checking SUBDOMAIN ID' | |
SUBDOMAIN_ID=$(aws route53 list-hosted-zones | jq -r '.HostedZones[] | select(.Name=="'${cluster_name}'.your-domain.com.") | .Id') | |
if [ -z "$SUBDOMAIN_ID" ]; then | |
echo 'SUBDOMAIN not found, creating new hosted zone' | |
ID=$(uuidgen) && aws route53 create-hosted-zone --name $cluster_name.your-domain.com --caller-reference $ID | jq .DelegationSet.NameServers | |
SUBDOMAIN_ID=$(aws route53 list-hosted-zones | jq -r '.HostedZones[] | select(.Name=="'${cluster_name}'.your-domain.com.") | .Id') | |
fi | |
TOP_LEVEL_HOSTED_ZONE=$(aws route53 list-hosted-zones | jq -r '.HostedZones[] | select(.Name=="your-domain.com.") | .Id') | |
echo 'Getting nameservers' | |
NAMESERVER1=$(aws route53 get-hosted-zone --id ${SUBDOMAIN_ID} | jq -r '.DelegationSet.NameServers[0]') | |
NAMESERVER2=$(aws route53 get-hosted-zone --id ${SUBDOMAIN_ID} | jq -r '.DelegationSet.NameServers[1]') | |
NAMESERVER3=$(aws route53 get-hosted-zone --id ${SUBDOMAIN_ID} | jq -r '.DelegationSet.NameServers[2]') | |
NAMESERVER4=$(aws route53 get-hosted-zone --id ${SUBDOMAIN_ID} | jq -r '.DelegationSet.NameServers[3]') | |
read -r -d '' JSON_BLOB << EOM | |
{ | |
"HostedZoneId": "HOSTED_ZONE_ID", | |
"ChangeBatch": { | |
"Comment": "Create a subdomain NS record in the parent domain", | |
"Changes": [ | |
{ | |
"Action": "UPSERT", | |
"ResourceRecordSet": { | |
"Name": "CLUSTER_NAME.your-domain.com", | |
"Type": "NS", | |
"TTL": 300, | |
"ResourceRecords": [ | |
{ | |
"Value": "NAMESERVER1" | |
}, | |
{ | |
"Value": "NAMESERVER2" | |
}, | |
{ | |
"Value": "NAMESERVER3" | |
}, | |
{ | |
"Value": "NAMESERVER4" | |
} | |
] | |
} | |
} | |
] | |
} | |
} | |
EOM | |
CHANGE_RESOURCE_RECORD=$(echo $JSON_BLOB | sed "s/NAMESERVER1/${NAMESERVER1}/" | sed "s/NAMESERVER2/${NAMESERVER2}/" | sed "s/NAMESERVER3/${NAMESERVER3}/" | sed "s/NAMESERVER4/${NAMESERVER4}/" | sed "s/CLUSTER_NAME/${cluster_name}/" | sed "s~HOSTED_ZONE_ID~${TOP_LEVEL_HOSTED_ZONE}~") | |
echo 'Creating change resource' | |
aws route53 change-resource-record-sets --hosted-zone-id ${TOP_LEVEL_HOSTED_ZONE} --cli-input-json "${CHANGE_RESOURCE_RECORD}" | |
} | |
for cmd in "$@"; do | |
shift | |
case ${cmd} in | |
deploy) | |
deploy "$@" | |
;; | |
create_subdomain) | |
create_subdomain "$@" | |
;; | |
export) | |
export_kubeconfig "$@" | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment