-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodjixd.sh
More file actions
228 lines (212 loc) · 6.66 KB
/
codjixd.sh
File metadata and controls
228 lines (212 loc) · 6.66 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
#!/bin/bash
# Configuration
CONFIG_FILE="/etc/codjixd.conf"
BASE_DIR="${BASE_DIR:-/opt/codjixd}"
LOG_DIR="${LOG_DIR:-${BASE_DIR}/logs}"
PID_DIR="${PID_DIR:-${BASE_DIR}/pids}"
SERVICES_DIR="${SERVICES_DIR:-${BASE_DIR}/services}"
VERSION="1.0.2"
# Load configuration file if it exists
[ -f "$CONFIG_FILE" ] && source "$CONFIG_FILE"
# Check if a service is running
is_running() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null 2>&1; then
return 0
else
rm -f "$PID_FILE"
return 1
fi
else
return 1
fi
}
# Rotate logs: move current logs to .old and start a new log file
rotate_logs() {
if [ -f "$LOG_FILE.log" ]; then
echo -e "\n=== Log session end ===\n" >> "$LOG_FILE.log"
cat "$LOG_FILE.log" >> "${LOG_FILE}.old.log"
fi
echo -e "=== Log session started at $(date) ===\n" > "$LOG_FILE.log"
}
# Load dependencies if defined in the service script
load_dependencies() {
if source "$SERVICE_SCRIPT" &> /dev/null && [ -n "${depend_on[@]}" ]; then
for dependency in "${depend_on[@]}"; do
echo "[o] Loading dependency: $dependency"
# Start the dependency synchronously in a new session
setsid "$0" start "$dependency"
done
fi
}
# Run a health check if defined in the service script
run_health_check() {
if source "$SERVICE_SCRIPT" &> /dev/null && declare -f health_check > /dev/null; then
while true; do
sleep 5
if health_check; then
echo "[o] Service $SERVICE_NAME is healthy."
else
echo "[x] Service $SERVICE_NAME is not healthy."
fi
done
fi
}
# Start a service
start_service() {
rotate_logs
load_dependencies
if is_running; then
echo "[x] Service $SERVICE_NAME is already running (PID: $(cat "$PID_FILE"))."
return 1
fi
# Start the service in the background and capture its PID immediately
if source "$SERVICE_SCRIPT" &> /dev/null && declare -f start_fn > /dev/null; then
start_fn >> "$LOG_FILE.log" 2>&1 &
PID=$!
echo "$PID" > "$PID_FILE"
echo "[o] Service $SERVICE_NAME started (PID: $PID)."
run_health_check >> "$LOG_FILE.log" 2>&1 &
else
echo "[x]: Service $SERVICE_NAME requires a start function."
fi
}
# Stop a service
stop_service() {
if ! is_running; then
echo "[x] Service $SERVICE_NAME is not running."
return 1
fi
PID=$(cat "$PID_FILE")
# Kill the entire process group
PGID=$(ps -o pgid= -p "$PID" | grep -o '[0-9]*')
kill -9 -- -"$PGID"
rm -f "$PID_FILE"
echo "[o] Service $SERVICE_NAME stopped."
}
# Kill a service and its dependencies
kill_service() {
stop_service
if source "$SERVICE_SCRIPT" &> /dev/null && [ -n "${depend_on[@]}" ]; then
for dependency in "${depend_on[@]}"; do
echo "[x] Killing dependency: $dependency"
"$0" kill "$dependency"
done
fi
}
# Restart a service
restart_service() {
stop_service
sleep 2
start_service
}
# Show the status and metrics of a service
show_status() {
if is_running; then
PID_NUM=$(cat "$PID_FILE")
echo "[o] Service $SERVICE_NAME is running"
echo "[o] Metrics (PID: $PID_NUM):"
ps -o pid,%cpu,%mem,start_time,etime -p "$PID_NUM"
else
echo "[x] Service $SERVICE_NAME is not running."
fi
}
# Tail the logs of a service
tail_logs() {
echo "[o] Tailing logs for service: $SERVICE_NAME"
echo '[o] Press "h" to show help, "q" to stop it'
echo -e '[o] Press "F" for live logs, "CTRL + C" to stop it\n'
echo "1. Current run logs"
echo "2. Old history logs"
echo "3. Live logs"
echo "4. Clean up logs"
echo "0. Cancel"
read -p "Choose an option [1]: " option
option="${option:-1}"
args=""
case "$option" in
0) return 0 ;;
1) option="$LOG_FILE.log" ;;
2) option="$LOG_FILE.old.log" ;;
3) option="$LOG_FILE.log" && args="+F" ;;
4) rm -f $LOG_FILE.{log,old.log} > /dev/null && \
echo "[o] Done" && exit 0 ;;
*) echo "[x] Invalid option: $option" && return 1 ;;
esac
if [ ! -f "$option" ]; then
echo "[x] No logs for $SERVICE_NAME."
return 1
fi
less -M $args "$option"
}
# List all available services
list_services() {
if [ -d "$SERVICES_DIR" ]; then
SERVICES=$(find "$SERVICES_DIR" -name "*.sh" -exec basename {} .sh \;)
if [ -z "$SERVICES" ]; then
echo "[x] No services yet."
else
echo "[o] Available services:"
for SERVICE in $SERVICES; do
echo "- $SERVICE"
done
fi
else
echo "[x] No services yet."
fi
}
# Wrapper for actions that require a service
safe_service() {
if [ -z "$SERVICE_NAME" ]; then
echo "[x]: Service is required."
exit 1
fi
LOG_FILE="${LOG_DIR}/${SERVICE_NAME}"
PID_FILE="${PID_DIR}/${SERVICE_NAME}.pid"
SERVICE_SCRIPT="${SERVICES_DIR}/${SERVICE_NAME}.sh"
if [ ! -f "$SERVICE_SCRIPT" ]; then
echo "[x]: Service $SERVICE_NAME not found."
exit 1
fi
mkdir -p "$LOG_DIR" "$PID_DIR"
"$1"
}
# Main script logic
ACTION="$1"
SERVICE_NAME="$2"
case "$ACTION" in
start) safe_service start_service ;;
stop) safe_service stop_service ;;
restart) safe_service restart_service ;;
kill) safe_service kill_service ;;
status) safe_service show_status ;;
logs) safe_service tail_logs ;;
list|ls) list_services ;;
version|-v|--version)
echo "Codjixd v$VERSION"
;;
help|-h|--help|"")
echo "Usage: $0 <command> [<service>]"
echo "Commands:"
echo " start <service> Start a service"
echo " stop <service> Stop a service"
echo " restart <service> Restart a service"
echo " kill <service> Kill a service and its dependencies"
echo " status <service> Show status and metrics for a service"
echo " logs <service> Tail logs for a service"
echo " list, ls List all available services"
echo " version Show version information"
echo " help Show this help message"
echo "Options:"
echo " -v, --version Show version information"
echo " -h, --help Show this help message"
;;
*)
echo "[x]: Invalid operation '$ACTION'."
echo "Usage: $0 <command> [<service>]"
echo " $0 -v, --version Show version information"
echo " $0 -h, --help Show full help message"
exit 1
;;
esac