Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] All the updates and refactors #67

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7f90c3a
Do not create fstab by default
chrisroberts Jun 6, 2015
cd75d8e
Only set mount if file exists. Remove cgroup defaults
chrisroberts Jun 6, 2015
fe61b79
Add optional support for lxc daily ppa repo
chrisroberts Jun 6, 2015
015bc6a
Allow modifying container default attributes via node attributes
chrisroberts Jun 6, 2015
0fa086f
Default new attributes
chrisroberts Jun 6, 2015
95c20a5
Fix transposed resource/attribute names
chrisroberts Jun 6, 2015
17d7991
Force package upgrade when ppa is enabled
chrisroberts Jun 9, 2015
82721ca
Fix resource name within update subscription
chrisroberts Jun 9, 2015
9dba9c3
If no mount is provided, set to default fstab file and ensure exists
chrisroberts Jun 9, 2015
ef7c4d1
Toss in missing paren
chrisroberts Jun 9, 2015
02711aa
Add missing end keyword
chrisroberts Jun 10, 2015
44e4485
Remove guard on file and always ensure it exists
chrisroberts Jun 10, 2015
efdac41
Refactor the config LWRP to be free form and future compatible
chrisroberts Jun 19, 2015
c27ad5c
Update location used for utsname
chrisroberts Aug 10, 2015
26afa26
Only define constant if not already seen.
chrisroberts Aug 10, 2015
abe9681
Updates for ppa usage
chrisroberts Aug 10, 2015
a7e4920
Always generate fstab file, even when empty
chrisroberts Aug 11, 2015
4916fc9
Fetch data out for safety
chrisroberts Aug 11, 2015
3eb6748
Update the testing bundle
chrisroberts Aug 11, 2015
718920e
Stub in vagabond for default test until everything is settled
chrisroberts Aug 11, 2015
956380b
Update gem helper restriction
chrisroberts Sep 25, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,28 @@ suites:
provisioner:
name: chef_solo
run_list:
- 'recipe[lxc-test::default]'
- 'recipe[vagabond]'
attributes:
lxc:
enable_daily_ppa: true
vagabond:
bases:
centos_5:
enabled: false
centos_6:
enabled: true
centos_7:
enabled: true
ubuntu_1204:
enabled: true
ubuntu_1404:
enabled: true
debian_6:
enabled: true
debian_7:
enabled: true
debian_8:
enabled: true
- name: test-container
provisioner:
name: chef_zero
Expand Down
1 change: 1 addition & 0 deletions Batali
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Batali.define do
source 'https://supermarket.chef.io'
cookbook 'apt'
cookbook 'yum-epel'
cookbook 'vagabond', git: 'git://github.com/chrisroberts/chef-vagabond', :ref => 'develop'
cookbook 'lxc-test', path: './test/fixtures/cookbooks/lxc-test'

metadata
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source 'https://rubygems.org'

gem 'kitchen-vagrant'
gem 'batali'
gem 'batali-tk'
gem 'librarian-chef'

if(ENV['DEATH_TO_OMNIBUS'])
gem 'vagrant', :git => 'https://github.com/mitchellh/vagrant', :tag => 'v1.7.2'
gem 'vagrant', :git => 'https://github.com/mitchellh/vagrant', :tag => 'v1.7.4'
end
7 changes: 6 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

default[:lxc][:proxy][:enable] = false

default[:lxc][:elecksee][:version_restriction] = '~> 1.1.2'
default[:lxc][:elecksee][:version_restriction] = '~> 2.0.2'
default[:lxc][:elecksee][:action] = :install

default[:lxc][:default_config_enabled] = true
default[:lxc][:default_fstab_enabled] = false

default[:lxc][:default_config][:lxc_auto] = node[:lxc][:auto_start]
default[:lxc][:default_config][:use_lxc_bridge] = node[:lxc][:use_bridge]
default[:lxc][:default_config][:lxc_bridge] = node[:lxc][:bridge]
Expand All @@ -34,6 +37,8 @@

default[:lxc][:user_locks] = %w(ubuntu)

default[:lxc][:enable_daily_ppa] = false
default[:lxc][:ppa_packages] = ['lxc', 'lxc-templates']
default[:lxc][:packages] = node.platform_family?('rhel') ? ['lxc', 'lxc-templates', 'lxc-libs', 'bridge-utils', 'libcgroup'] : ['lxc']
default[:lxc][:mirror] = 'http://archive.ubuntu.com/ubuntu'
default[:lxc][:containers] = {}
Expand Down
92 changes: 35 additions & 57 deletions providers/config.rb
Original file line number Diff line number Diff line change
@@ -1,81 +1,59 @@
require 'securerandom'

def load_current_resource
require 'elecksee/lxc_file_config'

new_resource.utsname new_resource.container if new_resource.container
new_resource.utsname new_resource.name unless new_resource.utsname
if(new_resource.container)
new_resource.struct.lxc.utsname new_resource.container
end
unless(new_resource.struct[:lxc] && new_resource.struct[:lxc][:utsname])
new_resource.struct.lxc.utsname new_resource.name
end

@lxc = ::Lxc.new(
new_resource.utsname,
new_resource.struct.lxc.utsname,
:base_dir => node[:lxc][:container_directory],
:dnsmasq_lease_file => node[:lxc][:dnsmasq_lease_file]
)

new_resource.rootfs @lxc.rootfs.to_path unless new_resource.rootfs

new_resource.default_bridge node[:lxc][:bridge] unless new_resource.default_bridge
new_resource.mount @lxc.path.join('fstab').to_path unless new_resource.mount
config = ::Lxc::FileConfig.new(@lxc.container_config)
if((new_resource.network.nil? || new_resource.network.empty?))
if(config.network.empty?)
default_net = {
:type => :veth,
:link => new_resource.default_bridge,
:flags => :up,
:hwaddr => "00:16:3e#{SecureRandom.hex(3).gsub(/(..)/, ':\1')}"
}
else
default_net = config.network.first
default_net.delete(:ipv4) if default_net.has_key?(:ipv4)
default_net.merge!(:link => new_resource.default_bridge)
end
new_resource.network(default_net)
else
[new_resource.network].flatten.each_with_index do |net_hash, idx|
if(config.network[idx].nil? || config.network[idx][:hwaddr].nil?)
net_hash[:hwaddr] ||= "00:16:3e#{SecureRandom.hex(3).gsub(/(..)/, ':\1')}"
end
end
end
new_resource.cgroup(
Chef::Mixin::DeepMerge.merge(
Mash.new(
'devices.deny' => 'a',
'devices.allow' => [
'c *:* m',
'b *:* m',
'c 1:3 rwm',
'c 1:5 rwm',
'c 5:1 rwm',
'c 5:0 rwm',
'c 1:9 rwm',
'c 1:8 rwm',
'c 136:* rwm',
'c 5:2 rwm',
'c 254:0 rwm',
'c 10:229 rwm',
'c 10:200 rwm',
'c 1:7 rwm',
'c 10:228 rwm',
'c 10:232 rwm'
]
),
new_resource.cgroup
)
)
@config = ::Lxc::FileConfig.new(@lxc.container_config.to_path)
end

action :create do
_lxc = @lxc
_config = @config

directory @lxc.path.to_path do
action :create
end

if(new_resource.resource_style.to_s == 'merge')
if(node[:lxc][:original_configs].nil?)
node.set[:lxc][:original_configs] = {}
end
if(node[:lxc][:original_configs][new_resource.name].nil?)
node.set[:lxc][:original_configs][new_resource.name] = _config.state_hash
end
_config.state._merge!(new_resource.struct)
else
_config.state = new_resource.struct
end

file "lxc update_config[#{new_resource.utsname}]" do
path _lxc.container_config.to_path
content ::Lxc::FileConfig.generate_config(new_resource)
content _config.generate_content
mode 0644
end
end

action :delete do
_lxc = @lxc

if(node[:lxc][:original_configs] && node[:lxc][:original_configs][new_resource.name])
node.set[:lxc][:original_configs][new_resource.name] = nil
end

file "lxc delete_config[#{new_resource.name}]" do
path _lxc.container_config.to_path
action :delete
end
end
48 changes: 27 additions & 21 deletions providers/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ def load_current_resource
if(new_resource.static_ip && new_resource.static_gateway.nil?)
raise "Static gateway must be defined when static IP is provided (Container: #{new_resource.name})"
end

if(new_resource.default_config.nil?)
new_resource.default_config node[:lxc][:default_config_enabled]
end
if(new_resource.default_fstab.nil?)
new_resource.default_fstab node[:lxc][:default_fstab_enabled]
end

new_resource.default_bridge node[:lxc][:bridge] unless new_resource.default_bridge
node.run_state[:lxc] ||= Mash.new
node.run_state[:lxc][:meta] ||= Mash.new
Expand Down Expand Up @@ -49,14 +57,6 @@ def load_current_resource
end
end

#### Create container configuration bits
if(new_resource.default_config)
lxc_config new_resource.name do
action :create
default_bridge new_resource.default_bridge
end
end

if(new_resource.default_fstab)
lxc_fstab "proc[#{new_resource.name}]" do
container new_resource.name
Expand All @@ -75,6 +75,14 @@ def load_current_resource
end
end

#### Create container configuration bits
if(new_resource.default_config)
lxc_config new_resource.name do
action :create
default_bridge new_resource.default_bridge
end
end

if(new_resource.static_ip)
lxc_interface "eth0[#{new_resource.name}]" do
container new_resource.name
Expand All @@ -100,10 +108,6 @@ def load_current_resource
source 'fstab.erb'
cookbook 'lxc'
variables :container => new_resource.name
only_if do
node.run_state[:lxc][:fstabs] &&
node.run_state[:lxc][:fstabs][new_resource.name]
end
mode 0644
end

Expand All @@ -129,7 +133,10 @@ def load_current_resource
end

#### Use cached chef package from host if available
VERSION_REGEXP = %r{(\d+\.\d+\.\d+(-\d+)?)}
unless(defined?(VERSION_REGEXP))
VERSION_REGEXP = %r{(\d+\.\d+\.\d+(-\d+)?)}
end

if(%w(debian ubuntu).include?(new_resource.template) && system('ls /opt/chef*.deb 2>&1 > /dev/null'))
file_path = Dir.glob(::File.join('/opt', 'chef*.deb')).sort do |x,y|
version_x = x.scan(VERSION_REGEXP).flatten.first
Expand Down Expand Up @@ -221,18 +228,17 @@ def load_current_resource
end

#### Have initialize commands for the container? Run them now
ruby_block "lxc initialize_commands[#{new_resource.name}]" do
block do
new_resource.initialize_commands.each do |cmd|
new_resource.initialize_commands.each do |cmd|
ruby_block "lxc initialize_command[#{new_resource.name}:#{cmd}]" do
block do
Chef::Log.info "Running command on #{new_resource.name}: #{cmd}"
_lxc.container_command(cmd, 5)
end
only_if do
node.run_state[:lxc][:meta][new_resource.name][:new_container]
end
retries 5
end
only_if do
node.run_state[:lxc][:meta][new_resource.name][:new_container] &&
!new_resource.initialize_commands.empty?
end
retries 5
end

# Make sure we have chef in the container
Expand Down
38 changes: 37 additions & 1 deletion recipes/package.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
package 'software-properties-common' do
only_if{ node[:lxc][:enable_daily_ppa] }
end

execute 'enable lxc-daily ppa' do
command 'apt-add-repository ppa:ubuntu-lxc/daily -y'
only_if do
node.platform_family?('debian') &&
node[:lxc][:enable_daily_ppa]
end
not_if 'grep -R "^deb.*ppa.*lxc.daily" /etc/apt/sources.list*'
end

execute 'ppa update' do
command 'apt-get update'
action :nothing
subscribes :run, 'execute[enable lxc-daily ppa]', :immediately
end

execute 'lxc ppa system upgrade' do
command 'apt-get upgrade -yq'
action :nothing
subscribes :run, 'execute[enable lxc-daily ppa]', :immediately
environment 'DEBIAN_FRONTEND' => 'noninteractive'
end

ruby_block 'ppa lxc package list' do
block do
node.default[:lxc][:packages] = node[:lxc][:ppa_packages]
end
action :nothing
subscribes :create, 'execute[enable lxc-daily ppa]', :immediately
end

# install the server dependencies to run lxc
node[:lxc][:packages].each do |lxcpkg|
package lxcpkg
package lxcpkg do
subscribes :upgrade, 'execute[enable lxc-daily ppa]', :immediately
end
end
28 changes: 12 additions & 16 deletions resources/config.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
attr_reader :struct

actions :create, :delete
default_action :create

attribute :container, :kind_of => String, :default => nil # alias for utsname
attribute :utsname, :kind_of => String, :default => nil # defaults to resource name
attribute :aa_profile, :kind_of => String, :default => nil # platform specific?
attribute :network, :kind_of => [Array, Hash]
attribute :default_bridge, :kind_of => String
attribute :static_ip, :kind_of => String
attribute :pts, :kind_of => Numeric, :default => 1024
attribute :tty, :kind_of => Numeric, :default => 4
attribute :arch, :kind_of => String, :default => 'amd64'
attribute :devttydir, :kind_of => String, :default => 'lxc'
attribute :cgroup, :kind_of => Hash, :default => Mash.new
attribute :cap_drop, :kind_of => [String, Array], :default => %w(sys_module mac_admin)
attribute :mount, :kind_of => String
attribute :mount_entry, :kind_of => String
attribute :rootfs, :kind_of => [String,Pathname]
attribute :rootfs_mount, :kind_of => String
attribute :pivotdir, :kind_of => String
attribute :resource_style, :equal_to => [:replace, :merge, 'replace', 'merge'], :default => :merge

def method_missing(*args, &block)
unless(@struct)
require 'elecksee/lxc_file_config'
@struct = LxcStruct.new
@struct._set_state(:value_collapse => true)
end
@struct.method_missing(*args, &block)
end
4 changes: 2 additions & 2 deletions resources/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def initialize(*args)
attribute :static_ip, :kind_of => String
attribute :static_netmask, :kind_of => String, :default => '255.255.255.0'
attribute :static_gateway, :kind_of => String
attribute :default_config, :kind_of => [TrueClass, FalseClass], :default => true
attribute :default_fstab, :kind_of => [TrueClass, FalseClass], :default => true
attribute :default_config, :kind_of => [TrueClass, FalseClass]
attribute :default_fstab, :kind_of => [TrueClass, FalseClass]
attribute :container_commands, :kind_of => Array, :default => []
attribute :initialize_commands, :kind_of => Array, :default => []
attribute :clone, :kind_of => String
Expand Down
3 changes: 1 addition & 2 deletions templates/default/fstab.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Chef generated fstab!
<% node.run_state[:lxc][:fstabs][@container].each do |line| -%>
<% node.run_state[:lxc].fetch(:fstabs, {}).fetch(@container, {}).each do |line| -%>
<%= line %>
<% end -%>