-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.sh
More file actions
executable file
·291 lines (258 loc) · 8.31 KB
/
assembler.sh
File metadata and controls
executable file
·291 lines (258 loc) · 8.31 KB
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
#!/bin/bash
# This program is free software: you can redistribute it under the terms
# of the GNU General Public License, v. 3.0. If a copy of the GNU General
# Public License was not distributed with this file, see <https://www.gnu.org/licenses/>.
SCRIPT_ROOT=$(dirname $(readlink -f "${BASH_SOURCE[0]}"))
. "${SCRIPT_ROOT}/scripts/common.sh" || exit 1
show_help() {
echo "Usage: assembler [command] [options]"
echo ""
echo "Commands:"
echo " clone Clone or update repositories"
echo " deploy Deploy the projects"
echo " certs Copy SSL certificates from Let's Encrypt"
echo " drop Stop and remove containers, optionally delete their images"
echo " install Install the script and create a symbolic link in /usr/local/bin"
echo " uninstall Remove the symbolic link created by install"
echo " update Update assembler by checking for the latest version from Github"
echo ""
echo "Options:"
echo " --branch BRANCH Specify a branch for cloning/updating (optional for clone)"
echo " --project PROJECT Specify a project to clone/update or deploy (optional for clone, deploy, drop)"
echo " --no-proxy Disable reverse proxy (optional for deploy)"
echo " --no-management Disable management tools (optional for deploy, drop)"
echo " --rebuild Force rebuild of images without cache (optional for deploy)"
echo " --letsencrypt DOMAIN Specify the Let's Encrypt domain name (required for certs)"
echo " --destination DOMAIN Specify the destination domain name (required for certs)"
echo " --remove-images Remove Docker images after stopping and removing containers (optional for drop)"
echo " --no-version-check Skip the version comparison during the update (optional for update)"
}
if [ -z "$1" ]; then
show_help
exit 1
fi
COMMAND=$1
shift
handle_error() {
local message=$1
error "$message"
show_help
exit 1
}
check_for_extra_arguments() {
if [ $# -gt 0 ]; then
handle_error "Unexpected arguments after command: $@"
fi
}
case $COMMAND in
clone)
BRANCH=""
TARGET_REPO=""
while [[ "$1" =~ ^-- ]]; do
case $1 in
--branch)
shift
if [[ -z "$1" || "$1" =~ ^-- ]]; then
handle_error "Branch name is required after --branch."
fi
BRANCH=$1
shift
;;
--project)
shift
if [[ -z "$1" || "$1" =~ ^-- ]]; then
handle_error "Project name is required after --project."
fi
TARGET_REPO=$1
shift
;;
*)
handle_error "Unknown option: $1 for clone command."
;;
esac
done
check_for_extra_arguments "$@"
$SCRIPT_ROOT/scripts/clone_projects.sh ${TARGET_REPO:+--project $TARGET_REPO} ${BRANCH:+--branch $BRANCH}
;;
deploy)
PROXY_FLAG="--proxy"
MANAGEMENT_FLAG="--management"
TARGET_REPO=""
REBUILD_FLAG=""
while [[ "$1" =~ ^-- ]]; do
case $1 in
--no-proxy)
PROXY_FLAG=""
shift
;;
--no-management)
MANAGEMENT_FLAG=""
shift
;;
--rebuild)
REBUILD_FLAG="--rebuild"
shift
;;
--project)
shift
if [[ -z "$1" || "$1" =~ ^-- ]]; then
handle_error "Project name is required after --project."
fi
TARGET_REPO=$1
shift
;;
*)
handle_error "Unknown option: $1 for deploy command."
;;
esac
done
check_for_extra_arguments "$@"
$SCRIPT_ROOT/scripts/deploy.sh ${TARGET_REPO:+--project $TARGET_REPO} $PROXY_FLAG $MANAGEMENT_FLAG $REBUILD_FLAG
;;
certs)
LETSENCRYPT_DOMAIN=""
DESTINATION_DOMAIN=""
while [[ "$1" =~ ^-- ]]; do
case $1 in
--letsencrypt)
shift
if [[ -z "$1" || "$1" =~ ^-- ]]; then
handle_error "Let's Encrypt domain name is required after --letsencrypt."
fi
LETSENCRYPT_DOMAIN=$1
shift
;;
--destination)
shift
if [[ -z "$1" || "$1" =~ ^-- ]]; then
handle_error "Destination domain name is required after --destination."
fi
DESTINATION_DOMAIN=$1
shift
;;
*)
handle_error "Unknown option: $1 for certs command."
;;
esac
done
check_for_extra_arguments "$@"
if [ -z "$LETSENCRYPT_DOMAIN" ] || [ -z "$DESTINATION_DOMAIN" ]; then
handle_error "Both --letsencrypt and --destination options are required for certs command."
fi
LETSENCRYPT_PATH="/etc/letsencrypt/live/$LETSENCRYPT_DOMAIN"
DESTINATION_PATH="/etc/ssl/certs/$DESTINATION_DOMAIN"
$SCRIPT_ROOT/scripts/copy_certs.sh "$LETSENCRYPT_PATH" "$DESTINATION_PATH"
;;
drop)
PROXY_FLAG="--proxy"
MANAGEMENT_FLAG="--management"
REMOVE_IMAGES_FLAG=""
TARGET_REPO=""
while [[ "$1" =~ ^-- ]]; do
case $1 in
--no-proxy)
PROXY_FLAG=""
shift
;;
--no-management)
MANAGEMENT_FLAG=""
shift
;;
--remove-images)
REMOVE_IMAGES_FLAG="--remove-images"
shift
;;
--project)
shift
if [[ -z "$1" || "$1" =~ ^-- ]]; then
handle_error "Project name is required after --project."
fi
TARGET_REPO=$1
shift
;;
*)
handle_error "Unknown option: $1 for drop command."
;;
esac
done
check_for_extra_arguments "$@"
$SCRIPT_ROOT/scripts/drop.sh ${TARGET_REPO:+--project $TARGET_REPO} $PROXY_FLAG $MANAGEMENT_FLAG $REMOVE_IMAGES_FLAG
;;
install)
SCRIPT_PATH="$SCRIPT_ROOT/assembler.sh"
SYMLINK_PATH="/usr/local/bin/assembler"
if [[ ! -f "$SCRIPT_PATH" ]]; then
handle_error "Script not found at $SCRIPT_PATH"
fi
if [[ -L "$SYMLINK_PATH" ]]; then
warn "Assembler already installed."
sudo ln -sf "$SCRIPT_PATH" "$SYMLINK_PATH"
else
info "Installing assembler..."
sudo ln -s "$SCRIPT_PATH" "$SYMLINK_PATH"
fi
success "Installation complete. You can now run 'assembler' from anywhere."
;;
uninstall)
SYMLINK_PATH="/usr/local/bin/assembler"
if [[ -L "$SYMLINK_PATH" ]]; then
info "Uninstallating assembler..."
sudo rm "$SYMLINK_PATH"
success "Uninstallation complete. You can no longer run 'assembler' from the command line."
else
warn "Assembler is not installed."
fi
;;
update)
VERSION_FILE="$SCRIPT_ROOT/VERSION"
NO_VERSION_CHECK=false
CURRENT_VERSION=$(cat "$VERSION_FILE")
info "Checking for updates..."
while [[ "$1" =~ ^-- ]]; do
case $1 in
--no-version-check)
NO_VERSION_CHECK=true
shift
;;
*)
handle_error "Unknown option: $1 for update command."
;;
esac
done
if [ "$NO_VERSION_CHECK" = true ]; then
git pull origin main --quiet
git checkout main --quiet
success "Update complete (without version check)."
exit 0
fi
git fetch --tags --quiet
LATEST_VERSION=$(git describe --tags $(git rev-list --tags --max-count=1))
if [ -z "$LATEST_VERSION" ]; then
error "Could not fetch the latest version from GitHub."
exit 1
fi
echo "Current Version: $CURRENT_VERSION"
echo "Latest Version: $LATEST_VERSION"
if compare_versions "$LATEST_VERSION" "$CURRENT_VERSION"; then
read -p "An update is available. Would you like to proceed? (y/n) " choice
case "$choice" in
y | Y)
info "Updating to version $LATEST_VERSION..."
git checkout "$LATEST_VERSION" --quiet
success "Update successful. You are now on version $LATEST_VERSION."
;;
n | N)
warn "Update canceled."
;;
*)
error "Invalid choice. Update canceled."
;;
esac
else
warn "You are already on the latest version."
fi
;;
*)
handle_error "Unknown command: $COMMAND."
;;
esac