-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
138 lines (137 loc) · 7.01 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Little hack: add "symbolize_keys" method on Hash class (exists in ROR but not in pure Ruby)
class Hash; def symbolize_keys; Hash[self.map{ |k, v| [k.to_sym, v] }]; end; end
# Include YAML library to read config
require 'yaml'
# Init the main variables used in this file
current_dir = File.dirname(File.expand_path(__FILE__))
configuration = Hash.new
# Inject default configuration, eventually overriden by user values...
Dir["#{current_dir}/configuration/default/settings/*.yml"].each do |file|
# Extract the filename without the YAML extension to use it as "key"
key = File.basename(file, '.yml')
# Store the settings of the file in the keyed index of "configuration"
configuration.store(key, YAML.load_file(file))
# If user has override this settings, merge its defined values
if File.exist?("#{current_dir}/configuration/yours/settings/#{key}.yml")
configuration[key].merge!(YAML.load_file("#{current_dir}/configuration/yours/settings/#{key}.yml"))
end
end
# Inject default files path, eventually overriden by user ones...
['templates', 'starters'].each do |section|
configuration[section] = {} # Init the section in configuration variable
# Attach user defined files to the section configuration
if test ?d, "#{current_dir}/configuration/yours/#{section}"
Dir["#{current_dir}/configuration/yours/#{section}/*"].each do |file|
fileName = File.basename(file); configuration[section][fileName] = "#{current_dir}/configuration/yours/#{section}/#{fileName}"
end
end
# Attach default files to the section configuration
Dir["#{current_dir}/configuration/default/#{section}/*"].each do |file|
fileName = File.basename(file); if !configuration[section].key?(fileName); configuration[section][fileName] = file; end
end
end
# Fix the minimal version of vagrant to execute this script
Vagrant.require_version ">= #{configuration['vagrant']['minimal_version']}"
# Ensure required files are present, otherwise tell the user and stop
missing_files = []; configuration['vagrant']['required_files'].each do |file|
if !File.exist?("#{current_dir}/#{file}"); missing_files.push(file); print "File « #{file} » is missing.\n"; end
end; if missing_files.size > 0; exit; end
# Ensure required plugins are present, try to install if not
configuration['vagrant']['required_plugins'].each do |plugin|
if !Vagrant.has_plugin?(plugin)
print "Plugin #{plugin} is missing, try to install it..."
system "vagrant plugin install #{plugin}"
abort "Unable to install '#{plugin}'." unless Vagrant.has_plugin?(plugin)
end
end
# Load list of ignored plugins
ignored_plugins = File.exist?("#{current_dir}/configuration/yours/vagrant-plugins-ignored.yml")? \
YAML.load_file("#{current_dir}/configuration/yours/vagrant-plugins-ignored.yml"): []
# Check suggested plugins are present, ask for install if not
configuration['vagrant']['suggested_plugins'].each do |plugin|
if !Vagrant.has_plugin?(plugin) && !ignored_plugins.include?(plugin)
print "Plugin #{plugin} is missing, it's not required but suggested. Install it? [y/n/never] "
prompt = STDIN.gets.chomp
system "vagrant plugin install #{plugin}" if prompt == 'y'
# Add the plugin to the ignore list if the user denied it
if prompt == 'never'; ignored_plugins.push(plugin)
File.write "#{current_dir}/configuration/yours/vagrant-plugins-ignored.yml", YAML.dump(ignored_plugins)
end
end
end
# Configure the virtual machine
Vagrant.configure(configuration['vagrant']['config_version']) do |config|
# Set the machine name (visible in vargant status)
config.vm.define configuration['vm']['machineName']
# Allow SSH connection to the VM
config.ssh.forward_agent = true
# Change the SSH port to prevent collisions
config.ssh.port = configuration['vm']['sshPort']
# Configure the synchronized folders
configuration['vm']['syncFolders'].each do |folder|
config.vm.synced_folder folder['local'], folder['remote'], folder['options'].symbolize_keys
end
# Basic box informations
config.vm.box = configuration['vm']['box']
config.vm.hostname = configuration['vm']['hostname']
# Virtualbox specific settings...
config.vm.provider :virtualbox do |v|
v.name = configuration['vm']['hostname']
v.cpus = configuration['vm']['cpus']
v.memory = configuration['vm']['memory']
v.linked_clone = configuration['vm']['clone']
# Tell Virtualbox using specifics modifiers
configuration['vm']['virtualBoxModifiers'].each do |key, value|
v.customize ['modifyvm', :id, "--#{key}", value]
end
end
# Networking configuration...
config.vm.network :forwarded_port, id: 'ssh', guest: 22, host: configuration['vm']['sshPort']
config.vm.network :private_network, ip: configuration['vm']['private_IP'], auto_network: Vagrant.has_plugin?('vagrant-auto_network')
# Vagrant hostmanager plugin settings...
if Vagrant.has_plugin?('vagrant-hostmanager')
config.hostmanager.manage_host = true
config.hostmanager.enabled = true
config.hostmanager.aliases = []
# Build the list of domains / aliases we need on the VM
configuration['vhosts']['vhosts'].each do |domain,vhost|
config.hostmanager.aliases.push(domain)
config.hostmanager.aliases.concat(vhost['aliases']) if vhost['aliases']
end
# Tell vagrant to use Vagrant Hostmanager
config.vm.provision :hostmanager
end
# Vagrant cachier plugin settings...
if configuration['vm']['cachier']['is_activated'] = Vagrant.has_plugin?('vagrant-cachier')
config.cache.synced_folder_opts = configuration['vm']['cachier']['syncFolderOpts'].symbolize_keys
config.cache.auto_detect = (configuration['vm']['cachier']['enabledBuckets'].size == 0)
# In case of auto-detect has been disabled (buckets given), configure them...
if !config.cache.auto_detect
# Generic bucket is the only bucket with options. YAML loaded in Ruby convert keys in string but we need symbols here...
def bucketizeOptions(options) # <-- Custom function to convert "cache_dir" key to Ruby symbol
if options.key?('cache_dir'); options = options.symbolize_keys; else
options.each do |key, option|; options[key] = bucketizeOptions(option); end; end
options
end
# Activate the buckets
configuration['vm']['cachier']['enabledBuckets'].each do |bucket, options|
config.cache.enable :"#{bucket}", bucketizeOptions(options)
end
end
# Restrict the cache to this vm, or to the base box if we use clone
config.cache.scope = configuration['vm']['clone']? :box : :machine
end
# Configure ansible...
config.vm.provision 'ansible' do |ansible|
# Tell Ansible to use the appropriate playbook
if File.exist?("#{current_dir}/configuration/yours/playbook.yml")
ansible.playbook ="#{current_dir}/configuration/yours/playbook.yml"
else
ansible.playbook = "#{current_dir}/provisioning/playbook.yml"
end
# Send the generated configuration to Ansible
ansible.extra_vars = { configuration: configuration, vagrant_root: current_dir }
end
end