Configure Vagrant VMs using Ansible.

This way, Vagrant VMs and cloud-provided VMs are configured in the exact same way, hence avoiding configuration drift and subtle bugs associated to different versions of software.
This commit is contained in:
Marc CARRE committed 2017-02-08 14:15:59 +00:00
1 parent e5185e73ce
commit 67dbdd377c
3 files changed
+53 -112

No files matched your search

Vendored
+8 -18
View File
@@ -2,19 +2,6 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'vagrant-common.rb'))
vm_ip = "172.16.0.3" # arbitrary private IP
pkgs = %w(
docker-engine=1.10.2-0~wily
aufs-tools
build-essential
ethtool
iputils-arping
libpcap-dev
git
mercurial
bc
jq
)
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = VAGRANT_IMAGE
@@ -34,13 +21,16 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Set SSH keys up to be able to run smoke tests straightaway:
config.vm.provision "file", source: "~/.vagrant.d/insecure_private_key", destination: "/home/vagrant/src/github.com/weaveworks/weave/test/insecure_private_key"
install_build_deps config.vm, pkgs
install_go_toochain config.vm
tweak_user_env config.vm
tweak_docker_daemon config.vm
# Grant permissions on sources:
config.vm.provision :shell, :inline => 'sudo chown -R vagrant:vagrant ~vagrant/src', :privileged => false
cleanup config.vm
config.vm.provision 'ansible' do |ansible|
ansible.playbook = 'tools/config_management/setup_weave-net_dev.yml'
ansible.extra_vars = {
go_version: GO_VERSION
}
end
end
begin
+42 -35
View File
@@ -3,55 +3,62 @@
Vagrant.require_version ">= 1.7.0"
# these ought to match what is in config.sh
n_machines = 3
ip_prefix = "192.168.48"
ip_suffix_base = 10
N_MACHINES = 3
IP_PREFIX = '192.168.48'
IP_SUFFIX_BASE = 10
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'vagrant-common.rb'))
def configure_docker(host, hostname, ip)
pkgs = %w(docker-engine ethtool)
host.vm.box = VAGRANT_IMAGE
host.vm.provision :shell, :inline => "hostnamectl set-hostname "+hostname
host.vm.network "private_network", ip: ip
# Disable default Vagrant shared folder, which we don't need:
host.vm.synced_folder ".", "/vagrant", disabled: true
host.vm.provision :shell, :inline => "sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D"
host.vm.provision :shell, :inline => "echo deb https://apt.dockerproject.org/repo ubuntu-wily main > /etc/apt/sources.list.d/docker.list"
install_packages host.vm, pkgs
tweak_docker_daemon host.vm
cleanup host.vm
def ip_address(i)
"#{IP_PREFIX}.#{IP_SUFFIX_BASE + i}"
end
def configure_resolv_conf(host)
# Fix the resolution errors by using 8.8.8.8
host.vm.provision :shell, :inline => "echo 'nameserver 8.8.8.8' > /etc/resolv.conf"
def hostname(i)
"host#{i}"
end
ETC_HOSTS_ENTRIES = (1..N_MACHINES).map { |i| "#{ip_address(i)} #{hostname(i)}\n"}.join('')
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
(1..N_MACHINES).each do |i|
hostname = hostname(i)
config.vm.define hostname do |host|
host.vm.provider :virtualbox do |vb|
vb.name = hostname
vb.memory = 384
configure_nat_dns(vb)
end
config.vm.provider :virtualbox do |vb|
vb.memory = 384
configure_nat_dns(vb)
end
# Disable Vagrant 1.7 per host insecure key replacement
host.ssh.insert_key = false
# Disable Vagrant 1.7 per host insecure key replacement
config.ssh.insert_key = false
host.vm.box = VAGRANT_IMAGE
host.vm.network "private_network", ip: ip_address(i)
host.vm.hostname = hostname
(1..n_machines).each do |i|
config.vm.define "host#{i}" do |host|
ip_suffix = ip_suffix_base + i
configure_resolv_conf(host)
configure_docker(host, "host#{i}", "#{ip_prefix}.#{ip_suffix}")
host.vm.provision :shell, :inline => <<END
# Tests rely on hostnames, so we remove potentially incorrect entries (e.g. localhost), and add mappings of IP addresses to hostnames:
sed -i '/#{hostname}/d' /etc/hosts
echo "#{ETC_HOSTS_ENTRIES}" >> /etc/hosts
# Fix the resolution errors by using 8.8.8.8
echo 'nameserver 8.8.8.8' > /etc/resolv.conf
END
# Disable default Vagrant shared folder, which we don't need:
host.vm.synced_folder ".", "/vagrant", disabled: true
cleanup host.vm
# Only execute once the Ansible provisioner, when all the machines are up and ready:
if i == N_MACHINES
host.vm.provision 'ansible' do |ansible|
ansible.limit = 'all' # Disable default limit to connect to all the machines
ansible.playbook = '../tools/config_management/setup_weave-net_test.yml'
end
end
end
end
end
begin
+3 -59
View File
@@ -1,4 +1,4 @@
VAGRANT_IMAGE = 'ubuntu/wily64'
VAGRANT_IMAGE = 'bento/ubuntu-16.04'
VAGRANTFILE_API_VERSION = '2'
def get_go_version_from_build_dockerfile()
@@ -11,7 +11,6 @@ def get_go_version_from_build_dockerfile()
go_version
end
GO_BINARY_PATH = '/usr/local/go/bin'
GO_VERSION = get_go_version_from_build_dockerfile()
def configure_nat_dns(vb)
@@ -19,67 +18,12 @@ def configure_nat_dns(vb)
vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
end
def install_packages(vm, pkgs)
vm.provision :shell, :inline => <<SCRIPT
apt-get update -qq
apt-get install -qq -y --force-yes --no-install-recommends #{pkgs.join(' ')}
SCRIPT
end
def install_build_deps(vm, pkgs)
vm.provision :shell, :inline => <<SCRIPT
export DEBIAN_FRONTEND=noninteractive
apt-key adv \
--keyserver hkp://p80.pool.sks-keyservers.net:80 \
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo 'deb https://apt.dockerproject.org/repo ubuntu-wily main' \
> /etc/apt/sources.list.d/docker.list
SCRIPT
install_packages(vm, pkgs)
end
def install_go_toochain(vm)
vm.provision :shell, :inline => <<SCRIPT
curl -s https://storage.googleapis.com/golang/go#{GO_VERSION}.linux-amd64.tar.gz \
| tar xz -C /usr/local
#{GO_BINARY_PATH}/go clean -i net
#{GO_BINARY_PATH}/go install -tags netgo std
SCRIPT
end
def tweak_user_env(vm)
script = <<SCRIPT
echo 'export GOPATH="${HOME}"' \
>> ~vagrant/.profile
echo 'export PATH="${HOME}/bin:#{GO_BINARY_PATH}:${PATH}"' \
>> ~vagrant/.profile
ln -sf ~vagrant/src/github.com/weaveworks/weave ~vagrant/
sudo chown -R vagrant:vagrant ~vagrant/src
SCRIPT
vm.provision :shell, :inline => script, :privileged => false
end
def tweak_docker_daemon(vm)
vm.provision :shell, :inline => <<SCRIPT
usermod -a -G docker vagrant
mkdir -p /etc/systemd/system/docker.service.d
cat >/etc/systemd/system/docker.service.d/override.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// -H unix:///var/run/alt-docker.sock -H tcp://0.0.0.0:2375 -s overlay
EOF
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
SCRIPT
end
def cleanup(vm)
vm.provision :shell, :inline => <<SCRIPT
export DEBIAN_FRONTEND=noninteractive
## Who the hell thinks official images have to have both of these?
/etc/init.d/chef-client stop
/etc/init.d/puppet stop
[ ! -f /etc/init.d/chef-client ] || /etc/init.d/chef-client stop
[ ! -f /etc/init.d/puppet ] || /etc/init.d/puppet stop
apt-get -qq remove puppet chef
apt-get -qq autoremove
killall -9 chef-client 2>/dev/null || true