summaryrefslogtreecommitdiff
path: root/dh_runit
blob: 5b345d6d56db78bfa5749e7de641243ff4f9068d (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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/perl -w


use v5.10.1;
use strict;
use Debian::Debhelper::Dh_Lib;
use File::Find;
use File::stat;
use feature 'signatures';
no warnings 'experimental';

sub parse_options($opts) {
    my $conf = { enable => 1 };
    for my $opt (split(/,/, $opts)) {
        given($opt) {
            when (/^disable$/)     { $conf->{enable} = 0; };
            when (/^name=(.*)$/)   { $conf->{name} = $1; };
            when (/^logscript$/)   { $conf->{logscript} = 1};
            when (/^defaults$/)    { "do nothing"; };
            default                { error("unknown option `$opt'"); }
        }
    }
    return $conf;
}

sub ensure_executable($directory) {
    for my $f ('run', 'finish', 'log/run', 'log/finish') {
        my $file = "$directory/$f";
        doit('chmod', '+x', $file) if (-e $file);
    }
}

sub runit_autoscript($pkg, $script, $sed) {
    autoscript($pkg, $script, "$script-runit", $sed);
}

init();

PKG: foreach my $pkg (@{$dh{DOPACKAGES}}) {
    next if is_udeb($pkg);

    my @entries = ();
    if (my $pkgfile = pkgfile($pkg, 'runit')) {
        @entries = filedoublearray($pkgfile);
    }
    while (@ARGV) {
        (my $path, my $opts) = splice(@ARGV, 0, 2);
        push @entries, [$path, $opts];
    }

    my $tmp = tmpdir($pkg);
    my $sv_dir = "$tmp/etc/sv";

    for (@entries) {
        (my $path, my $opts) = @$_;
        error("can't read `$path'") unless -r $path;

        my $conf = parse_options($opts);
        my $name = $conf->{name} || basename($path);

        if ( -f $path) {
            install_dir("$sv_dir/$name");
            install_prog($path, "$sv_dir/$name/run");
        } elsif ( -d $path) {
            doit('cp', '-r', $path, "$sv_dir/$name");
            # Unfortunately, dh_fixperms does not handle executable bit here.
            ensure_executable("$sv_dir/$name");
        }
        make_symlink("/etc/sv/$name/supervise",
                     "/var/lib/runit/supervise/$name", $tmp);
        install_dir("$tmp/var/lib/runit/supervise/$name");
        install_dir("$tmp/etc/runit/runsvdir/default");

        my $substitutions = {
            NAME   => $name,
            ENABLE => $conf->{enable} ? "yes" : "no"
        };

        runit_autoscript($pkg, 'postrm', $substitutions);
        runit_autoscript($pkg, 'postinst', $substitutions);

        if ($conf->{logscript}) {
            my $logdir = "/var/log/runit/$name";

            install_dir("$sv_dir/$name/log");
            install_dir($tmp . $logdir);

            my $run_log = "$sv_dir/$name/log/run";
            open(RUN_LOG, ">$run_log") || die $!;
            print RUN_LOG << "HERE";
#!/bin/sh
chown -R runit-log:adm '$logdir'
chmod 750 '$logdir'
chmod u+rw,g+r,o-rwx '$logdir'/*
exec chpst -u runit-log svlogd -tt '$logdir'
HERE
            close(RUN_LOG);
            chmod(0755, $run_log);
            make_symlink("/etc/sv/$name/log/supervise",
                         "/var/lib/runit/log/supervise/$name", $tmp);
            install_dir("$tmp/var/lib/runit/log/supervise/$name");
        }
    }
    # runit=2.1.2-20 introduced 'runit-log' user
    # runit=2.1.2-23 introduced /lib/runit/invoke-run
    addsubstvar($pkg, 'runit:Conflicts', 'runit', '<< 2.1.2-23~');
    addsubstvar($pkg, 'runit:Breaks', 'runit', '<< 2.1.2-23~');
    addsubstvar($pkg, 'misc:Depends', 'runit-helper', '>= 2.8.1~');
}

# PROMISE: DH NOOP WITHOUT runit

=head1 NAME

dh_runit - install/enable runit runscripts

=head1 SYNOPSIS

B<dh_runit> [S<I<debhelper options>>] [I<path> I<options>] ...

=head1 DESCRIPTION

B<dh_runit> is a debhelper program that is responsible for
installing and enabling I<runit> runscripts. If a file named
F<debian/I<package>.runit> exists, then it ensures appropriate actions
are performed based on its content.

For runit, each unit of supervision (or, simply speaking, program) is
represented by a directory under F</etc/sv>, containing at least a F<run>
executable file. Each enabled unit of supervision is represented by a
symbolic link under F</etc/services> (which itself is a symbolic link to
F</etc/runit/runsvdir/default>) pointing to some directory under
F</etc/sv>.

B<dh_runit> reads arguments from the command line and
F<debian/I<package>.runit> in pairs, with the first item being the
path to a file or directory and the second being a set of options.
If the first argument names a file, it is treated as a 'run' script
to be installed at F</etc/sv/*/run> as an executable file. If the first
argument names a directory, that directory is copied as a whole to
F</etc/sv>.

Options are comma separated, like mount options. Unrecognized options
are errors. The following options are recognized:

=over

=item I<disable>

Install the runscript but do not enable it by default.
This means that the corresponding service will not be started.
The service can be enabled manually by the system administrator or
automatically using update-service(8).

=item I<name>=preferred-name

By default, the name of the directory under F</etc/sv> for a given
runscript is the basename of the path argument naming it. This
option allows overriding that default with an explicitly chosen
directory name.

=item I<logscript>

Install a standard F<log/run> script that invokes svlogd(8) with
the rights of the dedicated user. Specifying this option produces
an error if the path argument names a directory that already
contains a F</log/run> script.

=item I<defaults>

If you don't need other options, specify this one.

=back

=head1 SUBSTITUTION VARIABLES

Packages using B<dh_runit> do not depend on B<runit> but should include the
I<runit:Breaks> variable in their I<Breaks> field in I<debian/control>
to ensure that no breakages are caused by a too-old version of B<runit> package.

=head1 EXAMPLES

This section contains several example F<I<package>.runit> snippets.

  # In this case, a file is installed as a 'run' script. The directory
  # name under /etc/sv is derived from the file's basename (/etc/sv/script).
  path/to/file/to/be/installed/as/run/script defaults

  # Similar, but installs a directory as a whole. It is the package's
  # responsibility to ensure this directory contains everything required.
  path/to/directory defaults

  # Similar, but without creating a symlink under /etc/service.
  path/to/directory disable

  # Explicitly specifying a name to use for the directory under /etc/sv.
  # A standard log/run script will be created.
  path/to/directory name=my-preferred-name,logscript

=cut

# vim: et:sw=4