2014-03-16 17:59:18 +00:00
|
|
|
require 'tempfile'
|
|
|
|
|
2017-12-11 16:48:19 +00:00
|
|
|
require "vagrant-lxc/driver"
|
|
|
|
|
2014-03-16 17:59:18 +00:00
|
|
|
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!
|
2017-12-11 16:48:19 +00:00
|
|
|
lxc_base_path = Driver.new(0, LXC.sudo_wrapper).containers_path
|
2014-04-08 17:58:07 +00:00
|
|
|
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,
|
2017-12-11 16:48:19 +00:00
|
|
|
:lxc_base_path => lxc_base_path,
|
2017-02-07 20:40:32 +00:00
|
|
|
:pipework_regex => "#{ENV['HOME']}/\.vagrant\.d/gems/(?:\\d+?\\.\\d+?\\.\\d+?/)?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|
|
2014-07-25 00:50:24 +00:00
|
|
|
hash[cmd] = `which #{cmd}`.strip
|
|
|
|
end
|
|
|
|
hash['lxc_bin'] = Pathname(`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
|