Defined Type: tp::service

Defined in:
manifests/service.pp

Overview

This define manages the service of the given app, creating the relevant service definition file (currently supported only systemd files)

It’s declared in tp::install::image, tp::install::source and tp::install:file defines to automatically create the service files for the installed apps. It’s not expected to be declared directly.

Parameters:

  • ensure (Variant[Boolean,String]) (defaults to: present)

    What to do with the app service. When absent, service is stopped.

  • on_missing_data (Tp::Fail) (defaults to: pick(getvar('tp::on_missing_data'),'notify'))

    What to do if tinydata is missing.

  • settings (Hash) (defaults to: {})

    The hash with all the apps settings.

  • my_options (Hash) (defaults to: {})

    An as of options used in the systemd_template

  • manage_service (Boolean) (defaults to: true)

    If to manage the app’s service

  • mode (Enum['docker','normal']) (defaults to: 'normal')

    The mode of the service. Can be ‘docker’ or ‘normal’. With docker a docker image is started and managed as a service.

  • $systemd_template

    The template to use for the systemd service unit file. The default is inifile_with_stanzas which gets the values from $my_options hash merged with the internal $options_defaults hash.

  • docker_image (Optional[String]) (defaults to: undef)

    The container image to use, when mode is set to docker.

  • command_path (Optional[Stdlib::Absolutepath]) (defaults to: undef)

    The full path of the command to use when starting the service. In normal mode with systemd is the value of ExecStart

  • systemd_symlink (Optional[String]) (defaults to: undef)

    The symlink to systemd unit file under /lib/systemd/system. If not set link will be /etc/systemd/system/multi-user.target.wants/$app.service



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'manifests/service.pp', line 35

define tp::service (
  Variant[Boolean,String] $ensure              = present,

  Tp::Fail $on_missing_data = pick(getvar('tp::on_missing_data'),'notify'),

  Hash $settings                               = {},
  Hash $my_options                             = {},

  Boolean $manage_service                      = true,

  Enum['docker','normal'] $mode                = 'normal',

  Optional[String] $docker_image               = undef,
  Optional[Stdlib::Absolutepath] $command_path = undef,

  Optional[String] $systemd_symlink            = undef,

) {
  $app = $title
  $sane_app = regsubst($app, '/', '_', 'G')
  $real_command_path = pick ($command_path, "${tp::real_tp_params['destination']['path']}/${app}")

  case $facts['service_provider'] {
    'systemd': {
      if $mode == 'docker' {
        # Docker port mapping. Check tinydata/data/references for the available ports format
        case getvar('settings.image.ports') {
          # If settings.image.ports is undefined we map the main port in setting.ports
          undef: {
            $port_mapping = getvar('settings.ports.main.port') ? {
              undef   => '',
              default => "-p ${settings['ports']['main']['port']}:${settings['ports']['main']['port']}",
            }
          }
          String[0]: {
            $port_mapping = ''
          }
          Integer: {
            $port_mapping = "-p ${settings['image']['ports']}:${settings['image']['ports']}"
          }
          String[1]: {
            $port_mapping = "-p ${settings['image']['ports']}"
          }
          Array: {
            $port_mapping = join(getvar('settings.image.ports').map|$k| { "-p ${k}" }, ' ')
          }
          Hash: {
            $port_mapping = join(getvar('settings.image.ports').map |$k,$v| { "-p ${k}:${v}" }, ' ')
          }
          default: {
            tp::fail($on_missing_data, "tp::service ${app} - settings.image.ports is not a valid type")
          }
        }

        # Docker volumes or bind mounts mapping. Check tinydata/data/references for the available mounts format
        case getvar('settings.image.mounts') {
          # If settings.image.mounts is undefined we map all the dirs path in setting.dirs
          undef: {
            $mount_mapping = getvar('settings.dirs') ? {
              undef   => '',
              default => join(getvar('settings.dirs').map |$k,$v| { "-v ${v['path']}:${v['path']}" }, ' '),
            }
          }
          String[0]: {
            $mount_mapping = ''
          }
          String[1]: {
            $mount_mapping = "-v ${settings['image']['mounts']}"
          }
          Array: {
            $mount_mapping = join(getvar('settings.image.mounts').map|$k| { "-v ${k}" }, ' ')
          }
          Hash: {
            $mount_mapping = join(getvar('settings.image.mounts').map |$k,$v| { "-v ${k}:${v}" }, ' ')
          }
          default: {
            tp::fail($on_missing_data, "tp::service ${app} - settings.image.mounts is not a valid type")
          }
        }

        $docker_args = pick_default(getvar('settings.docker.args'),'')
        $options_defaults = {
          'Unit' => {
            'Description'   => pick(getvar('settings.description'),"${app} service"),
            'Documentation' => pick(getvar('settings.urls.documentation'),getvar('settings.urls.website'),"Search: ${app}"),
            'After'         => 'docker.service',
            'Requires'      => 'docker.service',
          },
          'Service' => {
#            'ExecStartPre' => "/usr/bin/docker stop ${app} ; /usr/bin/docker rm ${app} ; /usr/bin/docker pull ${settings['docker_image']}",
            'ExecStart'    => "/usr/bin/docker run --rm --name ${app} ${docker_args} ${port_mapping} ${mount_mapping} ${docker_image}",
            'Restart'      => 'always',
            'RestartSec'   => '10s',
          },
          'Install' => {
            'WantedBy' => 'multi-user.target',
          },
        }
      } else {
        $options_defaults = {
          'Unit' => {
            'Description'   => pick(getvar('settings.description'),"${app} service"),
            'Documentation' => pick(getvar('settings.urls.documentation'),getvar('settings.urls.website'),"Search: ${app}"),
          },
          'Service' => {
            'ExecStart'       => $real_command_path,
            'Restart'         => 'always',
            'RestartSec'      => '10s',
            'User'            => pick(getvar('settings.service.main.process_user'),getvar('settings.process_user'),'root'),
            'Group'           => pick(getvar('settings.service.main.process_group'),getvar('settings.process_group'),'root'),
            'EnvironmentFile' => pick(getvar('settings.configs.init.path'),getvar('settings.init_file_path'),"/etc/default/${app}"), # lint:ignore:140chars
            'ExecReload'      => '/bin/kill -HUP $MAINPID',
          },
          'Install' => {
            'WantedBy' => 'multi-user.target',
          },
        }
      }
      $options = delete_undef_values($options_defaults.deep_merge($my_options))
      file { "/lib/systemd/system/${app}.service":
        ensure  => $ensure,
        path    => "/lib/systemd/system/${app}.service",
        owner   => 'root',
        group   => 'root',
        mode    => '0644',
        content => template(pick(getvar('settings.image.systemd_template'),'tp/inifile_with_stanzas.erb')),
        notify  => [Exec['tp systemctl daemon-reload'], Service[$app]],
      }
      $symlink_path = pick($systemd_symlink,"/etc/systemd/system/multi-user.target.wants/${app}.service") # lint:ignore:140chars
      file { $symlink_path:
        ensure => link,
        target => "/lib/systemd/system/${app}.service",
        notify => Exec['tp systemctl daemon-reload'],
        before => Service[$app],
      }
    }
    default: {
      tp::fail($on_missing_data, "tp::service ${app} - Service_provider ${facts['service_provider']} is not supported") # lint:ignore:140chars
    }
  }

  if $manage_service {
    service { $app:
      ensure    => tp::ensure2service($ensure,'ensure'),
      enable    => tp::ensure2service($ensure,'enable'),
      hasstatus => true,
    }
  }
}