wireguard-vagrant/Vagrantfile
2021-07-28 08:55:19 +08:00

103 lines
4.3 KiB
Ruby

# -*- mode: ruby -*-
# vi: set ft=ruby :
$num_instances ||= 3
$instance_name_prefix ||= "wg"
$vm_memory ||= 2048
$vm_cpus ||= 1
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
(1..$num_instances).each do |i|
config.vm.define vm_name = "wg-%01d" % i do |node|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
node.vm.hostname = vm_name
node.vm.box = "ubuntu/focal64"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
ip_address = "192.168.33.10%d" % i
vpn_ip_address = "10.0.100.10%d" % i
node.vm.network "private_network", ip: ip_address
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
node.vm.synced_folder "./", "/vagrant"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
node.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
# vb.gui = true
# Customize the amount of memory on the VM:
vb.memory = "512"
end
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
node.vm.provision "shell", inline: <<-SHELL
sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
apt-get update
swapoff -a
SHELL
node.vm.provision "shell", path: "./bootstrap/wireguard-bootstrap.sh", args: [ip_address, vpn_ip_address]
config.trigger.before :destroy do |trigger|
trigger.ruby do |env, machine|
FileUtils.rm_f("wg-conf/wg-peer-#{vm_name}.conf")
end
end
# update all the machines wireguard configuration with all the other peers.
config.trigger.after :up do |trigger|
trigger.ruby do |env, machine|
env.active_machines.each do |vm_name, machine_provider|
m = env.machine(vm_name, machine_provider)
if m.state.id == :running
m.ui.info("Updating WireGuard peers...")
m.communicate.sudo("/vagrant/bootstrap/wireguard-update-peers.sh") do |type, data|
m.ui.info(data.chomp)
end
end
end
end
end
end
end
end