Class: Vagrant::LXC::Container::CLI

Inherits:
Object
  • Object
show all
Includes:
Util::Retryable
Defined in:
lib/vagrant-lxc/container/cli.rb

Defined Under Namespace

Classes: TransitionBlockNotProvided

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ CLI

Returns a new instance of CLI.



17
18
19
20
# File 'lib/vagrant-lxc/container/cli.rb', line 17

def initialize(name = nil)
  @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/container/cli.rb', line 10

def name
  @name
end

Instance Method Details

#attach(*cmd) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vagrant-lxc/container/cli.rb', line 62

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, target_rootfs_path, template_opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vagrant-lxc/container/cli.rb', line 34

def create(template, target_rootfs_path, template_opts = {})
  extra = template_opts.to_a.flatten
  extra.unshift '--' unless extra.empty?

  rootfs_args = target_rootfs_path ?
    ['-B', 'dir', '--dir', target_rootfs_path] :
    []

  run :create,
      '--template', template,
      '--name',     @name,
      *(rootfs_args + extra)
end

#destroyObject



48
49
50
# File 'lib/vagrant-lxc/container/cli.rb', line 48

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

#listObject



22
23
24
# File 'lib/vagrant-lxc/container/cli.rb', line 22

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

#shutdownObject



58
59
60
# File 'lib/vagrant-lxc/container/cli.rb', line 58

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

#start(configs = [], extra_opts = []) ⇒ Object



52
53
54
55
56
# File 'lib/vagrant-lxc/container/cli.rb', line 52

def start(configs = [], extra_opts = [])
  configs = configs.map { |conf| ["-s", conf] }.flatten
  configs += extra_opts if extra_opts
  run :start, '-d', '--name', @name, *configs
end

#stateObject



26
27
28
29
30
31
32
# File 'lib/vagrant-lxc/container/cli.rb', line 26

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

#transition_to(state) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:



74
75
76
77
78
79
80
# File 'lib/vagrant-lxc/container/cli.rb', line 74

def transition_to(state, &block)
  raise TransitionBlockNotProvided unless block_given?

  yield self

  run :wait, '--name', @name, '--state', state.to_s.upcase
end