This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
forked from vagrant-php/ubuntu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
228 lines (201 loc) · 7.48 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require 'json'
require 'yaml'
# hash deep merge method
# --------------------------------------------------------------------------
class ::Hash
def deep_merge(second)
merger = proc do |key, v1, v2|
if Hash === v1 && Hash === v2
v1.merge(v2, &merger)
elsif Array === v1 && Array === v2
v1 | v2
elsif [:undefined, nil, :nil].include?(v2)
v1
else
v2
end
end
self.merge(second.to_h, &merger)
end
end
# config
# --------------------------------------------------------------------------
setupPath = File.dirname(File.expand_path(__FILE__))
defaultConfigPath = setupPath + '/vagrant-default.yml'
userDefaultConfigPath = Dir.home + '/.vagrant-default-user.yml'
projectConfigPath = setupPath + '/../vagrant.yml'
userProjectConfigPath = setupPath + '/../vagrant-user.yml'
setupConfig = YAML.load_file(defaultConfigPath)
if File.file?(userDefaultConfigPath)
userDefaultConfig = YAML.load_file(userDefaultConfigPath)
setupConfig = setupConfig.deep_merge(userDefaultConfig)
end
if File.file?(projectConfigPath)
projectConfig = YAML.load_file(projectConfigPath)
setupConfig = setupConfig.deep_merge(projectConfig)
end
if File.file?(userProjectConfigPath)
userConfig = YAML.load_file(userProjectConfigPath)
setupConfig = setupConfig.deep_merge(userConfig)
end
# hostos
# --------------------------------------------------------------------------
if Vagrant::Util::Platform.bsd?
hostos = 'bsd'
elsif Vagrant::Util::Platform.darwin?
hostos = 'darwin'
elsif Vagrant::Util::Platform.linux?
hostos = 'linux'
elsif Vagrant::Util::Platform.windows?
hostos = 'windows'
else
hostos = 'unknown'
end
# applications
# --------------------------------------------------------------------------
setupConfig['applications'] = []
if not setupConfig['application'].nil?
setupConfig['applications'].push(setupConfig['application'])
end
setupConfig['subhosts'].each do |subhost|
if not subhost['application'].nil?
setupConfig['applications'].push(subhost['application'])
end
end
Vagrant.configure(2) do |config|
# Vagrant box
# --------------------------------------------------------------------------
config.vm.box = 'boxcutter/ubuntu1604'
config.vm.guest = 'ubuntu'
# General settings
# --------------------------------------------------------------------------
config.vm.hostname = setupConfig['hostname']
# Network
# --------------------------------------------------------------------------
if setupConfig['network']['ip'] == 'dhcp'
config.vm.network 'private_network', type: 'dhcp'
else
config.vm.network 'private_network', ip: setupConfig['network']['ip']
end
setupConfig['network']['forwarded_ports'].each do |id, options|
if options['host'] == 'ephemeral'
options['host'] = Random.rand(49152..65535)
end
config.vm.network :forwarded_port, id: id, guest: options['guest'], host: options['host'], auto_correct: true
end
# SSH stuff
# --------------------------------------------------------------------------
config.ssh.forward_agent = true
# Hostmanager
# --------------------------------------------------------------------------
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if hostname = (vm.ssh_info && vm.ssh_info[:host])
`vagrant ssh -c 'hostname -I'`.split()[1]
end
end
if not setupConfig['subhosts'].empty?
aliases = []
setupConfig['subhosts'].each do |subhost|
aliases.push(subhost['subhostname'] + '.' + setupConfig['hostname'])
end
config.hostmanager.aliases = aliases.join(' ')
end
# Synced folder
# --------------------------------------------------------------------------
config.vm.synced_folder '.', '/vagrant', disabled: true
if setupConfig['sharetype'] == 'native'
config.vm.synced_folder './..', '/vagrant'
elsif setupConfig['sharetype'] == 'nfs' or setupConfig['sharetype'] == 'nfs-bindfs'
config.nfs.map_uid = Process.uid
config.nfs.map_gid = Process.gid
nfsoptions = {
:create => true,
:nfs => true,
:nfs_udp => setupConfig['nfsoptions']['udp'],
:bsd__nfs_options => setupConfig['nfsoptions']['bsd'],
:linux__nfs_options => setupConfig['nfsoptions']['linux']
}
if setupConfig['sharetype'] == 'nfs-bindfs'
config.vm.synced_folder './..', '/vagrant-nfs', nfsoptions
config.bindfs.bind_folder '/vagrant-nfs', '/vagrant'
else
config.vm.synced_folder './..', '/vagrant', nfsoptions
end
elsif setupConfig['sharetype'] == 'sshfs'
config.vm.synced_folder './..', '/vagrant', type: 'sshfs'
else
print "no valid sharetype, please take a look into README.md!\n"
end
# Resources of our box
# --------------------------------------------------------------------------
# define cpu count
if setupConfig['cpus']
cpus = setupConfig['cpus']
else
if hostos == 'bsd' || hostos == 'darwin'
cpus = `sysctl -n hw.ncpu`.to_i
elsif hostos == 'linux'
cpus = `nproc`.to_i
else
cpus = 2
end
end
# for virtualbox
config.vm.provider 'virtualbox' do |v|
v.name = setupConfig['hostname']
v.memory = setupConfig['memory']
v.cpus = cpus
if setupConfig['virtualbox'].key?(hostos)
setupConfig['virtualbox'][hostos].each do |key, value|
v.customize ['modifyvm', :id, '--' + key, value]
end
end
end
# for vmware fusion (osx)
config.vm.provider "vmware_fusion" do |v|
v.vmx['displayname'] = setupConfig['hostname']
v.vmx['memsize'] = setupConfig['memory']
v.vmx['numvcpus'] = cpus
v.vmx['vhv.enable'] = 'TRUE'
end
# for vmware workstation (windows, linux)
config.vm.provider "vmware_workstation" do |v|
v.vmx['displayname'] = setupConfig['hostname']
v.vmx['memsize'] = setupConfig['memory']
v.vmx['numvcpus'] = cpus
v.vmx['vhv.enable'] = 'TRUE'
end
# Provisioning
# --------------------------------------------------------------------------
config.vm.provision 'shell' do |sh|
sh.path = 'ansible/ansible-on-guest.sh'
sh.args = ['ansible/playbook.yml', setupConfig.to_json.split(' ').join('\u0020')]
end
if setupConfig['subhosts'].empty?
if setupConfig['application']
config.vm.provision 'shell', run: "always" do |sh|
sh.path = "bindmount/" + setupConfig['application'] + ".sh"
sh.args = [
setupConfig['application'],
'/var/' + setupConfig['hostname'],
'/vagrant',
]
end
end
else
setupConfig['subhosts'].each do |subhost|
if subhost['application']
config.vm.provision 'shell', run: "always" do |sh|
sh.path = "bindmount/" + subhost['application'] + ".sh"
sh.args = [
subhost['application'],
'/var/' + subhost['subhostname'],
'/vagrant/' + subhost['subhostname'],
]
end
end
end
end
end