Class: Chef::Provisioning::LXCDriver::Driver

Inherits:
Driver
  • Object
show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provisioning/lxc_driver/driver.rb

Overview

Provisions machines in lxc.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver_url, config) ⇒ Driver

Returns a new instance of Driver.



35
36
37
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 35

def initialize(driver_url, config)
  super
end

Class Method Details

.canonicalize_url(driver_url, config) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 26

def self.canonicalize_url(driver_url, config)
  scheme, lxc_path = driver_url.split(':', 2)
  if lxc_path.nil? || lxc_path == ':'
    lxc_path = LXC.global_config_item('lxc.lxcpath')
  end
  lxc_path = File.realpath(lxc_path)
  [ "lxc:#{lxc_path}", config ]
end

.from_url(driver_url, config) ⇒ Object

URL scheme: lxc:<path> <path> defaults to LXC config ‘lxc.lxcpath’ canonical URL calls realpath on <path>



22
23
24
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 22

def self.from_url(driver_url, config)
  Driver.new(driver_url, config)
end

Instance Method Details

#allocate_machine(action_handler, machine_spec, machine_options) ⇒ Object

Valid machine options: :template - template name :template_options - additional arguments for templates :backingstore - backing storage (lvm, thinpools, btrfs etc) :config_file - <path> path to LXC file a la wiki.archlinux.org/index.php/Linux_Containers#Configuration_file :extra_config - { ‘key’ => ‘value’, … } a set of LXC config key/value pairs to individually set. Merges with, and overrides any values in config_file.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 50

def allocate_machine(action_handler, machine_spec, machine_options)
  # Create the container if it does not exist
  if machine_spec.location
    ct = LXC::Container.new(machine_spec.location['name'], lxc_path)
  else
    ct = LXC::Container.new(machine_spec.name, lxc_path)
    if ct.defined?
      # Should this be a warning? Configurable, at least.
      raise "container #{machine_spec.name} already exists and is not managed by this LXC driver."
    end
  end

  unless ct.defined?
    action_handler.perform_action "create lxc container #{ct.name}" do
      #
      # Set config
      #
      # TODO if config file changes, reload container?
      if machine_options[:config_file]
        ct.load_config(machine_options[:config_file])
      end
      if machine_options[:extra_config]
        machine_options[:extra_config].each_pair do |key, value|
          ct.set_config_item(key, value)
        end
      end

      #
      # Create the machine
      ct.create(
        machine_options[:template],
        machine_options[:backingstore],
        machine_options[:devspecs] || {},
        machine_options[:flags] || 0,
        machine_options[:template_options] || []
      )

      machine_spec.location = {
        'driver_url' => driver_url,
        'driver_version' => Chef::Provisioning::LXCDriver::VERSION,
        'name' => machine_spec.name,
        'host_node' => action_handler.host_node,
        'allocated_at' => Time.now.utc.to_s
      }
    end
  end
end

#connect_to_machine(machine_spec, machine_options) ⇒ Object

Connect to machine without acquiring it



120
121
122
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 120

def connect_to_machine(machine_spec, machine_options)
  machine_for(machine_spec, machine_options)
end

#destroy_machine(action_handler, machine_spec, machine_options) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 124

def destroy_machine(action_handler, machine_spec, machine_options)
  if machine_spec.location
    ct = LXC::Container.new(machine_spec.location['name'], lxc_path)
    if ct.defined?
      action_handler.perform_action "delete lxc container #{machine_spec.location['name']}" do
        ct.destroy
      end
    end
  end
  convergence_strategy_for(machine_spec, machine_options).cleanup_convergence(action_handler, machine_spec)
end

#lxc_pathObject



39
40
41
42
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 39

def lxc_path
  scheme, lxc_path = driver_url.split(':', 2)
  lxc_path
end

#ready_machine(action_handler, machine_spec, machine_options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 98

def ready_machine(action_handler, machine_spec, machine_options)
  ct = LXC::Container.new(machine_spec.location['name'], lxc_path)

  # Unfreeze the frozen
  if ct.state == :frozen
    action_handler.perform_action "unfreeze lxc container #{machine_spec.location['name']} (state is #{ct.state})" do
      ct.unfreeze
    end
  end

  # Get stopped containers running
  if ct.defined? and not ct.running?
    action_handler.perform_action "start lxc container #{machine_spec.location['name']} (state is #{ct.state})" do
      ct.start
    end
  end

  # Create machine object for callers to use
  machine_for(machine_spec, machine_options)
end

#stop_machine(action_handler, node) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/chef/provisioning/lxc_driver/driver.rb', line 136

def stop_machine(action_handler, node)
  if machine_spec.location
    ct = LXC::Container.new(machine_spec.location['name'], lxc_path)
    if ct.running?
      action_handler.perform_action "delete lxc container #{machine_spec.location['name']}" do
        ct.stop
      end
    end
  end
end