2 Commits

Author SHA1 Message Date
a97eb30314 testing 2023-08-23 17:31:52 -04:00
9b5be29a1a Update Vagrantfile to use external settings 2023-08-21 18:46:47 -04:00
3 changed files with 26 additions and 71 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
.playbook .playbook
.vagrant .vagrant*
.vscode .vscode
/environments/ /environments/

49
Vagrantfile vendored
View File

@@ -1,43 +1,45 @@
# -*- mode: ruby -*- # -*- mode: ruby -*-
# vi: set ft=ruby : # vi: set ft=ruby :
SSH_FORWARD=ENV["SSH_FORWARD"] require 'yaml'
if !(SSH_FORWARD == "true") settings_path = '.vagrant.yml'
SSH_FORWARD = false settings = {}
if File.exist?(settings_path)
settings = YAML.load_file(settings_path)
end end
PLAYBOOK=ENV["PLAYBOOK"] VAGRANT_BOX = settings['VAGRANT_BOX'] || 'debian/bookworm64'
if !PLAYBOOK VAGRANT_CPUS = settings['VAGRANT_CPUS'] || 2
if File.exist?('.playbook') VAGRANT_MEM = settings['VAGRANT_MEM'] || 2048
PLAYBOOK = IO.read('.playbook').split("\n")[0] SSH_FORWARD = settings['SSH_FORWARD'] || false
end
# Default to shell environment variable: PLAYBOOK (priority #1)
PLAYBOOK=ENV["PLAYBOOK"]
if !PLAYBOOK || PLAYBOOK.empty?
# PLAYBOOK setting in .vagrant.yml (priority #2)
PLAYBOOK = settings['PLAYBOOK'] || false
if !PLAYBOOK || PLAYBOOK.empty? if !PLAYBOOK || PLAYBOOK.empty?
PLAYBOOK = "\nERROR: Set env PLAYBOOK" puts "[VAGRANTFILE ERROR]: Set PLAYBOOK setting in .vagrant.yml"
abort
end end
else
File.write(".playbook", PLAYBOOK)
end end
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.vm.box = "debian/bullseye64" config.vm.box = VAGRANT_BOX
config.vm.network "private_network", type: "dhcp" config.vm.network "private_network", type: "dhcp"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.synced_folder "./scratch", "/vagrant/scratch"
config.ssh.forward_agent = SSH_FORWARD config.ssh.forward_agent = SSH_FORWARD
# Machine Name # Libvrit provider
config.vm.define :moxie do |moxie| #
end
config.vm.provider :libvirt do |libvirt| config.vm.provider :libvirt do |libvirt|
libvirt.cpus = 2 libvirt.cpus = VAGRANT_CPUS
libvirt.memory = 4096 libvirt.memory = VAGRANT_MEM
libvirt.default_prefix = ""
end end
config.vm.provider "virtualbox" do |vbox| # Virtualbox provider
vbox.memory = 4096 config.vm.provider :virtualbox do |vbox|
vbox.cpus = VAGRANT_CPUS
vbox.memory = VAGRANT_MEM
end end
# Provision with Ansible # Provision with Ansible
@@ -46,5 +48,4 @@ Vagrant.configure("2") do |config|
ansible.compatibility_mode = "2.0" ansible.compatibility_mode = "2.0"
ansible.playbook = "dev/" + PLAYBOOK + ".yml" ansible.playbook = "dev/" + PLAYBOOK + ".yml"
end end
end end

View File

@@ -1,46 +0,0 @@
#!/bin/bash
# Find private key file
PRIVATE_KEY="$(find .vagrant -name "private_key")"
# Does the private_key file exist?
if [ ! -f "$PRIVATE_KEY" ]; then
echo "[ERROR] File not found at \"$PRIVATE_KEY\""
exit 1
fi
# Is the private_key a valid SSH key?
echo "Checking validity of private key at $(pwd)/$PRIVATE_KEY"
if ! ssh-keygen -l -f "$PRIVATE_KEY"; then
echo "[Error] The private key at \"$PRIVATE_KEY\" is invalid (CODE: $?)"
exit 1
fi
# Find an IP on the VM for the SSH tunnel
HOST_IP="$(vagrant ssh -c "hostname -I | cut -d' ' -f${HOSTNAME_FIELD:-1}" 2>/dev/null | sed 's/.$//')"
# SSH command to match in processes table
CMD="ssh -fNT -i $PRIVATE_KEY -L 8443:localhost:8443 -L 80:localhost:80 -L 443:localhost:443.*vagrant@$HOST_IP"
# Not just after PIDs
# shellcheck disable=SC2009
PS_TUNNELS="$(ps aux | grep -e "$CMD" | grep -v grep)"
PS_COUNTER="$(echo "$PS_TUNNELS" | wc -l)"
if [ "$PS_COUNTER" -gt 0 ]; then
echo "[ERROR] Tunnel(s) already seems to exist (counted $PS_COUNTER)"
echo \""$PS_TUNNELS"\"
exit 1
fi
# Create an SSH tunnel
echo "Starting background SSH connection for localhost port forwarding"
set -x
ssh -fNT -i "$PRIVATE_KEY" \
-L 8443:localhost:8443 \
-L 80:localhost:80 \
-L 443:localhost:443 \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
vagrant@"${HOST_IP}" 2>/dev/null