-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathVagrantfile
127 lines (103 loc) · 3.66 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
# One Vagrantfile to rule them all!
#
# This is a generic Vagrantfile that can be used without modification in
# a variety of situations. Hosts and their properties are specified in
# `vagrant-hosts.yml`. Provisioning is done by a shell script in the directory
# `provisioning/` with the same name as the host name.
#
# See https://github.com/bertvv/vagrant-shell-skeleton/ for details
require 'rbconfig'
require 'yaml'
# set default LC_ALL for all BOXES
ENV["LC_ALL"] = "en_US.UTF-8"
# Set your default base box here
DEFAULT_BASE_BOX = 'bento/almalinux-9'
# Directory containing VM provisioning scripts
PROVISIONING_SCRIPT_DIR = 'scripts/'
#
# No changes needed below this point
#
VAGRANTFILE_API_VERSION = '2'
PROJECT_NAME = '/' + File.basename(Dir.getwd)
# set custom vagrant-hosts file
vagrant_hosts = ENV['VAGRANT_HOSTS'] ? ENV['VAGRANT_HOSTS'] : 'vagrant-hosts.yml'
hosts = YAML.load_file(File.join(__dir__, vagrant_hosts))
# {{{ Helper functions
# Set options for the network interface configuration. All values are
# optional, and can include:
# - ip (default = DHCP)
# - netmask (default value = 255.255.255.0
# - mac
# - auto_config (if false, Vagrant will not configure this network interface
# - intnet (if true, an internal network adapter will be created instead of a
# host-only adapter)
def network_options(host)
options = {}
if host.key?('ip')
options[:ip] = host['ip']
options[:netmask] = host['netmask'] ||= '255.255.255.0'
else
options[:type] = 'dhcp'
end
options[:mac] = host['mac'].gsub(/[-:]/, '') if host.key?('mac')
options[:auto_config] = host['auto_config'] if host.key?('auto_config')
options[:virtualbox__intnet] = true if host.key?('intnet') && host['intnet']
options
end
def custom_synced_folders(vm, host)
return unless host.key?('synced_folders')
folders = host['synced_folders']
folders.each do |folder|
vm.synced_folder folder['src'], folder['dest'], folder['options']
end
end
# Adds forwarded ports to your Vagrant machine
#
# example:
# forwarded_ports:
# - guest: 88
# host: 8080
def forwarded_ports(vm, host)
if host.has_key?('forwarded_ports')
ports = host['forwarded_ports']
ports.each do |port|
vm.network "forwarded_port", guest: port['guest'], host: port['host']
end
end
end
# }}}
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
hosts.each do |host|
config.vm.define host['name'] do |node|
node.vm.box = host['box'] ||= DEFAULT_BASE_BOX
node.vm.box_url = host['box_url'] if host.key? 'box_url'
node.vm.hostname = host['name']
node.vm.network :private_network, **network_options(host)
custom_synced_folders(node.vm, host)
forwarded_ports(node.vm, host)
# VirtualBox-specific settings
node.vm.provider :virtualbox do |vb|
vb.memory = host['memory'] if host.key? 'memory'
vb.cpus = host['cpus'] if host.key? 'cpus'
# Add VM to a VirtualBox group
# WARNING: if the name of the current directory is the same as the
# host name, this will fail.
vb.customize ['modifyvm', :id, '--groups', PROJECT_NAME]
end
# VMware Desktop-specific settings
node.vm.provider :vmware_desktop do |vmw|
# vmw.gui = true
# Must be a multiple of 4MB
vmw.vmx["memsize"] = host['memory'] if host.key? 'memory'
vmw.vmx["numvcpus"] = host['cpus'] if host.key? 'cpus'
end
# Run provisioning script for the VM, if it exists.
provisioning_script = PROVISIONING_SCRIPT_DIR + host['name'] + '.sh'
if File.exist?(provisioning_script)
node.vm.provision 'shell', path: provisioning_script
end
end
end
end
# -*- mode: ruby -*-
# vi: ft=ruby :