|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# For docs, see the "Deployment" page in the Dev Guide. |
| 4 | + |
| 5 | +SUGGESTED_REPO_URL='https://github.com/IQSS/dataverse.git' |
| 6 | +SUGGESTED_BRANCH='develop' |
| 7 | + |
| 8 | +usage() { |
| 9 | + echo "Usage: $0 -r $REPO_URL -b $SUGGESTED_BRANCH" 1>&2 |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +REPO_URL=$SUGGESTED_REPO_URL |
| 14 | + |
| 15 | +while getopts ":r:b:" o; do |
| 16 | + case "${o}" in |
| 17 | + r) |
| 18 | + REPO_URL=${OPTARG} |
| 19 | + ;; |
| 20 | + b) |
| 21 | + BRANCH_NAME=${OPTARG} |
| 22 | + ;; |
| 23 | + *) |
| 24 | + usage |
| 25 | + ;; |
| 26 | + esac |
| 27 | +done |
| 28 | + |
| 29 | +AWS_CLI_VERSION=$(aws --version) |
| 30 | +if [[ "$?" -ne 0 ]]; then |
| 31 | + echo 'The "aws" program could not be executed. Is it in your $PATH?' |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +if [ "$BRANCH_NAME" = "" ]; then |
| 36 | + echo "No branch name provided. You could try adding \"-b $SUGGESTED_BRANCH\" or other branches listed at $SUGGESTED_REPO_URL" |
| 37 | + usage |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +if [[ $(git ls-remote --heads $REPO_URL $BRANCH_NAME | wc -l) -eq 0 ]]; then |
| 42 | + echo "Branch \"$BRANCH_NAME\" does not exist at $REPO_URL" |
| 43 | + usage |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +SECURITY_GROUP='dataverse-sg' |
| 48 | +GROUP_CHECK=$(aws ec2 describe-security-groups --group-name $SECURITY_GROUP) |
| 49 | +if [[ "$?" -ne 0 ]]; then |
| 50 | + echo "Creating security group \"$SECURITY_GROUP\"." |
| 51 | + aws ec2 create-security-group --group-name $SECURITY_GROUP --description "security group for Dataverse" |
| 52 | + aws ec2 authorize-security-group-ingress --group-name $SECURITY_GROUP --protocol tcp --port 22 --cidr 0.0.0.0/0 |
| 53 | + aws ec2 authorize-security-group-ingress --group-name $SECURITY_GROUP --protocol tcp --port 80 --cidr 0.0.0.0/0 |
| 54 | + aws ec2 authorize-security-group-ingress --group-name $SECURITY_GROUP --protocol tcp --port 443 --cidr 0.0.0.0/0 |
| 55 | + aws ec2 authorize-security-group-ingress --group-name $SECURITY_GROUP --protocol tcp --port 8080 --cidr 0.0.0.0/0 |
| 56 | +fi |
| 57 | + |
| 58 | +RANDOM_STRING="$(uuidgen | cut -c-8)" |
| 59 | +KEY_NAME="key-$USER-$RANDOM_STRING" |
| 60 | + |
| 61 | +PRIVATE_KEY=$(aws ec2 create-key-pair --key-name $KEY_NAME --query 'KeyMaterial' --output text) |
| 62 | +if [[ $PRIVATE_KEY == '-----BEGIN RSA PRIVATE KEY-----'* ]]; then |
| 63 | + PEM_FILE="$KEY_NAME.pem" |
| 64 | + printf -- "$PRIVATE_KEY" >$PEM_FILE |
| 65 | + chmod 400 $PEM_FILE |
| 66 | + echo "Your newly created private key file is \"$PEM_FILE\". Keep it secret. Keep it safe." |
| 67 | +else |
| 68 | + echo "Could not create key pair. Exiting." |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +# The AMI ID may change in the future and the way to look it up is with the |
| 73 | +# following command, which takes a long time to run: |
| 74 | +# |
| 75 | +# aws ec2 describe-images --owners 'aws-marketplace' --filters 'Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce' --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' --output 'text' |
| 76 | +# |
| 77 | +# To use this AMI, we subscribed to it from the AWS GUI. |
| 78 | +# AMI IDs are specific to the region. |
| 79 | +AMI_ID='ami-9887c6e7' |
| 80 | +# Smaller than medium lead to Maven and Solr problems. |
| 81 | +SIZE='t2.medium' |
| 82 | +echo "Creating EC2 instance" |
| 83 | +# TODO: Add some error checking for "ec2 run-instances". |
| 84 | +INSTANCE_ID=$(aws ec2 run-instances --image-id $AMI_ID --security-groups $SECURITY_GROUP --count 1 --instance-type $SIZE --key-name $KEY_NAME --query 'Instances[0].InstanceId' --block-device-mappings '[ { "DeviceName": "/dev/sda1", "Ebs": { "DeleteOnTermination": true } } ]' | tr -d \") |
| 85 | +echo "Instance ID: "$INSTANCE_ID |
| 86 | +echo "End creating EC2 instance" |
| 87 | + |
| 88 | +PUBLIC_DNS=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query "Reservations[*].Instances[*].[PublicDnsName]" --output text) |
| 89 | +PUBLIC_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query "Reservations[*].Instances[*].[PublicIpAddress]" --output text) |
| 90 | + |
| 91 | +USER_AT_HOST="centos@${PUBLIC_DNS}" |
| 92 | +echo "New instance created with ID \"$INSTANCE_ID\". To ssh into it:" |
| 93 | +echo "ssh -i $PEM_FILE $USER_AT_HOST" |
| 94 | + |
| 95 | +echo "Please wait at least 15 minutes while the branch \"$BRANCH_NAME\" from $REPO_URL is being deployed." |
| 96 | + |
| 97 | +# epel-release is installed first to ensure the latest ansible is installed after |
| 98 | +# TODO: Add some error checking for this ssh command. |
| 99 | +ssh -T -i $PEM_FILE -o 'StrictHostKeyChecking no' -o 'UserKnownHostsFile=/dev/null' -o 'ConnectTimeout=300' $USER_AT_HOST <<EOF |
| 100 | +sudo yum -y install epel-release |
| 101 | +sudo yum -y install git nano ansible |
| 102 | +git clone https://github.com/IQSS/dataverse-ansible.git dataverse |
| 103 | +export ANSIBLE_ROLES_PATH=. |
| 104 | +ansible-playbook -i dataverse/inventory dataverse/dataverse.pb --connection=local --extra-vars "dataverse_branch=$BRANCH_NAME dataverse_repo=$REPO_URL" |
| 105 | +EOF |
| 106 | + |
| 107 | +# Port 8080 has been added because Ansible puts a redirect in place |
| 108 | +# from HTTP to HTTPS and the cert is invalid (self-signed), forcing |
| 109 | +# the user to click through browser warnings. |
| 110 | +CLICKABLE_LINK="http://${PUBLIC_DNS}:8080" |
| 111 | +echo "To ssh into the new instance:" |
| 112 | +echo "ssh -i $PEM_FILE $USER_AT_HOST" |
| 113 | +echo "Branch \"$BRANCH_NAME\" from $REPO_URL has been deployed to $CLICKABLE_LINK" |
| 114 | +echo "When you are done, please terminate your instance with:" |
| 115 | +echo "aws ec2 terminate-instances --instance-ids $INSTANCE_ID" |
0 commit comments