forked from devudo/vagrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
88 lines (67 loc) · 3.11 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Look for project settings file.
if !(File.exists?("#{File.dirname(__FILE__)}/settings.yml"))
raise NoSettingsException
end
# Load the yml files
require 'yaml'
settings = YAML.load_file("#{File.dirname(__FILE__)}/settings.yml")
settings.merge!(YAML.load_file("#{File.dirname(__FILE__)}/settings.local.yml"))
# Clone project repo to `./src` if the folder doesn't exist yet, and the setting exists.
if !(File.directory?("#{File.dirname(__FILE__)}/app"))
system("git clone #{settings['app_repo']} #{File.dirname(__FILE__)}/app")
end
# Config the VM
config.vm.box = "hashicorp/precise64"
config.vm.hostname = settings['server_hostname']
config.nfs.map_uid = 1010
config.nfs.map_gid = 1010
# Sets IP of the guest machine and allows it to connect to the internet.
# @TODO: Add the adapter to settings.global.yml. Almost always wlan0
config.vm.network :private_network, ip: settings['vansible_ip']
config.vm.network :public_network, bridge: settings['vansible_adapter']
# Sync .ssh folder to guest machine.
config.vm.synced_folder "#{Dir.home}/.ssh", "/home/vagrant/.ssh_host"
# Read this user's host machine's public ssh key to pass to ansible.
if !(File.exists?("#{Dir.home}/.ssh/id_rsa.pub"))
raise NoSshKeyException
end
ssh_public_key = IO.read("#{Dir.home}/.ssh/id_rsa.pub").strip!
# ONLY WORKS if ansible is setup on the HOST machine.
# See https://github.com/mitchellh/vagrant/issues/2103
# @TODO: Uncomment once vagrant supports this.
# config.vm.provision "ansible" do |ansible|
# ansible.playbook = settings['vansible_playbook']
# ansible.tags = settings['vansible_tags']
# end
# Setup ansible and run the playbook.
# @TODO: Remove once vagrant supports ansible on guest.
config.vm.provision "shell", path: "tasks/setup-ansible.sh"
# Run ansible Provisioner via shell.
config.vm.provision "shell",
inline: "cd /vagrant; ansible-playbook -c local -i '#{settings['server_hostname']},' #{settings['vansible_playbook']} --extra-vars 'authorized_keys=\"#{ssh_public_key}\"'"
# Extra provisioning for vagrant
config.vm.provision "shell",
inline: "cd /vagrant; ansible-playbook -c local -i '#{settings['server_hostname']},' provision.vagrant.yml"
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", settings['vansible_memory']]
end
# Make local source code available to the VM
config.vm.synced_folder "app", "/app",
type: 'nfs'
end
##
# Our Exceptions
#
class NoSettingsException < Vagrant::Errors::VagrantError
error_message('Project settings file not found. Copy settings.project.example.yml to settings.project.yml, edit to match your project, then try again.')
end
class NoSrcException < Vagrant::Errors::VagrantError
error_message('Could not create ./src folder. Run as the owner of this folder. ')
end
class NoSshKeyException < Vagrant::Errors::VagrantError
error_message('An ssh public key could not be found at ~/.ssh/id_rsa.pub. Please generate one and try again.')
end