-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.sh
executable file
·232 lines (188 loc) · 5.43 KB
/
cli.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
# Project Management CLI
# ==============================================================================
#
# Utility CLI tool automating setup up, deployment, and general project
# management.
#
# Helps work with AWS infrastructure, particularly AWS CDK.
#
# See README.org for more details and usage examples.
# ------------------------------------------------------------------------------
set -e # Exit immediately if any command returns a non-zero status
SCRIPT_DIR=$(cd $(dirname $0) && pwd)
WORKING_DIR=$(pwd)
FNAME=${0##/*}
DIRNAME=${WORKING_DIR##*/}
HANDLER_DIR="$WORKING_DIR/src"
CONFIG_FILE="$WORKING_DIR/config.py"
OUTPUT_FILE="$WORKING_DIR/bin/package.zip"
ENV_PROD=".venv_prod"
ENV_DEV=".venv_dev"
log() {
script_name=${0##*/}
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "== $script_name $timestamp >> $@"
}
# ==============================================================================
# Required Config & Environment variables
# ==============================================================================
create_config() {
if [ -f "$CONFIG_FILE" ]; then
read -p "$CONFIG_FILE exists. Overwrite it? (y/n): " -n 1 -r
echo
if [[ $REPLY = "y" ]] || [[ $REPLY = "Y" ]]
then
cp config.template "$CONFIG_FILE"
log "Overwrote config file."
fi
else
cp config.template "$CONFIG_FILE"
log "Created config file."
fi
}
# Returns the value of the given config variable
#
# Usage:
# get_config <key>
#
# Arguments:
# key: The config variable name (string)
#
# Example: AWS_PROFILE=$(get_config AWS_PROFILE)
# ------------
get_config() {
export PYTHONDONTWRITEBYTECODE=1 # Prevent any .pyc generation
python -c "from config import $1; print($1)"
}
# ==============================================================================
# Utilities
# ==============================================================================
# Removes all packages from a virtual environment
#
# Usage:
# clear_venv <name>
#
# Arguments:
# name: The name of the environment (string)
#
# Example: clear_venv $ENV_DEV
# ------------
clear_venv() {
source $1/bin/activate
pip freeze | xargs pip uninstall -y
}
# ==============================================================================
# CLI Commands
# ==============================================================================
cdk_bootstrap() {
source $ENV_DEV/bin/activate
ACCOUNT=$(get_config AWS_ACCOUNT)
REGION=$(get_config AWS_REGION)
PROFILE=$(get_config AWS_PROFILE)
cdk bootstrap aws://$ACCOUNT/$REGION --profile $PROFILE
}
cdk_destroy() {
source $ENV_DEV/bin/activate
ACCOUNT=$(get_config AWS_ACCOUNT)
REGION=$(get_config AWS_REGION)
PROFILE=$(get_config AWS_PROFILE)
cdk destroy --all --profile $PROFILE
log "!!NOTE: DyanamoDB tables were probably not deleted, check output!!"
}
build() {
log "Building..."
source $ENV_PROD/bin/activate
pip install -r requirements/prod.txt
# Ensure output dir exists with no file
rm -f $OUTPUT_FILE
dir_name=$(dirname "$OUTPUT_FILE")
mkdir -p $dir_name
site_packages=$(python -c "import sys; print(sys.path[-1])")
cd $site_packages
zip -r $OUTPUT_FILE .
cd $HANDLER_DIR
zip -r $OUTPUT_FILE .
log "Finished building output file: $OUTPUT_FILE"
}
deploy() {
log "Deploying..."
source $ENV_DEV/bin/activate
cdk synth
PROFILE=$(get_config AWS_PROFILE)
cdk deploy --profile $PROFILE
log "Finished deploying"
}
setup() {
log "Creating dev environment"
python3 -m venv $ENV_DEV
source $ENV_DEV/bin/activate
pip install -r requirements/dev.txt
pip install -r requirements/prod.txt
deactivate
log "Creating prod environment"
python3 -m venv $ENV_PROD
source $ENV_PROD/bin/activate
pip install -r requirements/prod.txt
deactivate
create_config
log "Finished setting up project"
}
teardown() {
read -p "Are you sure you want to remove generated files? (y/n): " -n 1 -r
echo
if [[ $REPLY = "y" ]] || [[ $REPLY = "Y" ]]
then
rm -rf $ENV_DEV
rm -rf $ENV_PROD
rm -rf $CONFIG_FILE
log "Tore down generated files"
fi
}
help() {
echo "
$DIRNAME Development CLI
Commands should be executed from project root directory:
e.g ~/.../$DIRNAME/
Usage: $FNAME [command]
Commands:
================ Core ======================================================
setup (s) Setup project for development
build (b) Builds project to bin/package.zip
deploy (d) Deploys project to AWS
help (*) Help
---------------- Utility & One-Offs ----------------------------------------
teardown Tears down anything generated by setup
clear_dev Clears the dev virtual environment completely
cdk_bootstrap Runs the cdk bootstrap for your defined environment
cdk_destroy Runs the cdk destroy for your defined environment and
removes all resources associated with your project
"
}
set -x # Prints each command to std:err
case "$1" in
setup|s)
setup
;;
teardown)
teardown
;;
build|b)
build
;;
deploy|d)
deploy
;;
clear_dev)
clear_venv $ENV_DEV
;;
cdk_bootstrap)
cdk_bootstrap
;;
cdk_destroy)
cdk_destroy
;;
*)
help
;;
esac