-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolkit.sh
executable file
·183 lines (169 loc) · 6.24 KB
/
toolkit.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
# Initial Setup: Fill out config.json and add required python packages to requirements.txt
# Build -> Run this to install all requiremed packages using the lambdaci container. Packages are added to the ./src folder.
# Pack -> Package necessary files in the ./src directory to a zip file excluding folder and files. Zipped package is added to the ./dist folder.
# Upload -> Checks for existing lambda. Create if it doesn't exist or Update if it exists.
# Test -> Run lambda and output results to local logs.
function readJson() {
UNAMESTR=$(uname)
if [[ "$UNAMESTR" == 'Linux' ]]; then
SED_EXTENDED='-r'
elif [[ "$UNAMESTR" == 'Darwin' ]]; then
SED_EXTENDED='-E'
fi
VALUE=$(grep -m 1 "\"${2}\"" "${1}" | sed "${SED_EXTENDED}" 's/^ *//;s/.*: *"//;s/",?//')
if [ ! "$VALUE" ]; then
echo "Error: Cannot find \"${2}\" in ${1}" >&2
exit 1
else
echo "$VALUE"
fi
}
function build() {
echo
QUESTION="Have you filled in the requirements.txt file properly? (y/n) "
echo "$QUESTION" >> "log/toolkit-$TIMESTAMP.txt"
read -p "$QUESTION" -n 1 -r
echo
echo "User Input Recorded: $REPLY " >> "log/toolkit-$TIMESTAMP.txt"
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo
echo "Installing required packages..." | tee -a "log/toolkit-$TIMESTAMP.txt"
docker run -it -v "$PWD:/host/user" -e "USER=$USER" -e "HOME=/host/user" --rm -w /host/user lambci/lambda:build-python3.6 bash -c "pip3 install -r requirements.txt -t ./src" >> "log/toolkit-$TIMESTAMP.txt"
echo
echo "All packages installed." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
fi
}
function pack() {
echo
echo Packaging lambda... | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
cd src ||
zip -r --exclude=*.dist-info* --exclude=*.vscode* --exclude=*pycache* --exclude=*.git --exclude=*.zip ../dist/lambda_function.zip . >> "../log/toolkit-$TIMESTAMP.txt"
echo "Packaging complete. The zip file can be found in the dist folder." | tee -a "../log/toolkit-$TIMESTAMP.txt"
cd ..
echo
}
function upload() {
echo
echo "Checking if lambda exists." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
LAMBDA=$(aws lambda get-function --function-name "$NAME" 2>> "log/toolkit-$TIMESTAMP.txt")
echo "$LAMBDA" >> "log/toolkit-$TIMESTAMP.txt"
if [[ $LAMBDA == *"Configuration"* ]]; then
QUESTION="A lambda already exists with this name. Do you wish to update (y/n) "
echo "$QUESTION" >> "log/toolkit-$TIMESTAMP.txt"
read -p "$QUESTION" -n 1 -r
echo "User Input Recorded: $REPLY " >> "log/toolkit-$TIMESTAMP.txt"
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo
echo "Updating lambda code..." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
RESULT=$(aws lambda update-function-code --function-name "$NAME" --zip-file fileb://dist/lambda_function.zip)
echo "$RESULT" >> "log/toolkit-$TIMESTAMP.txt"
if [[ $RESULT == *"FunctionName"* ]]; then
echo "The lambda was updated successfully." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
else
echo "The lambda was not updated successfully. Please inspect the logs." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
fi
else
echo
echo "The lambda was not uploaded." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
fi
else
QUESTION="No lambda exists with this name. Do you wish to create (y/n) "
echo "$QUESTION" >> "log/toolkit-$TIMESTAMP.txt"
read -p "$QUESTION" -n 1 -r
echo "User Input Recorded: $REPLY " >> "log/toolkit-$TIMESTAMP.txt"
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo
echo Creating lambda... | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
RESULT=$(aws lambda create-function --function-name "$NAME" --region "$REGION" --zip-file fileb://dist/lambda_function.zip --role "$ROLE" --handler "$HANDLER" --runtime "$RUNTIME" --timeout "$TIMEOUT" --memory-size "$MEMORY")
echo "$RESULT" >> "log/toolkit-$TIMESTAMP.txt"
if [[ $RESULT == *"FunctionName"* ]]; then
echo "The lambda was created successfully." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
else
echo "The lambda was not created successfully. Please inspect the logs." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
fi
else
echo
echo "The lambda was not uploaded." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
fi
fi
}
function test() {
echo
echo Testing lambda... | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
# docker run --rm -v $PWD:/var/task lambci/lambda:build-python3.6 /bin/sh -c $HANDLER
aws lambda invoke --invocation-type RequestResponse --function-name "$NAME" --region "$REGION" --log-type Tail "log/run-$TIMESTAMP.txt" >> "log/toolkit-$TIMESTAMP.txt"
echo "Run complete." | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
echo "To see output of lambda go to $PWD/log/run-$TIMESTAMP.txt" | tee -a "log/toolkit-$TIMESTAMP.txt"
echo
}
NAME=$(readJson config.json name) || exit 1
HANDLER=$(readJson config.json handler) || exit 1
REGION=$(readJson config.json region) || exit 1
ROLE=$(readJson config.json role) || exit 1
RUNTIME=$(readJson config.json runtime) || exit 1
TIMEOUT=$(readJson config.json timeout) || exit 1
MEMORY=$(readJson config.json memory) || exit 1
TIMESTAMP="$(date +%Y%m%d%H%M%S)"
echo "START OF LOG" > "log/toolkit-$TIMESTAMP.txt"
echo
echo "Checking for active aws session" | tee -a "log/toolkit-$TIMESTAMP.txt"
LOGGEDIN=$(aws ecr get-login 2>&1)
echo "$LOGGEDIN" >> "log/toolkit-$TIMESTAMP.txt"
if [[ $LOGGEDIN == *"error"* ]]; then
echo
echo 'No active user session. Please make sure you are logged in to aws via aws cli and try again.'
echo
else
echo
echo 'Active user session found.'
echo
if [ "$1" == "build" ]; then
build
fi
if [ "$1" == "pack" ]; then
echo
QUESTION="Would you like to re-build required packages from requirements.txt (y/n) "
echo "$QUESTION" >> "log/toolkit-$TIMESTAMP.txt"
read -p "$QUESTION" -n 1 -r
echo "User Input Recorded: $REPLY " >> "log/toolkit-$TIMESTAMP.txt"
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
build
fi
pack
fi
if [ "$1" == "upload" ]; then
echo
QUESTION="Would you like to re-build required packages from requirements.txt (y/n) "
echo "$QUESTION" >> "log/toolkit-$TIMESTAMP.txt"
read -p "$QUESTION" -n 1 -r
echo "User Input Recorded: $REPLY " >> "log/toolkit-$TIMESTAMP.txt"
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
build
fi
pack
upload
fi
if [ "$1" == "test" ]; then
test
fi
fi
echo "To see logs go to $PWD/log/toolkit-$TIMESTAMP.txt" | tee -a "log/toolkit-$TIMESTAMP.txt"
echo