This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 376
/
build
executable file
·129 lines (109 loc) · 3.36 KB
/
build
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
#!/bin/bash
set -e
ROOT=${ROOT:-$(pwd)}
TYPE=$1
SERVICE=$2
service_dir=$ROOT/$SERVICE
build_dir=$service_dir/build
display_usage () {
echo "Usage: $0 TYPE SERVICE"
}
# Check if there are at least 2 arguments
if [ $# -lt 2 ]; then
display_usage
exit 1
fi
# Check if the service exists
if [ ! -f $service_dir/metadata.yaml ]; then
echo "Service $SERVICE does not exist"
exit 1
fi
# Check for quiet mode
if [ ! -z $QUIET ]; then
export OUTPUT_FILE=$(mktemp)
exec 5>&1 6>&2 1>$OUTPUT_FILE 2>&1
fi
cleanup () {
CODE=$?
if [ ! -z $QUIET ]; then
if [ ! $CODE -eq 0 ]; then
cat $OUTPUT_FILE >&5
fi
rm $OUTPUT_FILE
fi
}
trap cleanup EXIT
# Copy the CloudFormation template and create artifacts for every environment
build_cloudformation () {
# Create build folder if it doesn't exist
if [ ! -d $build_dir ]; then
mkdir $build_dir
fi
# Copy CloudFormation template
if [ -f $service_dir/template.yaml ]; then
cp -p $service_dir/template.yaml $build_dir/template.yaml
fi
# Create artifacts
if [ ! -d $build_dir/artifacts/ ]; then
mkdir $build_dir/artifacts/
fi
$ROOT/tools/helpers/build_artifacts $service_dir $build_dir/artifacts/
}
# Copy resources into the build folder
build_resources () {
# Create build folder if it doesn't exist
if [ ! -d $build_dir ]; then
mkdir $build_dir
fi
# Copying resource files
if [ -d $service_dir/resources/ ]; then
cp -rp $service_dir/resources/ $build_dir/resources/
fi
}
# Copy source code and install requirements for each function
build_python3 () {
# Create build folder if it doesn't exist
if [ ! -d $build_dir ]; then
mkdir $build_dir
fi
# Copying source files
if [ -d $service_dir/src/ ]; then
for function in $(ls $service_dir/src/); do
# Only perform the build operation if the checksum file doesn't exist OR the checksum doesn't match
if [ ! -f $build_dir/src/$function.md5 -o "$(find $service_dir/src/$function -type f -exec md5sum {} \; | sort -k 2 | md5sum)" != "$(cat $build_dir/src/$function.md5)" ]; then
cp -rp $service_dir/src/ $build_dir/src/
if [ -f $build_dir/src/$function/requirements.txt ]; then
python3 -m pip install --upgrade \
--requirement $build_dir/src/$function/requirements.txt \
--target $build_dir/src/$function/
find $service_dir/src/$function -type f -exec md5sum {} \; | sort -k 2 | md5sum > $build_dir/src/$function.md5
fi
else
echo "Checksum match for function $function. Skipping"
fi
done
fi
}
# Resolve the openapi.yaml file
build_openapi () {
# Create build folder if it doesn't exist
if [ ! -d $build_dir ]; then
mkdir $build_dir
fi
openapi=resources/openapi.yaml
# Resolve OpenAPI files
if [ -f $service_dir/$openapi ]; then
# Create resources folder if it doesn't exist
if [ ! -d $build_dir/resources/ ]; then
mkdir $build_dir/resources/
fi
speccy resolve $service_dir/$openapi -o $build_dir/$openapi
fi
}
type build_$TYPE | grep -q "function" &>/dev/null || {
echo "Unsupported type: $TYPE"
echo
display_usage
exit 1
}
build_$TYPE