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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vagrant-lxc/driver/cli.rb', line 104

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
      extra = ['--namespaces', namespaces]
    end
  end

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

#config(param) ⇒ Object



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

def config(param)
  run(:config, param).gsub("\n", '')
end

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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.reject { |elem| elem.empty? }
  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

#destroyObject



80
81
82
# File 'lib/vagrant-lxc/driver/cli.rb', line 80

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

#info(*cmd) ⇒ Object



124
125
126
# File 'lib/vagrant-lxc/driver/cli.rb', line 124

def info(*cmd)
  run(:info, '--name', @name, *cmd)
end

#listObject



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

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

#start(options = []) ⇒ Object



84
85
86
# File 'lib/vagrant-lxc/driver/cli.rb', line 84

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

#stateObject



49
50
51
52
53
54
55
# 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

#stopObject

lxc-stop will exit 2 if machine was already stopped Man Page: 2 The specified container exists but was not running.



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

def stop
  attach '/sbin/halt' if supports_attach?
  begin
    run :stop, '--name', @name
  rescue LXC::Errors::ExecuteError => e
    if e.exitcode == 2
       @logger.debug "Machine already stopped, lxc-stop returned 2"
    else
		raise e
    end
  end
end

#supports_attach?Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/vagrant-lxc/driver/cli.rb', line 145

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| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/vagrant-lxc/driver/cli.rb', line 128

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

#update_config(path) ⇒ Object



45
46
47
# File 'lib/vagrant-lxc/driver/cli.rb', line 45

def update_config(path)
  run('update-config', '-c', path)
end

#versionObject



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

def version
  return @version if @version
  @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