Files
vagrant-docker-swarm/manage.sh
2025-10-10 17:47:09 +01:00

165 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
# Bare Bones Vagrant Management Script
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_header() {
echo -e "${BLUE}=== $1 ===${NC}"
}
# Function to start all machines
start_all() {
print_header "Starting All Machines"
vagrant up
print_status "All machines started successfully!"
}
# Function to stop all machines
stop_all() {
print_header "Stopping All Machines"
vagrant halt
print_status "All machines stopped successfully!"
}
# Function to destroy all machines
destroy_all() {
print_header "Destroying All Machines"
print_warning "This will permanently delete all machines!"
read -p "Are you sure? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
vagrant destroy -f
print_status "All machines destroyed successfully!"
else
print_status "Operation cancelled."
fi
}
# Function to show status
show_status() {
print_header "Machine Status"
vagrant status
}
# Function to access a specific machine
access_machine() {
local machine=${1:-host}
print_header "Accessing $machine"
vagrant ssh "$machine"
}
# Function to run Ansible commands
run_ansible() {
local command=${1:-ping}
print_header "Running Ansible $command"
case "$command" in
ping)
ansible all -i inventory -m ping
;;
setup)
ansible-playbook -i inventory setup-playbook.yml
;;
deploy)
ansible-playbook -i inventory deploy-playbook.yml
;;
list)
ansible all -i inventory --list-hosts
;;
facts)
ansible all -i inventory -m setup
;;
*)
print_error "Unknown Ansible command: $command"
print_status "Available commands: ping, setup, deploy, list, facts"
;;
esac
}
# Function to show help
show_help() {
echo "Bare Bones Vagrant Management Script"
echo ""
echo "Usage: $0 [COMMAND] [MACHINE]"
echo ""
echo "Commands:"
echo " start Start all machines"
echo " stop Stop all machines"
echo " destroy Destroy all machines (permanent)"
echo " status Show machine status"
echo " ssh MACHINE Access machine via SSH"
echo " ansible COMMAND Run Ansible command (ping, play, docker, list, facts)"
echo " help Show this help message"
echo ""
echo "Machines:"
echo " host Host machine (192.168.56.1)"
echo " machine1 Machine 1 (192.168.56.10)"
echo " machine2 Machine 2 (192.168.56.11)"
echo " machine3 Machine 3 (192.168.56.12)"
echo " machine4 Machine 4 (192.168.56.13)"
echo ""
echo "Ansible Commands:"
echo " ping Test connectivity to all hosts"
echo " setup Install dependencies (Python, tools, swap)"
echo " deploy Deploy applications and services"
echo " list List all hosts"
echo " facts Gather system facts"
echo ""
echo "Examples:"
echo " $0 start # Start all machines"
echo " $0 ssh host # Access host machine"
echo " $0 ansible ping # Test Ansible connectivity"
echo " $0 ansible setup # Install dependencies"
echo " $0 ansible deploy # Deploy applications"
}
# Main script logic
case "${1:-help}" in
start)
start_all
;;
stop)
stop_all
;;
destroy)
destroy_all
;;
status)
show_status
;;
ssh)
access_machine "$2"
;;
ansible)
run_ansible "$2"
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $1"
show_help
exit 1
;;
esac