Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
223 lines (204 loc) · 6.18 KB
/
Copy pathrun.sh
File metadata and controls
223 lines (204 loc) · 6.18 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
#!/bin/bash
set -e
echo "========================================="
echo "PyUnit 2.0 - Install/Update Script"
echo "========================================="
# Detect Docker image tag based on git branch
if [ -d ".git" ]; then
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "main")
case "$BRANCH" in
main)
DOCKER_TAG="latest"
;;
dev)
DOCKER_TAG="dev"
;;
*)
DOCKER_TAG="$BRANCH"
;;
esac
else
DOCKER_TAG="latest"
fi
DOCKER_IMAGE="xatang/pyunit:${DOCKER_TAG}"
echo "Image: ${DOCKER_IMAGE}"
echo ""
# Detect OS
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
echo "OS: $OS"
else
echo "Cannot detect OS"
exit 1
fi
# Check/Install Docker
echo ""
echo "Checking Docker..."
if command -v docker &> /dev/null; then
echo "Docker: OK"
else
echo "Installing Docker..."
case "$OS" in
ubuntu|debian|raspbian)
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/$OS/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/$OS $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
;;
*)
echo "Unsupported OS"
exit 1
;;
esac
fi
if ! groups | grep -q docker; then
sudo usermod -aG docker $USER
fi
# Check/Install Python dependencies for idryer_api.py
echo ""
echo "Checking Python dependencies..."
if command -v python3 &> /dev/null; then
echo "Python3: OK"
# Check if requests module is installed
if ! python3 -c "import requests" 2>/dev/null; then
echo "Installing python3-requests..."
case "$OS" in
ubuntu|debian|raspbian)
sudo apt-get update
sudo apt-get install -y python3-requests
;;
*)
echo "Warning: Could not install python3-requests automatically"
echo "Please install manually: pip3 install requests"
;;
esac
else
echo "python3-requests: OK"
fi
else
echo "Warning: Python3 not found. Installing..."
case "$OS" in
ubuntu|debian|raspbian)
sudo apt-get update
sudo apt-get install -y python3 python3-requests
;;
*)
echo "Warning: Could not install Python3 automatically"
;;
esac
fi
# Detect Docker Compose command
echo ""
if docker compose version &> /dev/null; then
DC="docker compose"
else
DC="docker-compose"
fi
echo "Docker Compose: $DC"
# Auto-detect IP
echo ""
IP=$(hostname -I | awk '{print $1}')
[ -z "$IP" ] && IP="localhost"
echo "Detected IP: $IP"
# Backup local config files if they exist
echo ""
echo "Backing up local configurations..."
if [ -f ".env" ]; then
cp .env .env.backup
echo "✓ Backed up .env"
fi
# Update from git (if in git repo)
echo ""
if [ -d ".git" ]; then
echo "Updating from repository..."
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "main")
git pull origin $CURRENT_BRANCH || echo "Pull failed, continuing..."
echo "Repository updated from branch: $CURRENT_BRANCH"
else
echo "Not a git repository, skipping update"
fi
# Create required files for Docker volume mounts
echo ""
echo "Initializing required files..."
touch pyunit.db
touch pyunit.db-wal
touch pyunit.db-shm
touch app.log
touch dryer.log
echo "✓ Files initialized"
# Update .env with detected IP
echo ""
if [ -f ".env.backup" ]; then
echo "Restoring .env from backup..."
# Extract old IP if exists
OLD_IP=$(grep "API_URL=" .env.backup | sed 's/.*http:\/\/\([^:]*\):.*/\1/' || echo "")
if [ -n "$OLD_IP" ] && [ "$OLD_IP" != "localhost" ]; then
IP=$OLD_IP
echo "Using previous IP: $IP"
fi
mv .env.backup .env
fi
if [ -f ".env" ]; then
echo "Updating configuration in .env..."
# Extract PORT from .env
PORT=$(grep "^PORT=" .env | cut -d'=' -f2)
[ -z "$PORT" ] && PORT="5000"
# Update DOCKER_IMAGE
sed -i "s|DOCKER_IMAGE=.*|DOCKER_IMAGE=${DOCKER_IMAGE}|" .env
# Update IPs in .env
sed -i "s|API_URL=.*|API_URL=http://${IP}:${PORT}/api|" .env
sed -i "s|WS_URL=.*|WS_URL=ws://${IP}:${PORT}/api|" .env
echo "✓ .env updated with:"
echo " - Docker image: ${DOCKER_IMAGE}"
echo " - IP: $IP"
echo " - PORT: $PORT"
else
echo "Error: .env file not found!"
echo "Please restore from git or create manually"
exit 1
fi
# Start PyUnit
echo ""
echo "Starting PyUnit..."
sudo $DC down 2>/dev/null || true
sudo $DC pull
# Clean up old unused images to free disk space
echo ""
echo "Cleaning up old Docker images..."
DANGLING_IMAGES=$(sudo docker images -f "dangling=true" -q)
if [ -n "$DANGLING_IMAGES" ]; then
sudo docker rmi $DANGLING_IMAGES 2>/dev/null || true
echo "✓ Removed dangling images"
else
echo "✓ No dangling images to clean"
fi
# Optionally clean old pyunit images (keep current tag only)
OLD_PYUNIT_IMAGES=$(sudo docker images xatang/pyunit --format "{{.Repository}}:{{.Tag}}" | grep -v "${DOCKER_TAG}" || true)
if [ -n "$OLD_PYUNIT_IMAGES" ]; then
echo "Found old PyUnit images:"
echo "$OLD_PYUNIT_IMAGES"
read -p "Remove old PyUnit images? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "$OLD_PYUNIT_IMAGES" | xargs -r sudo docker rmi 2>/dev/null || true
echo "✓ Removed old PyUnit images"
else
echo "✓ Keeping old images"
fi
fi
sudo $DC up -d
echo ""
echo "========================================="
echo "Done! Access: http://${IP}:${PORT}"
echo "========================================="
echo ""
echo "Useful commands:"
echo " sudo $DC logs -f # View logs"
echo " sudo $DC restart # Restart"
echo " sudo $DC down # Stop"
echo " sudo $DC ps # Status"
echo "========================================="