-
Notifications
You must be signed in to change notification settings - Fork 2
/
Vagrantfile
109 lines (95 loc) · 3.17 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
# Iterate over directories to find the owner of the VagrantFile
paths = Dir.pwd.split('/')
hostname = nil
while hostname == nil do
dir = paths.join('/')
current = paths.pop
if File.exists?("#{dir}/VagrantFile")
hostname = current.gsub('.', '')
end
end
# Default vagrant config.
vconfig = {
"vagrant_hostname" => "#{hostname}.local",
"vagrant_synced_folders" => [{
"local_path" => ".",
"destination" => "/drupal",
"id" => "drupal",
"type" => "nfs"
}],
"vagrant_memory" => 1024,
"vagrant_cpus" => 2
}
# User defined vagrant config.
vagrant_config = "#{dir}/.vagrant.config.yml"
if File.exist?(vagrant_config)
user_config = YAML::load_file(vagrant_config)
vconfig = vconfig.merge user_config
end
Vagrant.require_version '>= 1.7.0'
Vagrant.configure("2") do |config|
config.vm.hostname = vconfig['vagrant_hostname']
config.vm.network :private_network, type: 'dhcp'
config.ssh.insert_key = false
config.ssh.forward_agent = true
# Synced folders.
for synced_folder in vconfig['vagrant_synced_folders'];
config.vm.synced_folder synced_folder['local_path'], synced_folder['destination'],
type: synced_folder['type'],
rsync__auto: "true",
rsync__exclude: synced_folder['excluded_paths'],
rsync__args: ["--verbose", "--archive", "--delete", "-z", "--chmod=ugo=rwX"],
id: synced_folder['id']
end
# Ansible
ansible_config = "#{dir}/.vagrant.ansible.yml"
if File.exist?(ansible_config)
# Upload .vagrant.ansible.yml
config.vm.provision "ansible_config", type: "file" do |s|
s.source = ansible_config
s.destination = "/ansible/vagrant.config.yml"
end
end
# Provision box
config.vm.provision "ansible", type: "shell" do |s|
s.privileged = true
s.inline = 'ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook /ansible/playbook.yml --sudo -c local -i "127.0.0.1vagrant,"'
end
# VirtualBox.
config.vm.provider :virtualbox do |v|
v.name = vconfig['vagrant_hostname']
v.memory = vconfig['vagrant_memory']
v.cpus = vconfig['vagrant_cpus']
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--ioapic", "on"]
end
# Create drush alias.
if File.directory?("#{Dir.home}/.drush")
alias_file = "#{Dir.home}/.drush/" + vconfig['vagrant_hostname'] + ".aliases.drushrc.php"
if ARGV[0] == "destroy"
File.delete(alias_file) if File.exist?(alias_file)
else
require 'erb'
class DrushAlias
attr_accessor :hostname, :uri, :key, :root
def template_binding
binding
end
end
template_file = File.dirname(File.expand_path(__FILE__)) + '/drush.alias.erb'
template = File.read(template_file)
# @TODO - Do this better.
alias_file = File.open(alias_file, "w+")
da = DrushAlias.new
da.hostname = vconfig['vagrant_hostname']
da.uri = vconfig['vagrant_hostname']
da.key = "#{Dir.home}/.vagrant.d/insecure_private_key"
da.root = vconfig['vagrant_synced_folders'][0]['destination']
alias_file << ERB.new(template).result(da.template_binding)
alias_file.close
end
end
end