Class: Vagrant::LXC::Driver::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-lxc/driver/cli.rb

Defined Under Namespace

Classes: TargetStateNotReached, TransitionBlockNotProvided

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sudo_wrapper, name = nil) ⇒ CLI

Returns a new instance of CLI.



20
21
22
23
24
# 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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/vagrant-lxc/driver/cli.rb', line 10

def name
  @name
end

Instance Method Details

#attach(*cmd) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant-lxc/driver/cli.rb', line 78

def attach(*cmd)
  cmd = ['--'] + cmd

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

  run :attach, '--name', @name, *((extra || []) + cmd)
end

#create(template, config_file, template_opts = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vagrant-lxc/driver/cli.rb', line 47

def create(template, 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,
      '--template', template,
      '--name',     @name,
      *(config_opts),
      *extra
end

#destroyObject



62
63
64
# File 'lib/vagrant-lxc/driver/cli.rb', line 62

def destroy
  run :destroy, '--name', @name
end

#listObject



26
27
28
# File 'lib/vagrant-lxc/driver/cli.rb', line 26

def list
  run(:ls).split(/\s+/).uniq
end

#shutdownObject



74
75
76
# File 'lib/vagrant-lxc/driver/cli.rb', line 74

def shutdown
  run :shutdown, '--name', @name
end

#start(options = []) ⇒ Object



66
67
68
# File 'lib/vagrant-lxc/driver/cli.rb', line 66

def start(options = [])
  run :start, '-d', '--name', @name, *Array(options)
end

#stateObject



39
40
41
42
43
44
45
# File 'lib/vagrant-lxc/driver/cli.rb', line 39

def state
  if @name && run(:info, '--name', @name, retryable: true) =~ /^state:[^A-Z]+([A-Z]+)$/
    $1.downcase.to_sym
  elsif @name
    :unknown
  end
end

#stopObject



70
71
72
# File 'lib/vagrant-lxc/driver/cli.rb', line 70

def stop
  run :stop, '--name', @name
end

#transition_to(target_state, tries = 30, timeout = 1) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vagrant-lxc/driver/cli.rb', line 90

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
    raise TargetStateNotReached.new target_state, last_state
  end
end

#versionObject



30
31
32
33
34
35
36
37
# File 'lib/vagrant-lxc/driver/cli.rb', line 30

def version
  if run(:version) =~ /lxc version:\s+(.+)\s*$/
    $1.downcase
  else
    # TODO: Raise an user friendly error
    raise 'Unable to parse lxc version!'
  end
end