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',
|
2015-01-11 22:59:38 +00:00
|
|
|
:template_root => Vagrant::LXC.source_root.join('templates').to_s,
|
|
|
|
:cmd_paths => build_cmd_paths_hash,
|
|
|
|
:pipework_regex => "#{ENV['HOME']}/\.vagrant\.d/gems/gems/vagrant-lxc.+/scripts/pipework"
|
2014-07-25 00:50:24 +00:00
|
|
|
)
|
|
|
|
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|
|
2015-01-11 22:59:38 +00:00
|
|
|
%w( which cat mkdir cp chown chmod rm tar chown ip ifconfig brctl ).each do |cmd|
|
2016-05-11 18:21:26 +00:00
|
|
|
hash[cmd] = `sudo which #{cmd}`.strip
|
2014-07-25 00:50:24 +00:00
|
|
|
end
|
2016-05-11 18:21:26 +00:00
|
|
|
hash['lxc_bin'] = Pathname(`sudo which lxc-create`.strip).parent.to_s
|
2015-03-19 23:21:57 +00:00
|
|
|
hash['ruby'] = Gem.ruby
|
2014-07-25 00:50:24 +00:00
|
|
|
end
|
|
|
|
end
|
2014-03-16 17:59:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|