-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-install.sh
executable file
·343 lines (286 loc) · 10.4 KB
/
post-install.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
# Capture Spin Variables
SPIN_ACTION=${SPIN_ACTION:-"install"}
SPIN_PHP_VERSION="${SPIN_PHP_VERSION:-8.3}"
SPIN_PHP_DOCKER_IMAGE="${SPIN_PHP_DOCKER_IMAGE:-serversideup/php:${SPIN_PHP_VERSION}-cli}"
# Set project variables
project_dir=${SPIN_PROJECT_DIRECTORY:-"$(pwd)/template"}
php_dockerfile="Dockerfile"
template_src_dir=${SPIN_TEMPLATE_TEMPORARY_SRC_DIR:-"$(pwd)"}
template_src_dir_absolute=$(realpath "$template_src_dir")
# Initialize the service variables
mariadb="1"
redis=""
use_github_actions=""
###############################################
# Functions
###############################################
add_php_extensions() {
echo "${BLUE}Adding custom PHP extensions...${RESET}"
local dockerfile="$project_dir/$php_dockerfile"
# Check if Dockerfile exists
if [ ! -f "$dockerfile" ]; then
echo "Error: $dockerfile not found."
return 1
fi
# Uncomment the USER root line
line_in_file --action replace --file "$dockerfile" "# USER root" "USER root"
# Add RUN command to install extensions
local extensions_string="${php_extensions[*]}"
line_in_file --action replace --file "$dockerfile" "# RUN install-php-extensions" "RUN install-php-extensions $extensions_string"
echo "Custom PHP extensions added."
}
initialize_git_repository() {
local current_dir=""
current_dir=$(pwd)
cd "$project_dir" || exit
echo "Initializing Git repository..."
git init
# Exclude vendor from git
line_in_file --file ".gitignore" \
"/vendor" \
".env*" \
"!.env.example" \
"public/wp-content/uploads/*"
cd "$current_dir" || exit
}
process_selections() {
[[ $mariadb ]] && configure_mariadb
[[ $redis ]] && configure_redis
[[ $use_github_actions ]] && configure_github_actions
echo "Services configured."
}
select_features() {
while true; do
clear
echo "${BOLD}${YELLOW}Select which features you'd like to use:${RESET}"
echo -e "${redis:+$BOLD$BLUE}1) Redis${RESET}"
echo "Press a number to select/deselect."
echo "Press ${BOLD}${BLUE}ENTER${RESET} to continue or skip."
read -s -r -n 1 key
case $key in
1)
[[ $redis ]] && redis="" || redis="1"
;;
'') break ;;
esac
done
}
select_github_actions() {
while true; do
clear
echo "${BOLD}${YELLOW}Would you like to use GitHub Actions?${RESET}"
if [ "$use_github_actions" = "1" ]; then
echo -e "${BOLD}${BLUE}1) Yes${RESET}"
echo "2) No"
else
echo "1) Yes"
echo -e "${BOLD}${BLUE}2) No${RESET}"
fi
echo "Press a number to select/deselect."
echo "Press ${BOLD}${BLUE}ENTER${RESET} to continue."
read -s -n 1 key
case $key in
1) use_github_actions="1";;
2) use_github_actions="" ;;
'') break ;;
esac
done
}
select_php_extensions() {
clear
echo "${BOLD}${YELLOW}What PHP extensions would you like to include?${RESET}"
echo ""
echo "${BLUE}Default extensions:${RESET}"
echo "ctype, curl, dom, fileinfo, filter, hash, mbstring, mysqli,"
echo "opcache, openssl, pcntl, pcre, pdo_mysql, pdo_pgsql, redis,"
echo "session, tokenizer, xml, zip"
echo ""
echo "${BLUE}See available extensions:${RESET}"
echo "https://serversideup.net/docker-php/available-extensions"
echo ""
echo "Enter additional extensions as a comma-separated list (no spaces).${RESET}"
echo "Example: gd,imagick,intl"
echo ""
echo "${BOLD}${YELLOW}Enter comma separated extensions below or press ${BOLD}${BLUE}ENTER${RESET} ${BOLD}${YELLOW}to use default extensions.${RESET}"
read -r extensions_input
# Remove spaces and split into array
IFS=',' read -r -a php_extensions <<<"${extensions_input// /}"
# Print selected extensions for confirmation
while true; do
if [ ${#php_extensions[@]} -gt 0 ]; then
clear
echo "${BOLD}${YELLOW}These extensions names must be supported in the PHP version you selected.${RESET}"
echo "Learn more here: https://serversideup.net/docker-php/available-extensions"
echo ""
echo "${BLUE}PHP Version:${RESET} $SPIN_PHP_VERSION"
echo "${BLUE}Extensions:${RESET}"
for extension in "${php_extensions[@]}"; do
echo "- $extension"
done
echo ""
echo "${BOLD}${YELLOW}Are these selections correct?${RESET}"
echo "Press ${BOLD}${BLUE}ENTER${RESET} to continue or ${BOLD}${BLUE}any other key${RESET} to go back and change selections."
read -n 1 -s -r key
echo
if [[ $key == "" ]]; then
echo "${GREEN}Continuing with selected extensions...${RESET}"
break
else
echo "${YELLOW}Returning to extension selection...${RESET}"
select_php_extensions
return
fi
else
break
fi
done
}
set_colors() {
if [[ -t 1 ]]; then
RAINBOW="
$(printf '\033[38;5;196m')
$(printf '\033[38;5;202m')
$(printf '\033[38;5;226m')
$(printf '\033[38;5;082m')
"
RED=$(printf '\033[31m')
GREEN=$(printf '\033[32m')
YELLOW=$(printf '\033[33m')
BLUE=$(printf '\033[34m')
DIM=$(printf '\033[2m')
BOLD=$(printf '\033[1m')
RESET=$(printf '\033[m')
else
RAINBOW=""
RED=""
GREEN=""
YELLOW=""
BLUE=""
DIM=""
BOLD=""
RESET=""
fi
}
configure_wordpress() {
echo "Configuring WordPress..."
source "$project_dir/configure-wordpress.sh"
rm "$project_dir/configure-wordpress.sh"
mv "$project_dir/load-environment.php" "$project_dir/public/load-environment.php"
local current_dir=""
current_dir=$(pwd)
cd "$project_dir" || exit
line_in_file --file ".dockerignore" \
"public/wp-content/" \
"!public/wp-content/languages" \
"!public/wp-content/plugins" \
"!public/wp-content/themes"
cd "$current_dir" || exit
echo "Done configuring WordPress."
}
###############################################
# Functions
###############################################
configure_github_actions() {
local service_name="github-actions"
merge_blocks "$service_name"
}
configure_mariadb() {
# TODO
echo "Configuring maria db... Done."
}
configure_redis() {
local service_name="redis"
merge_blocks "$service_name"
echo "$service_name: Updating the .env and .env.example files..."
line_in_file --action replace --file "$project_dir/.env" --file "$project_dir/.env.example" "REDIS_PASSWORD" "REDIS_PASSWORD=redispassword"
}
docker_yq() {
local yq_version="4.44.2"
docker run --rm \
--user "${SPIN_USER_ID}:${SPIN_GROUP_ID}" \
-v "${project_dir}:/workdir" \
-v "${template_src_dir_absolute}:/src" \
"mikefarah/yq:$yq_version" \
"$@"
}
merge_blocks() {
local service_name=$1
local blocks_dir="$template_src_dir_absolute/blocks/$service_name"
if [[ ! -d $blocks_dir ]]; then
echo "${BOLD}${RED}The blocks directory for \"$service_name\" does not exist. Exiting...${RESET}"
echo "Could not find the blocks directory at:"
echo "$blocks_dir"
exit 1
fi
echo "${BLUE}Updating files for $service_name...${RESET}"
find "$blocks_dir" -type f | while read -r block; do
# Extract the relative path of the file within the blocks directory
local rel_path=${block#"$blocks_dir/"}
# Determine the destination file
local destination="${project_dir}/${rel_path}"
# Create the destination directory if it doesn't exist
mkdir -p "$(dirname "$destination")"
# Check if the file is a YAML file
if [[ "$block" =~ \.(yml|yaml)$ ]]; then
# If the destination file doesn't exist, create it
if [[ ! -f "$destination" ]]; then
echo "{}" >"$destination"
fi
# Get relative paths for Docker volume mounts
local rel_block="${block#"${template_src_dir_absolute}/"}"
local rel_destination="${destination#"${project_dir}/"}"
# Merge the block into the destination file, appending values
docker_yq eval-all \
'select(fileIndex == 0) * select(fileIndex == 1)' \
"/workdir/$rel_destination" "/src/$rel_block" \
-i
echo "$service_name: Updated ${rel_path}"
else
# For non-YAML files, simply copy the file
cp "$block" "$destination"
echo "$service_name: Copied ${rel_path}"
fi
done
}
###############################################
# Main
###############################################
set_colors
select_php_extensions
select_features
select_github_actions
# Clean up the screen before moving forward
clear
# Set PHP Version of Project
line_in_file --action replace --file "$project_dir/$php_dockerfile" "FROM serversideup" "FROM serversideup/php:${SPIN_PHP_VERSION}-fpm-apache AS base"
# Add environment variables
cp "$project_dir/.env.example" "$project_dir/.env"
# Add PHP Extensions if available
if [ ${#php_extensions[@]} -gt 0 ]; then
add_php_extensions
fi
# Install Composer dependencies
if [[ "$SPIN_INSTALL_DEPENDENCIES" == "true" ]]; then
docker pull "$SPIN_PHP_DOCKER_IMAGE"
if [[ "$SPIN_ACTION" == "init" ]]; then
echo "Re-installing composer dependencies..."
(cd $project_dir && spin run php composer install)
else
echo "Installing Dependencies..."
(cd $project_dir && spin run php composer require vlucas/phpdotenv)
(cd $project_dir && spin run php composer require serversideup/spin --dev)
fi
fi
# Process the user selections
process_selections
# Configure APP_URL
configure_wordpress
# Configure Server Contact
line_in_file --action exact --ignore-missing --file "$project_dir/.infrastructure/conf/traefik/prod/traefik.yml" "[email protected]" "$SERVER_CONTACT"
line_in_file --action exact --ignore-missing --file "$project_dir/.spin.yml" "[email protected]" "$SERVER_CONTACT"
if [[ ! -d "$project_dir/.git" ]]; then
initialize_git_repository
fi
# Export actions so it's available to the main Spin script
export SPIN_USER_TODOS