-
Notifications
You must be signed in to change notification settings - Fork 0
/
flop
executable file
·205 lines (186 loc) · 10.5 KB
/
flop
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
#!/usr/bin/env bash
set -euo pipefail
find_this () {
THIS="${1:?'must provide script path, like "${BASH_SOURCE[0]}"'}"
trap "fatal >&2 'FATAL: could not resolve parent directory of ${THIS}'" EXIT
[ "${THIS:0:1}" == "/" ] || THIS="$(pwd -P)/${THIS}"
THIS_DIR="$(dirname -- "${THIS}")"
THIS_DIR="$(cd -P -- "${THIS_DIR}" && pwd)"
THIS="${THIS_DIR}/$(basename -- "${THIS}")"
trap "" EXIT
}
export -f find_this
find_this "${BASH_SOURCE[0]}"
export FLOP_CLI="flop"
main () {
local USAGE=$(cat <<EOF
USAGE: ${FLOP_CLI} COMMAND [TFVARS_FILE_NAME]
FilmDrop test utility
Supported commands:
help (-h, --help) show this message
create (mk) create a new flop cluster
destroy (rm) destroy a flop cluster and associated state
init initialize flop configuration
list (ls) list all resources in flop cluster state
output (out) list all outputs for the flop cluster
test review flop cluster changes current flop cluster state
update apply updates to existing flop
validate validate flop configuration
Command arguments:
TFVARS_FILENAME name of tfvars input file, ex: default.tfvars
All commmands support 'help' for more information.
EOF
)
export FLOP_COMMAND="${1:-}"; shift ||:
case "${FLOP_COMMAND:-}" in
create|mk) export TFVARS_FILE=$@
export STAC_SERVER_TAG=`cat $TFVARS_FILE | grep version | grep -o '".*"' | sed 's/"//g'`
export STAC_SERVER_DIR="stac-server-${STAC_SERVER_TAG:1}"
export PROJECT_NAME_VAR=`cat $TFVARS_FILE | grep project_name | grep -o '".*"' | sed 's/"//g'`
export ENVIRONMENT_VAR=`cat $TFVARS_FILE | grep environment | grep -o '".*"' | sed 's/"//g'`
if [ $(echo -n "$PROJECT_NAME_VAR" | wc -c) -le 8 ]
then
echo 'project_name is 8 characters or less'
else
echo 'Error: project_name is greater than 8 characters'
exit 1
fi
if [ $(echo -n "$ENVIRONMENT_VAR" | wc -c) -le 5 ]
then
echo 'environment is 5 characters or less'
else
echo 'Error: environment is greater than 5 characters'
exit 1
fi
if
cat $TFVARS_FILE | grep deploy_stac_server | grep "true"
then
echo 'deploy_stac_server is true, building stac-server dependencies'
source $HOME/.nvm/nvm.sh
nvm install v18
nvm use v18
echo "Building stac-server..."
curl -L -f --no-progress-meter -o - "https://github.com/stac-utils/stac-server/archive/refs/tags/${STAC_SERVER_TAG}.tar.gz" | tar -xz
cd "$STAC_SERVER_DIR"
npm install
BUILD_PRE_HOOK=true npm run build
mkdir -p ../modules/stac-server/lambda/api
cp dist/api/api.zip ../modules/stac-server/lambda/api/
mkdir -p ../modules/stac-server/lambda/ingest
cp dist/ingest/ingest.zip ../modules/stac-server/lambda/ingest/
mkdir -p ../modules/stac-server/lambda/pre-hook
cp dist/pre-hook/pre-hook.zip ../modules/stac-server/lambda/pre-hook/
cd ..
fi
if
cat $TFVARS_FILE | grep include_historical_ingest | grep "true" ||
cat $TFVARS_FILE | grep include_ongoing_ingest | grep "true"
then
cd modules/stac-server/historical-ingest/lambda/
pip install -r requirements.txt --target package
cd package
zip -r ../../lambda.zip .
cd ../
zip ../lambda.zip main.py
cd ../../../../
fi
terraform apply -var-file=$TFVARS_FILE ;;
destroy|rm) export stac_opensearch_domain_name=`terraform output stac_opensearch_domain_name`
export stac_opensearch_domain_name="${stac_opensearch_domain_name//\"}"
export DELETE_OPENSEARCH_DOMAIN="no"
if [[ "$stac_opensearch_domain_name" != "" && !("$stac_opensearch_domain_name" =~ ".aoss.amazonaws.com") && "$stac_opensearch_domain_name" != *"Warning"* ]]; then
echo "We detected a Stac Server OpenSearch Domain $stac_opensearch_domain_name running in flop environment..."
echo "Do you really want to destroy the Stac Server OpenSearch domain along with other resources?"
echo "There is no undo. Only 'yes' will be accepted to confirm."
echo ""
read -p "Enter a value: " opensearch_delete
export DELETE_OPENSEARCH_DOMAIN=$opensearch_delete
fi
if [ "$DELETE_OPENSEARCH_DOMAIN" == "yes" ]; then
terraform state rm "module.filmdrop.module.stac-server[0].module.stac-server.aws_opensearch_domain.stac_server_opensearch_domain"
aws opensearch delete-domain --domain-name $stac_opensearch_domain_name --output text > .opensearch.info
fi
terraform destroy -var-file=$@ ;;
init) terraform init ;;
list|ls) terraform state list ;;
output|out) terraform output ;;
test) terraform plan -var-file=$@ ;;
update) export TFVARS_FILE=$@
export STAC_SERVER_TAG=`cat $TFVARS_FILE | grep version | grep -o '".*"' | sed 's/"//g'`
export STAC_SERVER_DIR="stac-server-${STAC_SERVER_TAG:1}"
export PROJECT_NAME_VAR=`cat $TFVARS_FILE | grep project_name | grep -o '".*"' | sed 's/"//g'`
export ENVIRONMENT_VAR=`cat $TFVARS_FILE | grep environment | grep -o '".*"' | sed 's/"//g'`
if [ $(echo -n "$PROJECT_NAME_VAR" | wc -c) -le 8 ]
then
echo 'project_name is 8 characters or less'
else
echo 'Error: project_name is greater than 8 characters'
exit 1
fi
if [ $(echo -n "$ENVIRONMENT_VAR" | wc -c) -le 5 ]
then
echo 'environment is 5 characters or less'
else
echo 'Error: environment is greater than 5 characters'
exit 1
fi
if
cat $TFVARS_FILE | grep deploy_stac_server | grep "true"
then
echo 'deploy_stac_server is true, building stac-server dependencies'
source $HOME/.nvm/nvm.sh
nvm install v18
nvm use v18
echo "Building stac-server..."
curl -L -f --no-progress-meter -o - "https://github.com/stac-utils/stac-server/archive/refs/tags/${STAC_SERVER_TAG}.tar.gz" | tar -xz
cd "$STAC_SERVER_DIR"
npm install
BUILD_PRE_HOOK=true npm run build
mkdir -p ../modules/stac-server/lambda/api
cp dist/api/api.zip ../modules/stac-server/lambda/api/
mkdir -p ../modules/stac-server/lambda/ingest
cp dist/ingest/ingest.zip ../modules/stac-server/lambda/ingest/
mkdir -p ../modules/stac-server/lambda/pre-hook
cp dist/pre-hook/pre-hook.zip ../modules/stac-server/lambda/pre-hook/
cd ..
fi
if
cat $TFVARS_FILE | grep include_historical_ingest | grep "true" ||
cat $TFVARS_FILE | grep include_ongoing_ingest | grep "true"
then
cd modules/stac-server/historical-ingest/lambda/
pip install -r requirements.txt --target package
cd package
zip -r ../../lambda.zip .
cd ../
zip ../lambda.zip main.py
cd ../../../../
fi
terraform apply -var-file=$TFVARS_FILE ;;
validate) export TFVARS_FILE=$@
export PROJECT_NAME_VAR=`cat $TFVARS_FILE | grep project_name | grep -o '".*"' | sed 's/"//g'`
export ENVIRONMENT_VAR=`cat $TFVARS_FILE | grep environment | grep -o '".*"' | sed 's/"//g'`
if [ $(echo -n "$PROJECT_NAME_VAR" | wc -c) -le 8 ]
then
echo 'project_name is 8 characters or less'
else
echo 'Error: project_name is greater than 8 characters'
exit 1
fi
if [ $(echo -n "$ENVIRONMENT_VAR" | wc -c) -le 5 ]
then
echo 'environment is 5 characters or less'
else
echo 'Error: environment is greater than 5 characters'
exit 1
fi
terraform validate ;;
help|-h|--help) echo "$USAGE"; exit ;;
?*) fatal "unknown command: '$FLOP_COMMAND'" 1 ;;
*) echo "$USAGE"; exit ;;
esac
}
# check if we have been sourced and exit
# allows us to do testing by sourcing this file
(return 0 2>/dev/null) && return
main "$@"