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