summaryrefslogtreecommitdiff
path: root/manifests/machine.pp
blob: 17cd3a9c51f7187f163d59157b579cc87f995659 (plain)
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
# @summary Configuration and provisioning for a single container.
# @param name
#   Will be used for both the directory name, and the hostname in the container.
# @param template
#   Which template this machine should be configured from.
#
#   Templates needs to be manually configured behorehand.
#
#   The value 'none' is special, since it allows the machine to be
#   managed without a template. The template parameter is however
#   required, since a machine without a template needs to be manually
#   configured through some other mean.
#
# @param domain
#   Domain part of FQDN of container.
#
# @param config
#   Configuration for the machine, as per systemd.nspawn(5).
#   Will be merged with `nspawn::config` per the `$merge` variable.
#
# @param merge
#   How this nodes configuration should be merged with the defalut hash.
#
#   - deep
#     Stdlib's `deep_merge` will be used, with us on the right.
#   - shallow
#     `$nspawn::config + $config`
#   - replace
#     The upstream will be ignored.
#
# @param ensure
define nspawn::machine (
  Variant[String, Enum['none']] $template,
  String $domain = $trusted['domain'],
  Nspawn::Systemd::Nspawn $config = {},
  Enum['deep', 'shallow', 'override'] $merge = 'deep',
  Enum['present', 'absent'] $ensure = 'present',
) {
  $root = "${nspawn::machine_dir}/${name}"

  $almost_final_config = $merge ? {
    'deep'     => deep_merge($nspawn::config, $config),
    'shallow'  => $nspawn::config + $config,
    'override' => $config,
  }

  $final_config = $nspawn::config_base + $almost_final_config

  file { "${nspawn::nspawn_dir}/${name}.nspawn":
    ensure  => $ensure,
    content => epp("${module_name}/systemd/nspawn.epp", {
        'data' => $final_config
    }),
  }

  if $ensure == 'present' {
    # if $machine_dir has a quota set, then this inherits it
    unless $template == 'none' {
      exec { "Initialize ${name} from template":
        cmd     => [
          'btrfs', 'snapshot',
          "${nspawn::template_dir}/${template}",
          $root,
        ],
        creates => $root,
        path    => ['/bin', '/usr/bin', '/sbin', '/usr/sbin'],
      }
    }

    file { "${root}/etc/passwd":
      content => "${name}\n",
    }

    file_line { "${root}/etc/hosts ::1":
      line  => "::1\t${name}.${domain}\t${name}",
      match => "${name}.${domain}",
      path  => "${root}/etc/hosts",
    }
  } else {
    service { "systemd-nspawn@${name}":
      ensure => stopped,
      enable => false,
    }

    exec { "Remove btrfs subvolume ${root}":
      cmd    => ['btrfs', 'subvolume', 'delete', $root],
      onlyif => [['test', '-d', $root]],
      path   => ['/bin', '/usr/bin', '/sbin', '/usr/sbin'],
    }
  }
}