blob: f9963b14a0eb072f5443a1db5fd3acfebb99cd40 [file] [log] [blame]
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
version: "3"
vars:
SSH: "ssh -oStrictHostKeyChecking=no"
tasks:
prereq:
silent: true
cmds:
- test -n "$AWS_SECRET_ACCESS_KEY" || die "Please configure AWS with 'ops config aws'"
- aws --version >/dev/null || die 'You need to install and configure aws cli tool before running this command.'
- >
aws ec2 describe-key-pairs --key-names $AWS_SSHKEY --query 'KeyPairs[*].{KeyName: KeyName}' >/dev/null
|| die 'Please generate or import the configured key before continuing.'
- "{{.SSH}} -V 2>/dev/null || die 'you need an ssh client in your PATH'"
- config AWS_PREREQ_OK="true"
status:
- config AWS_PREREQ_OK
vm-list:
silent: true
cmds:
- task: prereq
- echo "*** Created VM:"
- >
if ! aws ec2 describe-instances
--filters "Name=tag:Ops,Values=true"
--query 'Reservations[].Instances[].[Tags[?Key==`OpsName`].Value | [0], Tags[?Key==`OpsType`].Value | [0], PublicIpAddress, State.Name]'
--output text
| awk '{printf "%-15s %-10s %-15s %s\n", $1, $2, $3, $4}' ;
then echo "no vm found" ;
fi
vm-create:
silent: true
cmds:
- task: prereq
- |
export NAME={{._name_}}
envsubst -i ubuntu.cf -o _vm-create.cf
- echo "*** Creating the VM."
- >
aws cloudformation create-stack
--stack-name ops-{{._name_}}
--template-body file://_vm-create.cf
|| true
- echo "*** Waiting until the VM is started..."
- >
aws cloudformation wait
stack-create-complete --stack-name ops-{{._name_}}
- task: vm-getip
- >
echo "*** Waiting until the VM is ready..." ;
retry -t 1000 -m 1800 {{.SSH}}
"$AWS_VM_IMAGE_USER@$(cat _vm-ip)"
sudo cloud-init status --wait
vm-getip:
silent: true
cmds:
- |
aws ec2 describe-instances --output json \
--filters Name=tag:Ops,Values=true Name=tag:OpsName,Values={{._name_}} Name=instance-state-name,Values=running \
| jq -r '.Reservations[].Instances[].PublicIpAddress' >_vm-ip
test -n "$(cat _vm-ip)" || die "Error: VM {{._name_}} not found"
- cat _vm-ip
vm-kubeconfig:
silent: false
cmds:
- task: prereq
- echo "*** Getting kubeconfig (if available) for:"
- task: vm-getip
- >
{{.SSH}} "$AWS_VM_IMAGE_USER@$(cat _vm-ip)"
sudo cat /etc/kubeconfig >_kubeconfig
- cp _kubeconfig "$OPS_TMP/kubeconfig" "$OPS_TMP/aws-{{._name_}}.kubeconfig"
vm-delete:
cmds:
- aws cloudformation delete-stack --stack-name ops-{{._name_}}
- aws cloudformation wait stack-delete-complete --stack-name ops-{{._name_}}
- if test -e "$OPS_TMP/aws-{{._name_}}" ; then rm "$OPS_TMP/aws-{{._name_}}" ; fi
zone-create:
silent: true
cmds:
- >
aws route53 create-hosted-zone --name "{{._zone_}}"
--caller-reference "{{._zone_ |replace "." "-"}}" --output table
|| true
- echo "Please delegate those name Servers for {{._zone_}}"
- >
aws route53 get-hosted-zone
--id "$(aws route53 list-hosted-zones --query "HostedZones[?Name=='{{._zone_}}.'].Id" --output text)"
--query 'DelegationSet.NameServers' --output text
zone-delete:
silent: true
cmds:
- |
ID=$(aws route53 list-hosted-zones --query "HostedZones[?Name=='{{._zone_}}.'].Id" --output text)
aws route53 delete-hosted-zone --id "$ID" --output table
zone-getid:
silent: true
cmds:
- |
if test -n "{{._zone_}}"
then
aws route53 list-hosted-zones --output text \
--query "HostedZones[?Name=='{{._zone_}}.'].Id" >_zone-id
test -n "$(cat _zone-id)" || die "Error: zone {{._zone_}} not found"
fi
zone-list:
silent: true
cmds:
- |
if test -z "{{._zone_}}"
then aws route53 list-hosted-zones | jq -r '.HostedZones[] | [.Id, .Name] | @tsv'
else
ID=$(aws route53 list-hosted-zones --query "HostedZones[?Name=='{{._zone_}}.'].Id" --output text)
aws route53 list-resource-record-sets --hosted-zone-id "$ID" --output table
fi
zone-update:
silent: true
cmds:
- task: zone-getid
- cmd: |
export DOMAIN={{._zone_}}
if test -n "{{.__vm}}"
then export REC=A
export VAL=$($OPS cloud aws vm-getip "{{.__vm}}")
elif test -n "{{.__ip}}"
then export REC=A
export VAL="{{.__ip}}"
elif test -n "{{.__cname}}"
then export REC=CNAME
export VAL="{{.__cname}}"
fi
if test -n "{{.__host}}"
then export HOST="{{.__host}}"
elif {{.__wildcard}}
then export HOST="*"
fi
#echo :$DOMAIN
#echo :$HOST
#echo :$REC
#echo :$VAL
echo "Updating: $DOMAIN.$HOST $REC $VAL"
envsubst -i upsert.json -o _upsert.json
#- cat _upsert.json
- >
aws route53 change-resource-record-sets
--hosted-zone-id=$(cat _zone-id)
--change-batch file://_upsert.json