-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vagrantfile
141 lines (120 loc) · 4.61 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- mode: ruby -*-
# Minions config, sample config:
#
# # file: ./minions.yml
# ---
# - name: app0
# - name: db-masterdb
# - name: sessions1
#
require 'yaml'
vagrant_minions = YAML.load_file 'minions.yml'
$update_salt_ip = <<SALT_IP
if [ -d /etc/salt ]; then
if [ -f /home/vagrant/grains ]; then
cp /home/vagrant/grains /etc/salt/grains
fi
fi
if [ -f /home/vagrant/ip ]; then
# Make sure its really only has a line with numbers
# we will not validate if its a valid IP further.
if grep -q '^[1-9].*[1-9]$' /home/vagrant/ip; then
IP=`cat /home/vagrant/ip`
else
# Lets not give bogus data then
>&2 echo "File in webplatform/ops clone the file ../vagrant-workbench/.ip will update itself by Vagrant"
>&2 echo "This file must contain ONLY an IP address so that this minion can connect to it."
>&2 echo "Giving 127.0.0.1 as salt master in /etc/hosts as a fallback."
IP="127.0.0.1"
fi
else
# No /home/vagrant/ip file
# Lets not give bogus data
>&2 echo "Could not fine vagrant-workbench IP address, please make sure there is a VM there."
>&2 echo "Giving 127.0.0.1 as salt master in /etc/hosts as a fallback."
IP="127.0.0.1"
fi
# If line with string salt dont exist
if grep -q 'salt$' /etc/hosts; then
# Line exists, lets update entry
sed -i "s/.*salt$/$IP\tsalt/" /etc/hosts
echo "Updated /etc/hosts entry IP for salt to $IP"
else
# Append it to the hosts file
# I tried to do an echo here, didnt work, lets keep it
# like that here instead.
cat /home/vagrant/ip | sed -e "s/$/\tsalt/" >> /etc/hosts
echo "Added /etc/hosts entry IP for salt to $IP"
fi
SALT_IP
$post_up_message = <<POST_UP
This is a WebPlatform operations Vagrant minions minion
This VM is meant to be managed by a salt master.
If you got no error message so far about the vagrant-workbench IP address,
you should be good to go and see this minion in the other VM.
Have fun!
POST_UP
Vagrant.configure("2") do |config|
config.vm.box = 'trusty-cloud'
config.vm.box_url = 'https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box'
config.ssh.forward_agent = true
config.vm.network "private_network", type: "dhcp"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provision "file", source: "../vagrant-workbench/.ip", destination: "/home/vagrant/ip", run: "always"
config.vm.provision "file", source: "../vagrant-workbench/.grains", destination: "/home/vagrant/grains", run: "always"
if Vagrant.has_plugin?("vagrant-cachier")
# Configure cached packages to be shared between instances of the same base box.
# More info on http://fgrehm.viewdocs.io/vagrant-cachier/usage
config.cache.scope = :box
config.cache.synced_folder_opts = {
type: :nfs,
mount_options: ['rw', 'vers=3', 'tcp', 'nolock']
}
# ref: http://docs.vagrantup.com/v2/synced-folders/basic_usage.html
end
config.cache.enable :apt
# Common VirtualBox settings
config.vm.provider "virtualbox" do |v|
v.customize ['modifyvm', :id, '--ostype', 'Ubuntu_64']
end
# Common Salt settings
config.vm.provision :salt do |s|
s.verbose = false
end
vagrant_minions.each do |minion|
config.vm.define minion["name"] do |m|
m.vm.hostname = minion["name"] + ".workbench.wpdn"
m.vm.provider "virtualbox" do |v|
v.name = m.vm.hostname
if minion["cpu_cap"]
v.customize ["modifyvm", :id, "--cpuexecutioncap", minion["cpu_cap"].to_i.to_s]
end
if minion["ram"]
v.customize ["modifyvm", :id, "--memory", minion["ram"].to_i.to_s]
end
end
m.vm.provision "shell" do |s|
s.inline = "touch /etc/salt/minion.d/workbench.conf ; grep -q -e $1 /etc/salt/minion.d/workbench.conf || printf \"id: $1\" >> /etc/salt/minion.d/workbench.conf"
s.args = minion["name"]
end
if minion["mounts"].is_a?(Array)
minion['mounts'].each do |dir|
config.vm.synced_folder dir['host'], dir['guest'], type: 'nfs', create: true
end
end
if minion["ports"].is_a?(Array)
minion['ports'].each do |port|
# Gotta figure out how to do proper if here. #gottalearnruby
if port["protocol"]
config.vm.network :forwarded_port, host: port['host'], guest: port['guest'], protocol: port['protocol'], auto_correct: true
else
config.vm.network :forwarded_port, host: port['host'], guest: port['guest'], auto_correct: true
end
end
end
end
end
config.vm.provision :shell, inline: $update_salt_ip, run: "always"
config.vm.post_up_message = $post_up_message
end
# vi: set ft=ruby :