-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
Closes #90
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,41 @@ set from container's configuration file that is usually kept under | |
For other configuration options, please check [lxc.conf manpages](http://manpages.ubuntu.com/manpages/quantal/man5/lxc.conf.5.html). | ||
|
||
|
||
### Avoiding `sudo` passwords | ||
|
||
This plugin requires **a lot** of `sudo`ing since [user namespaces](https://wiki.ubuntu.com/LxcSecurity) | ||
are not supported on mainstream kernels. In order to work around that we can use | ||
a really dumb Ruby wrapper script like the one below and add a `NOPASSWD` entry | ||
to our `/etc/sudoers` file: | ||
|
||
```ruby | ||
#!/usr/bin/env ruby | ||
exec ARGV.join(' ') | ||
``` | ||
|
||
For example, you can save the code above under your `/usr/bin/lxc-vagrant-wrapper`, | ||
turn it into an executable script by running `chmod +x /usr/bin/lxc-vagrant-wrapper` | ||
and add the line below to your `/etc/sudoers` file: | ||
|
||
``` | ||
USERNAME ALL=NOPASSWD:/usr/bin/lxc-vagrant-wrapper | ||
``` | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
fgrehm
Author
Owner
|
||
|
||
In order to tell vagrant-lxc to use that script when `sudo` is needed, you can | ||
pass in the path to the script as a configuration for the provider: | ||
|
||
```ruby | ||
Vagrant.configure("2") do |config| | ||
config.vm.provider :lxc do |lxc| | ||
lxc.sudo_wrapper = '/usr/bin/lxc-vagrant-wrapper' | ||
end | ||
end | ||
``` | ||
|
||
If you want to set the `sudo_wrapper` globally, just add the code above to your | ||
`~/.vagrant.d/Vagrantfile`. | ||
|
||
|
||
### Base boxes | ||
|
||
Please check [the wiki](https://github.com/fgrehm/vagrant-lxc/wiki/Base-boxes) | ||
|
@@ -108,8 +143,6 @@ base boxes and information on [how to build your own](https://github.com/fgrehm/ | |
|
||
* The plugin does not detect forwarded ports collision, right now you are | ||
responsible for taking care of that. | ||
* There is a hell lot of `sudo`s involved and this will probably be around until | ||
[user namespaces](https://wiki.ubuntu.com/LxcSecurity) are supported or I'm able to handle [#90](https://github.com/fgrehm/vagrant-lxc/issues/90) | ||
* [Does not tell you if dependencies are not met](https://github.com/fgrehm/vagrant-lxc/issues/11) | ||
(will probably just throw up some random error) | ||
* + bunch of other [core features](https://github.com/fgrehm/vagrant-lxc/issues?labels=core&milestone=&page=1&state=open) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module Vagrant | ||
module LXC | ||
class SudoWrapper | ||
# Include this so we can use `Subprocess` more easily. | ||
include Vagrant::Util::Retryable | ||
|
||
def initialize(wrapper_path = nil) | ||
@wrapper_path = wrapper_path | ||
@logger = Log4r::Logger.new("vagrant::lxc::shell") | ||
end | ||
|
||
def run(*command) | ||
command.unshift @wrapper_path if @wrapper_path | ||
execute *(['sudo'] + command) | ||
end | ||
|
||
private | ||
|
||
# TODO: Review code below this line, it was pretty much a copy and | ||
# paste from VirtualBox base driver and has no tests | ||
def execute(*command, &block) | ||
# Get the options hash if it exists | ||
opts = {} | ||
opts = command.pop if command.last.is_a?(Hash) | ||
|
||
tries = 0 | ||
tries = 3 if opts[:retryable] | ||
|
||
sleep = opts.fetch(:sleep, 1) | ||
|
||
# Variable to store our execution result | ||
r = nil | ||
|
||
retryable(:on => LXC::Errors::ExecuteError, :tries => tries, :sleep => sleep) do | ||
# Execute the command | ||
r = raw(*command, &block) | ||
|
||
# If the command was a failure, then raise an exception that is | ||
# nicely handled by Vagrant. | ||
if r.exit_code != 0 | ||
if @interrupted | ||
@logger.info("Exit code != 0, but interrupted. Ignoring.") | ||
else | ||
raise LXC::Errors::ExecuteError, :command => command.inspect | ||
end | ||
end | ||
end | ||
|
||
# Return the output, making sure to replace any Windows-style | ||
# newlines with Unix-style. | ||
r.stdout.gsub("\r\n", "\n") | ||
end | ||
|
||
def raw(*command, &block) | ||
int_callback = lambda do | ||
@interrupted = true | ||
@logger.info("Interrupted.") | ||
end | ||
|
||
# Append in the options for subprocess | ||
command << { :notify => [:stdout, :stderr] } | ||
|
||
Vagrant::Util::Busy.busy(int_callback) do | ||
Vagrant::Util::Subprocess.execute(*command, &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
Correct me if I'm wrong, wouldn't this allow
USERNAME
to run any privileged command without a password?Wouldn't collecting all the programs that need sudo to get this plugin to work and making them
NOPASSWD
make more sense than a script that allows you to execute any command?