forked from niklas/chef-lxc
/
default.rb
143 lines (119 loc) · 4.03 KB
/
default.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#
# Cookbook Name:: lxc
# Recipe:: default
#
# Copyright 2010, Company
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
package 'debootstrap'
package 'apt-cacher-ng'
include_recipe 'lxc::manage'
include_recipe 'lxc::network_bridge'
host = node[:container]
directory host[:base_directory] do
action :create
mode '0755'
owner 'root'
group 'root'
end
# Followed instruction from
# http://blog.bodhizazen.net/linux/lxc-configure-ubuntu-lucid-containers/
search(:virtual_machines) do |guest|
# Bootstrap
domain = guest[:domain] || host[:default][:domain]
hostname = "#{guest[:id]}.#{domain}"
variant = guest[:variant] ||= host[:default][:variant]
suite = guest[:suite ] ||= host[:default][:suite ]
mirror = guest[:mirror ] ||= host[:default][:mirror ]
packages= guest[:packages] ||= host[:default][:packages]
guest[:ipv4] ||= host[:default][:ipv4]
rootfs = host[:base_directory] / hostname + '.rootfs'
execute "debootstrap" do
command "debootstrap --variant=#{variant} --include #{packages.join(',')} #{suite} #{rootfs} #{mirror}"
action :run
not_if "test -f #{rootfs / 'etc' / 'issue'}"
end
template host[:base_directory] / hostname + '.lxc.conf' do
source "lxc.conf.erb"
variables :host => host, :guest => guest, :rootfs => rootfs, :hostname => hostname
action :create
end
template host[:base_directory] / hostname + '.fstab' do
source 'fstab.erb'
variables :host => host, :guest => guest, :rootfs => rootfs, :hostname => hostname
action :create
end
file rootfs / 'etc' / 'inittab' do
action :delete
end
file rootfs / 'etc' / 'hostname' do
backup false
content hostname
action :create
end
file rootfs / 'etc' / 'hosts' do
backup false
action :create
content %Q~127.0.0.1 #{hostname} #{guest[:id]} localhost\n~
end
template rootfs / 'etc' / 'apt' / 'sources.list' do
source 'rootfs/sources.list.erb'
variables :host => host, :guest => guest
end
bash 'remove as many init scripts as possible' do
only_if %Q~test -f #{rootfs}/etc/init/hwclock.conf~
code <<-EOSH
rm #{rootfs}/etc/init/{hwclock,mount,plymouth,udev,network,tty5,tty6}*
true
EOSH
end
bash 'remove pointless services' do
only_if %Q'test -f #{rootfs}/etc/rc0.d/S*umountfs'
code <<-EOSH
chroot #{rootfs} /usr/sbin/update-rc.d -f umountfs remove
chroot #{rootfs} /usr/sbin/update-rc.d -f hwclock.sh remove
chroot #{rootfs} /usr/sbin/update-rc.d -f hwclockfirst.sh remove
chroot #{rootfs} /usr/sbin/update-rc.d -f umountroot remove
chroot #{rootfs} /usr/sbin/update-rc.d -f ondemand remove
EOSH
end
template rootfs / 'etc' / 'init' / 'vm.conf' do
source 'rootfs/init-vm.conf.erb'
action :create
end
template rootfs / 'etc' / 'init' / 'vm-net.conf' do
source 'rootfs/init-net.conf.erb'
variables :host => node
end
template rootfs / 'usr' / 'sbin' / 'install-chef.sh' do
source 'rootfs/install-chef.sh.erb'
variables :host => host, :guest => guest
mode '0755'
end
template rootfs / 'etc' / 'init' / 'chef-install.conf' do
source 'rootfs/chef-install.conf.erb'
variables :host => host, :guest => guest
end
directory rootfs / 'etc' / 'chef' do
action :create
owner 'chef'
group 'chef'
mode '0755'
end
execute "register vm at chef server" do
private_key = rootfs / 'etc' / 'chef' / 'client.pem'
command %Q~knife client -u #{node[:fqdn]} -k /etc/chef/client.pem --no-editor create #{hostname} -f #{private_key}~
action :run
not_if "test -f #{private_key}"
end
end