]> WPIA git - infra.git/blob - modules/lxc/manifests/init.pp
chg: extract lxc-bind-mounts to own resources
[infra.git] / modules / lxc / manifests / init.pp
1 class lxc {
2     file {"/data/log":
3         ensure => 'directory'
4     }
5     package{ 'lxc':
6         ensure => 'installed'
7     }->
8     exec {'lxc-base-image-created':
9         logoutput => on_failure,
10         command => '/usr/bin/lxc-create -n base-image -t debian -- -r stretch --packages=gnupg2,puppet,lsb-release,debconf-utils && rm -r /var/lib/lxc/base-image/rootfs/var/lib/apt/lists',# gnupg2 needed for puppet managing apt-keys
11         unless => '/usr/bin/test -d /var/lib/lxc/base-image',
12         timeout => '0'
13     }
14     package {'bridge-utils':
15         ensure => 'installed'
16     } -> file {'/etc/network/interfaces.d/lxcbr0':
17         source => 'puppet:///modules/lxc/lxcbr0'
18     } ~> exec{'stop all containers':
19       command => '/bin/bash -c \'for i in $(lxc-ls); do if [[ $i != base-image ]]; then lxc-stop -n "$i"; fi; done\'',
20       refreshonly => true,
21     } -> exec {'ifup lxcbr0':
22       command => '/sbin/ifdown lxcbr0; /sbin/ifup lxcbr0',
23       refreshonly => true,
24       subscribe => File['/etc/network/interfaces.d/lxcbr0']
25     }
26     
27     define container_bind($container, $out = $name, $target, $option = ""){
28       file{"/var/lib/lxc/$container/rootfs/$target":
29         ensure => directory,
30         require => File_line["lxc-$container-conf5"],
31         before => Exec["lxc-$container-started"]
32       }->
33       file_line { "lxc-$container-mount-$out":
34         path   => "/var/lib/lxc/$container/config",
35         line   => "lxc.mount.entry = $out $target none bind$option 0 0",
36         require=> [File_line["lxc-$container-conf5"], File["$out"]],
37         notify  => Exec["lxc-$container-started"],
38       }
39     }
40     define container ($contname, $ip, $dir = [], $bind = {}, $confline = []) {
41         exec {"lxc-$contname-issue-cert":
42           command => "/usr/bin/puppet ca destroy \"$contname\";/usr/bin/puppet ca generate \"$contname\"",
43           unless => "/usr/bin/[ -f /var/lib/puppet/ssl/private_keys/$contname.pem ] && /usr/bin/[ -f /var/lib/puppet/ssl/certs/$contname.pem ]",
44           before => Exec["lxc-$contname-started"]
45         }
46         $ipv6 = $ipsv6[$contname]
47
48         exec{ "lxc-$contname-created":
49             logoutput => on_failure,
50             command   => "/usr/bin/lxc-copy -n base-image -N $contname",
51             unless    => "/usr/bin/test -d /var/lib/lxc/$contname",
52             timeout   => '0',
53             require   => [Package['lxc'],Exec['lxc-base-image-created']],
54         } -> file_line {"lxc-$contname-conf1":
55             path   => "/var/lib/lxc/$contname/config",
56             line   => 'lxc.network.type = veth',
57             notify => Exec["lxc-$contname-started"],
58         } -> file_line {"lxc-$contname-conf2":
59             path   => "/var/lib/lxc/$contname/config",
60             line   => 'lxc.network.link = lxcbr0',
61             notify => Exec["lxc-$contname-started"],
62         } -> file_line {"lxc-$contname-conf3":
63             path   => "/var/lib/lxc/$contname/config",
64             line   => 'lxc.network.flags = up',
65             notify => Exec["lxc-$contname-started"],
66         } -> file_line {"lxc-$contname-conf4":
67             path   => "/var/lib/lxc/$contname/config",
68             line   => "lxc.network.ipv4 = $ip/24",
69             notify => Exec["lxc-$contname-started"],
70         } -> file_line {"lxc-$contname-conf5":
71             path   => "/var/lib/lxc/$contname/config",
72             line   => 'lxc.network.ipv4.gateway = 10.0.3.1',
73             notify => Exec["lxc-$contname-started"],
74       } -> file_line {"lxc-$contname-conf6":
75             path   => "/var/lib/lxc/$contname/config",
76             line   => "lxc.network.ipv6 = $ipv6/64",
77             match  => '^lxc\.network\.ipv6 =',
78             notify => Exec["lxc-$contname-started"],
79         } -> file_line {"lxc-$contname-conf7":
80             path   => "/var/lib/lxc/$contname/config",
81             line   => 'lxc.network.ipv6.gateway = fc00:0001::0000:0001',
82             match  => '^lxc\.network\.ipv6\.gateway =',
83             notify => Exec["lxc-$contname-started"],
84         } -> file_line {"lxc-$contname-network":
85             path   => "/var/lib/lxc/$contname/rootfs/etc/network/interfaces",
86             line   => 'iface eth0 inet manual',
87             match  => '^iface eth0 inet',
88             notify => Exec["lxc-$contname-started"],
89         } -> exec {"lxc-$contname-started":
90             path => '/usr/bin',
91             refreshonly   => true,
92             refresh   => "/usr/bin/lxc-stop -n $contname ; /usr/bin/lxc-start -dn $contname; /usr/bin/lxc-attach -n $contname -- bash -c 'while ! [[ -S /run/systemd/private ]]; do sleep 1; done'",
93         }-> exec {"lxc-$contname-started1":
94             command   => "/usr/bin/lxc-start -dn $contname; /usr/bin/lxc-attach -n $contname -- bash -c 'while ! [[ -S /run/systemd/private ]]; do sleep 1; done'",
95             unless    => "/usr/bin/[ \"\$(lxc-info -Hsn $contname)\" != \"STOPPED\" ]",
96         }
97         $dir.each |String $in| {
98           file { "/var/lib/lxc/$contname/rootfs/$in":
99             ensure  => 'directory',
100             notify => Exec["lxc-$contname-started"],
101             require => File_line["lxc-$contname-conf5"]
102           }
103         }
104         $bind.each |String $out, Struct[{target=>String, Optional[option]=>String}] $in| {
105           file_line { "lxc-$contname-mount-$out":
106            path   => "/var/lib/lxc/$contname/config",
107            line   => "lxc.mount.entry = $out ${in[target]} none bind${in[option]} 0 0",
108            require=> [File_line["lxc-$contname-conf5"], File["$out"]],
109            notify  => Exec["lxc-$contname-started"],
110           }
111         }
112         file {"/data/log/$contname":
113            ensure => 'directory'
114         }->
115         file_line { "lxc-$contname-mount-journal":
116            path   => "/var/lib/lxc/$contname/config",
117            line   => "lxc.mount.entry = /data/log/$contname var/log/journal none bind 0 0",
118            require=> File_line["lxc-$contname-conf5"],
119            notify  => Exec["lxc-$contname-started"],
120         }
121         file {"/var/lib/lxc/$contname/rootfs/var/log/journal":
122             ensure  => 'directory',
123             notify => Exec["lxc-$contname-started"],
124             require => File_line["lxc-$contname-conf5"]
125         }
126         $confline.each |Integer $idx, String $in| {
127          file_line { "lxc-$contname-confline-extra-$idx":
128            path   => "/var/lib/lxc/$contname/config",
129            line   => "$in",
130            require=> File_line["lxc-$contname-conf5"],
131            notify  => Exec["lxc-$contname-started"],
132          }
133         }
134         file {"/var/lib/lxc/$contname/rootfs/var/lib/puppet":
135              ensure => 'directory',
136              require => Exec["lxc-$contname-created"]
137         }
138         file {"/var/lib/lxc/$contname/rootfs/var/lib/puppet/ssl":
139              ensure => 'directory'
140         }
141         file {"/var/lib/lxc/$contname/rootfs/var/lib/puppet/ssl/private_keys/":
142              ensure => 'directory'
143         }
144         file {"/var/lib/lxc/$contname/rootfs/var/lib/puppet/ssl/certs/":
145              ensure => 'directory'
146         }
147         Exec["lxc-$contname-started1"] ->
148         file_line {"lxc-$contname-hosts":
149             path   => "/var/lib/lxc/$contname/rootfs/etc/hosts",
150             line   => '10.0.3.1 puppet puppet.lan host01';
151         }->
152         file_line {"lxc-$contname-hosts-local":
153             path   => "/var/lib/lxc/$contname/rootfs/etc/hosts",
154             line   => "127.0.0.1 $contname"
155         }->
156         file_line {"lxc-$contname-resolv1":
157             path   => "/var/lib/lxc/$contname/rootfs/etc/resolv.conf",
158             ensure => 'absent',
159             match_for_absence => "true",
160             match  => '^domain ',
161             line   => ''
162         }->
163         file_line {"lxc-$contname-resolv2":
164             path   => "/var/lib/lxc/$contname/rootfs/etc/resolv.conf",
165             ensure => 'absent',
166             match_for_absence => "true",
167             match  => '^search ',
168             line   => ''
169         } ->
170         exec {"lxc-$contname-install-puppet":
171           command => "/usr/bin/lxc-attach -n \"$contname\" -- apt-get update && /usr/bin/lxc-attach -n \"$contname\" -- apt-get install -y puppet",
172           timeout => '0',
173           creates => "/var/lib/lxc/$contname/rootfs/usr/bin/puppet"
174         } ->
175         file {"/var/lib/lxc/$contname/rootfs/var/lib/puppet/ssl/private_keys/$contname.pem":
176           source => "file:///var/lib/puppet/ssl/private_keys/$contname.pem",
177           notify => Exec["lxc-$contname-puppet-restart"],
178         } ->
179         file {"/var/lib/lxc/$contname/rootfs/var/lib/puppet/ssl/certs/$contname.pem":
180           source => "file:///var/lib/puppet/ssl/certs/$contname.pem",
181           notify => Exec["lxc-$contname-puppet-restart"],
182         }
183         exec {"lxc-$contname-puppet-restart":
184           command => "/usr/bin/lxc-attach -n $contname -- systemctl stop puppet",
185           timeout   => '0',
186           refreshonly => 'true'
187         } ~>
188         exec {"lxc-$contname-refresh":
189           command => "/usr/bin/lxc-attach -n $contname -- puppet agent --onetime --no-daemonize --verbose",
190           timeout   => '0',
191           # TODO figure out a way to verify puppet launches
192           creates => "/var/lib/lxc/$contname/rootfs/certified"
193           ##creates => "/var/lib/lxc/$contname/rootfs/lib/systemd/system/puppet.service"
194         } ~>
195         exec {"lxc-$contname-puppet-start":
196           command => "/usr/bin/lxc-attach -n $contname -- systemctl start puppet",
197           timeout   => '0',
198           refreshonly => 'true'
199         }
200     }
201
202 }