This repository has been archived by the owner on Sep 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
83 lines (69 loc) · 2.7 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
# Author: Andreas Litt (@LAndreas)
# Source: https://github.com/LAndreas/vagrantfile-bootstrap
VAGRANT_PLUGINS = %w(vagrant-hostsupdater)
VAGRANT_PLUGINS.each do |plugin|
unless Vagrant.has_plugin?(plugin)
puts "Plugin '#{plugin}' is not installed! Installing..."
system "vagrant plugin install #{plugin}"
end
end
# Load configration file
DOTENV_FILE = '.vagrantfile'
begin
File.read(DOTENV_FILE).each_line do |line|
line.strip!
next if line.empty? || line.start_with?('#')
k, v = line.split('=').map { |s| s.gsub('\'', '').strip }
ENV[k] = v
end
rescue Exception => e
fail "File #{DOTENV_FILE} is invalid."
end if File.exists?(DOTENV_FILE)
# Configure all the things \o/
HOST_SSH_KEYS = begin
glob_pattern = ENV['HOST_SSH_KEYS'] || '*'
Dir.glob("#{File.expand_path('~')}/.ssh/{#{glob_pattern}}.pub")
end
BOX_NAME = ENV['BOX_NAME'] || 'base'
USER_NAME = ENV['USER_NAME'] || 'vagrant'
HOSTNAME = ENV['HOSTNAME'] || 'vagrant'
PRIVATE_IP = ENV['PRIVATE_IP'] || '192.168.150.150'
USER_SHELL = '/bin/bash'
USER_HOME = "/home/#{USER_NAME}"
AUTHORIZED_KEYS = HOST_SSH_KEYS.map do |p|
File.read(p).strip
end
def output(msg)
puts "==> config: #{msg}"
end
output 'Configuration:'
output "\tBox: #{BOX_NAME}"
output "\tUser: #{USER_NAME}"
output "\tHostname: #{HOSTNAME}"
output "\tHostname Aliases: #{ENV['HOSTNAME_ALIASES']}"
output "\tPrivate IP: #{PRIVATE_IP}"
output "\tUsed SSH Keys: #{HOST_SSH_KEYS.join(', ')}"
Vagrant.configure(2) do |config|
config.vm.tap do |vm|
vm.box = BOX_NAME
vm.hostname = HOSTNAME
vm.network('private_network', ip: PRIVATE_IP)
vm.provision 'shell', inline: <<-EOF
id -u #{USER_NAME} > /dev/null 2>&1 || useradd #{USER_NAME}
usermod --home #{USER_HOME} #{USER_NAME} > /dev/null 2>&1
usermod --shell #{USER_SHELL} #{USER_NAME} > /dev/null 2>&1
echo "#{USER_NAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/00-vagrant-sudo
mkdir -p #{USER_HOME}/.ssh
touch #{USER_HOME}/.ssh/authorized_keys
echo "#{AUTHORIZED_KEYS.join("\n")}" > /tmp/host_keys
sort < /tmp/host_keys > /tmp/host_keys.sorted
sort < #{USER_HOME}/.ssh/authorized_keys > /tmp/guest_keys.sorted
cat /tmp/guest_keys.sorted > #{USER_HOME}/.ssh/authorized_keys
comm -23 /tmp/host_keys.sorted /tmp/guest_keys.sorted >> #{USER_HOME}/.ssh/authorized_keys
chown #{USER_NAME}:#{USER_NAME} #{USER_HOME} #{USER_HOME}/.ssh #{USER_HOME}/.ssh/authorized_keys
EOF
vm.provision 'shell', privileged: false, path: ENV['AFTER_PROVISION_SCRIPT'] if ENV['AFTER_PROVISION_SCRIPT']
vm.synced_folder '.', "#{USER_HOME}/app", type: 'nfs'
end
config.hostsupdater.aliases = ENV['HOSTNAME_ALIASES'].split(',').map(&:strip) if ENV['HOSTNAME_ALIASES']
end