forked from dcflachs/compose_plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeploy.sh
More file actions
213 lines (185 loc) · 6.28 KB
/
deploy.sh
File metadata and controls
213 lines (185 loc) · 6.28 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
#!/bin/bash
set -euo pipefail
# Default values
VERSION=""
DEV=false
REMOTE_HOSTS=()
USER_NAME="root"
REMOTE_DIR="/tmp"
PACKAGE_PATH=""
SKIP_BUILD=false
COMPOSE_VERSION="5.0.2"
QUICK=false
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ARCHIVE_DIR="$SCRIPT_DIR/archive"
in_container=false
if [[ -f "/.dockerenv" ]] || grep -qE '/docker|/lxc|/kubepods' /proc/1/cgroup 2>/dev/null; then
in_container=true
fi
# Prefer the workspace archive in /code when running in a container.
ARCHIVE_DIR="$SCRIPT_DIR/archive"
if [[ "$in_container" == true && -d "/code" ]]; then
ARCHIVE_DIR="$SCRIPT_DIR/archive"
fi
usage() {
cat <<EOF
Usage: $0 [options]
-Version <version>
-Dev
-RemoteHost <host1,host2,...>
-User <ssh-user>
-RemoteDir <remote-dir>
-PackagePath <package.txz>
-SkipBuild
-ComposeVersion <compose-version>
-Quick
-Help
EOF
exit 1
}
# parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-Version|--Version)
VERSION="$2"; shift 2;;
-Dev|--Dev)
DEV=true; shift;;
-RemoteHost|--RemoteHost)
IFS=',' read -r -a REMOTE_HOSTS <<< "$2"; shift 2;;
-User|--User)
USER_NAME="$2"; shift 2;;
-RemoteDir|--RemoteDir)
REMOTE_DIR="$2"; shift 2;;
-PackagePath|--PackagePath)
PACKAGE_PATH="$2"; shift 2;;
-SkipBuild|--SkipBuild)
SKIP_BUILD=true; shift;;
-ComposeVersion|--ComposeVersion)
COMPOSE_VERSION="$2"; shift 2;;
-Quick|--Quick)
QUICK=true; shift;;
-Help|--Help|-h)
usage;;
*)
echo "Unknown option $1"; usage;;
esac
done
if [[ "$QUICK" == true ]]; then
if [[ ${#REMOTE_HOSTS[@]} -eq 0 ]]; then
echo "RemoteHost is required when using -Quick"; exit 1
fi
REPO_ROOT=$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || true)
if [[ -z "$REPO_ROOT" ]]; then
echo "Unable to resolve git repository root from $SCRIPT_DIR"; exit 1
fi
QUICK_PREFIX="source/compose.manager/"
QUICK_REMOTE_ROOT="/usr/local/emhttp/plugins/compose.manager"
mapfile -t UNSTAGED < <(git -C "$REPO_ROOT" diff --name-only --diff-filter=ACMR -- "$QUICK_PREFIX")
mapfile -t STAGED < <(git -C "$REPO_ROOT" diff --cached --name-only --diff-filter=ACMR -- "$QUICK_PREFIX")
mapfile -t CHANGED_FILES < <(printf '%s\n' "${UNSTAGED[@]}" "${STAGED[@]}" | sort -u)
if [[ ${#CHANGED_FILES[@]} -eq 0 ]]; then
echo "No tracked staged/unstaged file changes found under source/compose.manager.";
exit 0
fi
echo "Files queued for quick sync (${#CHANGED_FILES[@]}):"
printf ' %s\n' "${CHANGED_FILES[@]}"
for host in "${REMOTE_HOSTS[@]}"; do
target="$USER_NAME@$host"
printf '\nQuick deploy to %s :\n' "$target"
for relative in "${CHANGED_FILES[@]}"; do
[[ $relative == $QUICK_PREFIX* ]] || continue
subpath=${relative#"$QUICK_PREFIX"}
local_path="$REPO_ROOT/$relative"
if [[ ! -f "$local_path" ]]; then
echo "Skipping missing local file: $relative"; continue
fi
remote_file="$QUICK_REMOTE_ROOT/$subpath"
remote_parent=$(dirname "$remote_file")
ssh "$target" mkdir -p "$remote_parent"
scp "$local_path" "$target:$remote_file"
done
done
echo "Quick deployment complete."
exit 0
fi
if [[ -n "$PACKAGE_PATH" ]]; then
if [[ ! -f "$PACKAGE_PATH" ]]; then
echo "PackagePath not found: $PACKAGE_PATH"; exit 1
fi
PACKAGE_PATH=$(realpath "$PACKAGE_PATH")
else
if [[ "$SKIP_BUILD" == true ]]; then
shopt -s nullglob
files=("$ARCHIVE_DIR"/compose.manager-*-noarch-*.txz)
shopt -u nullglob
if [[ ${#files[@]} -eq 0 ]]; then
echo "No package found in archive"; exit 1
fi
PACKAGE_PATH=$(printf '%s\n' "${files[@]}" | sort -r | head -n1)
if [[ -z "$PACKAGE_PATH" ]]; then
echo "No package found in archive"; exit 1
fi
else
build_args=()
[[ -n "$VERSION" ]] && build_args+=("-Version" "$VERSION")
[[ "$DEV" == true ]] && build_args+=("-Dev")
[[ -n "$COMPOSE_VERSION" ]] && build_args+=("-ComposeVersion" "$COMPOSE_VERSION")
echo "Building package via build.sh..."
bash "$SCRIPT_DIR/build.sh" "${build_args[@]}"
# Use nullglob and ls -t to avoid xargs running with no input (which lists current directory)
shopt -s nullglob
files=("$ARCHIVE_DIR"/compose.manager-*-noarch-*.txz)
shopt -u nullglob
if [[ ${#files[@]} -eq 0 ]]; then
echo "Archive directory content:"
ls -al "$ARCHIVE_DIR" 2>/dev/null || echo "Cannot list archive directory"
echo "Archive directory: $ARCHIVE_DIR"
echo "Build completed but package not found"; exit 1
fi
echo "Checking archive directory: $ARCHIVE_DIR"
ls -al "$ARCHIVE_DIR"
PACKAGE_PATH=$(printf '%s\n' "${files[@]}" | sort -r | head -n1)
echo "Candidate package path: $PACKAGE_PATH"
if [[ -z "$PACKAGE_PATH" ]]; then
echo "Build completed but package not found"
echo "Archive dir content:"
ls -al "$ARCHIVE_DIR"
exit 1
fi
fi
fi
PACKAGE_NAME=$(basename "$PACKAGE_PATH")
if [[ ${#REMOTE_HOSTS[@]} -eq 0 ]]; then
echo "No RemoteHost specified — build only, skipping deploy. Package: $PACKAGE_PATH"
exit 0
fi
PLUGIN_PATH="$SCRIPT_DIR/archive/compose.manager.plg"
if [[ ! -f "$PLUGIN_PATH" ]]; then
PLUGIN_PATH="$SCRIPT_DIR/compose.manager.plg"
fi
if [[ ! -f "$PLUGIN_PATH" ]]; then
echo "Plugin file not found"; exit 1
fi
INSTALL_SCRIPT_LOCAL="$SCRIPT_DIR/install.sh"
if [[ ! -f "$INSTALL_SCRIPT_LOCAL" ]]; then
echo "Install script not found: $INSTALL_SCRIPT_LOCAL"; exit 1
fi
for host in "${REMOTE_HOSTS[@]}"; do
target="$USER_NAME@$host"
remote_package="$REMOTE_DIR/$PACKAGE_NAME"
remote_plugin="$REMOTE_DIR/$(basename "$PLUGIN_PATH")"
remote_install_script="$REMOTE_DIR/install.sh"
printf '\nDeploying to %s :\n' "$target"
echo " Local package : $PACKAGE_PATH"
echo " Local .plg : $PLUGIN_PATH"
echo " Local install : $INSTALL_SCRIPT_LOCAL"
echo " Remote target : $target:$REMOTE_DIR"
echo "Uploading package, .plg and install.sh via SCP..."
scp "$PACKAGE_PATH" "$target:$REMOTE_DIR/"
scp "$PLUGIN_PATH" "$target:$REMOTE_DIR/"
scp "$INSTALL_SCRIPT_LOCAL" "$target:$remote_install_script"
echo "Executing remote install script..."
# shellcheck disable=SC2029
ssh "$target" bash "$remote_install_script" "$remote_package" "$remote_plugin" && ssh "$target" rm -f "$remote_install_script"
echo "Deployment complete to $host"
done