-
Notifications
You must be signed in to change notification settings - Fork 0
FAQ: Vagrant: Setting Up and Managing Your Vagrant VM
Link to Vagrant
This guide provides a comprehensive solution for managing a remote Vagrant image with a configuration file held in a Git repository. It includes steps for downloading and provisioning the VM, resetting it to its original state, and automatically detecting changes in provisioning.
-
Remote Vagrant Image and Configuration:
- The Vagrant configuration file (
Vagrantfile
) and necessary provisioning scripts are stored in a Git repository. - When developers clone the repository, they get the
Vagrantfile
and provisioning scripts. - The
Vagrantfile
includes instructions to download and provision the VM usingapt-get
and other necessary configurations.
- The Vagrant configuration file (
-
Resetting the VM:
- Developers can reset the VM back to its original state by destroying and recreating it using a provided script.
-
Automatic Provisioning Detection:
- When developers pull the latest changes from the Git repository, the setup detects any changes in the provisioning scripts and prompts for a VM rebuild if needed.
<repository-root>
├── Vagrantfile
├── provision.sh
├── check_provisioning.sh
├── vm_reset.sh
└── vm_start.sh
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
# Run the provisioning check script before starting the VM
config.trigger.before :up do
run "bash check_provisioning.sh"
end
config.vm.provision "shell", path: "provision.sh"
config.vm.synced_folder ".", "/vagrant"
end
#!/bin/bash
# Update package lists
sudo apt-get update
# Install necessary packages
sudo apt-get install -y git curl vim
#!/bin/bash
# Path to the stored hash file
HASH_FILE=".provision_hash"
# Generate a new hash for the provisioning script
NEW_HASH=$(sha256sum provision.sh | awk '{ print $1 }')
# Check if the hash file exists
if [ -f "$HASH_FILE" ]; then
OLD_HASH=$(cat "$HASH_FILE")
if [ "$NEW_HASH" != "$OLD_HASH" ]; then
echo "Provisioning script has changed. Please run './vm_reset.sh' to rebuild the VM."
fi
fi
# Store the new hash
echo "$NEW_HASH" > "$HASH_FILE"
#!/bin/bash
# Destroy the current VM
vagrant destroy -f
# Recreate and provision the VM
vagrant up
#!/bin/bash
# Start the VM
vagrant up
First, clone the repository containing the Vagrant configuration and scripts:
git clone <repository-url>
cd <repository-directory>
To start the VM, simply run the provided vm_start.sh
script:
./vm_start.sh
This script will:
- Check for changes in the provisioning script using
check_provisioning.sh
. - Start the VM and apply the provisioning defined in
provision.sh
.
If you need to reset the VM to its original state, run the vm_reset.sh
script:
./vm_reset.sh
This script will:
- Destroy the current VM.
- Recreate and re-provision the VM from scratch.
To update your local repository with the latest changes from the remote repository, run:
git pull
Before the VM starts, the check_provisioning.sh
script will automatically run to detect any changes in the provisioning script. If changes are detected, you will see a message prompting you to reset the VM using ./reset.sh
.
- Vagrantfile: Defines the VM configuration and provisioning.
- provision.sh: Contains commands to install required packages and configurations.
- check_provisioning.sh: Checks if the provisioning script has changed and prompts for a VM reset if needed.
- vm_reset.sh: Destroys and recreates the VM.
- vm_start.sh: Starts the VM.