This repository was archived by the owner on Feb 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdatabox.sh
More file actions
executable file
·114 lines (99 loc) · 3.1 KB
/
Copy pathdatabox.sh
File metadata and controls
executable file
·114 lines (99 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-r|--region)
REGION="$2"
shift # past argument
;;
-u|--username)
USERNAME="$2"
shift # past argument
;;
-i|--instance)
INSTANCE="$2"
shift # past argument
;;
-v|--volume_size)
VOLUME_SIZE="$2"
shift # past argument
;;
-a|--ami_id)
AMI_ID="$2"
shift # past argument
;;
-s|--snapshot_id)
SNAPSHOT_ID="$2"
shift # past argument
;;
-p|--playbook)
PLAYBOOK="$2"
shift # past argument
;;
# unknown option
*)
;;
esac
shift # past argument or value
done
case "$1" in
"up") echo "Creating DataBox"
# If I don't specify the region use a default value
if [ -z "${REGION+x}" ]; then
export REGION="eu-west-2"
fi
# If I don't specify the username get it from logged in user
if [ -z "${USERNAME+x}" ]; then
export USERNAME=`whoami`
fi
# If I don't specify the instance type use a default value
if [ -z "${INSTANCE+x}" ]; then
export INSTANCE="t2.micro"
fi
# If I don't specify the volume size use a default value
if [ -z "${VOLUME_SIZE+x}" ]; then
export VOLUME_SIZE="40"
fi
# If I don't specify the AMI id set it to empty string
if [ -z "${AMI_ID+x}" ]; then
export AMI_ID=""
fi
# If I don't specify the AMI id set it to empty string
if [ -z "${SNAPSHOT_ID+x}" ]; then
export SNAPSHOT_ID=""
fi
# If I don't specify a custom playback set it to the default: databox.yml
if [ -z "${PLAYBOOK+x}" ]; then
export PLAYBOOK="playbooks/databox.yml"
fi
# Launch Terraform passing that user as parameter
terraform apply --var username=$USERNAME --var aws_region=$REGION --var instance_type=$INSTANCE --var volume_size=$VOLUME_SIZE --var ami_id=$AMI_ID --var snapshot_id=$SNAPSHOT_ID
# Get DataBox IP from state after the script completes
export DATABOX_IP=`terraform output ec2_ip`
# Run Ansible script using the above IP address
ansible-playbook -i "$DATABOX_IP," -K "$PLAYBOOK" -u ubuntu
;;
"down") echo "Destroying DataBox"
# If I don't specify the region use a default value
if [ -z "${REGION+x}" ]; then
export REGION="eu-west-2"
fi
# Get DataBox IP from state after the script completes
export DATABOX_IP=`terraform output ec2_ip`
# Run ansible teardown tasks
ansible-playbook -i "$DATABOX_IP," -K playbooks/teardown.yml -u ubuntu
terraform destroy --var aws_region=$REGION
;;
*) echo "DataBox - create and destroy AWS instances for Data Science"
echo "./databox.sh up - Create a DataBox"
echo " -- options -- "
echo " -r|--region - AWS region"
echo " -u|--username - Username used to name the AWS objects"
echo " -i|--instance - AWS instance type"
echo " -v|--volume_size - EBS volume size"
echo " -a|--ami_id - AMI id"
echo " -p|--playbook - Ansible playbook for post deployment tasks"
echo " -s|--snapshot_id - Id of the snapshot from which to create volume"
echo "./databox.sh down - Destroy the DataBox"
esac