Skip to content

Commit c1028f1

Browse files
committed
refactor: consolidate SSH calls in delete and status scripts
- Streamlined the delete.sh script by consolidating multiple SSH calls into single calls for checking container and configuration existence, improving efficiency. - Updated status.sh to fetch all container data in one SSH call, reducing overhead and enhancing performance. - Enhanced data extraction using awk and jq for better readability and maintainability. - Improved output handling for container details and health checks, ensuring clearer status reporting.
1 parent 1b25de8 commit c1028f1

File tree

2 files changed

+319
-281
lines changed

2 files changed

+319
-281
lines changed

cmd/delete.sh

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,44 @@ echo -e " Shutdown Timeout: ${CYAN}${SHUTDOWN_TIMEOUT}s${NC}"
260260
echo ""
261261

262262
# Warning
263-
# Step 0: Check if environment exists
263+
# Step 0: Check if environment exists (consolidated check)
264264
echo -e "${BLUE}Checking if environment '${ENVIRONMENT}' exists...${NC}"
265265
echo ""
266266

267-
# Check for containers on Application Server
268-
CONTAINERS_EXIST=$(ssh -i "$APP_SSH_KEY" "${APP_USER}@${APP_SERVER}" \
269-
"docker ps -a --filter 'name=${CONTAINER_PATTERN}-' --format '{{.Names}}' 2>/dev/null || true")
267+
# Consolidated check via single SSH call to Application Server
268+
APP_CHECK=$(axon_ssh "app" -i "$APP_SSH_KEY" "${APP_USER}@${APP_SERVER}" bash <<EOF_CHECK
269+
# Check for containers
270+
CONTAINERS=\$(docker ps -a --filter "name=${CONTAINER_PATTERN}-" --format '{{.Names}}' 2>/dev/null || true)
271+
if [ -n "\$CONTAINERS" ]; then
272+
echo "===CONTAINERS==="
273+
echo "\$CONTAINERS"
274+
fi
275+
EOF_CHECK
276+
)
277+
278+
# Extract container list
279+
CONTAINERS_EXIST=$(echo "$APP_CHECK" | awk '/===CONTAINERS===/{flag=1; next} flag')
280+
281+
# Consolidated check via single SSH call to System Server
282+
SYS_CHECK=$(axon_ssh "sys" -i "$SYSTEM_SSH_KEY" "${SYSTEM_USER}@${SYSTEM_SERVER}" bash <<EOF_SYS_CHECK
283+
# Check if configs exist
284+
if [ -f "${NGINX_SITE_FILE}" ]; then
285+
echo "SITE_EXISTS=yes"
286+
else
287+
echo "SITE_EXISTS=no"
288+
fi
289+
290+
if [ -f "${NGINX_UPSTREAM_FILE}" ]; then
291+
echo "UPSTREAM_EXISTS=yes"
292+
else
293+
echo "UPSTREAM_EXISTS=no"
294+
fi
295+
EOF_SYS_CHECK
296+
)
270297

271-
# Check for nginx configs on System Server
272-
SITE_EXISTS=$(ssh -i "$SYSTEM_SSH_KEY" "${SYSTEM_USER}@${SYSTEM_SERVER}" \
273-
"[ -f '${NGINX_SITE_FILE}' ] && echo 'yes' || echo 'no'")
274-
UPSTREAM_EXISTS=$(ssh -i "$SYSTEM_SSH_KEY" "${SYSTEM_USER}@${SYSTEM_SERVER}" \
275-
"[ -f '${NGINX_UPSTREAM_FILE}' ] && echo 'yes' || echo 'no'")
298+
# Extract existence flags
299+
SITE_EXISTS=$(echo "$SYS_CHECK" | grep "SITE_EXISTS=" | cut -d'=' -f2)
300+
UPSTREAM_EXISTS=$(echo "$SYS_CHECK" | grep "UPSTREAM_EXISTS=" | cut -d'=' -f2)
276301

277302
# Check if anything exists
278303
if [ -z "$CONTAINERS_EXIST" ] && [ "$SITE_EXISTS" = "no" ] && [ "$UPSTREAM_EXISTS" = "no" ]; then
@@ -339,10 +364,10 @@ if [ "$FORCE" = false ]; then
339364
echo ""
340365
fi
341366

342-
# Step 1: Remove Docker containers from Application Server
343-
echo -e "${BLUE}Step 1/4: Removing Docker containers from Application Server...${NC}"
367+
# Steps 1-2: Remove Docker containers and images (consolidated single SSH call)
368+
echo -e "${BLUE}Steps 1-2/4: Removing Docker containers and images from Application Server...${NC}"
344369

345-
ssh -i "$APP_SSH_KEY" "${APP_USER}@${APP_SERVER}" "bash -s" <<EOF
370+
axon_ssh "app" -i "$APP_SSH_KEY" "${APP_USER}@${APP_SERVER}" bash <<EOF_APP_CLEANUP
346371
set -e
347372
348373
# Find all containers for this environment
@@ -365,20 +390,12 @@ else
365390
echo " ✓ Containers stopped gracefully"
366391
echo ""
367392
echo " Removing containers..."
368-
echo "\$CONTAINERS" | xargs -r docker rm > /dev/null 2>&1
393+
echo "\$CONTAINERS" | xargs docker rm > /dev/null 2>&1
369394
echo " ✓ Containers removed"
370395
fi
371-
EOF
372396
373-
echo -e " ${GREEN}✓ Containers cleaned up${NC}"
374397
echo ""
375398
376-
# Step 2: Remove Docker images from Application Server
377-
echo -e "${BLUE}Step 2/4: Removing Docker images from Application Server...${NC}"
378-
379-
ssh -i "$APP_SSH_KEY" "${APP_USER}@${APP_SERVER}" "bash -s" <<EOF
380-
set -e
381-
382399
# Find images matching the environment pattern
383400
# Images are typically tagged as: {product}:{env} or {registry}/{product}:{env}
384401
IMAGES=\$(docker images --filter "reference=*${PRODUCT_NAME}*:*${NORMALIZED_ENV}*" --format "{{.Repository}}:{{.Tag}}" 2>/dev/null || true)
@@ -390,38 +407,32 @@ else
390407
echo "\$IMAGES" | sed 's/^/ - /'
391408
echo ""
392409
echo " Removing images..."
393-
echo "\$IMAGES" | xargs -r docker rmi -f > /dev/null 2>&1
410+
echo "\$IMAGES" | xargs docker rmi -f > /dev/null 2>&1
394411
echo " ✓ Images removed"
395412
fi
396-
EOF
413+
EOF_APP_CLEANUP
397414

398-
echo -e " ${GREEN}Images cleaned up${NC}"
415+
echo -e " ${GREEN}Application Server cleanup completed${NC}"
399416
echo ""
400417

401-
# Step 3: Remove nginx site configuration from System Server
402-
echo -e "${BLUE}Step 3/4: Removing nginx site configuration...${NC}"
418+
# Steps 3-4: Remove nginx configurations and reload (consolidated single SSH call)
419+
echo -e "${BLUE}Steps 3-4/4: Removing nginx configurations and reloading...${NC}"
403420

404-
ssh -i "$SYSTEM_SSH_KEY" "${SYSTEM_USER}@${SYSTEM_SERVER}" "bash -s" <<EOF
421+
axon_ssh "sys" -i "$SYSTEM_SSH_KEY" "${SYSTEM_USER}@${SYSTEM_SERVER}" bash <<EOF_NGINX_CLEANUP
405422
set -e
406423
424+
# Remove site configuration
407425
if [ -f "${NGINX_SITE_FILE}" ]; then
408426
echo " Removing: ${NGINX_SITE_FILE}"
409427
${USE_SUDO} rm -f "${NGINX_SITE_FILE}"
410428
echo " ✓ Site config removed"
411429
else
412430
echo " Site config not found (already removed or never created)"
413431
fi
414-
EOF
415432
416-
echo -e " ${GREEN}✓ Site configuration cleaned up${NC}"
417433
echo ""
418434
419-
# Step 4: Remove nginx upstream configuration and reload nginx
420-
echo -e "${BLUE}Step 4/4: Removing nginx upstream configuration...${NC}"
421-
422-
ssh -i "$SYSTEM_SSH_KEY" "${SYSTEM_USER}@${SYSTEM_SERVER}" "bash -s" <<EOF
423-
set -e
424-
435+
# Remove upstream configuration
425436
if [ -f "${NGINX_UPSTREAM_FILE}" ]; then
426437
echo " Removing: ${NGINX_UPSTREAM_FILE}"
427438
${USE_SUDO} rm -f "${NGINX_UPSTREAM_FILE}"
@@ -439,13 +450,13 @@ if ${USE_SUDO} nginx -t 2>&1 | grep -q 'successful'; then
439450
${USE_SUDO} nginx -s reload
440451
echo " ✓ nginx reloaded"
441452
else
442-
echo " ${RED}✗ nginx configuration test failed${NC}"
453+
echo " ✗ nginx configuration test failed"
443454
echo " Please check nginx configuration manually"
444455
exit 1
445456
fi
446-
EOF
457+
EOF_NGINX_CLEANUP
447458

448-
echo -e " ${GREEN}Upstream configuration cleaned up${NC}"
459+
echo -e " ${GREEN}System Server cleanup completed${NC}"
449460
echo ""
450461

451462
# Success

0 commit comments

Comments
 (0)