github.com/dustinrc/deis@v1.10.1-0.20150917223407-0894a5fb979e/contrib/aws/create (about)

     1  #!/usr/bin/env bash
     2  
     3  set -eo pipefail -o nounset
     4  
     5  export DEIS_NUM_INSTANCES=3
     6  
     7  function aws-setup-keypair {
     8    local deis_auth_key="${1}"
     9  
    10    rigger-log "Importing ${deis_auth_key} keypair to EC2"
    11  
    12    # TODO: don't hardcode --key-names
    13    if ! aws ec2 describe-key-pairs --key-names "deis" >/dev/null ; then
    14      rigger-log "Importing ${deis_auth_key} keypair to EC2"
    15      aws ec2 import-key-pair --key-name deis \
    16          --public-key-material file://${deis_auth_key}.pub \
    17          --output text
    18    fi
    19  }
    20  
    21  function aws-provision-cluster {
    22    local stack_name="${1}"
    23  
    24    # customize cloudformation.json to use m3.medium instances
    25    cat > $DEIS_ROOT/contrib/aws/cloudformation.json <<EOF
    26    [
    27        {
    28            "ParameterKey":     "KeyPair",
    29            "ParameterValue":   "deis"
    30        }
    31    ]
    32  EOF
    33  
    34    rigger-log "Provisioning ${DEIS_NUM_INSTANCES}-node CoreOS"
    35  
    36    "${DEIS_ROOT}/contrib/aws/provision-aws-cluster.sh" "${stack_name}"
    37  
    38    # discard changes to cloudformation.json
    39    git checkout -- "${DEIS_ROOT}/contrib/aws/cloudformation.json"
    40  }
    41  
    42  function aws-get-elb-dns-name {
    43    local stack_name="${1}"
    44  
    45    aws cloudformation describe-stacks \
    46        --stack-name "${stack_name}" \
    47        --max-items 1 \
    48        --query 'Stacks[].[ Outputs[0].[ OutputValue ] ]' \
    49        --output=text
    50  }
    51  
    52  function aws-get-elb-name {
    53    local elb_dns_name="${1}"
    54  
    55    aws elb describe-load-balancers \
    56      --query 'LoadBalancerDescriptions[].[ DNSName,LoadBalancerName ]' \
    57      --output=text | grep -F ${elb_dns_name} | head -n1 | cut -f2
    58  }
    59  
    60  function aws-setup-route53 {
    61    local stack_name="${1}"
    62    local domain="${2}"
    63  
    64    rigger-log "Setting up Route53 zone..."
    65  
    66    python "${DEIS_ROOT}/contrib/aws/route53-wildcard.py" create "${domain}" "$(aws-get-elb-dns-name ${stack_name})"
    67  }
    68  
    69  function aws-get-instance-id {
    70    local stack_name="${1}"
    71  
    72    local instance_ids=$(aws ec2 describe-instances \
    73        --filters Name=tag:aws:cloudformation:stack-name,Values=${stack_name} Name=instance-state-name,Values=running \
    74        --query 'Reservations[].Instances[].[ InstanceId ]' \
    75        --output text)
    76  
    77    cut -d " " -f1 <<< ${instance_ids}
    78  }
    79  
    80  function aws-deisctl-tunnel {
    81    local stack_name="${1}"
    82  
    83    aws ec2 describe-instances \
    84        --instance-ids=$(aws-get-instance-id ${stack_name}) \
    85        --filters Name=tag:aws:cloudformation:stack-name,Values=${stack_name} Name=instance-state-name,Values=running \
    86        --query 'Reservations[].Instances[].[ PublicDnsName ]' \
    87        --output text
    88  }
    89  
    90  (
    91    cd ${DEIS_ROOT}
    92    rigger-log "Creating CloudFormation stack ${STACK_NAME}"
    93    aws-setup-keypair "${DEIS_TEST_AUTH_KEY}"
    94    aws-provision-cluster "${STACK_NAME}"
    95  )
    96  
    97  export ELB_DNS_NAME=$(aws-get-elb-dns-name "${STACK_NAME}")
    98  export ELB_NAME=$(aws-get-elb-name "${ELB_DNS_NAME}")
    99  export DEIS_TEST_DOMAIN="${STACK_TAG}.${DEIS_TEST_DOMAIN}"
   100  
   101  (
   102    cd ${DEIS_ROOT}
   103    aws-setup-route53 "${STACK_NAME}" "${DEIS_TEST_DOMAIN}"
   104    aws-get-instance-id "${STACK_NAME}"
   105  )
   106  
   107  export DEISCTL_TUNNEL="$(aws-deisctl-tunnel ${STACK_NAME})"
   108  rigger-log "DEISCTL_TUNNEL=${DEISCTL_TUNNEL}"
   109  rigger-save-vars DEISCTL_TUNNEL \
   110                   ELB_DNS_NAME \
   111                   ELB_NAME \
   112                   STACK_TAG \
   113                   STACK_NAME \
   114                   DEIS_TEST_DOMAIN