toft-puppet 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. data/Gemfile +3 -0
  2. data/Gemfile.lock +62 -0
  3. data/Rakefile +121 -0
  4. data/features/checker.feature +14 -0
  5. data/features/chef.feature +70 -0
  6. data/features/command.feature +20 -0
  7. data/features/node.feature +41 -0
  8. data/features/puppet.feature +40 -0
  9. data/features/step_definitions/centos/checks.rb +9 -0
  10. data/features/step_definitions/checker.rb +15 -0
  11. data/features/step_definitions/chef.rb +43 -0
  12. data/features/step_definitions/command.rb +28 -0
  13. data/features/step_definitions/node.rb +65 -0
  14. data/features/step_definitions/puppet.rb +7 -0
  15. data/features/support/env.rb +25 -0
  16. data/fixtures/chef/attributes.json +9 -0
  17. data/fixtures/chef/cookbooks/test/attributes/default.rb +4 -0
  18. data/fixtures/chef/cookbooks/test/recipes/attribute.rb +19 -0
  19. data/fixtures/chef/cookbooks/test/recipes/default.rb +4 -0
  20. data/fixtures/chef/cookbooks/test/recipes/role.rb +4 -0
  21. data/fixtures/chef/roles/test.rb +3 -0
  22. data/fixtures/puppet/conf/fileserver.conf +3 -0
  23. data/fixtures/puppet/conf/puppet.conf +15 -0
  24. data/fixtures/puppet/conf/puppet_exec.conf +9 -0
  25. data/fixtures/puppet/conf/puppet_fileserver.conf +8 -0
  26. data/fixtures/puppet/conf/puppet_modules.conf +7 -0
  27. data/fixtures/puppet/conf/puppet_template.conf +8 -0
  28. data/fixtures/puppet/manifests/fileserver/conf/test_fileserver +1 -0
  29. data/fixtures/puppet/manifests/nodes/test_node.pp +26 -0
  30. data/fixtures/puppet/manifests/site.pp +1 -0
  31. data/fixtures/puppet/manifests/templates/template_test +2 -0
  32. data/fixtures/puppet/manifests/test.pp +8 -0
  33. data/fixtures/puppet/manifests/test_fileserver.pp +14 -0
  34. data/fixtures/puppet/manifests/test_install.pp +5 -0
  35. data/fixtures/puppet/manifests/test_module.pp +5 -0
  36. data/fixtures/puppet/manifests/test_service.pp +11 -0
  37. data/fixtures/puppet/manifests/test_template.pp +12 -0
  38. data/fixtures/puppet/modules/test_module/manifests/init.pp +8 -0
  39. data/lib/toft.rb +39 -0
  40. data/lib/toft/chef/chef_attributes.rb +29 -0
  41. data/lib/toft/chef/chef_runner.rb +77 -0
  42. data/lib/toft/command_executor.rb +16 -0
  43. data/lib/toft/file_checker.rb +47 -0
  44. data/lib/toft/node.rb +243 -0
  45. data/lib/toft/node_controller.rb +32 -0
  46. data/lib/toft/puppet/puppet_runner.rb +38 -0
  47. data/lib/toft/version.rb +3 -0
  48. data/scripts/bin/centos/lxc-prepare-host +172 -0
  49. data/scripts/bin/centos/provision_vagrant +11 -0
  50. data/scripts/bin/share/install-chef-ubuntu.sh +19 -0
  51. data/scripts/bin/share/lxc-create-centos-image +60 -0
  52. data/scripts/bin/ubuntu/lxc-create-ubuntu-image +77 -0
  53. data/scripts/bin/ubuntu/lxc-prepare-host +199 -0
  54. data/scripts/bin/ubuntu/provision_vagrant +9 -0
  55. data/scripts/lxc-templates/files/rc.local +38 -0
  56. data/scripts/lxc-templates/lxc-centos-6 +279 -0
  57. data/scripts/lxc-templates/lxc-lenny +255 -0
  58. data/scripts/lxc-templates/lxc-lucid +313 -0
  59. data/scripts/lxc-templates/lxc-natty +237 -0
  60. data/spec/fixtures/illegal_syntax.json +1 -0
  61. data/spec/spec_helper.rb +6 -0
  62. data/spec/toft/chef/chef_attributes_spec.rb +39 -0
  63. data/spec/toft/chef/chef_runner_spec.rb +34 -0
  64. data/spec/toft/node_spec.rb +18 -0
  65. data/spec/toft/puppet/puppet_runner_spec.rb +26 -0
  66. metadata +234 -0
@@ -0,0 +1,11 @@
1
+ cd /vagrant
2
+ sudo yum -y install wget
3
+ wget http://dl.dropbox.com/u/43220259/toft-lxc-0.0.6.noarch.rpm
4
+ sudo yum erase -y dhclient
5
+ sudo yum install -y --nogpgcheck toft-lxc-0.0.6.noarch.rpm
6
+ sudo yum install -y bind dhcp dhclient bridge-utils
7
+ sudo ./scripts/bin/centos/lxc-prepare-host
8
+ sudo yum install -y ruby ruby-devel ruby-docs ruby-ri ruby-irb ruby-rdoc rubygems
9
+ sudo gem install bundler --no-ri --no-rdoc
10
+ sudo bundle install
11
+ sudo cp natty-i386-puppet.tar.gz /var/cache/lxc/natty-i386.tar.gz
@@ -0,0 +1,19 @@
1
+ #!/bin/bash
2
+
3
+ username=`id -nu`
4
+ if [ ! "$username" = "root" ]; then
5
+ echo "This command has to be run as root!"
6
+ exit 1
7
+ fi
8
+
9
+ echo "deb http://apt.opscode.com/ `lsb_release -cs`-0.10 main" | tee /etc/apt/sources.list.d/opscode.list
10
+
11
+ mkdir -p /etc/apt/trusted.gpg.d
12
+ gpg --keyserver keys.gnupg.net --recv-keys 83EF826A
13
+ gpg --export packages@opscode.com | tee /etc/apt/trusted.gpg.d/opscode-keyring.gpg > /dev/null
14
+ apt-get update
15
+ apt-get install ucf --force-yes -y
16
+ yes | apt-get install opscode-keyring --force-yes -y # permanent upgradeable keyring
17
+
18
+ export DEBIAN_FRONTEND=noninteractive
19
+ apt-get install chef --force-yes -qy
@@ -0,0 +1,60 @@
1
+ #!/bin/bash
2
+
3
+ if [ $# -eq 0 ]; then
4
+ echo "Usage: `basename $0` <centos-6|centos-5|centos-4>"
5
+ exit 1
6
+ fi
7
+
8
+ username=`id -nu`
9
+ if [ ! "$username" = "root" ]; then
10
+ echo "This command has to be run as root!"
11
+ exit 1
12
+ fi
13
+
14
+ cache="/var/cache/lxc"
15
+ suite=$1
16
+
17
+ arch=$(arch)
18
+ if [ "$arch" == "x86_64" ]; then
19
+ arch=amd64
20
+ fi
21
+
22
+ if [ "$arch" == "i686" ]; then
23
+ arch=i386
24
+ fi
25
+
26
+ if [ -e "$cache/$suite-$arch.tar.gz" ]; then
27
+ echo "Cache rootfs already exists!"
28
+ exit 0
29
+ fi
30
+
31
+ # clean the cache dir
32
+ rm -rf $cache/$suite-$arch
33
+
34
+ # install latest rinse
35
+ if [[ ! `type rinse` ]]; then
36
+ echo "Rinse does not exist. Installing..."
37
+ (cd /tmp && \
38
+ wget http://www.steve.org.uk/Software/rinse/rinse-1.9.1.tar.gz && \
39
+ tar zxf rinse-1.9.1.tar.gz && \
40
+ cd rinse-1.9.1 && \
41
+ make install)
42
+ fi
43
+
44
+ # create centos image using rinse
45
+ cat <<EOF > /tmp/after_post_install
46
+ chroot $cache/$suite-$arch rpm -Uvh http://rbel.co/rbel6
47
+ chroot $cache/$suite-$arch yum -y install tar man sudo bind-utils openssh-server openssh-clients rubygem-chef
48
+ EOF
49
+ chmod +x /tmp/after_post_install
50
+
51
+ echo "Creating $suite-$arch image"
52
+ rinse --config /etc/rinse/rinse.conf \
53
+ --arch=$arch --distribution=$suite \
54
+ --directory=$cache/$suite-$arch \
55
+ --after-post-install=/tmp/after_post_install
56
+
57
+ # compress root image
58
+ echo "Packaging rootfs ..."
59
+ (cd $cache/$suite-$arch && tar --exclude=$suite-$arch.tar.gz -zcf $suite-$arch.tar.gz .)
60
+ mv $cache/$suite-$arch/$suite-$arch.tar.gz $cache
@@ -0,0 +1,77 @@
1
+ #!/bin/bash
2
+
3
+ if [ $# -eq 0 ]; then
4
+ echo "Usage: `basename $0` <lenny|lucid|natty>"
5
+ exit 1
6
+ fi
7
+
8
+ username=`id -nu`
9
+ if [ ! "$username" = "root" ]; then
10
+ echo "This command has to be run as root!"
11
+ exit 1
12
+ fi
13
+
14
+ cache="/var/cache/lxc"
15
+ suite=$1
16
+
17
+ arch=$(arch)
18
+ if [ "$arch" == "x86_64" ]; then
19
+ arch=amd64
20
+ fi
21
+
22
+ if [ "$arch" == "i686" ]; then
23
+ arch=i386
24
+ fi
25
+
26
+ if [ -e "$cache/$suite-$arch.tar.gz" ]; then
27
+ echo "Cache rootfs already exists!"
28
+ exit 0
29
+ fi
30
+
31
+ # clean the cache dir
32
+ rm -rf $cache/$suite-$arch
33
+
34
+ lenny_packages=ifupdown,locales,libui-dialog-perl,netbase,net-tools,dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,dnsutils,dhcp3-client,ssh,lsb-release,wget,gpgv,gnupg,sudo,ruby,rubygems1.8,ruby-dev,libopenssl-ruby,build-essential,ssl-cert
35
+ lucid_packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,dnsutils,dhcp3-client,ssh,lsb-release,wget,gpgv,gnupg,sudo,ruby,rubygems1.8,ruby-dev,libopenssl-ruby,build-essential,ssl-cert
36
+ natty_packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,dnsutils,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase,lxcguest,sudo,ruby,rubygems1.8,ruby-dev,libruby,build-essential,wget,ssl-cert
37
+ # check the mini ubuntu was not already downloaded
38
+ rm -rf "$cache/$suite-$arch"
39
+ mkdir -p "$cache/$suite-$arch"
40
+ if [ $? -ne 0 ]; then
41
+ echo "Failed to create '$cache/$suite-$arch' directory"
42
+ exit 1
43
+ fi
44
+
45
+ # download a mini ubuntu into a cache
46
+ echo "Downloading ubuntu minimal ..."
47
+ eval "packages=\$${suite}_packages"
48
+ cmd="debootstrap --verbose --variant=minbase --components=main,universe --arch=$arch --include=$packages $suite $cache/$suite-$arch"
49
+ echo $cmd
50
+ eval $cmd
51
+ if [ $? -ne 0 ]; then
52
+ echo "Failed to download the rootfs, aborting."
53
+ exit 1
54
+ fi
55
+
56
+ echo "Download complete."
57
+
58
+ # install chef
59
+ cat <<EOF > "$cache/$suite-$arch/tmp/install-chef-ubuntu.sh"
60
+ echo "deb http://apt.opscode.com/ $suite-0.10 main" | tee /etc/apt/sources.list.d/opscode.list
61
+
62
+ mkdir -p /etc/apt/trusted.gpg.d
63
+ gpg --keyserver keys.gnupg.net --recv-keys 83EF826A
64
+ gpg --export packages@opscode.com | tee /etc/apt/trusted.gpg.d/opscode-keyring.gpg > /dev/null
65
+ apt-get update
66
+ apt-get install ucf --force-yes -y
67
+ yes | apt-get install opscode-keyring --force-yes -y # permanent upgradeable keyring
68
+
69
+ export DEBIAN_FRONTEND=noninteractive
70
+ apt-get install chef --force-yes -qy
71
+ EOF
72
+ chroot "$cache/$suite-$arch" bash /tmp/install-chef-ubuntu.sh
73
+
74
+ # compress root image
75
+ echo "Packaging rootfs ..."
76
+ (cd $cache/$suite-$arch && tar --exclude=$suite-$arch.tar.gz -zcf $suite-$arch.tar.gz .)
77
+ mv $cache/$suite-$arch/$suite-$arch.tar.gz $cache
@@ -0,0 +1,199 @@
1
+ #!/bin/bash
2
+
3
+ username=`id -nu`
4
+ if [ ! "$username" = "root" ]; then
5
+ echo "This command has to be run as root!"
6
+ exit 1
7
+ fi
8
+
9
+ OS=`cat /etc/issue | cut -f 1 -d " " -s`
10
+
11
+ wget http://dl.dropbox.com/u/43220259/toft-lxc_0.0.6_all.deb
12
+ sudo apt-get -y remove apparmor
13
+ sudo dpkg -i toft-lxc_0.0.6_all.deb
14
+ sudo apt-get -fy install
15
+
16
+ gateway_ip="192.168.20.1"
17
+ subnet="192.168.20.0"
18
+ netmask="255.255.255.0"
19
+ range="192.168.20.2 192.168.20.254"
20
+ domain=foo
21
+
22
+ # setup bridge and nat
23
+ if [[ ! `ip link ls dev br0` ]]; then
24
+ brctl addbr br0
25
+ ifconfig br0 $gateway_ip netmask $netmask up
26
+ iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
27
+ sysctl -w net.ipv4.ip_forward=1
28
+ fi
29
+
30
+ if [[ ! `grep "auto br0" /etc/network/interfaces` ]]; then
31
+ cat <<EOF >> /etc/network/interfaces
32
+ auto br0
33
+ iface br0 inet static
34
+ address $gateway_ip
35
+ netmask $netmask
36
+ bridge_stp off
37
+ bridge_maxwait 5
38
+ pre-up brctl addbr br0
39
+ post-up /usr/sbin/brctl setfd br0 0
40
+ EOF
41
+ fi
42
+
43
+ sudo sed -i "s/#*[ ^I]*net\.ipv4\.ip_forward[ ^I]*=[ ^I]*[01]/net\.ipv4\.ip_forward = 1/" /etc/sysctl.conf
44
+
45
+ iptables-save > /etc/firewall.conf
46
+ echo "#!/bin/sh" > /etc/network/if-up.d/iptables
47
+ echo "iptables-restore < /etc/firewall.conf" >> /etc/network/if-up.d/iptables
48
+ chmod +x /etc/network/if-up.d/iptables
49
+
50
+ # setup cgroup
51
+ if [[ ! -d /cgroup ]]; then
52
+ mkdir -p /cgroup
53
+ fi
54
+
55
+ if [[ ! `mount | grep cgroup` ]]; then
56
+ mount none -t cgroup /cgroup
57
+ fi
58
+
59
+ if [[ ! `grep "/cgroup" /etc/fstab` ]]; then
60
+ cat <<EOF >> /etc/fstab
61
+ none /cgroup cgroup defaults 0 0
62
+ EOF
63
+ fi
64
+
65
+ # setup nameserver
66
+ cat <<EOF > /etc/bind/db.foo
67
+ \$ORIGIN $domain.
68
+ \$TTL 7200 ; 2 hours
69
+ @ IN SOA ns1.$domain. hostmaster.$domain. (
70
+ 3641625943 ; serial
71
+ 36000 ; refresh (10 hours)
72
+ 900 ; retry (15 minutes)
73
+ 36000 ; expire (10 hours)
74
+ 7200 ; minimum (2 hours)
75
+ )
76
+ NS ns1.$domain.
77
+ ns1 A $gateway_ip
78
+ EOF
79
+
80
+ cat <<EOF > /etc/bind/named.conf.$domain
81
+ zone "$domain" in{
82
+ type master;
83
+ file "/etc/bind/db.$domain";
84
+ allow-update {any;};
85
+ };
86
+ EOF
87
+
88
+ ## set bind to forward original nameservers
89
+ original_nameservers=`grep nameserver /etc/resolv.conf | cut -d " " -f2 | sed s/$gateway_ip//`
90
+ bind_forward_options=''
91
+ if [[ -n `echo $original_nameservers | tr -d ' \n\t\r'` ]]; then
92
+ bind_forward_options="forwarders {
93
+ `echo $original_nameservers | xargs -n 1 | awk '{ print $1";" }'`
94
+ };
95
+ forward first;"
96
+ fi
97
+
98
+ cat <<EOF > /etc/bind/named.conf.options.foo
99
+ options {
100
+ directory "/var/cache/bind";
101
+ $bind_forward_options
102
+ auth-nxdomain no; # conform to RFC1035
103
+ listen-on-v6 { any; };
104
+ };
105
+ EOF
106
+
107
+ # debian bind9 package does not have this default-zones file
108
+ # add this file to make it consistent between debian and ubuntu
109
+ cat <<EOF > /etc/bind/named.conf.default-zones
110
+ // prime the server with knowledge of the root servers
111
+ zone "." {
112
+ type hint;
113
+ file "/etc/bind/db.root";
114
+ };
115
+
116
+ // be authoritative for the localhost forward and reverse zones, and for
117
+ // broadcast zones as per RFC 1912
118
+
119
+ zone "localhost" {
120
+ type master;
121
+ file "/etc/bind/db.local";
122
+ };
123
+
124
+ zone "127.in-addr.arpa" {
125
+ type master;
126
+ file "/etc/bind/db.127";
127
+ };
128
+
129
+ zone "0.in-addr.arpa" {
130
+ type master;
131
+ file "/etc/bind/db.0";
132
+ };
133
+
134
+ zone "255.in-addr.arpa" {
135
+ type master;
136
+ file "/etc/bind/db.255";
137
+ };
138
+ EOF
139
+
140
+ mv /etc/bind/named.conf /etc/bind/named.conf.old
141
+ cat <<EOF > /etc/bind/named.conf
142
+ include "/etc/bind/named.conf.options.foo";
143
+ include "/etc/bind/named.conf.local";
144
+ include "/etc/bind/named.conf.default-zones";
145
+ include "/etc/bind/named.conf.$domain";
146
+ EOF
147
+
148
+ sudo chmod 775 /etc/bind
149
+
150
+ /etc/init.d/bind9 restart
151
+
152
+ # add our nameserver into /etc/resolv.conf
153
+ if [[ ! `grep "nameserver $gateway_ip" /etc/resolv.conf` ]]; then
154
+ cp /etc/resolv.conf /etc/resolv.conf.old
155
+ cat <<EOF > /etc/resolv.conf
156
+ nameserver $gateway_ip
157
+ `cat /etc/resolv.conf`
158
+ EOF
159
+ fi
160
+
161
+ # setup dhcp server
162
+ if [[ $OS = "Ubuntu" ]]; then
163
+ dhcp_conf_dir=dhcp
164
+ dhcp_daemon=isc-dhcp-server
165
+ else
166
+ dhcp_conf_dir=dhcp3
167
+ dhcp_daemon=dhcp3-server
168
+ fi
169
+
170
+ mv /etc/$dhcp_conf_dir/dhcpd.conf /etc/$dhcp_conf_dir/dhcpd.conf.old
171
+ cat <<EOF > /etc/$dhcp_conf_dir/dhcpd.conf
172
+ ddns-updates on;
173
+ ddns-update-style interim;
174
+
175
+ ddns-domainname "$domain.";
176
+ option domain-name "$domain.";
177
+ option domain-name-servers $gateway_ip;
178
+
179
+ option ntp-servers $gateway_ip;
180
+ default-lease-time 600;
181
+ max-lease-time 7200;
182
+
183
+ authoritative;
184
+ log-facility local7;
185
+
186
+ zone $domain. {
187
+ primary localhost;
188
+ }
189
+
190
+ subnet $subnet netmask $netmask {
191
+ range $range;
192
+ option routers $gateway_ip;
193
+ }
194
+ EOF
195
+ sudo /etc/init.d/$dhcp_daemon restart
196
+
197
+ sudo gem install bundler --no-ri --no-rdoc
198
+ sudo bundle install
199
+
@@ -0,0 +1,9 @@
1
+ cd /vagrant
2
+ wget http://dl.dropbox.com/u/43220259/toft-lxc_0.0.6_all.deb
3
+ sudo apt-get -y remove apparmor
4
+ sudo dpkg -i toft-lxc_0.0.6_all.deb
5
+ sudo apt-get -fy install
6
+ sudo ./scripts/bin/ubuntu/lxc-prepare-host
7
+ sudo gem install bundler --no-ri --no-rdoc
8
+ sudo bundle install
9
+ sudo cp natty-i386.tar.gz /var/cache/lxc/
@@ -0,0 +1,38 @@
1
+ #!/bin/bash
2
+
3
+ hostname=`hostname`
4
+ gateway_ip="192.168.20.1"
5
+ domain=foo
6
+
7
+ function get_ip {
8
+ echo `ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
9
+ }
10
+
11
+ function update_ns {
12
+ ip=`get_ip`
13
+ # add default route to host and update ns
14
+ cat <<END > /tmp/nsupdate.txt
15
+ server $gateway_ip
16
+ update delete ${hostname}.$domain
17
+ update add ${hostname}.$domain 86400 A $ip
18
+ send
19
+ END
20
+
21
+ route add default gw $gateway_ip
22
+ nsupdate /tmp/nsupdate.txt
23
+ }
24
+
25
+ if [[ -n `get_ip` ]] ; then # if manually set ip address
26
+ # use host dns server
27
+ rm /etc/resolv.conf
28
+ cat <<END > /etc/resolv.conf
29
+ nameserver $gateway_ip
30
+ domain $domain
31
+ search $domain
32
+ END
33
+ else # if ip not set, use dhcp
34
+ /sbin/dhclient eth0
35
+ fi
36
+
37
+ update_ns
38
+ exit 0
@@ -0,0 +1,279 @@
1
+ #!/bin/bash
2
+
3
+ echo "Creating centos-6 node..."
4
+
5
+ configure_centos()
6
+ {
7
+ rootfs=$1
8
+ hostname=$2
9
+
10
+ # disable selinux in centos
11
+ mkdir -p $rootfs/selinux
12
+ echo 0 > $rootfs/selinux/enforce
13
+
14
+ # add host root ssh access
15
+ mkdir $rootfs/root/.ssh
16
+ chmod 0600 $rootfs/root/.ssh
17
+ cat <<-EOF > $rootfs/root/.ssh/authorized_keys
18
+ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCguB7XL3ARzLZYLsIMZe4UUO371m+H5C6V8MhtmSlgXtgHDo7eZhNSm5zCeoyGd32OKeLxuxCCEkXfDDF1aa2a6twcASE3pmWNdnBS7auiOH4P7g+eQ4Aw9v7DdESbIVgHF/NDiAEFFdmApYNM3oCX2FhEVNVKxkkIokUr4axYFJzmJ6Xoi5Sd8JtPC85FZVXqDucZDnHQlOcCkbSo0UOmsWQGwtu8eUHoDeUG0dB8ntb9xlBeLctdrAPhuFYCX8IfFkdcakkzv61ETPbKE6g9wdTDC/TEep7/AHGYmarziRnwKiVOL1jnE1coOJLqy8wOC3dKGmRZy9D4sTc+FRV root insecure public key
19
+ EOF
20
+
21
+ cp /usr/lib/lxc/templates/files/rc.local $rootfs/etc/rc.local
22
+
23
+ # set the hostname
24
+ cat <<EOF > $rootfs/etc/hostname
25
+ $hostname
26
+ EOF
27
+ # set minimal hosts
28
+ cat <<EOF > $rootfs/etc/hosts
29
+ 127.0.0.1 localhost $hostname
30
+ EOF
31
+
32
+ cat <<EOF > $rootfs/etc/init/console.conf
33
+ # console - mingetty
34
+ #
35
+ # This service maintains a console on tty1 from the point the system is
36
+ # started until it is shut down again.
37
+
38
+ start on stopped rc RUNLEVEL=[2345]
39
+ stop on runlevel [!2345]
40
+
41
+ respawn
42
+ exec /sbin/mingetty console
43
+ EOF
44
+
45
+ cat <<EOF > $rootfs/etc/fstab
46
+ none /dev/pts devpts defaults 0 0
47
+ EOF
48
+
49
+ [ ! -f $rootfs/etc/init/kexec-disable.conf ] || mv $rootfs/etc/init/kexec-disable.conf $rootfs/etc/init/kexec-disable.conf.orig
50
+ sed -i -e 's:/sbin/start_udev:#/sbin/start_udev:' $rootfs/etc/rc.d/rc.sysinit
51
+
52
+ sed -i -e 's:\(Defaults *requiretty\):# \1:' $rootfs/etc/sudoers
53
+
54
+ # create necessary devices
55
+ rm $rootfs/dev/null
56
+ mknod -m 666 $rootfs/dev/null c 1 3
57
+ mknod -m 666 $rootfs/dev/random c 1 8
58
+ mknod -m 666 $rootfs/dev/urandom c 1 9
59
+ mkdir -m 755 $rootfs/dev/pts
60
+ mknod -m 666 $rootfs/dev/tty c 5 0
61
+ mknod -m 666 $rootfs/dev/tty0 c 4 0
62
+ mknod -m 666 $rootfs/dev/tty1 c 4 1
63
+ mknod -m 666 $rootfs/dev/tty2 c 4 2
64
+ mknod -m 666 $rootfs/dev/tty3 c 4 3
65
+ mknod -m 666 $rootfs/dev/tty4 c 4 4
66
+ mknod -m 600 $rootfs/dev/console c 5 1
67
+ mknod -m 666 $rootfs/dev/full c 1 7
68
+ mknod -m 600 $rootfs/dev/initctl p
69
+
70
+ # change root password
71
+ echo "Set root password to 'root'"
72
+ echo "root:root" | chroot $rootfs chpasswd
73
+
74
+ return 0
75
+ }
76
+
77
+ copy_centos()
78
+ {
79
+ cache=$1
80
+ arch=$2
81
+ rootfs=$3
82
+
83
+ # make a local copy of the minicentos
84
+ echo "Extracting rootfs image to $rootfs ..."
85
+ mkdir $rootfs
86
+ tar zxf $cache/centos-6-$arch.tar.gz -C $rootfs || return 1
87
+ return 0
88
+ }
89
+
90
+ install_centos()
91
+ {
92
+ cache="/var/cache/lxc"
93
+ rootfs=$1
94
+ mkdir -p /var/lock/subsys/
95
+ (
96
+ flock -n -x 200
97
+ if [ $? -ne 0 ]; then
98
+ echo "Cache repository is busy."
99
+ return 1
100
+ fi
101
+
102
+ arch=$(arch)
103
+ if [ "$arch" == "x86_64" ]; then
104
+ arch=amd64
105
+ fi
106
+
107
+ if [ "$arch" == "i686" ]; then
108
+ arch=i386
109
+ fi
110
+
111
+ echo "Checking image cache in $cache/rootfs-$arch ... "
112
+ if [ ! -e "$cache/rootfs-$arch" ]; then
113
+ if [ $? -ne 0 ]; then
114
+ echo "Failed to download 'centos base'"
115
+ return 1
116
+ fi
117
+ fi
118
+
119
+ copy_centos $cache $arch $rootfs
120
+ if [ $? -ne 0 ]; then
121
+ echo "Failed to copy rootfs"
122
+ return 1
123
+ fi
124
+
125
+ return 0
126
+
127
+ ) 200>/var/lock/subsys/lxc
128
+
129
+ return $?
130
+ }
131
+
132
+ copy_configuration()
133
+ {
134
+ path=$1
135
+ rootfs=$2
136
+ name=$3
137
+
138
+ cat <<EOF >> $path/config
139
+ lxc.utsname = $name
140
+
141
+ lxc.tty = 4
142
+ lxc.pts = 1024
143
+ lxc.rootfs = $rootfs
144
+ lxc.mount = $path/fstab
145
+
146
+ lxc.cgroup.devices.deny = a
147
+
148
+ lxc.cgroup.devices.allow = b 1:0 rwm
149
+ lxc.cgroup.devices.allow = b 1:1 rwm
150
+ lxc.cgroup.devices.allow = c 1:1 rwm
151
+ lxc.cgroup.devices.allow = c 1:2 rwm
152
+ lxc.cgroup.devices.allow = c 1:4 rwm
153
+ lxc.cgroup.devices.allow = c 1:6 rwm
154
+ lxc.cgroup.devices.allow = c 1:7 rwm
155
+ lxc.cgroup.devices.allow = c 1:11 rwm
156
+
157
+ lxc.cgroup.devices.allow = c 2:* rwm
158
+ lxc.cgroup.devices.allow = c 3:* rwm
159
+
160
+ # /dev/null and zero
161
+ lxc.cgroup.devices.allow = c 1:3 rwm
162
+ lxc.cgroup.devices.allow = c 1:5 rwm
163
+ # consoles
164
+ lxc.cgroup.devices.allow = c 5:1 rwm
165
+ lxc.cgroup.devices.allow = c 5:0 rwm
166
+ lxc.cgroup.devices.allow = c 4:0 rwm
167
+ lxc.cgroup.devices.allow = c 4:1 rwm
168
+ # /dev/{,u}random
169
+ lxc.cgroup.devices.allow = c 1:9 rwm
170
+ lxc.cgroup.devices.allow = c 1:8 rwm
171
+ lxc.cgroup.devices.allow = c 136:* rwm
172
+ lxc.cgroup.devices.allow = c 5:2 rwm
173
+ # rtc
174
+ lxc.cgroup.devices.allow = c 254:0 rwm
175
+ EOF
176
+
177
+ cat <<EOF > $path/fstab
178
+ proc $rootfs/proc proc nodev,noexec,nosuid 0 0
179
+ devpts $rootfs/dev/pts devpts defaults 0 0
180
+ sysfs $rootfs/sys sysfs defaults 0 0
181
+ EOF
182
+
183
+ if [ $? -ne 0 ]; then
184
+ echo "Failed to add configuration"
185
+ return 1
186
+ fi
187
+
188
+ return 0
189
+ }
190
+
191
+ clean()
192
+ {
193
+ cache="/var/cache/lxc"
194
+
195
+ if [ ! -e $cache ]; then
196
+ exit 0
197
+ fi
198
+
199
+ # lock, so we won't purge while someone is creating a repository
200
+ (
201
+ flock -n -x 200
202
+ if [ $? != 0 ]; then
203
+ echo "Cache repository is busy."
204
+ exit 1
205
+ fi
206
+
207
+ echo -n "Purging the download cache..."
208
+ rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
209
+ exit 0
210
+
211
+ ) 200>/var/lock/subsys/lxc
212
+ }
213
+
214
+ usage()
215
+ {
216
+ cat <<EOF
217
+ $1 -h|--help -p|--path=<path> --clean
218
+ EOF
219
+ return 0
220
+ }
221
+
222
+ options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
223
+ if [ $? -ne 0 ]; then
224
+ usage $(basename $0)
225
+ exit 1
226
+ fi
227
+ eval set -- "$options"
228
+
229
+ while true
230
+ do
231
+ case "$1" in
232
+ -h|--help) usage $0 && exit 0;;
233
+ -p|--path) path=$2; shift 2;;
234
+ -n|--name) name=$2; shift 2;;
235
+ -c|--clean) clean=$2; shift 2;;
236
+ --) shift 1; break ;;
237
+ *) break ;;
238
+ esac
239
+ done
240
+
241
+ if [ ! -z "$clean" -a -z "$path" ]; then
242
+ clean || exit 1
243
+ exit 0
244
+ fi
245
+
246
+ if [ -z "$path" ]; then
247
+ echo "'path' parameter is required"
248
+ exit 1
249
+ fi
250
+
251
+ if [ "$(id -u)" != "0" ]; then
252
+ echo "This script should be run as 'root'"
253
+ exit 1
254
+ fi
255
+
256
+ rootfs=$path/rootfs
257
+
258
+ install_centos $rootfs
259
+ if [ $? -ne 0 ]; then
260
+ echo "failed to install centos"
261
+ exit 1
262
+ fi
263
+
264
+ configure_centos $rootfs $name
265
+ if [ $? -ne 0 ]; then
266
+ echo "failed to configure centos for a container"
267
+ exit 1
268
+ fi
269
+
270
+ copy_configuration $path $rootfs $name
271
+ if [ $? -ne 0 ]; then
272
+ echo "failed write configuration file"
273
+ exit 1
274
+ fi
275
+
276
+ if [ ! -z $clean ]; then
277
+ clean || exit 1
278
+ exit 0
279
+ fi