FAQ: Vagrant: Setting Up and Managing Your Vagrant VM - rdkcentral/ut-core GitHub Wiki
Setting Up and Managing Your Vagrant VM
Link to Vagrant
Overview
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.
Solution
-
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
Vagrantfileand provisioning scripts. - The
Vagrantfileincludes instructions to download and provision the VM usingapt-getand 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.
Git Repository Structure
<repository-root>
├── Vagrantfile
├── provision.sh
├── check_provisioning.sh
├── vm_reset.sh
└── vm_start.sh
Scripts and Configuration
Vagrantfile
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
provision.sh
#!/bin/bash
# Update package lists
sudo apt-get update
# Install necessary packages
sudo apt-get install -y git curl vim
check_provisioning.sh
#!/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"
vm_reset.sh
#!/bin/bash
# Destroy the current VM
vagrant destroy -f
# Recreate and provision the VM
vagrant up
vm_start.sh
#!/bin/bash
# Start the VM
vagrant up
Instructions for Manual
Setting Up and Managing Your Vagrant VM
1. Clone the Repository
First, clone the repository containing the Vagrant configuration and scripts:
git clone <repository-url>
cd <repository-directory>
2. Start the VM
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.
3. Reset the VM
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.
4. Pull the Latest Changes
To update your local repository with the latest changes from the remote repository, run:
git pull
5. Check for Provisioning Changes
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.
Scripts Overview
- 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.