summaryrefslogtreecommitdiff
path: root/manifests/service.pp
blob: 85a7ec5bbe064454be67da0aa00bbbb46925e922 (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
define runit::service (
  $user    = root,       # the service's user name
  $group   = root,       # the service's group name
  $enable  = true,       # shall the service be linked to /etc/service
  $ensure  = present,    # shall the service be present in /etc/sv
  # start command - either one of these three must be declared - it defines the content of the run script /etc/sv/$name/run
  $command = undef,      # the most simple way;  just state command here - it may not daemonize itself,
                         # but rather stay in the foreground;  all output is logged automatically to $logdir/current
                         # this uses a default template which provides logging
  $source  = undef,      # specify a source file on your puppet master 
  $content = undef,      # specify the content directly (mostly via 'template')
  # finish command - defines the content of the finish script /etc/sv/$name/finish
  $finish_command = '',
  $finish_source  = undef,
  $finish_content = undef,
  # service directory - this is required if you use 'command'
  $rundir  = undef,
  # logging stuff
  $logger  = true,       # shall we setup an logging service;  if you use 'command' before, 
                         # all output from command will be logged automatically to $logdir/current
  $_logdir  = undef,
  $timeout = 7           # service restart/stop timeouts (only relevant for 'enabled' services)
) {
  
  # using the following construct, because '$logdir = "${rundir}/log"' in the
  # define statement produces compilation warnings  
  if $_logdir == undef {
    $logdir = "${rundir}/log"
  } else {
    $logdir = $_logdir
  }

  # FixMe: Validate parameters
  # fail("Only one of 'command', 'content', or 'source' parameters is allowed")

  if $command != undef and $rundir == undef {
    fail( "You need to specify 'rundir': That's the directory from which the service will be started.")
  }

  # resource defaults
  File { owner => root, group => root, mode => 644 }

  $svbase = "/etc/sv/${name}"
  
  # creating the logging sub-service, if requested
  if $logger == true {
    runit::service{ "${name}/log":
      user => $user, group => $group, enable => false, ensure => $ensure, logger => false,
      content => template('runit/logger_run.erb'),
    }
  }
  
  # the main service stuff
  file {
    "${svbase}":
      ensure => $ensure ? {
        present => directory,
        default => absent,
        },
        purge => true,
        force => true,
      ;
    "${svbase}/run":
      content => $content ? {
        undef   => template('runit/run.erb'),
        default => $content,
      },
      source  => $source,
      ensure  => $ensure,
      mode    => 755,
      ;
    "${svbase}/finish":
      content => $finish_content ? {
        undef   => template('runit/finish.erb'),
        default => $finish_content,
      },
      source  => $finish_source,
      ensure  => $ensure,
      mode    => 755,
      ;
  }
  
  # eventually enabling/disabling the service
  if $enable == true {
    debug( "Service ${name}: ${_ensure_enabled}" )
    runit::service::enabled { $name: ensure => $ensure, timeout => $timeout }
  }

}