-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVagrantfile_functionProgramming
More file actions
109 lines (96 loc) · 2.86 KB
/
Vagrantfile_functionProgramming
File metadata and controls
109 lines (96 loc) · 2.86 KB
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
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
require 'json'
filename = "config"
if File.file? "#{filename}.json"
machines = JSON.parse(File.read("#{filename}.json"))
else
machines = YAML.load_file("#{filename}.yaml")
end
Vagrant.configure("2") do |config|
machines.each do |machine|
config.vm.define "#{machine['name']}" do |new_vm|
set_box(machine, new_vm)
set_forwarded_ports(machine, new_vm)
set_cpus_and_memory(machine, new_vm)
update_package_manager(machine, new_vm)
install_packages(machine, new_vm)
set_shared_folders(machine, new_vm)
run_scripts(machine, new_vm)
end
end
end
def set_box(machine, new_vm)
new_vm.vm.box = "#{machine['box']}"
end
def set_forwarded_ports(machine, new_vm)
unless machine['forwarded_ports'].nil?
machine['forwarded_ports'].each do |ports|
new_vm.vm.network "forwarded_port", guest: ports['guest'], host: ports['host']
end
end
end
def provision_run(exe, machine, new_vm)
new_vm.vm.provider "virtualbox" do |vb|
exe[machine, vb]
end
end
def set_cpus_and_memory(machine, new_vm)
prov_machine_cpu = -> (machine, vb){ vb.cpus = "#{machine['cpus']}" }
prov_machine_mem = -> (machine, vb){ vb.memory = "#{machine['memory']}" }
unless machine['cpus'].nil?
provision_run(prov_machine_cpu, machine, new_vm)
end
unless machine['memory'].nil?
provision_run(prov_machine_mem, machine, new_vm)
end
end
#def set_cpus_and_memory(machine, new_vm)
# new_vm.vm.provider "virtualbox" do |vb|
# unless machine['cpus'].nil?
# vb.cpus = "#{machine['cpus']}"
# end
# unless machine['memory'].nil?
# vb.memory = "#{machine['memory']}"
# end
# end
#end
def update_package_manager(machine, new_vm)
unless machine['package_manager'].nil?
if machine['package_manager'] == "apt-get"
new_vm.vm.provision "shell", inline: "sudo #{machine['package_manager']} upgrade -y"
else
new_vm.vm.provision "shell", inline: "sudo #{machine['package_manager']} update -y"
end
end
end
def install_packages(machine, new_vm)
unless machine['packages'].nil?
machine['packages'].each do |pkg|
if machine['package_manager'] == "apk"
new_vm.vm.provision "shell", inline: <<-SHELL
sudo #{machine['package_manager']} add #{pkg}
SHELL
else
new_vm.vm.provision "shell", inline: <<-SHELL
sudo #{machine['package_manager']} install -y "#{pkg}"
SHELL
end
end
end
end
def set_shared_folders(machine, new_vm)
unless machine['shared_folders'].nil?
machine['shared_folders'].each do |dir|
new_vm.vm.synced_folder dir['host'], dir['guest']
end
end
end
def run_scripts(machine, new_vm)
unless machine['scripts'].nil?
machine['scripts'].each do |script|
new_vm.vm.provision "shell", path: "vagrant_scripts/#{script}"
end
end
end