-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVagrantfile
355 lines (305 loc) · 13.5 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# -*- mode: ruby -*-
# vi: set ft=ruby :
# required modules
require 'yaml'
# *********************************************************************************************
# Local Methods
# *********************************************************************************************
def get_ansible_version(exe)
/^[^\s]+ (.+)$/.match(`#{exe} --version`) { |match| return match[1] }
end
def walk(obj, &fn)
if obj.is_a?(Array)
obj.map { |value| walk(value, &fn) }
elsif obj.is_a?(Hash)
obj.each_pair { |key, value| obj[key] = walk(value, &fn) }
else
obj = yield(obj)
end
end
# *********************************************************************************************
# Local Variables
# *********************************************************************************************
# Fix ENV['VBOX_INSTALL_PATH'] in Windows OS
if Vagrant::Util::Platform.windows?
ENV['VBOX_INSTALL_PATH'] = ENV['VBOX_MSI_INSTALL_PATH']
end
# Absolute paths on the host machine.
host_project_dir = File.dirname(File.expand_path(__FILE__))
host_config_dir = ENV['LINUXVM_CONFIG_DIR'] ? "#{host_project_dir}/#{ENV['LINUXVM_CONFIG_DIR']}" : host_project_dir
# Absolute paths on the guest machine.
guest_project_dir = '/vagrant'
guest_config_dir = ENV['LINUXVM_CONFIG_DIR'] ? "/vagrant/#{ENV['LINUXVM_CONFIG_DIR']}" : guest_project_dir
# Custom configuration
linuxvm_config = ENV['LINUXVM_CONFIG_FILE'] || 'vagrant'
# Load default VM configurations.
vconfig = YAML.load_file("#{host_project_dir}/default.conf")
# Use optional user.conf and local.conf for configuration overrides.
['user.conf', 'local.conf', "#{linuxvm_config}"].each do |config_file|
if File.exist?("#{host_config_dir}/#{config_file}")
cfg_data = YAML.load_file("#{host_config_dir}/#{config_file}")
if cfg_data then vconfig.merge!(cfg_data) end
end
end
# Replace jinja variables in config.
vconfig = walk(vconfig) do |value|
while value.is_a?(String) && value.match(/{{ .* }}/)
value = value.gsub(/{{ (.*?) }}/) { vconfig[Regexp.last_match(1)] }
end
value
end
# EmLinux Tools Version
emlinux_version = vconfig['vm_emlinux']
# Get Host Name and Machine Name
hostname = vconfig['vm_hostname']
machname = vconfig.include?('vm_machname') ? vconfig['vm_machname'] : hostname
# Check Vagrant version
Vagrant.require_version ">= 1.8.6"
# Configuration
Vagrant.configure(2) do |config|
# Vagrant box.
config.vm.box = vconfig['vagrant_box']
# The url from where the 'config.vm.box' box will be fetched if it doesn't already exist.
if vconfig.include?('vagrant_url')
config.vm.box_url = vconfig['vagrant_url']
end
# Set machine name
config.vm.define machname
# Set machine disk size
if vconfig.include?('vm_disksize')
unless Vagrant.has_plugin?('vagrant-disksize')
puts "'vagrant-disksize' plugin is required. Install it by running:"
puts " vagrant plugin install vagrant-disksize"
puts
exit
end
config.disksize.size = "#{vconfig['vm_disksize']}GB"
end
# *********************************************************************************************
# Network options.
# *********************************************************************************************
# Network Host Name.
config.vm.hostname = hostname
# Network Config
if vconfig.include?('vm_networks')
vconfig['vm_networks'].each do |network|
if network['enabled'] && network['enabled'] == true
options = {}
if network.include?('ip')
options[:ip] = network['ip']
end
if network.include?('bridge')
options[:bridge] = network['bridge']
end
if network.include?('dhcp_defroute')
options[:use_dhcp_assigned_default_route] = network['dhcp_defroute']
end
if options
config.vm.network network['access'], options
else
config.vm.network network['access']
end
end
end
end
# *********************************************************************************************
# SSH options.
# *********************************************************************************************
# Use correct user name and password for SSH access
if ARGV[0] == "ssh"
config.ssh.username = vconfig['vm_username']
config.ssh.password = vconfig['vm_userpass']
end
# Vagrant will automatically insert a keypair to use for SSH (default: true).
config.ssh.insert_key = true
# Use Vagrant-provided SSH private keys (don't use any keys stored in ssh-agent, default: true).
config.ssh.keys_only = true
# Use agent forwarding over SSH connections (default: false).
config.ssh.forward_agent = true
# Use X11 forwarding over SSH connections (default: false).
config.ssh.forward_x11 = false
# *********************************************************************************************
# Synced folders
# *********************************************************************************************
# Default synced folder
if Vagrant::Util::Platform.windows?
config.vm.synced_folder host_project_dir, guest_project_dir, type: "virtualbox"
else
config.vm.synced_folder host_project_dir, guest_project_dir
end
# User defined synced folders
if vconfig.include?('vm_folders')
vconfig['vm_folders'].each do |synced_folder|
if synced_folder['enabled'] && synced_folder['enabled'] == true
config.vm.synced_folder synced_folder['local_path'], synced_folder['dest_path'], {
type: synced_folder['type'],
rsync__auto: 'true',
rsync__exclude: synced_folder['exclude'],
rsync__args: ['--verbose', '--archive', '--delete', '-z', '--chmod=ugo=rwX'],
id: synced_folder['id'],
create: synced_folder.include?('create') ? synced_folder['create'] : false,
mount_options: synced_folder.include?('mount_opts') ? synced_folder['mount_opts'] : []
}
end
end
end
# *********************************************************************************************
# Persistent storage.
# *********************************************************************************************
if vconfig.include?('vm_pstorage')
pstorage = vconfig['vm_pstorage']
if pstorage.include?('enabled') && pstorage['enabled'] == true
unless Vagrant.has_plugin?('vagrant-persistent-storage')
puts "'vagrant-persistent-storage' plugin is required. Install it by running:"
puts " vagrant plugin install vagrant-persistent-storage"
puts
exit
end
config.persistent_storage.enabled = true
config.persistent_storage.mountname = 'pstorage'
config.persistent_storage.size = pstorage['size']
config.persistent_storage.location = pstorage['location']
config.persistent_storage.filesystem = pstorage['fstype']
config.persistent_storage.mountpoint = pstorage['destpath']
config.persistent_storage.use_lvm = false
if pstorage.include?('mopts')
config.persistent_storage.mountoptions = pstorage['mopts']
end
end
end
# *********************************************************************************************
# Virtualbox settings
# *********************************************************************************************
if Vagrant.has_plugin?('vagrant-vbguest')
config.vbguest.installer_arguments = "--nox11 -- --force"
config.vbguest.auto_update = true
# config.vbguest.iso_path = 'http://download.virtualbox.org/virtualbox/%{version}/VBoxGuestAdditions_%{version}.iso' #% {version: '5.1.14'}
# config.trigger.after :up { run "vagrant vbguest --auto-reboot --no-provision" }
else
if Vagrant::Util::Platform.windows?
puts "'vagrant-vbguest' plugin is required. Install it by running:"
puts " vagrant plugin install vagrant-vbguest"
puts
exit
end
end
# Set VirtualBox.
config.vm.provider :virtualbox do |vb|
if Vagrant::VERSION =~ /^1.8/
vb.linked_clone = true
end
vb.name = hostname
vb.memory = vconfig.include?('vm_memory') ? vconfig['vm_memory'] : 1024
vb.cpus = vconfig.include?('vm_cpus') ? vconfig['vm_cpus'] : 1
vb.gui = vconfig.include?('vm_gui') ? vconfig['vm_gui'] : false
# Customize provider default configuration
# - make the DNS resolution faster
# - enable USB support
vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
vb.customize ['modifyvm', :id, '--natdnshostresolver2', 'on']
vb.customize ['modifyvm', :id, '--ioapic', 'on']
vb.customize ["modifyvm", :id, "--usb", "on"]
vb.customize ["modifyvm", :id, "--usbehci", "off"]
#vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
if vconfig.include?('vm_gui') && vconfig['vm_gui'] == true
vb.customize ["modifyvm", :id, "--vram", "32"]
end
end
# *********************************************************************************************
# Scripts
# *********************************************************************************************
$init_script = <<-'END'
sudo apt-get update -y
#sudo apt-get upgrade -y
sudo apt-get install -y build-essential ncurses-dev
sudo apt-get install -y gcc-arm-linux-gnueabi
sudo apt-get install -y u-boot-tools qemu-user-static
sudo apt-get install -y debian-keyring debian-archive-keyring
echo '>>> Set password for %{username} user to %{userpass}'
echo '%{username}:%{userpass}' | sudo chpasswd
END
if Vagrant::Util::Platform.windows?
$init_script += <<-'END'
echo '>>> Install samba server'
sudo apt-get install -y samba
echo '[global]' > /etc/samba/smb.conf
echo ' workgroup = WORKGROUP' >> /etc/samba/smb.conf
echo ' server string = Samba server (Ubuntu)' >> /etc/samba/smb.conf
echo ' server role = standalone server' >> /etc/samba/smb.conf
echo ' guest account = %{username}' >> /etc/samba/smb.conf
echo ' admin users = %{username}' >> /etc/samba/smb.conf
echo ' unix password sync = yes' >> /etc/samba/smb.conf
echo ' map to guest = bad user' >> /etc/samba/smb.conf
echo ' usershare allow guests = yes' >> /etc/samba/smb.conf
echo ' dns proxy = no' >> /etc/samba/smb.conf
echo '[%{username}]' >> /etc/samba/smb.conf
echo ' comment = Share' >> /etc/samba/smb.conf
echo ' path = /home/%{username}' >> /etc/samba/smb.conf
echo ' browsable = yes' >> /etc/samba/smb.conf
echo ' guest ok = yes' >> /etc/samba/smb.conf
echo ' read only = no' >> /etc/samba/smb.conf
echo ' create mask = 0777' >> /etc/samba/smb.conf
echo ' directory mask = 0777' >> /etc/samba/smb.conf
sudo systemctl restart smbd.service nmbd.service
echo '>>> Install emlinux-tools dependencies'
sudo apt-get install -y git parted tree lzop gzip zip bc binfmt-support debootstrap
END
if emlinux_version == 'develop'
$init_script += <<-'END'
echo '>>> Install emlinux-tools into /opt/emLinux directory'
sudo git clone https://github.com/molejar/emLinux /opt/emLinux
sudo chmod a+x /opt/emLinux/install.sh
sudo /opt/emLinux/install.sh -q
END
else
$init_script += <<-'END'
echo '>>> Install emlinux-tools'
sudo wget --quiet https://github.com/molejar/emLinux/releases/download/%{emlinux_tools_release}/emlinux-tools_%{emlinux_tools_release}_all.deb
sudo dpkg -i emlinux-tools_%{emlinux_tools_release}_all.deb
sudo rm emlinux-tools_%{emlinux_tools_release}_all.deb
END
end
else
$init_script += <<-'END'
echo '>>> Install emlinux-tools dependencies'
sudo apt-get install -y git parted tree lzop gzip zip bc binfmt-support debootstrap
echo '>>> Install emlinux-tools'
sudo chmod a+x %{emlinux_tools_install}
sudo %{emlinux_tools_install} -q
END
end
$init_script += <<-'END'
echo '>>> Install tftp server'
sudo apt-get install -y tftpd-hpa
echo 'TFTP_USERNAME="tftp"' >> /etc/default/tftpd-hpa
echo 'TFTP_DIRECTORY="%{tftpdir}"' >> /etc/default/tftpd-hpa
echo 'TFTP_ADDRESS="[::]:69"' >> /etc/default/tftpd-hpa
echo 'TFTP_OPTIONS="--secure --create"' >> /etc/default/tftpd-hpa
sudo mkdir -p %{tftpdir}
sudo chown -R tftp %{tftpdir}
sudo systemctl restart tftpd-hpa.service
echo '>>> Install nfs server'
sudo apt-get install -y nfs-kernel-server nfs-common portmap
echo '%{nfsdir} *(rw,async,nohide,insecure,no_subtree_check,no_root_squash)' >> /etc/exports
sudo mkdir -p %{nfsdir}
sudo /etc/init.d/nfs-kernel-server restart
sudo systemctl restart nfs-kernel-server.service
sudo chown -R %{username}:%{username} /home/%{username}
END
# Execute initialization script
config.vm.provision "shell", inline: $init_script % {
:nfsdir => "/srv/nfs",
:tftpdir => "/srv/tftp",
:username => vconfig['vm_username'],
:userpass => vconfig['vm_userpass'],
:emlinux_tools_install => "#{guest_project_dir}/install.sh",
:emlinux_tools_release => "#{emlinux_version}"
}
# Execute bootstrap script
if vconfig.include?('vm_bootstrap')
script = ""
vconfig['vm_bootstrap'].each { |cmd| script += "#{cmd}\n" }
script += 'echo ">>> VM Ready, run: vagrant ssh"'
config.vm.provision "shell", inline: script
end
end