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

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

Defined Under Namespace

Classes: ShutdownNotSupported, TargetStateNotReached, TransitionBlockNotProvided

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sudo_wrapper, name = nil) ⇒ CLI

Returns a new instance of CLI.



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

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



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

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

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

    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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant-lxc/driver/cli.rb', line 48

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
rescue Errors::ExecuteError => e
  if e.stderr =~ /already exists/i
    raise Errors::ContainerAlreadyExists, name: @name
  else
    raise
  end
end

#destroyObject



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

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

#listObject



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

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

#shutdownObject



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

def shutdown
  if system('which lxc-shutdown > /dev/null')
    run :shutdown, '--name', @name
  else
    # REFACTOR: Do not use exception to control the flow
    raise ShutdownNotSupported
  end
end

#start(options = []) ⇒ Object



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

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

#stateObject



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

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

#stopObject



77
78
79
80
# File 'lib/vagrant-lxc/driver/cli.rb', line 77

def stop
  attach '/sbin/halt'
  run :stop, '--name', @name
end

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

Yields:

  • (_self)

Yield Parameters:

Raises:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vagrant-lxc/driver/cli.rb', line 110

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

#versionObject



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

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