-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVagrantfile
192 lines (165 loc) · 6.29 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'fileutils'
require 'pathname'
require 'tempfile'
require 'yaml'
# ===== Settings Loader =====
def load_settings(overrides)
# default settings
settings = {
'ansible_extra_vars' => {},
'ansible_inventory_path' => 'vagrant.py',
'ansible_limit' => 'all',
'ansible_playbook' => nil,
'ansible_raw_arguments' => nil,
'box' => 'ubuntu/trusty64',
'disable_default_synced_folder' => true,
'domain' => '.local',
'hostmanager_enabled' => true,
'hostmanager_ignore_private_ip' => false,
'hostmanager_include_offline' => true,
'hostmanager_manage_guest' => true,
'hostmanager_manage_host' => true,
'memory' => 512,
'provider' => 'virtualbox',
'shell' => nil,
'synced_directories' => [],
'user' => 'vagrant',
'vms' => [{'name' => 'default', 'ip' => '192.168.123.123'}]
}
# merge overrides
settings.merge!(overrides)
# inherit global settings for undefined vm settings
# using select to avoid inheriting certain keys
# - vms: we don't want settings['vms']['vms']
# - shell: global shell setting defines a shell command that runs on all hosts
# - hostmanager*: hostmanager settings do not apply to vms
vm_settings = settings.select {|k,v| k != 'vms' and k != 'shell' and not k.start_with?('hostmanager')}
settings['vms'].map! {|vm| vm_settings.merge(vm)}
return settings
end
# ===== SSH Config =====
def update_ssh_config(domain, vms)
# setup variables
ssh_config_path = ENV['HOME'] + '/.ssh/config'
# assemble ssh config string
ssh_start_msg = "## start vagrant managed config ##\n"
ssh_end_msg = "## end vagrant managed config ##\n"
ssh_hosts = "Host *#{domain}\n"\
" UserKnownHostsFile /dev/null\n"\
" StrictHostKeyChecking no\n"\
" PasswordAuthentication no\n"\
" IdentitiesOnly yes\n"\
" LogLevel FATAL\n"
vms.each do |vm|
ssh_hosts += "Host #{vm['name'] + domain}\n"\
" User #{vm['user']}\n"\
" HostName #{vm['ip']}\n"\
" IdentityFile #{Pathname.new(__FILE__).dirname}/.vagrant/machines/#{vm['name']}/virtualbox/private_key\n"
end
ssh_config = ssh_start_msg + ssh_hosts + ssh_end_msg
# create ~/.ssh/config
if not File.exists?(ssh_config_path)
File.new(ssh_config_path, 'w')
end
# check if ~/.ssh/config is currently managed by vagrant
vagrant_managed_file = File.readlines(ssh_config_path).grep(ssh_start_msg).any?
# append vagrant ssh config if it doesn't exist, or
# overwrite existing vagrant ssh config
if not vagrant_managed_file
File.open(ssh_config_path, 'a') do |file|
file.puts "\n" + ssh_config
end
else
# open temp file for updating
tmp_file = Tempfile.new('ssh_config-' + Time.now.to_i.to_s)
begin
File.open(ssh_config_path, 'r') do |file|
vagrant_managed = false
file.each_line do |line|
if line == ssh_start_msg
vagrant_managed = true
tmp_file.puts ssh_config
next
elsif line == ssh_end_msg
vagrant_managed = false
elsif vagrant_managed == false
tmp_file.puts line
end
end
end
tmp_file.close
FileUtils.mv(tmp_file.path, ssh_config_path)
ensure
tmp_file.close
tmp_file.unlink
end
end
end
# ===== Vagrant Config =====
# get dirname of Vagrantfile
dirname = Pathname.new(__FILE__).dirname
# load yaml vagrant config
vagrant_config = dirname + 'vagrant.yml'
if File.exists?(vagrant_config)
settings = load_settings(YAML.load_file(vagrant_config))
else
settings = load_settings({})
end
# update ssh config
update_ssh_config(settings['domain'], settings['vms'])
Vagrant.configure(2) do |config|
# hostmanager config
if settings['hostmanager_enabled']
config.hostmanager.enabled = settings['hostmanager_enabled']
config.hostmanager.ignore_private_ip = settings['hostmanager_ignore_private_ip']
config.hostmanager.include_offline = settings['hostmanager_include_offline']
config.hostmanager.manage_guest = settings['hostmanager_manage_guest']
config.hostmanager.manage_host = settings['hostmanager_manage_host']
end
# base shell commands
config.vm.provision 'shell', inline: settings['shell'] if settings['shell']
# configure VMs
settings['vms'].each do |vm|
config.vm.define vm['name'] do |item|
item.vm.box = vm['box']
item.vm.hostname = vm['name'] + settings['domain']
# default alias to vm name
item.hostmanager.aliases = vm['aliases'].collect { |a| a + settings['domain'] } if settings['hostmanager_enabled'] and vm['aliases']
# vm network config
item.vm.network 'private_network', ip: vm['ip']
# vm provider and resource settings
item.vm.provider vm['provider'] do |vb|
vb.memory = vm['memory']
# Use paravirtualized network in virtualbox for better performance
# https://www.virtualbox.org/manual/ch06.html#nichardware
if vm['provider'] == 'virtualbox'
vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
end
end
# disable default synced folder
item.vm.synced_folder ".", "/vagrant", disabled: settings['disable_default_synced_folder']
# vm synced folders
vm['synced_directories'].each do |mnt|
item.vm.synced_folder mnt['src'], mnt['dest'],
owner: vm['user'],
group: vm['user']
end
# vm shell commands
item.vm.provision 'shell', inline: vm['shell'] if vm['shell']
# run ansible on vm
if vm['ansible_playbook']
item.vm.provision 'ansible' do |ansible|
ansible.host_key_checking = false
ansible.inventory_path = dirname + vm['ansible_inventory_path']
ansible.limit = vm['ansible_limit']
ansible.raw_arguments = vm['ansible_raw_arguments']
ansible.extra_vars = vm['ansible_extra_vars']
ansible.playbook = vm['ansible_playbook']
end
end
end
end
end