This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Vagrantfile
215 lines (181 loc) · 6.82 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
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
Vagrant.configure(2) do |config|
# Required Plugins
# --------------------------------------------------------------------------
config.vagrant.plugins = ['vagrant-hostmanager']
# Vagrant box
# --------------------------------------------------------------------------
config.vm.box = setupConfig['box']
config.vm.guest = 'ubuntu'
if (Vagrant.has_plugin? 'vagrant-disksize')
config.disksize.size = setupConfig['disksize']
end
# General settings
# --------------------------------------------------------------------------
config.vm.hostname = setupConfig['hostname']
# Network
# --------------------------------------------------------------------------
config.vm.network 'private_network', ip: setupConfig['network']['ip']
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.manage_guest = true
if setupConfig['role']['nginx']
aliases = []
certificates = [setupConfig['hostname']];
setupConfig['nginx']['server'].each do |hostconfig|
hostconfig['server_name'].split(' ').each do |serverNamePart|
if serverNamePart != '' and serverNamePart != setupConfig['hostname']
aliases.push(serverNamePart)
certificates.push(serverNamePart)
end
end
end
config.hostmanager.aliases = aliases.join(' ')
setupConfig['nginx']['certificates'] = certificates
end
# Synced folder
# --------------------------------------------------------------------------
config.vm.synced_folder '.', '/vagrant', disabled: true
if hostos == 'windows'
smboptions = {
:create => true,
:smb => true
}
config.vm.synced_folder './..', '/vagrant', smboptions
else
nfsoptions = {
:create => true,
:nfs => true,
:mount_options => setupConfig['nfsoptions']['mount_options'],
:nfs_udp => setupConfig['nfsoptions']['udp'],
:bsd__nfs_options => setupConfig['nfsoptions']['bsd'],
:linux__nfs_options => setupConfig['nfsoptions']['linux']
}
config.vm.synced_folder './..', '/vagrant', nfsoptions
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
elsif hostos == 'windows'
cpus = `echo %NUMBER_OF_PROCESSORS%`.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 libvirt
config.vm.provider 'libvirt' do |l|
l.memory = setupConfig['memory']
l.cpus = cpus
l.qemu_use_session = false
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 'ansible_local' do |ansible|
ansible.playbook = File.basename(setupPath) + '/ansible/playbook.yml'
ansible.extra_vars = setupConfig.deep_merge({ansible_python_interpreter: "/usr/bin/python3"})
end
setupConfig['bindmounts'].each do |bindmount|
script = <<-SCRIPT
mkdir -p /var/bindmount/#{bindmount}/
chown -Rf vagrant:vagrant /var/bindmount/#{bindmount}/
mkdir -p /vagrant/#{bindmount}/
mount -o bind /var/bindmount/#{bindmount}/ /vagrant/#{bindmount}/
SCRIPT
config.vm.provision 'shell', inline: script, name: 'Bindmount for ' + bindmount, run: 'always'
end
end