forked from GetValkyrie/valkyrie-mount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.rb
141 lines (124 loc) · 4.81 KB
/
mount.rb
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
# These faux plugins make NFS mount in an easy read-write capacity.
module VagrantPlugins
module Valkyrie
module Action
class Mount
def initialize(app, env)
@app = app
@machine = env[:machine]
@ui = env[:ui]
@logger = Log4r::Logger.new("ValkyrieMount::action::ValkyrieMount")
@valkyrie_vms = ENV.fetch('VALKYRIE_VMS', 'valkyrie').split(',')
@plugin_dir = File.expand_path(File.dirname(__FILE__))
@project_path = ENV.fetch('VALKYRIE_PROJECT_PATH', '.')
@semaphore_path = @project_path+'/.valkyrie/cache/first_run_complete'
@current_ssh_username = @machine.config.ssh.username
end
def call(env)
if valkyrie_vm?
if env[:machine_action] == :up
@ui.info "Checking NFS user/group mapping."
if !File.exist?(@semaphore_path)
@ui.info "Fixing NFS user/group mapping."
@ui.detail "Setting up SSH access for the 'ubuntu' user."
grant_ssh_access
@ui.detail "Refreshing SSH connection, to login as 'ubuntu'."
refresh_ssh_connection('ubuntu')
@ui.detail "Installing Ansible from sources."
install_ansible
@ui.detail "Running Ansible playbook to re-map users and groups."
run_playbook
@ui.detail "Refreshing SSH connection, to login normally."
refresh_ssh_connection(@current_ssh_username)
@ui.detail "Writing semaphore file."
write_semaphore
end
end
end
# Proceed with the rest of the middleware stack
@app.call(env)
end
def grant_ssh_access
@machine.communicate.sudo("cp /home/vagrant/.ssh/authorized_keys /home/ubuntu/.ssh/authorized_keys")
@machine.communicate.sudo("chown ubuntu:ubuntu /home/ubuntu/.ssh/authorized_keys")
@machine.communicate.sudo("chmod 600 /home/ubuntu/.ssh/authorized_keys")
end
def install_ansible
ansible_bootstrap = "https://raw.githubusercontent.com/ErgonLogicEnterprises/ansible-bootstrap/master/install-ansible-ppa.sh"
install_ansible = "curl -s #{ansible_bootstrap} | /bin/sh"
@machine.communicate.sudo(install_ansible) do |type, data|
if !data.chomp.empty?
@machine.ui.info(data.chomp)
end
end
end
def run_playbook
@machine.communicate.upload(@plugin_dir+'/mount.yml', "/tmp/mount.yml")
@machine.communicate.upload(@plugin_dir+'/inventory', "/tmp/inventory")
ansible_playbook = "PYTHONUNBUFFERED=1 "
ansible_playbook << "ANSIBLE_FORCE_COLOR=true "
ansible_playbook << "ansible-playbook "
ansible_playbook << "/tmp/mount.yml "
ansible_playbook << "-i /tmp/inventory "
ansible_playbook << "--connection=local "
ansible_playbook << "--sudo "
ansible_playbook << "--extra-vars \""
ansible_playbook << "host_os="+RUBY_PLATFORM+' '
ansible_playbook << "host_gid="+Process.gid.to_s+' '
ansible_playbook << "host_uid="+Process.uid.to_s+' '
ansible_playbook << "\""
@machine.communicate.sudo(ansible_playbook) do |type, data|
if !data.chomp.empty?
@machine.ui.info(data.chomp)
end
end
end
def refresh_ssh_connection(username)
@machine.communicate.instance_variable_get(:@connection).close
@machine.config.ssh.username = username
end
def write_semaphore
cache_path = @project_path+'/.valkyrie/cache'
system("mkdir -p #{cache_path}")
system("date > #{@semaphore_path}")
end
def valkyrie_vm?
@valkyrie_vms.include?(@machine.name.to_s)
end
end
class RemoveSemaphore < Mount
def call(env)
if valkyrie_vm?
if env[:machine_action] == :destroy
if File.exist?(@semaphore_path)
@ui.detail "Removing semaphore"
delete_semaphore
end
end
end
# Proceed with the rest of the middleware stack
@app.call(env)
end
def delete_semaphore
File.delete(@semaphore_path)
end
end
end
end
end
module VagrantPlugins
module Valkyrie
class MountPlugin < Vagrant.plugin('2')
name 'ValkyrieMount'
description <<-DESC
Improve NFS workflows for local dev.
DESC
action_hook("Valkyrie", :machine_action_up) do |hook|
hook.append(Action::Mount)
end
action_hook("Valkyrie", :machine_action_destroy) do |hook|
hook.append(Action::RemoveSemaphore)
end
end
end
end