class Vagrant::LXC::Command::Sudoers

Public Class Methods

new(argv, env) click to toggle source
Calls superclass method
# File lib/vagrant-lxc/command/sudoers.rb, line 8
def initialize(argv, env)
  super
  @argv
  @env = env
end

Public Instance Methods

execute() click to toggle source
# File lib/vagrant-lxc/command/sudoers.rb, line 14
def execute
  options = { user: ENV['USER'] }

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: vagrant lxc sudoers"
    opts.separator ""
    opts.on('-u user', '--user user', String, "The user for which to create the policy (defaults to '#{options[:user]}')") do |u|
      options[:user] = u
    end
  end

  argv = parse_options(opts)
  return unless argv

  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
sudoers_path() click to toggle source
# File lib/vagrant-lxc/command/sudoers.rb, line 38
def sudoers_path
  "/etc/sudoers.d/vagrant-lxc"
end

Private Instance Methods

build_cmd_paths_hash() click to toggle source
# File lib/vagrant-lxc/command/sudoers.rb, line 82
def build_cmd_paths_hash
  {}.tap do |hash|
    %w( which cat mkdir cp chown chmod rm tar chown ip ifconfig brctl ).each do |cmd|
      hash[cmd] = %x`which #{cmd}`.strip
    end
    hash['lxc_bin'] = Pathname(%x`which lxc-create`.strip).parent.to_s
  end
end
create_sudoers!(user, command) click to toggle source
# File lib/vagrant-lxc/command/sudoers.rb, line 60
def create_sudoers!(user, command)
  sudoers = Tempfile.new('vagrant-lxc-sudoers').tap do |file|
    file.puts "# Automatically created by vagrant-lxc"
    #file.puts "#{user} ALL=(root) NOPASSWD: #{command}"
    file.puts "%vagrant ALL=(root) NOPASSWD: #{command}"
  end
  sudoers.close
  sudoers.path
end
create_wrapper!() click to toggle source

This requires vagrant 1.5.2+ github.com/mitchellh/vagrant/commit/3371c3716278071680af9b526ba19235c79c64cb

# File lib/vagrant-lxc/command/sudoers.rb, line 45
def create_wrapper!
  wrapper = Tempfile.new('lxc-wrapper').tap do |file|
    template = Vagrant::Util::TemplateRenderer.new(
      'sudoers.rb',
      :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"
      :pipework_regex => "/usr/share/vagrant/gems/gems/vagrant-lxc.+/scripts/pipework"
    )
    file.puts template.render
  end
  wrapper.close
  wrapper.path
end
su_copy(files) click to toggle source
# File lib/vagrant-lxc/command/sudoers.rb, line 70
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"
end