Skip to content

Commit 467f745

Browse files
committed
Adds a basic Linux Vagrant setup, stolen from Nomad.
1 parent ea19a6e commit 467f745

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

Vagrantfile

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
#
4+
5+
LINUX_BASE_BOX = "bento/ubuntu-16.04"
6+
7+
Vagrant.configure(2) do |config|
8+
config.vm.define "linux", autostart: true, primary: true do |vmCfg|
9+
vmCfg.vm.box = LINUX_BASE_BOX
10+
vmCfg.vm.hostname = "linux"
11+
vmCfg = configureProviders vmCfg,
12+
cpus: suggestedCPUCores()
13+
14+
vmCfg = configureLinuxProvisioners(vmCfg)
15+
16+
vmCfg.vm.synced_folder '.',
17+
'/opt/gopath/src/github.com/hashicorp/consul'
18+
19+
vmCfg.vm.network "forwarded_port", guest: 8500, host: 8500, auto_correct: true
20+
end
21+
end
22+
23+
def configureLinuxProvisioners(vmCfg)
24+
vmCfg.vm.provision "shell",
25+
privileged: true,
26+
inline: 'rm -f /home/vagrant/linux.iso'
27+
28+
vmCfg.vm.provision "shell",
29+
privileged: true,
30+
path: './scripts/vagrant-linux-priv-go.sh'
31+
32+
return vmCfg
33+
end
34+
35+
def configureProviders(vmCfg, cpus: "2", memory: "2048")
36+
vmCfg.vm.provider "virtualbox" do |v|
37+
v.memory = memory
38+
v.cpus = cpus
39+
end
40+
41+
["vmware_fusion", "vmware_workstation"].each do |p|
42+
vmCfg.vm.provider p do |v|
43+
v.enable_vmrun_ip_lookup = false
44+
v.vmx["memsize"] = memory
45+
v.vmx["numvcpus"] = cpus
46+
end
47+
end
48+
49+
vmCfg.vm.provider "virtualbox" do |v|
50+
v.memory = memory
51+
v.cpus = cpus
52+
end
53+
54+
return vmCfg
55+
end
56+
57+
def suggestedCPUCores()
58+
case RbConfig::CONFIG['host_os']
59+
when /darwin/
60+
Integer(`sysctl -n hw.ncpu`) / 2
61+
when /linux/
62+
Integer(`cat /proc/cpuinfo | grep processor | wc -l`) / 2
63+
else
64+
2
65+
end
66+
end

scripts/vagrant-linux-priv-go.sh

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
3+
function install_go() {
4+
local go_version=1.9.1
5+
local download=
6+
7+
download="https://storage.googleapis.com/golang/go${go_version}.linux-amd64.tar.gz"
8+
9+
if [ -d /usr/local/go ] ; then
10+
return
11+
fi
12+
13+
wget -q -O /tmp/go.tar.gz ${download}
14+
15+
tar -C /tmp -xf /tmp/go.tar.gz
16+
sudo mv /tmp/go /usr/local
17+
sudo chown -R root:root /usr/local/go
18+
}
19+
20+
install_go
21+
22+
# Ensure that the GOPATH tree is owned by vagrant:vagrant
23+
mkdir -p /opt/gopath
24+
chown -R vagrant:vagrant /opt/gopath
25+
26+
# Ensure Go is on PATH
27+
if [ ! -e /usr/bin/go ] ; then
28+
ln -s /usr/local/go/bin/go /usr/bin/go
29+
fi
30+
if [ ! -e /usr/bin/gofmt ] ; then
31+
ln -s /usr/local/go/bin/gofmt /usr/bin/gofmt
32+
fi
33+
34+
35+
# Ensure new sessions know about GOPATH
36+
if [ ! -f /etc/profile.d/gopath.sh ] ; then
37+
cat <<EOT > /etc/profile.d/gopath.sh
38+
export GOPATH="/opt/gopath"
39+
export PATH="/opt/gopath/bin:\$PATH"
40+
EOT
41+
chmod 755 /etc/profile.d/gopath.sh
42+
fi

0 commit comments

Comments
 (0)