github.com/rochacon/deis@v1.0.2-0.20150903015341-6839b592a1ff/tests/bin/accept/options/provider/aws.sh (about)

     1  # add random characters after STACK_TAG to avoid collisions
     2  STACK_TAG="${STACK_TAG:-test}-${DEIS_TEST_ID}"
     3  
     4  export DEIS_NUM_INSTANCES=${DEIS_NUM_INSTANCES:-3}
     5  export DEIS_TEST_DOMAIN="${STACK_TAG}.${DEIS_TEST_DOMAIN}"
     6  export STACK_NAME="${STACK_NAME:-deis-${STACK_TAG}}"
     7  
     8  function _setup-provider-dependencies {
     9    # install python requirements for this script
    10    pip install --disable-pip-version-check awscli boto docopt
    11  }
    12  
    13  function aws-setup-keypair {
    14    local deis_auth_key="${1}"
    15  
    16    rerun_log "Importing ${deis_auth_key} keypair to EC2"
    17  
    18    # TODO: don't hardcode --key-names
    19    if ! aws ec2 describe-key-pairs --key-names "deis" >/dev/null ; then
    20      rerun_log "Importing ${deis_auth_key} keypair to EC2"
    21      aws ec2 import-key-pair --key-name deis \
    22          --public-key-material file://~/.ssh/${deis_auth_key}.pub \
    23          --output text
    24    fi
    25  }
    26  
    27  function aws-provision-cluster {
    28    local stack_name="${1}"
    29  
    30    # customize cloudformation.json to use m3.medium instances
    31    cat > $DEIS_ROOT/contrib/aws/cloudformation.json <<EOF
    32    [
    33        {
    34            "ParameterKey":     "KeyPair",
    35            "ParameterValue":   "deis"
    36        },
    37        {
    38            "ParameterKey":     "InstanceType",
    39            "ParameterValue":   "m3.medium"
    40        }
    41    ]
    42  EOF
    43  
    44    rerun_log "Provisioning ${DEIS_NUM_INSTANCES}-node CoreOS"
    45  
    46    "${DEIS_ROOT}/contrib/aws/provision-aws-cluster.sh" "${stack_name}"
    47  
    48    # discard changes to cloudformation.json
    49    git checkout -- "${DEIS_ROOT}/contrib/aws/cloudformation.json"
    50  }
    51  
    52  function aws-get-elb-dns-name {
    53    local stack_name="${1}"
    54  
    55    aws cloudformation describe-stacks \
    56        --stack-name "${stack_name}" \
    57        --max-items 1 \
    58        --query 'Stacks[].[ Outputs[0].[ OutputValue ] ]' \
    59        --output=text
    60  }
    61  
    62  function aws-get-elb-name {
    63    local elb_dns_name="${1}"
    64  
    65    aws elb describe-load-balancers \
    66      --query 'LoadBalancerDescriptions[].[ DNSName,LoadBalancerName ]' \
    67      --output=text | grep -F ${elb_dns_name} | head -n1 | cut -f2
    68  }
    69  
    70  function aws-setup-route53 {
    71    local stack_name="${1}"
    72    local domain="${2}"
    73  
    74    rerun_log "Setting up Route53 zone..."
    75  
    76    python "${DEIS_ROOT}/contrib/aws/route53-wildcard.py" create "${domain}" "$(aws-get-elb-dns-name ${stack_name})"
    77  }
    78  
    79  function aws-destroy-route53 {
    80    local stack_name="${1}"
    81    local domain="${2}"
    82  
    83    local elb_dns_name="$(aws-get-elb-dns-name ${stack_name})"
    84  
    85    if [ -n "${elb_dns_name}" ]; then
    86      rerun_log "Removing Route53 zone..."
    87      python "${DEIS_ROOT}/contrib/aws/route53-wildcard.py" delete "${domain}" "${elb_dns_name}"
    88    fi
    89  }
    90  
    91  function aws-get-instance-id {
    92    local stack_name="${1}"
    93  
    94    local instance_ids=$(aws ec2 describe-instances \
    95        --filters Name=tag:aws:cloudformation:stack-name,Values=${stack_name} Name=instance-state-name,Values=running \
    96        --query 'Reservations[].Instances[].[ InstanceId ]' \
    97        --output text)
    98  
    99    cut -d " " -f1 <<< ${instance_ids}
   100  }
   101  
   102  function aws-deisctl-tunnel {
   103    local stack_name="${1}"
   104  
   105    aws ec2 describe-instances \
   106        --instance-ids=$(aws-get-instance-id ${stack_name}) \
   107        --filters Name=tag:aws:cloudformation:stack-name,Values=${stack_name} Name=instance-state-name,Values=running \
   108        --query 'Reservations[].Instances[].[ PublicDnsName ]' \
   109        --output text
   110  }
   111  
   112  function check-elb-service {
   113    local elb_name="${1}"
   114  
   115    ATTEMPTS=45
   116    SLEEPTIME=10
   117    COUNTER=1
   118    IN_SERVICE=0
   119    until [ $IN_SERVICE -ge 1 ]; do
   120        if [ $COUNTER -gt $ATTEMPTS ]; then exit 1; fi  # timeout after 7 1/2 minutes
   121        if [ $COUNTER -ne 1 ]; then sleep $SLEEPTIME; fi
   122        rerun_log "Waiting for ELB (${elb_name}) to see an instance in InService..."
   123        IN_SERVICE=$(aws elb describe-instance-health \
   124            --load-balancer-name "${elb_name}" \
   125            --query 'InstanceStates[].State' \
   126            | grep InService | wc -l)
   127    done
   128  }
   129  
   130  function _create {
   131    rerun_log "Creating CloudFormation stack ${STACK_NAME}"
   132  
   133    aws-setup-keypair "${DEIS_TEST_AUTH_KEY}"
   134  
   135    aws-provision-cluster "${STACK_NAME}"
   136  
   137    export ELB_DNS_NAME=$(aws-get-elb-dns-name "${STACK_NAME}")
   138    export ELB_NAME=$(aws-get-elb-name "${ELB_DNS_NAME}")
   139  
   140    aws-setup-route53 "${STACK_NAME}" "${DEIS_TEST_DOMAIN}"
   141  
   142    aws-get-instance-id "${STACK_NAME}"
   143  
   144    export DEISCTL_TUNNEL="$(aws-deisctl-tunnel ${STACK_NAME})"
   145  
   146    rerun_log "DEISCTL_TUNNEL=${DEISCTL_TUNNEL}"
   147  }
   148  
   149  function _destroy {
   150    rerun_log "Attempting to destroy ${STACK_NAME}..."
   151  
   152    aws cloudformation delete-stack --stack-name "${STACK_NAME}"
   153  
   154    aws-destroy-route53 "${STACK_NAME}" "${DEIS_TEST_DOMAIN}"
   155  }
   156  
   157  function _check-cluster {
   158    check-elb-service "${ELB_NAME}"
   159  }