-
Notifications
You must be signed in to change notification settings - Fork 38
/
env_prep.sh
executable file
·152 lines (127 loc) · 4.31 KB
/
env_prep.sh
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
set -e
# Set variables
logfile=prep.log
base_home_dir=aws-batch-example
lambda_code_path=lambda
lambda_func_name=trigger
lambda_pkg_name=${lambda_func_name}.zip
temp_path=${lambda_code_path}/.temp
docker_path=docker
start_repo_name=batchpocstart
start_docker_path=${docker_path}/${start_repo_name}
proc_repo_name=batchpocprocess
proc_docker_path=${docker_path}/${proc_repo_name}
# Define Usage
function usage()
{
echo "Usage: $0 {args}"
echo "Where valid args are: "
echo " -b <bucket> (REQUIRED) -- bucket name to sync to"
echo " -p <profile> -- Profile to use for AWS commands, defaults to 'default'"
echo " -r <release> -- Release variable used for bucket path, defaults to 'develop'"
exit 1
}
# Builds and send the trigger lambda to s3
function build_lambda()
{
# Make a temp dir to build in
mkdir -p ${temp_path}
# Copy code to the temp path
cp ${lambda_code_path}/${lambda_func_name}/* ${temp_path}
# Install requirements to temp path
cd ${temp_path}
pip install -r requirements.txt -t .
# Make a build directory and zip up the build package
zip -r ../${lambda_pkg_name} ./*
# Move back home
numbdirs=$(awk -F"/" '{print NF-1}' <<< "./${temp_path}")
for i in $(seq 1 ${numbdirs}); do cd ../;done
# Remove the temparary build dir
rm -r ${temp_path}
local_pkg_path=${lambda_code_path}/${lambda_pkg_name}
aws s3 cp ${local_pkg_path} s3://${BUCKET}/${RELEASE}/${lambda_code_path}/${lambda_pkg_name} --profile ${PROFILE} --exclude *.git/* --exclude *.swp
}
# Create ECR repos and get login token
function setup_ecr()
{
# Check to see if image repository exists
# If the start repo does not exist create it
repos=$(aws ecr describe-repositories --profile ${PROFILE} --query "repositories[].repositoryName" --output text)
[[ $repos =~ (^|[[:space:]])${1}($|[[:space:]]) ]] && echo 'Repo Exists' || aws ecr create-repository --profile ${PROFILE} --repository-name ${1}
#for repo in $repos
#do
#if [ $repo = ${1} ]
#then
#exit 0
#fi
#aws ecr create-repository --profile ${PROFILE} --repository-name ${1}
#done
}
# Builds and tags docker containers
# Takes 3 parameters, directory, image tag, and repo domain
function build_docker()
{
# Move into the docker dir
cd ${1}
# Build the image
docker build -t ${2} .
# Tag
docker tag ${2}:latest ${3}/${2}:latest
# Push
docker push ${3}/${2}:latest
# Move back home
numbdirs=$(awk -F"/" '{print NF-1}' <<< "./${1}")
for i in $(seq 1 ${numbdirs}); do cd ../;done
}
# Parse args
if [[ "$#" -lt 2 ]] ; then
echo 'parse error'
usage
fi
PROFILE=default
RELEASE=develop
while getopts "p:r:b:" opt; do
case $opt in
p)
PROFILE=$OPTARG
;;
b)
BUCKET=$OPTARG
;;
r)
RELEASE=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG"
usage
;;
esac
done
# Makes sure you're in the right directory
CWD=$(echo $PWD | rev | cut -d'/' -f1 | rev)
if [ $CWD != ${base_home_dir} ]
then
echo "These tools are expecting to be ran from the base of the aws-batch-example repo. If you edited the name of the directory edit the env_prep.sh script."
exit 1
fi
echo -e "Starting prep process.\nIf this script does not report success check the log.\nLogs can be found at ${logfile}"
{
# Setup AWS vars
REGION=$(aws configure list --profile ${PROFILE} | grep region | awk '{print $2}')
ACCOUNT_ID=$(aws ec2 describe-security-groups --query 'SecurityGroups[0].OwnerId' --output text --profile ${PROFILE})
# Build your lambda
build_lambda
# Setup the ECR repositories
setup_ecr ${start_repo_name}
setup_ecr ${proc_repo_name}
# Get docker credentials for the repositories
# outdated command from v1
# aws ecr get-login --no-include-email --profile ${PROFILE} | bash
# This command is supported using the latest version of AWS CLI version 2 or in v1.17.10 or later of AWS CLI version 1
aws ecr get-login-password --region ${REGION} --profile ${PROFILE} | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
# Build and push docker images
build_docker ${start_docker_path} ${start_repo_name} ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
build_docker ${proc_docker_path} ${proc_repo_name} ${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
} 1> $logfile
echo -e "Successfully finished prep.\n Lambda built\n ECR setup\n Docker containers built and pushed"