Class: Chef::Provider::Lxc

Inherits:
Chef::Provider show all
Includes:
LXCHelper
Defined in:
lib/chef/provider/lxc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LXCHelper

#recipe_in_container

Constructor Details

#initialize(new_resource, run_context) ⇒ Lxc

Returns a new instance of Lxc.



10
11
12
# File 'lib/chef/provider/lxc.rb', line 10

def initialize(new_resource, run_context)
  super(new_resource, run_context)
end

Instance Attribute Details

#ctObject (readonly)

Returns the value of attribute ct.



8
9
10
# File 'lib/chef/provider/lxc.rb', line 8

def ct
  @ct
end

Instance Method Details

#action_createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chef/provider/lxc.rb', line 25

def action_create
  unless ct.defined?
    converge_by("create container '#{ct.name}'") do
      template = new_resource.lxc_template.type
      template_options = new_resource.lxc_template.options
      flags = 0
      ct.create(
        new_resource.lxc_template.type,
        new_resource.block_device,
        new_resource.bdev_specs,
        new_resource.flags,
        new_resource.lxc_template.options
      )
      update_config
    end
  end
end

#action_destroyObject



87
88
89
90
91
92
93
# File 'lib/chef/provider/lxc.rb', line 87

def action_destroy
  if ct.defined?
    converge_by("destroy container '#{ct.name}'") do
      ct.destroy
    end
  end
end

#action_rebootObject



64
65
66
67
68
# File 'lib/chef/provider/lxc.rb', line 64

def action_reboot
  converge_by("reboot container '#{ct.name}'") do
    ct.reboot
  end
end

#action_startObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/provider/lxc.rb', line 70

def action_start
  unless ct.running?
    converge_by("start container '#{ct.name}'") do
      ct.start
      if new_resource.wait_for_network
        until ct.ip_addresses.empty?
          Chef::Log.debug('waiting for ip allocation')
          sleep 1
        end
      end
    end
  end
  unless new_resource.recipe_block.nil?
    recipe_in_container(ct, &new_resource.recipe_block)
  end
end

#action_stopObject



56
57
58
59
60
61
62
# File 'lib/chef/provider/lxc.rb', line 56

def action_stop
  if ct.running?
    converge_by("stop container '#{ct.name}'") do
      ct.stop
    end
  end
end

#load_current_resourceObject



18
19
20
21
22
23
# File 'lib/chef/provider/lxc.rb', line 18

def load_current_resource
  @ct = ::LXC::Container.new(new_resource.container_name)
  if (new_resource.action == 'start') or (new_resource.action == 'stop')
    raise ArgumentError, 'Can not start or stop non-existent container'
  end
end

#update_configObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/provider/lxc.rb', line 43

def update_config
  updated_items = []
  new_resource.config.each do |key, expected_value|
    if ct.config_item(key) != expected_value
      ct.set_config_item(key, expected_value)
      updated_items << key
    end
  end
  unless updated_items.empty?
    ct.save_config
  end
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/chef/provider/lxc.rb', line 14

def whyrun_supported?
  true
end