-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.sh
executable file
·175 lines (138 loc) · 6.23 KB
/
build.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
#!/usr/bin/env bash
## START STANDARD SITE BUILD SCRIPT INCLUDE
readonly THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")"
readonly BOOTSTRAP="$(dirname "$THIS_SCRIPT")/resources/bootstrap.inc.sh"
readonly BOOTSTRAP_VERSION=v0.11
[ -f "$BOOTSTRAP" ] && source "$BOOTSTRAP" || source <(curl -fs https://raw.githubusercontent.com/keymanapp/shared-sites/$BOOTSTRAP_VERSION/bootstrap.inc.sh)
## END STANDARD SITE BUILD SCRIPT INCLUDE
readonly API_KEYMAN_DB_CONTAINER_NAME=api-keyman-com-db
readonly API_KEYMAN_DB_CONTAINER_DESC=api-keyman-com-db
readonly API_KEYMAN_DB_IMAGE_NAME=api-keyman-com-db
readonly API_KEYMAN_CONTAINER_NAME=api-keyman-com-website
readonly API_KEYMAN_CONTAINER_DESC=api-keyman-com-app
readonly API_KEYMAN_IMAGE_NAME=api-keyman-com-website
readonly HOST_API_KEYMAN_COM=api.keyman.com.localhost
source _common/keyman-local-ports.inc.sh
source _common/docker.inc.sh
################################ Main script ################################
builder_describe \
"Setup api.keyman.com site to run via Docker." \
"configure" \
"clean" \
"build" \
"start" \
"stop" \
"test" \
":db Build the database" \
":app Build the site"
builder_parse "$@"
function test_docker_container() {
echo "TIER_TEST" > tier.txt
# Note: ci.yml replicates these
# Run unit tests
docker exec $API_KEYMAN_CONTAINER_DESC sh -c "vendor/bin/phpunit --testdox"
# Lint .php files for obvious errors
docker exec $API_KEYMAN_CONTAINER_DESC sh -c "find . -name '*.php' | grep -v '/vendor/' | xargs -n 1 -d '\\n' php -l"
# Check all internal links
# NOTE: link checker runs on host rather than in docker image
npx broken-link-checker http://localhost:8058 --ordered --recursive --host-requests 10 -e --filter-level 3
rm tier.txt
}
builder_run_action configure bootstrap_configure
builder_run_action clean:db clean_docker_container $API_KEYMAN_DB_IMAGE_NAME $API_KEYMAN_DB_CONTAINER_NAME
builder_run_action clean:app clean_docker_container $API_KEYMAN_IMAGE_NAME $API_KEYMAN_CONTAINER_NAME
builder_run_action stop:db stop_docker_container $API_KEYMAN_DB_IMAGE_NAME $API_KEYMAN_DB_CONTAINER_NAME
builder_run_action stop:app stop_docker_container $API_KEYMAN_IMAGE_NAME $API_KEYMAN_CONTAINER_NAME
# Build the Docker containers
function build_docker_container_db() {
local IMAGE_NAME=$1
local CONTAINER_NAME=$2
# Download docker image. --mount option requires BuildKit
DOCKER_BUILDKIT=1 docker build -t $API_KEYMAN_DB_IMAGE_NAME -f mssql.Dockerfile .
}
builder_run_action build:db build_docker_container_db $API_KEYMAN_DB_IMAGE_NAME $API_KEYMAN_DB_CONTAINER_NAME
builder_run_action build:app build_docker_container $API_KEYMAN_IMAGE_NAME $API_KEYMAN_CONTAINER_NAME
# Custom start actions for db and app different from shared-sites
function start_docker_container_db() {
local IMAGE_NAME=$1
local CONTAINER_NAME=$2
local CONTAINER_DESC=$3
# HOST not applicable
local PORT=$4
local CONTAINER_ID=$(get_docker_container_id $CONTAINER_NAME)
if [ ! -z "$CONTAINER_ID" ]; then
builder_die "container $CONTAINER_ID has already been started"
fi
# Start the Docker container
if [ -z $(get_docker_image_id $IMAGE_NAME) ]; then
builder_die "ERROR: Docker container doesn't exist. Run \"./build.sh build\" first"
fi
# Setup database
builder_echo "Setting up DB container"
docker run -m 2048m --rm -d -p $PORT:1433 \
-e "ACCEPT_EULA=Y" \
-e "MSSQL_AGENT_ENABLED=true" \
-e "MSSQL_SA_PASSWORD=yourStrong(\!)Password" \
--name $CONTAINER_DESC \
$CONTAINER_NAME
builder_echo green "SQL Server Listening on localhost:$PORT"
}
function start_docker_container_app() {
local IMAGE_NAME=$1
local CONTAINER_NAME=$2
local CONTAINER_DESC=$3
local HOST=$4
local PORT=$5
_verify_vendor_is_not_folder
local CONTAINER_ID=$(get_docker_container_id $CONTAINER_NAME)
if [ ! -z "$CONTAINER_ID" ]; then
builder_die "$HOST container $CONTAINER_ID has already been started"
fi
# Start the Docker container
if [ -z $(get_docker_image_id $IMAGE_NAME) ]; then
builder_die "ERROR: Docker container doesn't exist. Run \"./build.sh build\" first"
fi
if [[ $OSTYPE =~ msys|cygwin ]]; then
# Windows needs leading slashes for path
SITE_HTML="//$(pwd):/var/www/html/"
else
SITE_HTML="$(pwd):/var/www/html/"
fi
ADD_HOST=
if [[ $OSTYPE =~ linux-gnu ]]; then
# Linux needs --add-host parameter
ADD_HOST="--add-host host.docker.internal:host-gateway"
fi
db_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${API_KEYMAN_DB_IMAGE_NAME})
builder_echo "Spooling up site container"
docker run --rm -m 200m -d -p $PORT:80 -v ${SITE_HTML} \
-e 'api_keyman_com_mssql_pw=yourStrong(\!)Password' \
-e api_keyman_com_mssql_user=sa \
-e 'api_keyman_com_mssqlconninfo=sqlsrv:Server='$db_ip',1433;TrustServerCertificate=true;Encrypt=false;Database=' \
-e api_keyman_com_mssql_create_database=true \
-e api_keyman_com_mssqldb=keyboards \
--name $CONTAINER_DESC \
${ADD_HOST} \
$CONTAINER_NAME
# Skip if link already exists
if [ ! -L vendor ]; then
# Create link to vendor/ folder
CONTAINER_ID=$(get_docker_container_id $CONTAINER_NAME)
if [ -z "$CONTAINER_ID" ]; then
builder_die "Docker container appears to have failed to start in order to create link to vendor/"
fi
docker exec -i $CONTAINER_ID sh -c "ln -s /var/www/vendor vendor && chown -R www-data:www-data vendor"
fi
# after starting container, we want to run an init script if it is present
if [ -f resources/init-container.sh ]; then
CONTAINER_ID=$(get_docker_container_id $CONTAINER_NAME)
if [ -z "$CONTAINER_ID" ]; then
builder_die "Docker container appears to have failed to start in order to run init-container.sh script"
fi
docker exec -i $CONTAINER_ID sh -c "./resources/init-container.sh"
fi
builder_echo green "Listening on http://$HOST:$PORT"
}
builder_run_action start:db start_docker_container_db $API_KEYMAN_DB_IMAGE_NAME $API_KEYMAN_DB_CONTAINER_NAME $API_KEYMAN_DB_CONTAINER_DESC $PORT_API_KEYMAN_COM_DB
builder_run_action start:app start_docker_container_app $API_KEYMAN_IMAGE_NAME $API_KEYMAN_CONTAINER_NAME $API_KEYMAN_CONTAINER_DESC $HOST_API_KEYMAN_COM $PORT_API_KEYMAN_COM
builder_run_action test:app test_docker_container