-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVagrantfile
184 lines (157 loc) · 6.54 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'win32ole'
require 'yaml'
file_system = WIN32OLE.new("Scripting.FileSystemObject")
drives = file_system.Drives
current_dir = File.dirname(File.expand_path(__FILE__))
default_config_file = "#{current_dir}/default-config.yml"
user_config_file = "#{current_dir}/user-config.yml"
yaml_config = YAML.load_file(default_config_file)
if File.file?(user_config_file)
user_config = YAML.load_file(user_config_file)
yaml_config.merge!(user_config)
end
if yaml_config.key?("vagrant_host_only_adapter_name")
vbox_adapter = yaml_config['vagrant_host_only_adapter_name']
else
if Gem.win_platform?
vbox_adapter = 'VirtualBox Host-Only Ethernet Adapter'
else
vbox_adapter = 'vboxnet0'
end
end
Vagrant.configure("2") do |config|
if yaml_config.key?("vm_box_url")
config.vm.box = yaml_config['vm_box_url']
end
config.vm.box = yaml_config['vm_box']
config.ssh.insert_key = false
config.vagrant.plugins = ["vagrant-vbguest", "vagrant-proxyconf"]
if Vagrant.has_plugin?("vagrant-vbguest")
config.vbguest.auto_update = yaml_config['vagrant_vbguest_enabled']
config.vbguest.no_remote = true
end
if Vagrant.has_plugin?("vagrant-proxyconf")
config.proxy.enabled = yaml_config['vagrant_proxy_enabled']
end
config.vm.synced_folder ".", "/vagrant"
drives.each do |drive|
if "#{drive.DriveType}" === "2"
config.vm.synced_folder "#{drive.DriveLetter}:/", "/mnt/#{drive.DriveLetter.downcase}", :mount_options => ["rw"]
end
end
config.vm.network :forwarded_port, guest: 22, host: yaml_config['vagrant_ssh_port'], host_ip: "127.0.0.1", id: "ssh"
config.vm.network :forwarded_port, guest: 2375, host: 2375, host_ip: "127.0.0.1", id: "docker"
if "#{yaml_config['vagrant_use_host_only']}" === "1"
config.vm.network "private_network", name: vbox_adapter,
ip: yaml_config['vagrant_host_only_ip'], netmask: yaml_config['vagrant_host_only_netmask'], adapter: yaml_config['vagrant_host_only_adapter']
end
config.vm.provider :virtualbox do |vb|
vb.name = "vagrant-wsl-docker"
host = RbConfig::CONFIG['host_os']
mem_divisor = yaml_config['vagrant_mem_divisor']
cpu_divisor = yaml_config['vagrant_cpu_divisor']
if host =~ /darwin|bsd/
cpus = `sysctl -n hw.ncpu`.to_i
mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / mem_divisor
elsif host =~ /linux/
cpus = `nproc`.to_i
mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / mem_divisor
elsif host =~ /mswin|mingw/
cpus = `wmic CPU Get NumberOfLogicalProcessors /Value`.strip.split('=')[1].to_i
mem = `wmic ComputerSystem Get TotalPhysicalMemory /Value`.strip.split('=')[1].to_i / 1024 / 1024 / mem_divisor
else
cpus = 1
mem = 1024
end
if cpus > 1
cpus = cpus / cpu_divisor
end
vb.customize ["modifyvm", :id, "--memory", mem]
vb.customize ["modifyvm", :id, "--cpus", cpus]
vb.customize ["modifyvm", :id, "--audio", "none"]
end
$script = <<-SCRIPT
type firewall-cmd
if [[ $? == 0 ]]; then
sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo firewall-cmd --zone=public --permanent --add-port=2375/tcp
sudo firewall-cmd --zone=public --add-port=2375/tcp
sudo firewall-cmd --zone=public --permanent --add-port=2370/tcp
sudo firewall-cmd --zone=public --add-port=2370/tcp
fi
SCRIPT
config.vm.provision "shell", inline: $script
if "#{yaml_config['vm_box']}".include?("centos/7") || "#{yaml_config['vm_box']}".include?("centos7") then
puts "==> CentOS 7 detected"
install_cmd = "sudo yum install -y epel-release python-pip haveged"
end
if "#{yaml_config['vm_box']}".include?("almalinux") || "#{yaml_config['vm_box']}".include?("almalinux") then
puts "==> AlmaLinux detected"
install_cmd = "sudo dnf install -y epel-release python-pip haveged"
end
if "#{yaml_config['vm_box']}".include?("ubuntu") then
puts "==> Ubuntu detected"
install_cmd = "sudo apt-get install -y python3-pip python-is-python3 haveged && sudo ln -s -f /usr/bin/pip3 /usr/bin/pip"
end
config.vm.provision "ansible_local" do |ansible|
ansible.verbose = false
ansible.compatibility_mode = "2.0"
ansible.install_mode = "pip_args_only"
ansible.pip_install_cmd = "#{install_cmd}"
ansible.pip_args = "ansible"
ansible.version = "latest"
ansible.become = true
ansible.playbook = "ansible/prepare.yml"
ansible.inventory_path = "ansible/hosts"
ansible.limit = "docker"
end
config.vm.provision "ansible_local" do |ansible|
ansible.install = false
ansible.compatibility_mode = "2.0"
ansible.install_mode = "pip_args_only"
ansible.pip_install_cmd = "#{install_cmd}"
ansible.pip_args = "ansible"
ansible.verbose = false
ansible.become = true
ansible.galaxy_role_file = yaml_config['ansible_requirements']
ansible.galaxy_roles_path = "/etc/ansible/roles"
ansible.galaxy_command = "sudo $(which ansible-galaxy) install --role-file=%{role_file} --roles-path=%{roles_path} --force"
ansible.playbook = yaml_config['ansible_playbook']
ansible.inventory_path = "ansible/hosts"
ansible.limit = "docker"
if yaml_config['vagrant_proxy_enabled'] === true
ansible.extra_vars = {
docker_daemon_envs: {
HTTP_PROXY: "#{yaml_config['vagrant_proxy_http']}",
HTTPS_PROXY: "#{yaml_config['vagrant_proxy_https']}",
NO_PROXY: "#{yaml_config['vagrant_proxy_no']}"
}
}
end
end
if "#{yaml_config['vagrant_use_host_only']}" === "1"
config.vm.post_up_message = <<MESSAGE
--------------------------------------------------------------------------------
Docker VM is now ready for use!
Local SSH port: #{yaml_config['vagrant_ssh_port']}
Host-only adapter IP: #{yaml_config['vagrant_host_only_ip']}
Access from outside VM:
export DOCKER_HOST=tcp://localhost:2375
.. or ..
export DOCKER_HOST=tcp://#{yaml_config['vagrant_host_only_ip']}:2375
--------------------------------------------------------------------------------
MESSAGE
else
config.vm.post_up_message = <<MESSAGE
--------------------------------------------------------------------------------
Docker VM is now ready for use!
Local SSH port: #{yaml_config['vagrant_ssh_port']}
Access from outside VM:
export DOCKER_HOST=tcp://localhost:2375
--------------------------------------------------------------------------------
MESSAGE
end
end