Repository navigation
Expand file tree
/
Copy pathdev.sh
More file actions
289 lines (232 loc) · 7.77 KB
/
Copy pathdev.sh
File metadata and controls
289 lines (232 loc) · 7.77 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
#!/bin/bash
################################################################################
# Script Name: dev.sh
# Description: Overwrite OpenPanel container files and restart service to apply.
# Usage: opencli dev [path]
# Docs: https://docs.openpanel.com
# Author: Stefan Pejcic
# Created: 07.03.2025
# Last Modified: 28.10.2026
# Company: openpanel.com
# Copyright (c) openpanel.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
################################################################################
set -e
# ======================================================================
# Constants
readonly CONTAINER_NAME="openpanel"
readonly DOCKER_CONTEXT="default"
readonly LAST_PATH_FILE="/tmp/last_dev_path"
readonly SUPPORTED_EXTENSIONS=("py" "html")
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly NC='\033[0m'
# ======================================================================
# Helpers
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
usage() {
cat << EOF
Usage: opencli dev [path]
Description:
This script is intended for development purposes only to overwrite system files
in the OpenPanel UI container. It will open the file in the nano editor, validate
its contents, and then copy the changes back to the container. Afterwards, it
restarts the OpenPanel container to pick up the new file and follow the logs.
Options:
path The path to a file inside the OpenPanel Docker container (either .py or .html).
If no path is provided, the script will display available files for you to choose.
Examples:
1. Edit a Python module by providing its path:
opencli dev modules/ssh.py
2. Edit an HTML template by providing its path:
opencli dev templates/base.html
3. Interactively select a file to edit:
opencli dev
EOF
exit 1
}
command_exists() {
command -v "$1" &> /dev/null
}
install_tool() {
local tool="$1"
install_package() {
local package="$1"
log_info "Attempting to install $package..."
if command_exists apt; then
apt install -y "$package" > /dev/null 2>&1
elif command_exists dnf; then
dnf install -y epel-release > /dev/null 2>&1
dnf install -y "$package" > /dev/null 2>&1
elif command_exists yum; then
yum install -y "$package" > /dev/null 2>&1
else
log_error "No supported package manager found (apt/dnf/yum)"
return 1
fi
}
if ! command_exists "$tool"; then
if ! install_package "$tool"; then
log_error "Failed to install $tool. Please install it manually."
exit 1
fi
log_info "$tool installed successfully"
fi
}
is_container_running() {
docker --context "$DOCKER_CONTEXT" ps --format "table {{.Names}}" | grep -q "^$CONTAINER_NAME$"
}
get_all_available_files() {
local available_files
if ! is_container_running; then
log_error "OpenPanel container is not running!"
exit 1
fi
available_files=$(docker --context "$DOCKER_CONTEXT" exec "$CONTAINER_NAME" \
find / -maxdepth 3 \( -name "app.py" -o -path "/modules/*.py" -o -path "/templates/*.html" \) \
2>/dev/null | sort | uniq)
if [ -z "$available_files" ]; then
log_error "No files found in the container!"
exit 1
fi
local last_path=""
if [ -f "$LAST_PATH_FILE" ]; then
last_path=$(cat "$LAST_PATH_FILE")
fi
path=$(echo "$available_files" | fzf --prompt="Select a file: " --query="$last_path")
if [ -z "$path" ]; then
log_warn "No file selected."
exit 1
fi
echo "$path" > "$LAST_PATH_FILE"
log_info "Selected file: $path"
}
is_supported_extension() {
local extension="$1"
local supported_ext
for supported_ext in "${SUPPORTED_EXTENSIONS[@]}"; do
if [[ "$extension" == "$supported_ext" ]]; then
return 0
fi
done
return 1
}
validate_file() {
local file="$1"
local ext="$2"
case "$ext" in
py)
grep -q "import" "$file" \
|| { log_error "Invalid Python file: missing import statements"; return 1; }
;;
html)
grep -qi "<div" "$file" \
|| { log_error "Invalid HTML file: missing HTML tags"; return 1; }
;;
*)
log_error "Unsupported file extension: $ext"
return 1
;;
esac
}
copy_to_container() {
local source_file="$1"
local target_path="$2"
if ! docker --context "$DOCKER_CONTEXT" cp "$source_file" "$CONTAINER_NAME:/$target_path"; then
log_error "Failed to copy file to container"
return 1
fi
log_info "File copied to container successfully"
return 0
}
restart_container_and_follow_logs() {
log_info "Restarting OpenPanel container to pick up the new file..."
if ! docker --context "$DOCKER_CONTEXT" restart "$CONTAINER_NAME"; then
log_error "Failed to restart container"
return 1
fi
log_info "Following new logs:"
docker --context "$DOCKER_CONTEXT" logs --follow --since=0s "$CONTAINER_NAME"
}
process_file() {
local file_path="$1"
local extension
local tmpfile
extension="${file_path##*.}"
if ! is_supported_extension "$extension"; then
log_error "Unsupported file extension: $extension"
log_error "Supported extensions: ${SUPPORTED_EXTENSIONS[*]}"
exit 1
fi
tmpfile=$(mktemp)
# cleanup tmpfile
cleanup_tmpfile() {
rm -f "$tmpfile"
}
trap cleanup_tmpfile EXIT
# open nano editor
nano "$tmpfile"
# abort if empty
if [ ! -s "$tmpfile" ]; then
log_error "Aborting: The file is empty!"
exit 1
fi
# validate HTML / PYTHON
if ! validate_file "$tmpfile" "$extension"; then
log_error "File validation failed"
exit 1
fi
# copy to container
if ! copy_to_container "$tmpfile" "$file_path"; then
exit 1
fi
# enable dev_mode
opencli config update dev_mode on > /dev/null 2>&1
# restart and tail
restart_container_and_follow_logs
}
# ======================================================================
main() {
local file_path="$1"
if [ $# -gt 0 ]; then
if [ -z "$file_path" ]; then
log_error "Invalid path provided!"
usage
fi
else
install_tool fzf
install_tool nano
get_all_available_files
file_path="$path"
fi
process_file "$file_path"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi