-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVagrantfile
More file actions
188 lines (147 loc) · 5.91 KB
/
Copy pathVagrantfile
File metadata and controls
188 lines (147 loc) · 5.91 KB
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
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
# Shamelessly ripped from Active Support core_ext. Need a better solution. Later.
class Hash
def deep_merge(other_hash, &block)
dup.deep_merge!(other_hash, &block)
end
def deep_merge!(other_hash, &block)
other_hash.each_pair do |current_key, other_value|
this_value = self[current_key]
self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
this_value.deep_merge(other_value, &block)
else
if block_given? && key?(current_key)
block.call(current_key, this_value, other_value)
else
other_value
end
end
end
self
end
end
settings = YAML.load_file 'vagrant.yml'
REQUIRED_PLUGINS = %w( vagrant-vbguest vagrant-berkshelf vagrant-omnibus vagrant-triggers)
PASSWORD_ERROR_MESSAGE = "You need to set your mariadb password in vagrant.yml to something other than 'password'"
def missing_plugins
@missing_plugins ||= REQUIRED_PLUGINS.reject { |plugin| Vagrant.has_plugin? plugin }
end
def missing_multiple_plugins?
missing_plugins.size > 1
end
if missing_plugins.any?
STDERR << "The following plugin#{'s' if missing_multiple_plugins?} are required: #{missing_plugins.join(" ")}\n"
STDERR << "Install #{missing_multiple_plugins? ? 'them' : 'it'} with this command: 'vagrant plugin install #{missing_plugins.join(" ")}'\n"
end
fail 'Required Vagrant plugins not installed. Please install before continuing' if missing_plugins.any?
fail PASSWORD_ERROR_MESSAGE if settings['mariadb']['password'] == 'password'
project_name = settings['project']['name']
mariadb_username = settings['mariadb']['username']
mariadb_password = settings['mariadb']['password']
forwarded_ports = settings['forwarded_ports'] || { :"3000" => 3000 }
vagrant_cpu_count = settings.fetch('vm', {}).fetch('cpu_count', 2)
vagrant_memory_size = settings.fetch('vm', {}).fetch('memory_size', '4096').to_s
vbguest_iso_path = settings.fetch('vbguest', {}).fetch('iso_path', nil)
chef_json = JSON.parse(File.read('vagrant.json'))
Vagrant.configure(2) do |config|
# Prevent problematic caching of synced folders
def remove_synced_folders_file(*_args)
`rm .vagrant/machines/default/virtualbox/synced_folders`
end
config.trigger.before [:reload], stdout: true, &method(:remove_synced_folders_file)
config.trigger.after [:halt], stdout: true, &method(:remove_synced_folders_file)
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
config.vm.box = 'ubuntu/xenial64'
config.vm.synced_folder '.', "/home/ubuntu/#{project_name}"
config.vbguest.auto_update = true
config.vbguest.iso_path = vbguest_iso_path if vbguest_iso_path
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.name = project_name
vb.memory = vagrant_memory_size
vb.cpus = vagrant_cpu_count
end
config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", auto_correct: true
forwarded_ports.each do |guest_port, host_port|
config.vm.network :forwarded_port, guest_ip: '0.0.0.0', guest: guest_port, host: host_port, autocorrect: true
end
config.ssh.keys_only = true
config.ssh.insert_key = true
config.ssh.forward_agent = true
config.omnibus.chef_version = '12.12.15'
config.berkshelf.enabled = false
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = ['cookbooks', 'site-cookbooks', 'berks-cookbooks']
chef.add_recipe 'apt_config'
chef.add_recipe 'apt'
chef.json = {
apt: {
compiletime: true
},
"run_list" => %w[
recipe[apt_config],
recipe[apt]
]
}
end
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = ['cookbooks', 'site-cookbooks', 'berks-cookbooks']
chef.add_recipe 'apt'
chef.add_recipe 'build-essential'
chef.add_recipe 'nodejs'
chef.add_recipe 'ruby_build'
chef.add_recipe 'ruby_rbenv::user'
chef.add_recipe 'mariadb::server'
chef.add_recipe 'mariadb::client'
chef.json = chef_json
end
user_creation_mysql = <<-SCRIPT_SQL
CREATE USER IF NOT EXISTS '#{mariadb_username}'@'localhost' IDENTIFIED BY '#{mariadb_password}';
CREATE USER IF NOT EXISTS '#{mariadb_username}'@'%' IDENTIFIED BY '#{mariadb_password}';
GRANT ALL PRIVILEGES ON \\`#{mariadb_username}\\` . * TO '#{mariadb_username}'@'localhost';
GRANT ALL PRIVILEGES ON \\`#{mariadb_username}\\` . * TO '#{mariadb_username}'@'%';
GRANT ALL PRIVILEGES ON \\`#{mariadb_username}_%\\` . * TO '#{mariadb_username}'@'localhost';
GRANT ALL PRIVILEGES ON \\`#{mariadb_username}_%\\` . * TO '#{mariadb_username}'@'%';
SCRIPT_SQL
setup_mariadb_script = <<-SCRIPT
#!/bin/bash
if [ ! -f ~/.setup_mariadb_script ]
then
echo '===== Creating MariaDB user ====='
echo "#{user_creation_mysql}"
mysql --password=this_is_a_really_terrible_password -e "#{user_creation_mysql}"
touch ~/.setup_mariadb_script
fi
SCRIPT
change_ssh_directory_script = <<-SCRIPT
if [ $(pwd) != "~/#{project_name}/#{project_name}" ]; then
echo "cd ~/#{project_name}/#{project_name}" >> ~/.profile
fi
SCRIPT
initialize_rails_app = <<-SCRIPT
echo 'Bundling...'
cd /home/ubuntu/#{project_name}/mod-picker
bundle
rbenv rehash
echo 'Creating DB'
rake db:create
echo 'Loading Schema'
rake db:schema:load
echo 'Seeding DB'
rake db:seed
SCRIPT
unprivileged_initialize_script = <<-SCRIPT
#!/bin/bash
if [ ! -f ~/.unprivileged_initialized ]
then
#{initialize_rails_app}
touch ~/.unprivileged_initialized
fi
SCRIPT
config.vm.provision :shell, name: 'Setup MariaDB', inline: setup_mariadb_script
config.vm.provision :shell, privileged: false, name: 'Unprivileged Initialization', inline: unprivileged_initialize_script
config.vm.provision :shell, name: 'Setup Home Directory', privileged: false, inline: change_ssh_directory_script
end