Skip to content

Commit 9cae4a6

Browse files
Merge branch 'yq-auto-install'
2 parents 87a369a + 64cd544 commit 9cae4a6

12 files changed

+104
-35
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
/shared/system-logs/*
1414
!/shared/system-logs/.gitkeep
1515

16+
/bin/*
17+
!/bin/.gitkeep
18+
19+
1620
**/.idea
1721
/.env
1822
/.env.ready.*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ graph TD;
142142
| git | N/A | Manual | - |
143143
| bash | 4.4 at least | Manual | - |
144144
| curl | N/A | Manual | - |
145-
| yq | 4.35.1 | Manual | Use v4.35.1 instead of the latest version. The lastest version causes a parsing error |
145+
| yq | 4.35.1 | Auto | Use v4.35.1 instead of the latest version. The lastest version causes a parsing error |
146146
| consul (docker image) | 1.14.11 | Auto | An error occurred due to a payload format issue while the lastest version of it was communicating with gliderlabs/registrator. |
147147
| gliderlabs/registrator (docker image) | master | Auto | |
148148
| nginx (docker image) | 1.25.4 | Auto | Considering changing it to a certain version, but until now no issues have been detected. |

apply-security.sh

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,24 @@ sudo chmod 750 *.sh || echo "[WARN] Running chmod 750 *.sh failed."
3232
sudo chmod 770 *.yml || echo "[WARN] Running chmod 770 *.yml failed."
3333
sudo chmod 740 .env.* || echo "[WARN] Running chmod 740 .env.* failed."
3434
sudo chmod 740 .env || echo "[WARN] Running chmod 740 .env failed."
35+
sudo chmod -R 750 bin || echo "[WARN] Running chmod 750 for the bin folder"
3536
sudo chmod 770 .gitignore || echo "[WARN] Running chmod 770 .gitignore failed."
3637
sudo chmod -R 770 .docker/ || echo "[WARN] Running chmod -R 770 .docker/ failed."
37-
sudo chown -R 0:${shared_volume_group_id} .docker/ || echo "[WARN] Running chgrp ${shared_volume_group_id} .docker/ failed."
38-
set_safe_filemode_on_app
38+
# Check if the OS is not Darwin (macOS) before running the command
39+
if [[ "$(uname)" != "Darwin" ]]; then
40+
sudo chown -R 0:${shared_volume_group_id} .docker/ || echo "[WARN] Running chgrp ${shared_volume_group_id} .docker/ failed."
41+
else
42+
echo "[NOTICE] Skipping chown command on Darwin (macOS) platform. See the README."
43+
fi
44+
45+
if [[ "$(uname)" != "Darwin" ]]; then
46+
sudo chown -R 0:${shared_volume_group_id} bin/ || echo "[WARN] Running chgrp ${shared_volume_group_id} bin/ failed."
47+
else
48+
echo "[NOTICE] Skipping chown command on Darwin (macOS) platform. See the README."
49+
fi
50+
51+
if [[ "$(uname)" != "Darwin" ]]; then
52+
set_safe_filemode_on_app
53+
else
54+
echo "[NOTICE] Skipping chown command on Darwin (macOS) platform. See the README."
55+
fi

bin/.gitkeep

Whitespace-only changes.

emergency-consul-down-and-up.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set -eu
33

44
source use-common.sh
5+
check_yq_installed
56
check_bash_version
67
check_gnu_grep_installed
78
check_gnu_sed_installed

emergency-nginx-down-and-up.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -eu
33

44
source use-common.sh
55

6+
check_yq_installed
67
check_bash_version
78
check_gnu_grep_installed
89
check_gnu_sed_installed

emergency-nginx-restart.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
set -eu
33

44
source use-common.sh
5+
6+
check_yq_installed
57
check_bash_version
68
check_gnu_grep_installed
79
check_gnu_sed_installed

remove-all-images.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set -eu
33

44
source use-common.sh
5+
check_yq_installed
56
check_bash_version
67
check_gnu_grep_installed
78
check_gnu_sed_installed

stop-all-containers.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
22
source use-common.sh
3+
check_yq_installed
34
check_bash_version
45
check_gnu_grep_installed
56
check_gnu_sed_installed

use-app.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ apply_env_service_name_onto_app_yaml(){
3535
check_yq_installed
3636

3737
if [[ ${orchestration_type} == 'stack' ]]; then
38-
yq -i "with(.services; with_entries(select(.key ==\"*-${new_state}\") | .key |= \"${project_name}-${new_state}\"))" docker-${orchestration_type}-${project_name}-${app_env}.yml || (echo "[ERROR] Failed to apply the green service name in the app YAML as ${project_name}." && exit 1)
39-
# yq eval '(.services.[] | select(.image == "${PROJECT_NAME}:blue")).image |= \"${project_name}-blue\"' -i docker-${orchestration_type}-${project_name}-blue.yml || (echo "[ERROR] Failed to apply image : ${project_name}-blue in the app YAML." && exit 1)
40-
yq -i "(.services.\"${project_name}-${new_state}\").image = \"${project_name}:${new_state}\"" -i docker-${orchestration_type}-${project_name}-${app_env}.yml || (echo "[ERROR] Failed to apply image : ${project_name}-${new_state} in the app YAML." && exit 1)
38+
bin/yq -i "with(.services; with_entries(select(.key ==\"*-${new_state}\") | .key |= \"${project_name}-${new_state}\"))" docker-${orchestration_type}-${project_name}-${app_env}.yml || (echo "[ERROR] Failed to apply the green service name in the app YAML as ${project_name}." && exit 1)
39+
# bin/yq eval '(.services.[] | select(.image == "${PROJECT_NAME}:blue")).image |= \"${project_name}-blue\"' -i docker-${orchestration_type}-${project_name}-blue.yml || (echo "[ERROR] Failed to apply image : ${project_name}-blue in the app YAML." && exit 1)
40+
bin/yq -i "(.services.\"${project_name}-${new_state}\").image = \"${project_name}:${new_state}\"" -i docker-${orchestration_type}-${project_name}-${app_env}.yml || (echo "[ERROR] Failed to apply image : ${project_name}-${new_state} in the app YAML." && exit 1)
4141
else
4242
echo "[NOTICE] PROJECT_NAME on .env is now being applied to docker-${orchestration_type}-${project_name}-${app_env}.yml."
43-
yq -i "with(.services; with_entries(select(.key ==\"*-blue\") | .key |= \"${project_name}-blue\"))" docker-${orchestration_type}-${project_name}-${app_env}.yml || (echo "[ERROR] Failed to apply the blue service name in the app YAML as ${project_name}." && exit 1)
43+
bin/yq -i "with(.services; with_entries(select(.key ==\"*-blue\") | .key |= \"${project_name}-blue\"))" docker-${orchestration_type}-${project_name}-${app_env}.yml || (echo "[ERROR] Failed to apply the blue service name in the app YAML as ${project_name}." && exit 1)
4444
sleep 2
45-
yq -i "with(.services; with_entries(select(.key ==\"*-green\") | .key |= \"${project_name}-green\"))" docker-${orchestration_type}-${project_name}-${app_env}.yml || (echo "[ERROR] Failed to apply the green service name in the app YAML as ${project_name}." && exit 1)
45+
bin/yq -i "with(.services; with_entries(select(.key ==\"*-green\") | .key |= \"${project_name}-green\"))" docker-${orchestration_type}-${project_name}-${app_env}.yml || (echo "[ERROR] Failed to apply the green service name in the app YAML as ${project_name}." && exit 1)
4646
fi
4747

4848
}
@@ -61,12 +61,12 @@ apply_docker_compose_environment_onto_app_yaml(){
6161

6262
for state in "${states[@]}"
6363
do
64-
yq -i '.services.'${project_name}'-'${state}'.environment = []' docker-${orchestration_type}-${project_name}-${app_env}.yml
65-
yq -i '.services.'${project_name}'-'${state}'.environment += "SERVICE_NAME='${state}'"' docker-${orchestration_type}-${project_name}-${app_env}.yml
64+
bin/yq -i '.services.'${project_name}'-'${state}'.environment = []' docker-${orchestration_type}-${project_name}-${app_env}.yml
65+
bin/yq -i '.services.'${project_name}'-'${state}'.environment += "SERVICE_NAME='${state}'"' docker-${orchestration_type}-${project_name}-${app_env}.yml
6666

67-
for ((i=1; i<=$(echo ${docker_compose_environment} | yq eval 'length'); i++))
67+
for ((i=1; i<=$(echo ${docker_compose_environment} | bin/yq eval 'length'); i++))
6868
do
69-
yq -i '.services.'${project_name}'-'${state}'.environment += "'$(echo ${docker_compose_environment} | yq -r 'to_entries | .['$((i-1))'].key')'='$(echo ${docker_compose_environment} | yq -r 'to_entries | .['$((i-1))'].value')'"' docker-${orchestration_type}-${project_name}-${app_env}.yml
69+
bin/yq -i '.services.'${project_name}'-'${state}'.environment += "'$(echo ${docker_compose_environment} | bin/yq -r 'to_entries | .['$((i-1))'].key')'='$(echo ${docker_compose_environment} | bin/yq -r 'to_entries | .['$((i-1))'].value')'"' docker-${orchestration_type}-${project_name}-${app_env}.yml
7070
done
7171
done
7272

@@ -107,11 +107,11 @@ apply_docker_compose_volumes_onto_app_real_yaml(){
107107

108108
for state in "${states[@]}"
109109
do
110-
#yq -i '.services.'${project_name}'-'${state}'.volumes = []' ./docker-${orchestration_type}-${project_name}-real.yml
110+
#bin/yq -i '.services.'${project_name}'-'${state}'.volumes = []' ./docker-${orchestration_type}-${project_name}-real.yml
111111

112112
for volume in "${docker_compose_real_selective_volumes[@]}"
113113
do
114-
yq -i '.services.'${project_name}'-'${state}'.volumes += '${volume}'' ./docker-${orchestration_type}-${project_name}-real.yml
114+
bin/yq -i '.services.'${project_name}'-'${state}'.volumes += '${volume}'' ./docker-${orchestration_type}-${project_name}-real.yml
115115
done
116116
done
117117

@@ -126,9 +126,9 @@ make_docker_build_arg_strings(){
126126

127127
local re=""
128128

129-
for ((i=1; i<=$(echo ${docker_build_args} | yq eval 'length'); i++))
129+
for ((i=1; i<=$(echo ${docker_build_args} | bin/yq eval 'length'); i++))
130130
do
131-
re="${re} --build-arg $(echo ${docker_build_args} | yq -r 'to_entries | .['$((i-1))'].key')=$(echo ${docker_build_args} | yq -r 'to_entries | .['$((i-1))'].value')"
131+
re="${re} --build-arg $(echo ${docker_build_args} | bin/yq -r 'to_entries | .['$((i-1))'].key')=$(echo ${docker_build_args} | bin/yq -r 'to_entries | .['$((i-1))'].value')"
132132
done
133133

134134
echo ${re}

use-common.sh

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ set_expose_and_app_port(){
6565
fi
6666

6767
if echo "${1}" | grep -Eq '^\[[0-9]+,[0-9]+\]$'; then
68-
expose_port=$(echo "$project_port" | yq e '.[0]' -)
69-
app_port=$(echo "$project_port" | yq e '.[1]' -)
68+
expose_port=$(echo "$project_port" | bin/yq e '.[0]' -)
69+
app_port=$(echo "$project_port" | bin/yq e '.[1]' -)
7070
else
7171
expose_port="$project_port"
7272
app_port="$project_port"
@@ -255,25 +255,67 @@ cache_global_vars() {
255255

256256
check_yq_installed(){
257257
required_version="4.35.1"
258+
yq_path="bin/yq"
259+
260+
# Function to download yq
261+
download_yq() {
262+
echo "[NOTICE] Downloading bin/yq version $required_version..." >&2
263+
264+
# Detect OS and architecture
265+
ARCH=$(uname -m)
266+
OS=$(uname | tr '[:upper:]' '[:lower:]')
267+
268+
# Determine the correct bin/yq binary based on architecture
269+
case "$OS-$ARCH" in
270+
linux-x86_64)
271+
YQ_BINARY="yq_linux_amd64"
272+
;;
273+
linux-aarch64)
274+
YQ_BINARY="yq_linux_arm64"
275+
;;
276+
linux-armv7l | linux-armhf)
277+
YQ_BINARY="yq_linux_arm"
278+
;;
279+
linux-i386 | linux-i686)
280+
YQ_BINARY="yq_linux_386"
281+
;;
282+
darwin-x86_64)
283+
YQ_BINARY="yq_darwin_amd64"
284+
;;
285+
darwin-arm64)
286+
YQ_BINARY="yq_darwin_arm64"
287+
;;
288+
*)
289+
echo >&2 "[ERROR] Unsupported OS or architecture: $OS-$ARCH"
290+
exit 1
291+
;;
292+
esac
293+
294+
DOWNLOAD_URL="https://github.com/mikefarah/yq/releases/download/v$required_version/$YQ_BINARY"
295+
296+
# Download yq
297+
curl -L "$DOWNLOAD_URL" -o "$yq_path"
298+
chmod +x "$yq_path"
299+
echo "[NOTICE] bin/yq version $required_version from $DOWNLOAD_URL has been downloaded to $yq_path." >&2
300+
}
258301

259-
# Check if yq is installed
260-
if ! command -v yq >/dev/null 2>&1; then
261-
echo >&2 "[ERROR] yq is NOT installed. Please install yq version $required_version manually."
262-
echo >&2 "You can download it from the following URL:"
263-
echo >&2 "https://github.com/mikefarah/yq/releases/download/v$required_version/yq_linux_amd64"
264-
exit 1
302+
# Check if bin/yq is installed in the bin directory
303+
if [ ! -f "$yq_path" ]; then
304+
echo "[WARNING] bin/yq is not found in $yq_path. Downloading..." >&2
305+
download_yq
265306
else
266-
# Check if installed version is not 4.35.1
267-
installed_version=$(yq --version | grep -oP 'version v\K[0-9.]+')
307+
# Check if installed bin/yq version is the required version
308+
installed_version=$("$yq_path" --version | grep -oP 'version v\K[0-9.]+')
268309
if [ "$installed_version" != "$required_version" ]; then
269-
echo >&2 "[ERROR] yq version is $installed_version. Please install yq version $required_version manually."
270-
echo >&2 "You can download it from the following URL:"
271-
echo >&2 "https://github.com/mikefarah/yq/releases/download/v$required_version/yq_linux_amd64"
272-
exit 1
310+
echo "[WARNING] bin/yq version is $installed_version, which is not the required version $required_version." >&2
311+
download_yq
312+
else
313+
echo "[NOTICE] bin/yq version $required_version is already installed in $yq_path." >&2
273314
fi
274315
fi
275316
}
276317

318+
277319
check_gnu_grep_installed() {
278320
# Check if grep is installed
279321
if ! command -v grep >/dev/null 2>&1; then

use-nginx.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ initiate_nginx_docker_compose_file(){
99
echo "[DEBUG] successfully copied docker-compose-app-nginx-original.yml"
1010
}
1111
apply_env_service_name_onto_nginx_yaml(){
12-
yq -i "with(.services; with_entries(select(.key ==\"*-nginx\") | .key |= \"${project_name}-nginx\"))" docker-compose-${project_name}-nginx.yml || (echo "[ERROR] Failed to apply the service name in the Nginx YML as ${project_name}." && exit 1)
12+
bin/yq -i "with(.services; with_entries(select(.key ==\"*-nginx\") | .key |= \"${project_name}-nginx\"))" docker-compose-${project_name}-nginx.yml || (echo "[ERROR] Failed to apply the service name in the Nginx YML as ${project_name}." && exit 1)
1313
}
1414
apply_ports_onto_nginx_yaml(){
1515

1616
check_yq_installed
1717

1818
echo "[NOTICE] PORTS on .env is now being applied to docker-compose-${project_name}-nginx.yml."
19-
yq -i '.services.'${project_name}'-nginx.ports = []' docker-compose-${project_name}-nginx.yml
20-
yq -i '.services.'${project_name}'-nginx.ports += "'${expose_port}':'${expose_port}'"' docker-compose-${project_name}-nginx.yml
19+
bin/yq -i '.services.'${project_name}'-nginx.ports = []' docker-compose-${project_name}-nginx.yml
20+
bin/yq -i '.services.'${project_name}'-nginx.ports += "'${expose_port}':'${expose_port}'"' docker-compose-${project_name}-nginx.yml
2121

2222
for i in "${additional_ports[@]}"
2323
do
2424
[ -z "${i##*[!0-9]*}" ] && (echo "[ERROR] Wrong port number on .env : ${i}" && exit 1);
25-
yq -i '.services.'${project_name}'-nginx.ports += "'$i:$i'"' docker-compose-${project_name}-nginx.yml
25+
bin/yq -i '.services.'${project_name}'-nginx.ports += "'$i:$i'"' docker-compose-${project_name}-nginx.yml
2626
done
2727

2828
}
@@ -57,7 +57,7 @@ apply_docker_compose_volumes_onto_app_nginx_yaml(){
5757

5858
for volume in "${docker_compose_nginx_selective_volumes[@]}"
5959
do
60-
yq -i '.services.'${project_name}'-'nginx'.volumes += '${volume}'' ./docker-compose-${project_name}-nginx.yml
60+
bin/yq -i '.services.'${project_name}'-'nginx'.volumes += '${volume}'' ./docker-compose-${project_name}-nginx.yml
6161
done
6262

6363
}

0 commit comments

Comments
 (0)