Add group annotations for container, database, and volume management operations

This commit is contained in:
sHa
2025-09-27 00:44:58 +03:00
parent 4817ca3d35
commit 80cefd02dd
7 changed files with 258 additions and 66 deletions

View File

@@ -1,6 +1,7 @@
# Universal container management operations
# Start service (or all services if no service specified)
[group: 'container']
start service="" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -26,6 +27,7 @@ start service="" compose-file="":
fi
# Stop service (or all services if no service specified)
[group: 'container']
stop service="" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -51,6 +53,7 @@ stop service="" compose-file="":
fi
# Restart service (or all services if no service specified)
[group: 'container']
restart service="" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -75,6 +78,7 @@ restart service="" compose-file="":
fi
# Show service status (specific service or all)
[group: 'container']
status service="" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -98,6 +102,7 @@ status service="" compose-file="":
fi
# View logs for specific service or all services
[group: 'container']
logs service="" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -121,6 +126,7 @@ logs service="" compose-file="":
fi
# Open shell in specific container
[group: 'container']
shell service compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -139,6 +145,7 @@ shell service compose-file="":
$compose_cmd $file_arg exec "$service" /bin/bash
# Execute command in specific service container
[group: 'container']
exec service cmd compose-file="":
#!/usr/bin/env bash
set -euo pipefail

View File

@@ -40,6 +40,7 @@ _detect_compose:
fi
# Environment detection and validation
[group: 'environment']
env-check:
#!/usr/bin/env bash
set -euo pipefail

View File

@@ -1,6 +1,7 @@
# Universal image operations - works for any project
# Build any project's container image
[group: 'images']
image-build project *args="":
#!/usr/bin/env bash
set -euo pipefail
@@ -57,6 +58,7 @@ image-build project *args="":
echo -e "${GREEN}✓ Local tags: $project:$tag, $project:latest${NC}"
# Push any project's image to registry
[group: 'images']
image-push project tag=DEFAULT_TAG:
#!/usr/bin/env bash
set -euo pipefail
@@ -131,6 +133,7 @@ image-push project tag=DEFAULT_TAG:
fi
# Pull any project's image from registry
[group: 'images']
image-pull project tag=DEFAULT_TAG:
#!/usr/bin/env bash
set -euo pipefail
@@ -283,6 +286,7 @@ image-info project tag="latest":
fi
# List all project images
[group: 'images']
images-list:
#!/usr/bin/env bash
set -euo pipefail

View File

@@ -244,3 +244,88 @@ mysql-shell service="mysql" compose-file="":
else
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD}"
fi
# Restore MySQL database from backup file
[group: 'database']
mysql-restore backup_file database service="mysql" compose-file="" backup_path="./backups":
#!/usr/bin/env bash
set -euo pipefail
backup_file="{{backup_file}}"
database="{{database}}"
service="{{service}}"
compose_file="{{compose-file}}"
backup_path="{{backup_path}}"
if [ -z "$backup_file" ] || [ -z "$database" ]; then
echo "Error: Backup file and database name are required" >&2
echo "Usage: just mysql-restore backup_file.sql database_name" >&2
exit 1
fi
# Build compose file argument
file_arg=""
if [ -n "$compose_file" ]; then
file_arg="$compose_file"
fi
# Check if backup file exists
if [ ! -f "$backup_path/$backup_file" ]; then
echo -e "{{RED}}Error: Backup file '$backup_path/$backup_file' not found{{NC}}" >&2
echo -e "{{YELLOW}}Available backups:{{NC}}"
ls -la "$backup_path/" 2>/dev/null || echo "No backups directory found at $backup_path"
exit 1
fi
# Display backup file info
echo -e "{{BLUE}}Backup file information:{{NC}}"
echo -e "{{YELLOW}}File:{{NC}} $backup_path/$backup_file"
echo -e "{{YELLOW}}Size:{{NC}} $(du -h "$backup_path/$backup_file" | cut -f1)"
echo -e "{{YELLOW}}Modified:{{NC}} $(stat -c %y "$backup_path/$backup_file" 2>/dev/null || stat -f %Sm "$backup_path/$backup_file")"
echo ""
# Detect backup type
if [[ "$backup_file" == *.gz ]]; then
backup_type="gzipped SQL"
echo -e "{{BLUE}}Backup type:{{NC}} $backup_type"
else
backup_type="plain SQL"
echo -e "{{BLUE}}Backup type:{{NC}} $backup_type"
fi
echo -e "{{BLUE}}Target database:{{NC}} $database"
echo -e "{{BLUE}}Target service:{{NC}} $service"
echo ""
echo -e "{{RED}}⚠️ WARNING: This will OVERWRITE the current database!{{NC}}"
echo -e "{{YELLOW}}This action will replace all data in '$database' database{{NC}}"
echo -e "{{YELLOW}}Make sure you have a backup of current data if needed{{NC}}"
echo ""
read -p "Are you sure you want to restore '$database' from '$backup_file'? Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
echo "Restore cancelled"
exit 1
fi
echo -e "{{BLUE}}Restoring database '$database' from $backup_file...{{NC}}"
# Restore database from backup file
if [[ "$backup_file" == *.gz ]]; then
# For gzipped files
if [ -n "$file_arg" ]; then
just exec "$service" "gunzip -c /backups/$backup_file | mysql -u root -p\${MYSQL_ROOT_PASSWORD} $database" "$file_arg"
else
just exec "$service" "gunzip -c /backups/$backup_file | mysql -u root -p\${MYSQL_ROOT_PASSWORD} $database"
fi
else
# For plain SQL files
if [ -n "$file_arg" ]; then
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} $database < /backups/$backup_file" "$file_arg"
else
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} $database < /backups/$backup_file"
fi
fi
echo -e "{{GREEN}}✓ Database '$database' restored successfully from $backup_file{{NC}}"

View File

@@ -1,6 +1,7 @@
# Universal PostgreSQL database operations
# Execute PostgreSQL SQL query
[group: 'database']
postgres-sql query service="postgres" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -31,6 +32,7 @@ postgres-sql query service="postgres" compose-file="":
fi
# Check PostgreSQL connection and status
[group: 'database']
postgres-check service="postgres" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -55,6 +57,7 @@ postgres-check service="postgres" compose-file="":
fi
# List PostgreSQL databases
[group: 'database']
postgres-list-databases service="postgres" compose-file="":
#!/usr/bin/env bash
set -euo pipefail
@@ -184,3 +187,86 @@ postgres-shell service="postgres" compose-file="":
else
just exec "$service" "psql -U postgres"
fi
# Restore PostgreSQL database from backup file
[group: 'database']
postgres-restore backup_file service="postgres" compose-file="" backup_path="./backups":
#!/usr/bin/env bash
set -euo pipefail
backup_file="{{backup_file}}"
service="{{service}}"
compose_file="{{compose-file}}"
backup_path="{{backup_path}}"
if [ -z "$backup_file" ]; then
echo "Error: Backup file name is required" >&2
echo "Usage: just postgres-restore backup_file.sql" >&2
exit 1
fi
# Build compose file argument
file_arg=""
if [ -n "$compose_file" ]; then
file_arg="$compose_file"
fi
# Check if backup file exists
if [ ! -f "$backup_path/$backup_file" ]; then
echo -e "{{RED}}Error: Backup file '$backup_path/$backup_file' not found{{NC}}" >&2
echo -e "{{YELLOW}}Available backups:{{NC}}"
ls -la "$backup_path/" 2>/dev/null || echo "No backups directory found at $backup_path"
exit 1
fi
# Display backup file info
echo -e "{{BLUE}}Backup file information:{{NC}}"
echo -e "{{YELLOW}}File:{{NC}} $backup_path/$backup_file"
echo -e "{{YELLOW}}Size:{{NC}} $(du -h "$backup_path/$backup_file" | cut -f1)"
echo -e "{{YELLOW}}Modified:{{NC}} $(stat -c %y "$backup_path/$backup_file" 2>/dev/null || stat -f %Sm "$backup_path/$backup_file")"
echo ""
# Detect backup type
if [[ "$backup_file" == *.gz ]]; then
backup_type="gzipped SQL"
echo -e "{{BLUE}}Backup type:{{NC}} $backup_type"
else
backup_type="plain SQL"
echo -e "{{BLUE}}Backup type:{{NC}} $backup_type"
fi
echo -e "{{BLUE}}Target service:{{NC}} $service"
echo ""
echo -e "{{RED}}⚠️ WARNING: This will OVERWRITE the current database!{{NC}}"
echo -e "{{YELLOW}}This action will replace all data in the target database{{NC}}"
echo -e "{{YELLOW}}Make sure you have a backup of current data if needed{{NC}}"
echo ""
read -p "Are you sure you want to restore from '$backup_file'? Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
echo "Restore cancelled"
exit 1
fi
echo -e "{{BLUE}}Restoring database from $backup_file...{{NC}}"
# Copy backup file to container and restore
if [[ "$backup_file" == *.gz ]]; then
# For gzipped files
if [ -n "$file_arg" ]; then
just exec "$service" "gunzip -c /backups/$backup_file | psql -U postgres" "$file_arg"
else
just exec "$service" "gunzip -c /backups/$backup_file | psql -U postgres"
fi
else
# For plain SQL files
if [ -n "$file_arg" ]; then
just exec "$service" "psql -U postgres < /backups/$backup_file" "$file_arg"
else
just exec "$service" "psql -U postgres < /backups/$backup_file"
fi
fi
echo -e "{{GREEN}}✓ Database restored successfully from $backup_file{{NC}}"

View File

@@ -1,6 +1,7 @@
# Registry operations
# Login to container registry
[group: 'registry']
registry-login:
#!/usr/bin/env bash
set -euo pipefail
@@ -48,6 +49,7 @@ registry-login:
fi
# Logout from container registry
[group: 'registry']
registry-logout:
#!/usr/bin/env bash
set -euo pipefail
@@ -67,6 +69,7 @@ registry-logout:
# Check authentication and registry status
[group: 'registry']
registry-check:
#!/usr/bin/env bash
set -euo pipefail

View File

@@ -1,24 +1,28 @@
# Universal volume management operations
# Clean all volumes (DESTRUCTIVE!)
# Clean all volumes used by compose file (DESTRUCTIVE!)
[group: 'volumes']
volumes-clean-all project_prefix compose-file="compose.yml":
volumes-clean-all compose-file="":
#!/usr/bin/env bash
set -euo pipefail
project_prefix="{{project_prefix}}"
compose_file="{{compose-file}}"
if [ -z "$project_prefix" ]; then
echo "Error: Project prefix is required" >&2
echo "Usage: just volumes-clean-all myproject" >&2
exit 1
fi
compose_cmd=$(just _detect_compose)
# Build compose file argument
file_arg=""
if [ -n "$compose_file" ]; then
file_arg="-f $compose_file"
fi
echo -e "{{RED}}⚠️ WARNING: This will DELETE ALL DATA!{{NC}}"
echo -e "{{YELLOW}}This will remove all volumes: postgres_data, postgres_lib, servapp_data, servapp_logs{{NC}}"
echo -e "{{YELLOW}}This will remove all volumes defined in the compose file{{NC}}"
# Show which volumes would be removed
echo -e "{{BLUE}}Volumes that will be removed:{{NC}}"
$compose_cmd $file_arg config --volumes 2>/dev/null || echo "Cannot list volumes from compose file"
echo ""
read -p "Are you sure? Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
@@ -27,31 +31,37 @@ volumes-clean-all project_prefix compose-file="compose.yml":
fi
echo -e "{{BLUE}}Stopping services...{{NC}}"
$compose_cmd -f "$compose_file" down
$compose_cmd $file_arg down
echo -e "{{BLUE}}Removing volumes...{{NC}}"
$compose_cmd -f "$compose_file" down -v
$compose_cmd $file_arg down -v
echo -e "{{GREEN}}✓ All volumes removed{{NC}}"
# Clean only PostgreSQL data volume (DESTRUCTIVE!)
# Remove specific volume by name (DESTRUCTIVE!)
[group: 'volumes']
volumes-clean-postgres project_prefix:
volumes-remove volume_name:
#!/usr/bin/env bash
set -euo pipefail
project_prefix="{{project_prefix}}"
volume_name="{{volume_name}}"
if [ -z "$project_prefix" ]; then
echo "Error: Project prefix is required" >&2
echo "Usage: just volumes-clean-postgres myproject" >&2
if [ -z "$volume_name" ]; then
echo "Error: Volume name is required" >&2
echo "Usage: just volumes-remove my_volume_name" >&2
exit 1
fi
runtime=$(just _detect_runtime)
echo -e "{{RED}}⚠️ WARNING: This will DELETE PostgreSQL DATA!{{NC}}"
echo -e "{{YELLOW}}This will remove postgres_data and postgres_lib volumes{{NC}}"
# Check if volume exists
if ! $runtime volume ls --format "table {{.Name}}" | grep -q "^${volume_name}$"; then
echo -e "{{YELLOW}}Volume '$volume_name' does not exist{{NC}}"
exit 0
fi
echo -e "{{RED}}⚠️ WARNING: This will DELETE VOLUME DATA!{{NC}}"
echo -e "{{YELLOW}}This will remove volume: $volume_name{{NC}}"
read -p "Are you sure? Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
@@ -59,41 +69,39 @@ volumes-clean-postgres project_prefix:
exit 1
fi
echo -e "{{BLUE}}Stopping PostgreSQL service...{{NC}}"
just stop
echo -e "{{BLUE}}Removing volume: $volume_name{{NC}}"
$runtime volume rm "$volume_name" 2>/dev/null || true
echo -e "{{BLUE}}Removing PostgreSQL volumes...{{NC}}"
$runtime volume rm servass_${project_prefix}_postgres_data servass_${project_prefix}_postgres_lib 2>/dev/null || true
echo -e "{{GREEN}}✓ Volume '$volume_name' removed{{NC}}"
echo -e "{{GREEN}}✓ PostgreSQL volumes removed{{NC}}"
echo -e "{{YELLOW}}Run 'just start' to create fresh PostgreSQL instance{{NC}}"
# Clean only ServApp data volume (DESTRUCTIVE!)
# Remove volumes matching pattern (DESTRUCTIVE!)
[group: 'volumes']
volumes-clean-servapp project_prefix app_container compose-file="compose.yml":
volumes-remove-pattern pattern:
#!/usr/bin/env bash
set -euo pipefail
project_prefix="{{project_prefix}}"
app_container="{{app_container}}"
compose_file="{{compose-file}}"
pattern="{{pattern}}"
if [ -z "$project_prefix" ]; then
echo "Error: Project prefix is required" >&2
echo "Usage: just volumes-clean-servapp myproject myapp" >&2
exit 1
fi
if [ -z "$app_container" ]; then
echo "Error: App container name is required" >&2
echo "Usage: just volumes-clean-servapp myproject myapp" >&2
if [ -z "$pattern" ]; then
echo "Error: Volume pattern is required" >&2
echo "Usage: just volumes-remove-pattern 'myproject_*'" >&2
exit 1
fi
runtime=$(just _detect_runtime)
echo -e "{{RED}}⚠️ WARNING: This will DELETE ServApp DATA!{{NC}}"
echo -e "{{YELLOW}}This will remove servapp_data and servapp_logs volumes{{NC}}"
# Find matching volumes
matching_volumes=$($runtime volume ls --format "table {{.Name}}" | grep "$pattern" || true)
if [ -z "$matching_volumes" ]; then
echo -e "{{YELLOW}}No volumes found matching pattern: $pattern{{NC}}"
exit 0
fi
echo -e "{{RED}}⚠️ WARNING: This will DELETE VOLUME DATA!{{NC}}"
echo -e "{{YELLOW}}Volumes matching pattern '$pattern':{{NC}}"
echo "$matching_volumes"
echo ""
read -p "Are you sure? Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
@@ -101,31 +109,29 @@ volumes-clean-servapp project_prefix app_container compose-file="compose.yml":
exit 1
fi
echo -e "{{BLUE}}Stopping ServApp service...{{NC}}"
compose_cmd=$(just _detect_compose)
$compose_cmd -f "$compose_file" stop "$app_container"
echo -e "{{BLUE}}Removing matching volumes...{{NC}}"
echo "$matching_volumes" | while IFS= read -r volume; do
if [ -n "$volume" ]; then
echo "Removing: $volume"
$runtime volume rm "$volume" 2>/dev/null || true
fi
done
echo -e "{{BLUE}}Removing ServApp volumes...{{NC}}"
$runtime volume rm servass_${project_prefix}_servapp_data servass_${project_prefix}_servapp_logs 2>/dev/null || true
echo -e "{{GREEN}}✓ Matching volumes removed{{NC}}"
echo -e "{{GREEN}}✓ ServApp volumes removed{{NC}}"
echo -e "{{YELLOW}}Run 'just start' to recreate ServApp with fresh data{{NC}}"
# List project volumes
# List all volumes or filter by pattern
[group: 'volumes']
volumes-list project_prefix:
volumes-list pattern="":
#!/usr/bin/env bash
set -euo pipefail
project_prefix="{{project_prefix}}"
if [ -z "$project_prefix" ]; then
echo "Error: Project prefix is required" >&2
echo "Usage: just volumes-list myproject" >&2
exit 1
fi
pattern="{{pattern}}"
runtime=$(just _detect_runtime)
echo -e "{{BLUE}}Project volumes:{{NC}}"
$runtime volume ls | grep "servass_${project_prefix}" || echo "No servass_${project_prefix} volumes found"
if [ -n "$pattern" ]; then
echo -e "{{BLUE}}Volumes matching pattern '$pattern':{{NC}}"
$runtime volume ls | grep "$pattern" || echo "No volumes found matching pattern: $pattern"
else
echo -e "{{BLUE}}All volumes:{{NC}}"
$runtime volume ls
fi