2025-08-03 11:54:46 +01:00
#!/usr/bin/env bash
# This script checks for Vagrant and VirtualBox prerequisites, ensures Vagrant VMs are running, and gathers network and system information from the VMs.
echo "Checking Vagrant prerequisites..."
# Check if Vagrant is installed
if ! command -v vagrant & > /dev/null; then
echo "Vagrant is not installed. Please install Vagrant to proceed."
exit 1
fi
2025-08-06 08:39:26 +01:00
# Check if running on Windows
if [ [ " $( uname -s) " = = *"MINGW" * || " $( uname -s) " = = *"CYGWIN" * || " $OSTYPE " = = "msys" ] ] ; then
echo "Detected Windows environment. Skipping VBoxManage check as Vagrant will handle VirtualBox interactions."
else
# Check if VirtualBox is installed (for non-Windows systems)
if ! command -v VBoxManage & > /dev/null; then
echo "VirtualBox is not installed. Please install VirtualBox to proceed."
exit 1
fi
2025-08-03 11:54:46 +01:00
fi
2025-08-07 19:08:46 +01:00
2025-08-03 11:54:46 +01:00
SCRIPT_DIR = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) "
echo " Script directory: $SCRIPT_DIR "
VAGRANT_DIR = " $SCRIPT_DIR /../vagrant/dev/ubuntu/ "
2025-08-04 21:44:28 +01:00
echo " Vagrant directory: $VAGRANT_DIR "
2025-08-03 11:54:46 +01:00
cd " $VAGRANT_DIR " || {
echo " Failed to change directory to Vagrant directory: $VAGRANT_DIR "
exit 1
}
# Check if any VMs are running; if not, run 'vagrant up'
if [ -z " $( vagrant status | grep 'running' ) " ] ; then
echo "No Vagrant VMs are running. Starting VMs with 'vagrant up'..."
vagrant up
fi
2025-08-07 19:08:46 +01:00
2025-08-03 11:54:46 +01:00
network_info = ( )
2025-08-04 21:44:28 +01:00
vagrant_ports = ( )
2025-08-03 11:54:46 +01:00
echo "Gathering network information from running VMs..."
running_vms = $( vagrant status | grep "running" | awk '{print $1}' )
for vm in $running_vms ; do
2025-08-07 10:22:11 +01:00
# Check network interfaces and get specific IPs
vm_ips = $( vagrant ssh " $vm " -c "ip -j addr" | jq -r ' .[ ] |
select ( .addr_info != null) |
.addr_info[ ] |
select ( .family = = "inet" and ( .local | startswith( "192.168.56.8" ) ) ) |
.local' )
# Save the VM's IP to the array if it matches our pattern
if [ ! -z " $vm_ips " ] ; then
network_info += ( " $vm : $vm_ips " )
fi
2025-08-03 11:54:46 +01:00
done
for vm in $running_vms ; do
echo $vm
echo "----------------------------------------"
vagrant ssh " $vm " -c "free" | grep 'Mem:' | awk '{print "Memory in bytes: " $2 " Memory in MB: " $2/1024/1024}'
vagrant ssh " $vm " -c "lscpu" | grep 'CPU(s):' | grep -v 'NUMA' | awk '{print "CPU count: " $2}'
echo "----------------------------------------"
done
echo "Network information gathered successfully."
2025-08-04 21:44:28 +01:00
# get vagrant ports
2025-08-07 10:22:11 +01:00
# echo "Gathering Vagrant port information..."
2025-08-04 21:44:28 +01:00
2025-08-06 08:39:26 +01:00
# Ensure unique ports are added to the vagrant_ports array
add_unique_port( ) {
local port = $1
for existing_port in " ${ vagrant_ports [@] } " ; do
if [ " $existing_port " = = " $port " ] ; then
return
fi
done
vagrant_ports += ( " $port " )
}
2025-08-05 13:52:33 +01:00
while read -r line; do
# Extract the port number
2025-08-07 10:22:11 +01:00
vagrant_ports += ( " $line " )
2025-08-05 13:52:33 +01:00
done < <( vagrant ssh-config | grep Port)
2025-08-04 21:44:28 +01:00
while read -r line; do
# Extract the port number
port = $( echo " $line " | awk '{print $2}' )
2025-08-06 08:39:26 +01:00
add_unique_port " $port "
2025-08-04 21:44:28 +01:00
done < <( vagrant ssh-config | grep Port)
2025-08-07 10:22:11 +01:00
ips = ( )
2025-08-03 11:54:46 +01:00
# Print network information
for info in " ${ network_info [@] } " ; do
echo "----------------------------------------"
echo -e " $info "
2025-08-07 10:22:11 +01:00
# vm2:192.168.56.81
ip_addr = $( echo " $info " | cut -d':' -f2)
ips += ( " $ip_addr " )
2025-08-03 11:54:46 +01:00
echo "----------------------------------------"
done
2025-08-04 21:44:28 +01:00
# Print Vagrant ports
echo "Creating Ansible inventory file..."
2025-08-07 19:08:46 +01:00
INVENTORY_FILE = "ansible/ansible_inventory.ini"
echo " Ansible inventory file: $INVENTORY_FILE "
2025-08-07 10:22:11 +01:00
echo "[all]" > " $INVENTORY_FILE "
2025-08-04 21:44:28 +01:00
i = 0
2025-08-07 10:22:11 +01:00
for info in " ${ network_info [@] } " ; do
port = "22"
vm = $( echo " $info " | cut -d':' -f1)
host_ip = $( echo " $info " | cut -d':' -f2)
2025-08-07 19:08:46 +01:00
echo " $vm ansible_host= $host_ip ansible_port= $port ansible_user=vagrant ansible_ssh_private_key_file=~/.ssh/ ${ vm } _key ansible_python_interpreter=/usr/bin/python3 " >> " $INVENTORY_FILE "
2025-08-04 21:44:28 +01:00
( ( i++) )
done
2025-08-07 10:22:11 +01:00
echo "" >> " $INVENTORY_FILE "
echo "[vms]" >> " $INVENTORY_FILE "
2025-08-04 21:44:28 +01:00
for vm in $running_vms ; do
2025-08-07 19:08:46 +01:00
if [ [ " $vm " = = *"vm" * ] ] ; then
echo $vm >> " $INVENTORY_FILE "
# vm=$(echo "$vm" | sed 's/vm//')
fi
2025-08-04 21:44:28 +01:00
done
2025-08-07 10:22:11 +01:00
echo " Ansible inventory file created at: $INVENTORY_FILE "
2025-08-04 21:44:28 +01:00