-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·255 lines (221 loc) · 9.04 KB
/
deploy.sh
File metadata and controls
executable file
·255 lines (221 loc) · 9.04 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
#!/bin/bash
# Deployment script for Cachet infrastructure on Podman
set -e
echo "=========================================="
echo "Cachet Infrastructure Deployment"
echo "=========================================="
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo -e "${YELLOW}Warning: Running as root. Rootless podman is recommended for better security.${NC}"
echo -e "${YELLOW}Continuing anyway... Press CTRL+C to abort or Enter to continue.${NC}"
read
fi
# Check if .env exists
if [ ! -f .env ]; then
echo -e "${RED}Warning: .env file not found${NC}"
echo "Copy it from .env.example to .env"
exit 1
fi
# Check if middleware/config.json exists
if [ ! -f middleware/config.json ]; then
echo -e "${RED}Warning: middleware/config.json file not found${NC}"
echo "Copy it from middleware/config.json.example to middleware/config.json"
exit 1
fi
# Check if middleware/prometheus.yml exists
if [ ! -f middleware/prometheus.yml ]; then
echo -e "${RED}Warning: middleware/prometheus.yml file not found${NC}"
echo "Copy it from middleware/prometheus.yml.example to middleware/prometheus.yml"
exit 1
fi
# Load environment variables
source .env
# Check required environment variables
REQUIRED_VARS=("DB_PASSWORD")
MISSING_VARS=()
for var in "${REQUIRED_VARS[@]}"; do
if [ -z "${!var}" ] || [ "${!var}" == "YOUR_"* ]; then
MISSING_VARS+=("$var")
fi
done
# Check APP_KEY and CACHET_API_TOKEN but allow temporary values
if [ -z "${APP_KEY}" ] || [ "${APP_KEY}" == "YOUR_"* ]; then
echo -e "${YELLOW}Warning: APP_KEY not set. You'll need to generate it after deployment with: make key${NC}"
fi
if [ -z "${CACHET_API_TOKEN}" ] || [ "${CACHET_API_TOKEN}" == "YOUR_"* ]; then
echo -e "${YELLOW}Warning: CACHET_API_TOKEN not set. Generate it from Cachet dashboard after setup.${NC}"
fi
if [ ${#MISSING_VARS[@]} -ne 0 ]; then
echo -e "${RED}Error: Missing or invalid required environment variables:${NC}"
for var in "${MISSING_VARS[@]}"; do
echo -e " - ${YELLOW}$var${NC}"
done
echo ""
echo "Please edit .env file and set these variables"
exit 1
fi
# Check if podman-compose is available
if ! command -v podman-compose &> /dev/null; then
echo -e "${YELLOW}podman-compose not found. ${NC}"
exit 1
fi
# Generate APP_KEY if not present in podman-setup/.env
echo ""
echo "Checking Laravel APP_KEY in .env..."
APP_KEY_VALUE=$(grep "^APP_KEY=" .env 2>/dev/null | cut -d= -f2 | tr -d '"')
if [ -z "$APP_KEY_VALUE" ] || [ "$APP_KEY_VALUE" = "" ]; then
echo "Generating new Laravel APP_KEY..."
RANDOM_KEY=$(openssl rand -base64 32)
# Update podman-setup/.env (this will be passed to container as ENV var)
if grep -q "^APP_KEY=" .env; then
sed -i "s|^APP_KEY=.*|APP_KEY=\"base64:${RANDOM_KEY}\"|" .env
else
echo "APP_KEY=\"base64:${RANDOM_KEY}\"" >> .env
fi
echo -e "${GREEN}✓${NC} APP_KEY generated and saved to .env"
else
echo -e "${GREEN}✓${NC} APP_KEY already exists in .env"
fi
# Ensure .env exists (used for default values not overridden by ENV vars)
echo ""
echo "Checking .env file..."
if [ ! -f .env ]; then
echo -e "${RED}Warning: .env not found. Copy it from .env.example...${NC}"
exit 1
else
echo -e "${GREEN}✓${NC} .env exists"
fi
# Configure webhook authentication in Traefik middlewares
echo ""
echo "Configuring webhook authentication..."
if [ ! -f traefik/dynamic/middlewares.yml ]; then
echo -e "${YELLOW}Warning: traefik/dynamic/middlewares.yml not found. Skipping webhook configuration.${NC}"
else
if [ -n "${WEBHOOK_BASIC_AUTH}" ]; then
# Use the string specified in .env
sed -i "s|WEBHOOK_CREDENTIALS_PLACEHOLDER|${WEBHOOK_BASIC_AUTH}|g" traefik/dynamic/middlewares.yml
echo -e "${GREEN}✓${NC} Webhook authentication configured: ${WEBHOOK_BASIC_AUTH}"
else
# Generate default authentication admin:admin
if ! command -v htpasswd &> /dev/null; then
echo -e "${YELLOW}htpasswd not found. Installing apache2-utils...${NC}"
sudo apt-get install -y apache2-utils || {
echo -e "${RED}Error: Failed to install apache2-utils. Please install it manually.${NC}"
exit 1
}
fi
DEFAULT_AUTH=$(htpasswd -nb admin admin | cut -d':' -f2)
sed -i "s|WEBHOOK_CREDENTIALS_PLACEHOLDER|admin:${DEFAULT_AUTH}|g" traefik/dynamic/middlewares.yml
echo -e "${YELLOW}Warning: WEBHOOK_BASIC_AUTH not set in .env. Using default admin:admin.${NC}"
fi
fi
echo ""
echo "=========================================="
echo "Starting deployment..."
echo "=========================================="
echo ""
echo "Starting core services (excluding middleware)..."
echo -e "${YELLOW}Note: Middleware will be started later after API token configuration${NC}"
podman-compose up -d --no-deps traefik postgres cachet
echo ""
echo "Waiting for services to be ready..."
sleep 15
# Run the AdminSeeder to create admin user and relative token for APIs
echo "Running AdminSeeder to create admin user and API token..."
adminseeder_output=$(podman-compose exec -T cachet php artisan db:seed --class=AdminSeeder --force 2>&1)
echo "--- AdminSeeder output ---"
echo "$adminseeder_output"
echo "-------------------------"
token=$(echo "$adminseeder_output" | grep -oP 'Generated Token: \K.*')
# Update the .env file with the generated token
if [ -n "$token" ]; then
if grep -q "^CACHET_API_TOKEN=" .env; then
# Token exists, update it (use # as delimiter to avoid conflicts with | in token)
sed -i "s#^CACHET_API_TOKEN=.*#CACHET_API_TOKEN=\"${token}\"#g" .env
echo "API token updated in .env file."
else
# Token doesn't exist, append it
echo "CACHET_API_TOKEN=\"${token}\"" >> .env
echo "API token added to .env file."
fi
else
echo "${RED}Error: Token not generated by AdminSeeder. Check the output above for details.${NC}"
exit 1
fi
echo ""
echo "Clearing and optimizing cache..."
podman-compose exec -T cachet php artisan optimize:clear
podman-compose exec -T cachet php artisan optimize
# === Build and Start Middleware and Setup Components ===
echo ""
echo "=========================================="
echo "Starting Middleware, Initializing Components"
echo "=========================================="
# Start middleware container
echo "Starting middleware container..."
podman-compose up -d middleware
# Wait for middleware to become healthy
echo "Waiting for middleware to become healthy..."
max_attempts=12
attempt=0
while [ $attempt -lt $max_attempts ]; do
if podman ps --filter label=io.podman.compose.project=status --format "{{.Names}} {{.Status}}" | grep -q "cachet-middleware.*healthy"; then
echo -e "${GREEN}✓${NC} Middleware is healthy"
break
fi
attempt=$((attempt + 1))
echo " Attempt $attempt/$max_attempts..."
sleep 5
done
if [ $attempt -eq $max_attempts ]; then
echo -e "${YELLOW}⚠${NC} Middleware is running but healthcheck not confirmed"
echo "You can check status with: podman ps --filter label=io.podman.compose.project=status"
echo "If middleware keeps failing, check logs with: podman logs cachet-middleware"
fi
# Setup components (run setup-components.py) with user prompt
echo ""
echo "Initializing Cachet components from Prometheus configuration..."
if [ ! -f middleware/prometheus.yml ]; then
echo -e "${RED}❌${NC} Prometheus configuration file not found: middleware/prometheus.yml"
exit 1
fi
if [ ! -f middleware/config.json ]; then
echo -e "${RED}❌${NC} Middleware configuration file not found: middleware/config.json"
exit 1
fi
echo "Found configuration files:"
echo " - Prometheus targets: middleware/prometheus.yml"
echo " - Component groups: middleware/config.json"
target_count=$(grep -c "status_page_alert: true" middleware/prometheus.yml || echo "0")
echo "Found approximately ${target_count} targets with status_page_alert enabled"
echo ""
read -p "Do you want to run setup-components.py to create components? This will DELETE all existing components! (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Running setup-components.py inside middleware container..."
if podman exec cachet-middleware python3 /app/setup-components.py --file /app/prometheus.yml; then
echo -e "${GREEN}✓${NC} Components created successfully!"
else
echo -e "${RED}❌${NC} Failed to create components"
echo "You can run setup-components.py manually with:"
echo " podman exec cachet-middleware python3 /app/setup-components.py --file /app/prometheus.yml"
exit 1
fi
else
echo -e "${YELLOW}⚠${NC} Component setup skipped by user."
fi
# Final summary
echo ""
echo "=========================================="
echo "SETUP COMPLETE! 🎉"
echo "=========================================="
echo "Your Cachet status page is now available!"
echo " 📊 Status Page: ${APP_URL}"
echo " 🔧 Manager Login: ${APP_URL}/dashboard/login"
echo " 📡 Webhook: ${APP_URL}/webhook"
echo ""