class Vagrant::LXC::Driver::CLI

Attributes

name[RW]

Public Class Methods

new(sudo_wrapper, name = nil) click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 20
def initialize(sudo_wrapper, name = nil)
  @sudo_wrapper = sudo_wrapper
  @name         = name
  @logger       = Log4r::Logger.new("vagrant::provider::lxc::container::cli")
end

Public Instance Methods

attach(*cmd) click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 93
def attach(*cmd)
  cmd = ['--'] + cmd

  if cmd.last.is_a?(Hash)
    opts       = cmd.pop
    namespaces = Array(opts[:namespaces]).map(&:upcase).join('|')

    # HACK: The wrapper script should be able to handle this
    if @sudo_wrapper.wrapper_path
      namespaces = "'#{namespaces}'"
    end

    if namespaces
      if supports_attach_with_namespaces?
        extra = ['--namespaces', namespaces]
      else
        raise LXC::Errors::NamespacesNotSupported
      end
    end
  end

  run :attach, '--name', @name, *((extra || []) + cmd)
end
config(param) click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 41
def config(param)
  if support_config_command?
    run(:config, param).gsub("\n", '')
  else
    raise Errors::CommandNotSupported, name: 'config', available_version: '> 1.x.x', version: version
  end
end
create(template, backingstore, backingstore_options, config_file, template_opts = {}) click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 57
def create(template, backingstore, backingstore_options, config_file, template_opts = {})
  if config_file
    config_opts = ['-f', config_file]
  end

  extra = template_opts.to_a.flatten
  extra.unshift '--' unless extra.empty?

  run :create,
      '-B', backingstore,
      '--template', template,
      '--name',     @name,
      *(backingstore_options.to_a.flatten),
      *(config_opts),
      *extra
rescue Errors::ExecuteError => e
  if e.stderr =~ /already exists/i
    raise Errors::ContainerAlreadyExists, name: @name
  else
    raise
  end
end
destroy() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 80
def destroy
  run :destroy, '--name', @name
end
list() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 26
def list
  run(:ls).split(/\s+/).uniq
end
start(options = []) click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 84
def start(options = [])
  run :start, '-d', '--name', @name, *Array(options)
end
state() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 49
def state
  if @name && run(:info, '--name', @name, retryable: true) =~ /^state:[^A-Z]+([A-Z]+)$/i
    $1.downcase.to_sym
  elsif @name
    :unknown
  end
end
stop() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 88
def stop
  attach '/sbin/halt' if supports_attach?
  run :stop, '--name', @name
end
support_config_command?() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 147
def support_config_command?
  version[0].to_i >= 1
end
support_version_command?() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 151
def support_version_command?
  @sudo_wrapper.run('which', 'lxc-version').strip.chomp != ''
rescue Vagrant::LXC::Errors::ExecuteError
  return false
end
supports_attach?() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 134
def supports_attach?
  unless defined?(@supports_attach)
    begin
      @supports_attach = true
      run(:attach, '--name', @name, '--', '/bin/true')
    rescue LXC::Errors::ExecuteError
      @supports_attach = false
    end
  end

  return @supports_attach
end
transition_to(target_state, tries = 30, timeout = 1) { |self| ... } click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 117
def transition_to(target_state, tries = 30, timeout = 1, &block)
  raise TransitionBlockNotProvided unless block_given?

  yield self

  while (last_state = self.state) != target_state && tries > 0
    @logger.debug "Target state '#{target_state}' not reached, currently on '#{last_state}'"
    sleep timeout
    tries -= 1
  end

  unless last_state == target_state
    # TODO: Raise an user friendly message
    raise TargetStateNotReached.new target_state, last_state
  end
end
version() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 30
def version
  return @version if @version
  @version = support_version_command? ? run(:version) : run(:create, '--version')
  if @version =~ /(lxc version:\s+|)(.+)\s*$/
    @version = $2.downcase
  else
    # TODO: Raise an user friendly error
    raise 'Unable to parse lxc version!'
  end
end

Private Instance Methods

run(command, *args) click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 159
def run(command, *args)
  @sudo_wrapper.run("lxc-#{command}", *args)
end
supports_attach_with_namespaces?() click to toggle source
# File lib/vagrant-lxc/driver/cli.rb, line 163
def supports_attach_with_namespaces?
  unless defined?(@supports_attach_with_namespaces)
    @supports_attach_with_namespaces = run(:attach, '-h', :show_stderr => true).values.join.include?('--namespaces')
  end

  return @supports_attach_with_namespaces
end