Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
780b89e
4990 first past instance creation
Sep 10, 2018
884ae90
check for existence of security group before creating it #4990
pdurbin Sep 11, 2018
8db31c9
4990 working script first pass
Sep 11, 2018
9d04a2b
#4990 clean up create and delete all PoC
Sep 11, 2018
15e4454
Ansible pre-setup and larger ec2 instance #4990
Sep 14, 2018
510a390
Add 8080 security group ec2 #4990
Sep 14, 2018
a35c847
pass branch to ansible correct #4990
Sep 14, 2018
cde128a
Deploy branch ec2-ansible #4990
Sep 17, 2018
490d941
Add line about terminating instance #4990
Sep 17, 2018
6ce1215
Merge branch 'develop' into 4990-ec2-ansible-scripting
Sep 17, 2018
7bc6af8
Add fixme #4990
Sep 17, 2018
19e2915
comment cleanup #4990
Sep 17, 2018
6570aea
Ignore *.pem #4990
Sep 18, 2018
1d1794c
consistency: 2 spaces for indentation
pdurbin Sep 18, 2018
7052fdf
fix typo, remove cruft #4990
pdurbin Sep 18, 2018
0ae6dc1
document script and exit early if aws is not installed #4990
pdurbin Sep 19, 2018
6c9dec0
add script to list instances #4990
pdurbin Sep 19, 2018
5a91095
print multiple lines of output and put DNS last #4990
pdurbin Sep 19, 2018
42c7391
create key pair per instance #4990
pdurbin Sep 20, 2018
36df01f
open ports 80 and 443, print link #4990
pdurbin Sep 20, 2018
9dbf2be
put port 8080 in clickable link to avoid browser warnings #4990
pdurbin Sep 20, 2018
be995cc
replace sed command with --extra-vars arg #4990
pdurbin Sep 20, 2018
5c053a6
support non-IQSS repos #4990
pdurbin Sep 21, 2018
713a896
switch to non-nested extra vars #4990
pdurbin Sep 21, 2018
7317998
cleanup #4990
pdurbin Sep 21, 2018
a3f9078
Doc fix and remove unneeded install #4990
Sep 21, 2018
5ed3eb1
15 minutes and add back epel #4990
Sep 21, 2018
59c158a
Install epel-release before, add comment #4990
Sep 21, 2018
7021ae1
not the spin up script from Installation Guide #4990
pdurbin Sep 24, 2018
562c9c7
provide guidance on $PATH #4990
pdurbin Sep 28, 2018
746eec5
no ".txt" in aws config files, link to configure docs #4990
pdurbin Sep 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions scripts/installer/ec2-create-instance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash -x
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High level comments as of 19e2915:

Great work. We need some documentation, probably in the dev guide, maybe in a new section called "deployment" or "validation" or somewhere under the existing "testing" page. The audience is someone that we've instructed to spin up an arbitrary branch. This person may not be a Python user and may not have the aws binary installed already. They will need help with all the config under ~/.aws that's required. We need to explain that only region = us-east-1 is supported, etc.

One comment on the shebang line is that we could consider removing "-x" but it's probably useful for now, even if it's a little verbose.

#Initially Referred to this doc: https://docs.aws.amazon.com/cli/latest/userguide/tutorial-ec2-ubuntu.html

#TODO: allow arbitrary repo, not just IQSS. Will require changing it on the ansible side as well
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@donsizemore and I talked about this at http://irclog.iq.harvard.edu/dataverse/2018-09-18 and he just made a change at IQSS/dataverse-ansible@7ea1f7a which may mean we need to adjust the sed command below.

Copy link
Copy Markdown
Contributor Author

@matthew-a-dunlap matthew-a-dunlap Sep 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my current sed will work fine with the change actually. Worth ensuring tho

DEPLOY_FILE=dataverse_deploy_info.txt
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is DEPLOY_FILE used? Is it cruft?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cruft!


if [ "$1" = "" ]; then
echo "No branch name provided"
exit 1
else
BRANCH_NAME=$1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make the indentation consistent? https://google.github.io/styleguide/shell.xml#Indentation says, "Indent 2 spaces. No tabs."

if [[ $(git ls-remote --heads https://github.com/IQSS/dataverse.git $BRANCH_NAME | wc -l) -eq 0 ]]; then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks slightly brittle but maybe it's fine for now.

echo "Branch does not exist on the Dataverse github repo"
exit 1
fi
fi

#Create security group if it doesn't already exist
echo "*Checking for existing security group"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor but "*Blah" freaks me out because it's almost but not quite Markdown. Maybe "* Blah" with a space?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me

GROUP_CHECK=$(aws ec2 describe-security-groups --group-name devenv-sg)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being new to AWS, I'm not sure what a security group is or why it's helpful or necessary.

if [[ "$?" -ne 0 ]]; then
echo "*Creating security group"
aws ec2 create-security-group --group-name devenv-sg --description "security group for development environment"
aws ec2 authorize-security-group-ingress --group-name devenv-sg --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name devenv-sg --protocol tcp --port 8080 --cidr 0.0.0.0/0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't port 80 be open too? And 443?

echo "*End creating security group"
else
echo "*Security group already exists."
fi

echo "*Checking for existing key pair"
if ! [ -f devenv-key.pem ]; then
echo "*Creating key pair"
PRIVATE_KEY=$(aws ec2 create-key-pair --key-name devenv-key --query 'KeyMaterial' --output text)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a way to avoid this error: An error occurred (InvalidKeyPair.Duplicate) when calling the CreateKeyPair operation: The keypair 'devenv-key' already exists.

if [[ $PRIVATE_KEY = '-----BEGIN RSA PRIVATE KEY-----'* ]]; then
printf -- "$PRIVATE_KEY">devenv-key.pem
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

devenv-key.pem (or whatever we call it) should be added to .gitignore.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a commit, I already had it done locally.

chmod 400 devenv-key.pem
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the key made read only?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best practice so that other users can't access it. Some systems won't even read the key if its not set to 400

echo "*New key pair created"
fi
echo "*End creating key pair"
else
echo "*Key pair alraedy exists."
fi

#AMI ID for centos7 acquired by this (very slow) query Sept 10th 2018
#This does not need to be run every time, leaving it in here so it is remembered
#aws ec2 describe-images --owners 'aws-marketplace' --filters 'Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce' --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' --output 'text'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to explain that "aw0evgkw8e5c1q413zgy5pjce" is a product code for "CentOS 7 (x86_64) - with Updates HVM" as described at https://aws.amazon.com/marketplace/pp/B00O7WM7QW and https://wiki.centos.org/Cloud/AWS . HVM stands for "Hardware Virtual Machine".

I'm a little confused about why this one time step is necessary. Is it to retrieve the string "ami-9887c6e7" as the one we want to use? From what I understand "ami-9887c6e7" is only available in the "us-east-1" region, which is weird to me.


#The AMI ID only works for region us-east-1, for now just forcing that
#Using this image ID a 1-time requires subscription per root account, which was done through the UI
#Also, change the instance size as your own peril. Previous attempts of setting it smaller than medium have caused solr and maven to crash weirdly during install
echo "*Creating ec2 instance"
INSTACE_ID=$(aws ec2 run-instances --image-id ami-9887c6e7 --security-groups devenv-sg --count 1 --instance-type t2.medium --key-name devenv-key --query 'Instances[0].InstanceId' --block-device-mappings '[ { "DeviceName": "/dev/sda1", "Ebs": { "DeleteOnTermination": true } } ]' | tr -d \")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "INSTACE_ID"

We might want to make "t2.medium" a variable so we can more easily change it.

We might want some more error checking here.

echo "Instance ID: "$INSTACE_ID
echo "*End creating EC2 instance"

PUBLIC_DNS=$(aws ec2 describe-instances --instance-ids $INSTACE_ID --query "Reservations[*].Instances[*].[PublicDnsName]" --output text)
PUBLIC_IP=$(aws ec2 describe-instances --instance-ids $INSTACE_ID --query "Reservations[*].Instances[*].[PublicIpAddress]" --output text)

echo "Connecting to the instance. This may take a minute as it is being spun up"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "may take a minute" message should probably be the last thing you see before the long wait and may need to say "may take 10 minutes" or so. Also, not only is the instance being spun up. Dataverse is being installed.


echo "New EC2 instance created at $PUBLIC_DNS"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this note about the IP can be rolled into the main message above.


#ssh into instance now and run ansible stuff
#Note: an attempt was made to pass the branch name in the ansible-playbook call
# via -e "dataverse.branch=$BRANCH_NAME", but it gets overwritten due to the order
# of operations for where ansible looks for variables.
ssh -i devenv-key.pem -o 'StrictHostKeyChecking no' -o 'UserKnownHostsFile=/dev/null' -o 'ConnectTimeout=300' centos@${PUBLIC_DNS} << EOF
sudo yum -y install git nano ansible
git clone https://github.com/IQSS/dataverse-ansible.git dataverse
export ANSIBLE_ROLES_PATH=.
sed -i "s/branch:/branch: $BRANCH_NAME/" dataverse/defaults/main.yml
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Over at http://irclog.iq.harvard.edu/dataverse/2018-09-18 @donsizemore suggests trying --extra-vars "dataverse.git.branch=9999-get-my-margarita"

He linked to https://docs.ansible.com/ansible/2.6/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable which shows "extra vars (always win precedence)".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this and could not get the variable preference to work. It seemed like the blank variable in the config was taking preference. But I may just be a noob about this. command line is definitely the lowest preference.

ansible-playbook -i dataverse/inventory dataverse/dataverse.pb --connection=local
EOF

echo "New EC2 instance created at $PUBLIC_DNS (Public IP $PUBLIC_IP ). When you are done, please terminate your instance with: aws ec2 terminate-instances --instance-ids $INSTACE_ID"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of just the IP we should give the user a link to click. And does it have to be an IP? Does it get a DNS entry automatically?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DNS would take extra code and would have extra cost

11 changes: 11 additions & 0 deletions scripts/installer/ec2-destroy-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

#This script gets all the instances from ec2 and sends terminate to them
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "destroy" script is useful but a "list" or "read" script (I'm thinking CRUD) would be nice. "Give me a list of all the running instances and a command for each instance to destroy some or all of them"

#Its pretty basic and probably shouldn't be trusted at this point. Namely:
# - You can kill instances other people are using
# - It will try to kill instances that are already dead, which makes output hard to read
# - If it fails for some reason it's hard to tell the script didn't work right

INSTANCES=$(aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId]' --output text)

aws ec2 terminate-instances --instance-ids $INSTANCES