-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpv-stack.sh
More file actions
executable file
·196 lines (172 loc) · 4.57 KB
/
pv-stack.sh
File metadata and controls
executable file
·196 lines (172 loc) · 4.57 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
#!/bin/bash
#
# pv-stack.sh - Management script for pv-stack Docker services
#
set -e
# Configuration
COMPOSE_FILE="docker-compose.pv-stack.yml"
PROJECT_NAME="docker-setup"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Change to script directory
cd "$SCRIPT_DIR"
# Check if compose file exists
if [[ ! -f "$COMPOSE_FILE" ]]; then
echo -e "${RED}Error: $COMPOSE_FILE not found in $SCRIPT_DIR${NC}"
exit 1
fi
# Helper function for docker compose commands
dc() {
docker compose -p "$PROJECT_NAME" -f "$COMPOSE_FILE" "$@"
}
# Get list of services from compose file
get_services() {
dc config --services 2>/dev/null | sort
}
# Validate service name
validate_service() {
local service="$1"
if [[ -z "$service" ]]; then
echo -e "${RED}Error: Service name required${NC}"
echo "Available services:"
get_services | sed 's/^/ - /'
exit 1
fi
if ! get_services | grep -q "^${service}$"; then
echo -e "${RED}Error: Unknown service '$service'${NC}"
echo "Available services:"
get_services | sed 's/^/ - /'
exit 1
fi
}
# Commands
cmd_restart() {
local service="$1"
validate_service "$service"
echo -e "${BLUE}Restarting $service...${NC}"
dc restart "$service"
echo -e "${GREEN}Done.${NC}"
}
cmd_recreate() {
local service="$1"
validate_service "$service"
echo -e "${YELLOW}Recreating $service (this will apply device/port/volume changes)...${NC}"
dc up -d "$service" --force-recreate
echo -e "${GREEN}Done.${NC}"
}
cmd_logs() {
local service="$1"
local lines="${2:-50}"
validate_service "$service"
dc logs -f --tail "$lines" "$service"
}
cmd_status() {
echo -e "${BLUE}PV-Stack Service Status${NC}"
echo "========================"
dc ps --format "table {{.Name}}\t{{.Status}}\t{{.Ports}}"
}
cmd_list() {
echo -e "${BLUE}Available services:${NC}"
get_services | sed 's/^/ - /'
}
cmd_up() {
local service="$1"
if [[ -n "$service" ]]; then
validate_service "$service"
echo -e "${GREEN}Starting $service...${NC}"
dc up -d "$service"
else
echo -e "${GREEN}Starting all services...${NC}"
dc up -d
fi
echo -e "${GREEN}Done.${NC}"
}
cmd_down() {
local service="$1"
if [[ -n "$service" ]]; then
validate_service "$service"
echo -e "${YELLOW}Stopping $service...${NC}"
dc stop "$service"
else
echo -e "${YELLOW}Stopping all services...${NC}"
dc down
fi
echo -e "${GREEN}Done.${NC}"
}
cmd_pull() {
local service="$1"
if [[ -n "$service" ]]; then
validate_service "$service"
echo -e "${BLUE}Pulling latest image for $service...${NC}"
dc pull "$service"
else
echo -e "${BLUE}Pulling latest images for all services...${NC}"
dc pull
fi
echo -e "${GREEN}Done.${NC}"
}
# Usage
usage() {
cat << EOF
${BLUE}pv-stack.sh${NC} - Management script for pv-stack Docker services
${YELLOW}Usage:${NC}
./pv-stack.sh <command> [service] [options]
${YELLOW}Commands:${NC}
${GREEN}restart${NC} <service> Restart a service (for environment variable changes)
${GREEN}recreate${NC} <service> Recreate a service (for device/port/volume changes)
${GREEN}logs${NC} <service> [n] Show logs (last n lines, default 50, follows)
${GREEN}status${NC} Show status of all services
${GREEN}list${NC} List available services
${GREEN}up${NC} [service] Start service(s)
${GREEN}down${NC} [service] Stop service(s)
${GREEN}pull${NC} [service] Pull latest image(s)
${YELLOW}When to use restart vs recreate:${NC}
${BLUE}restart${NC} - Environment variables (MQTT_SERVER, LOG_LEVEL, etc.)
${BLUE}recreate${NC} - Devices, ports, volumes, image changes
${YELLOW}Examples:${NC}
./pv-stack.sh restart seplos-modbus-mqtt
./pv-stack.sh recreate seplos-modbus-mqtt
./pv-stack.sh logs grafana 100
./pv-stack.sh status
EOF
}
# Main
case "${1:-}" in
restart)
cmd_restart "$2"
;;
recreate)
cmd_recreate "$2"
;;
logs)
cmd_logs "$2" "$3"
;;
status)
cmd_status
;;
list)
cmd_list
;;
up)
cmd_up "$2"
;;
down)
cmd_down "$2"
;;
pull)
cmd_pull "$2"
;;
-h|--help|help|"")
usage
;;
*)
echo -e "${RED}Unknown command: $1${NC}"
usage
exit 1
;;
esac