2014-03-16 17:59:18 +00:00
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
module Vagrant
|
|
|
|
module LXC
|
|
|
|
module Command
|
|
|
|
class Sudoers < Vagrant.plugin("2", :command)
|
2014-04-08 17:58:07 +00:00
|
|
|
|
|
|
|
def initialize(argv, env)
|
|
|
|
super
|
2014-04-23 14:27:33 +00:00
|
|
|
@argv
|
2014-04-08 17:58:07 +00:00
|
|
|
@env = env
|
|
|
|
end
|
|
|
|
|
2014-03-16 17:59:18 +00:00
|
|
|
def execute
|
2014-03-21 22:48:34 +00:00
|
|
|
options = { user: ENV['USER'] }
|
2014-03-16 17:59:18 +00:00
|
|
|
|
|
|
|
opts = OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: vagrant lxc sudoers"
|
|
|
|
opts.separator ""
|
2014-04-23 14:27:33 +00:00
|
|
|
opts.on('-u user', '--user user', String, "The user for which to create the policy (defaults to '#{options[:user]}')") do |u|
|
2014-03-21 22:53:49 +00:00
|
|
|
options[:user] = u
|
2014-03-16 17:59:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
argv = parse_options(opts)
|
|
|
|
return unless argv
|
|
|
|
|
2014-04-08 17:58:07 +00:00
|
|
|
wrapper_path = Vagrant::LXC.sudo_wrapper_path
|
|
|
|
wrapper = create_wrapper!
|
|
|
|
sudoers = create_sudoers!(options[:user], wrapper_path)
|
|
|
|
|
|
|
|
su_copy([
|
|
|
|
{source: wrapper, target: wrapper_path, mode: "0555"},
|
|
|
|
{source: sudoers, target: sudoers_path, mode: "0440"}
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
|
|
|
def sudoers_path
|
2014-09-23 02:07:12 +00:00
|
|
|
"/etc/sudoers.d/vagrant-lxc"
|
2014-03-16 17:59:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2014-07-25 00:50:24 +00:00
|
|
|
|
|
|
|
# This requires vagrant 1.5.2+ https://github.com/mitchellh/vagrant/commit/3371c3716278071680af9b526ba19235c79c64cb
|
2014-04-08 17:58:07 +00:00
|
|
|
def create_wrapper!
|
|
|
|
wrapper = Tempfile.new('lxc-wrapper').tap do |file|
|
2014-07-25 00:50:24 +00:00
|
|
|
template = Vagrant::Util::TemplateRenderer.new(
|
|
|
|
'sudoers.rb',
|
|
|
|
:template_root => Vagrant::LXC.source_root.join('templates').to_s,
|
|
|
|
:cmd_paths => build_cmd_paths_hash
|
|
|
|
)
|
|
|
|
file.puts template.render
|
2014-04-08 17:58:07 +00:00
|
|
|
end
|
|
|
|
wrapper.close
|
|
|
|
wrapper.path
|
|
|
|
end
|
2014-03-21 22:53:49 +00:00
|
|
|
|
2014-04-08 17:58:07 +00:00
|
|
|
def create_sudoers!(user, command)
|
|
|
|
sudoers = Tempfile.new('vagrant-lxc-sudoers').tap do |file|
|
|
|
|
file.puts "# Automatically created by vagrant-lxc"
|
2014-06-09 02:44:02 +00:00
|
|
|
file.puts "#{user} ALL=(root) NOPASSWD: #{command}"
|
2014-03-16 17:59:18 +00:00
|
|
|
end
|
|
|
|
sudoers.close
|
|
|
|
sudoers.path
|
|
|
|
end
|
|
|
|
|
2014-04-08 17:58:07 +00:00
|
|
|
def su_copy(files)
|
|
|
|
commands = files.map { |file|
|
|
|
|
[
|
|
|
|
"rm -f #{file[:target]}",
|
|
|
|
"cp #{file[:source]} #{file[:target]}",
|
|
|
|
"chown root:root #{file[:target]}",
|
|
|
|
"chmod #{file[:mode]} #{file[:target]}"
|
|
|
|
]
|
|
|
|
}.flatten
|
|
|
|
system "echo \"#{commands.join("; ")}\" | sudo sh"
|
2014-03-16 17:59:18 +00:00
|
|
|
end
|
2014-07-25 00:50:24 +00:00
|
|
|
|
|
|
|
def build_cmd_paths_hash
|
|
|
|
{}.tap do |hash|
|
|
|
|
%w( which cat mkdir cp chown chmod rm tar chown ).each do |cmd|
|
|
|
|
hash[cmd] = `which #{cmd}`.strip
|
|
|
|
end
|
|
|
|
hash['lxc_bin'] = Pathname(`which lxc-create`.strip).parent.to_s
|
|
|
|
end
|
|
|
|
end
|
2014-03-16 17:59:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|