-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdclamp
executable file
·192 lines (165 loc) · 5.56 KB
/
dclamp
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
#!/usr/bin/env bash
# Function to generate docker-compose.yml content
generate_docker_compose() {
# Load environment variables from dclamp.env file
if [ -f "$config_file" ]; then
# shellcheck source=dclamp.env
. "$config_file"
fi
# Function to append service configurations
append_service() {
cat <<EOL
$1:
extends:
<<: *file-extends
service: $1
EOL
}
# Initialize docker-compose.yml content with a note
docker_compose_content=$(cat <<EOL
# This file is generated automatically via dclamp script.
# Do not modify manually.
x-file-extends: &file-extends
file: compose-declaration.yml
services:
EOL
)
# Include activated services
for service in $ACTIVATED_SERVICES; do
docker_compose_content="$docker_compose_content$(append_service "$service")"
done
# Include PHP versions if specified
for php_version in $PHP_VERSIONS; do
docker_compose_content="$docker_compose_content
php${php_version//./}:
extends:
<<: *file-extends
service: php
build:
args:
PHP_VER: $php_version"
done
# Initialize volumes content
volumes_content=""
# Define the services that can have volumes
services_with_volumes=("elasticsearch" "opensearch" "mailpit")
# Check if any of the services are activated and include volumes section
for service in "${services_with_volumes[@]}"; do
if [[ " $ACTIVATED_SERVICES " = *" $service "* ]]; then
if [[ -z "$volumes_content" ]]; then
volumes_content="
volumes:"
fi
volume_name="${service}_data"
volumes_content="$volumes_content
$volume_name:"
fi
done
# Append volumes content to docker-compose content
docker_compose_content="$docker_compose_content$volumes_content"
printf "%s\n" "$docker_compose_content"
}
# Function to print usage information
print_usage() {
echo "Usage: dclamp <action> [options]"
echo "Actions:"
echo " generate Generate docker-compose.yml content"
echo " --print/-p Print to stdout"
echo " --file/-f <filename> Write to specified file (default: docker-compose.yml)"
echo " --config/-c <filename> Use specified .env file (default: dclamp.env)"
echo " config Setup configuration"
echo " help Display this usage information"
}
# Function to display available services
display_available_services() {
echo "Available services:"
services_section=$(awk '/^services:$/,/^$/ {print}' compose-declaration.yml)
echo "$services_section" | awk '/^[[:space:]]{2}[a-zA-Z0-9_-]+:/ && !/php:/ {gsub(/:/,"",$1); print " " $1}'
}
# Default values for options
filename="docker-compose.yml"
config_file="dclamp.env"
# Default values for configuration setup
default_php_versions="7.4 8.1"
default_activated_services="httpd mariadb opensearch"
# Parse command line arguments
action="$1"
shift
if [ -z "$action" ]; then
if [ ! -f "dclamp.env" ]; then
echo "dclamp.env file not found. Running config and generate actions automatically."
action="config"
generate="yes"
else
echo "No action provided. Displaying help."
print_usage
exit 0
fi
fi
case "$action" in
"generate")
print_mode="file"
while [ $# -gt 0 ]; do
case "$1" in
--print|-p)
print_mode="print"
shift
;;
--file|-f)
print_mode="file"
filename="$2"
shift 2
;;
--config|-c)
config_file="$2"
shift 2
;;
*)
echo "Invalid option: $1"
print_usage
exit 1
;;
esac
done
if [ "$print_mode" = "print" ]; then
generate_docker_compose
elif [ "$print_mode" = "file" ]; then
generate_docker_compose > "$filename"
echo "Generated docker-compose.yml content written to $filename"
fi
;;
"config")
echo "Configuration Setup:"
# Prompt for PHP versions with default value
read -rp "Define PHP versions (default: $default_php_versions): " php_versions
php_versions=${php_versions:-$default_php_versions}
# Display available services
display_available_services
# Prompt for activated services with default value
read -rp "Define activated services (default: $default_activated_services): " activated_services
activated_services=${activated_services:-$default_activated_services}
# Writing configurations to file
echo "PHP_VERSIONS='$php_versions'" > "$config_file"
echo "ACTIVATED_SERVICES='$activated_services'" >> "$config_file"
if [ ! -e ".env" ]; then
echo "Notice: .env file does not exist. Creating .env file."
echo "UID=$UID" > .env
fi
echo "Configurations written to $config_file"
# Generate docker-compose.yml if flagged
if [ "$generate" = "yes" ]; then
generate_docker_compose > "docker-compose.yml"
echo "Generated docker-compose.yml content written to docker-compose.yml"
else
echo "To generate docker-compose.yml, run: ./dclamp generate"
fi
;;
"help")
print_usage
;;
*)
echo "Invalid action: $action"
print_usage
exit 1
;;
esac