summaryrefslogtreecommitdiff
path: root/dh_runit
blob: 0e1d32003e91b877168e345f5eb1c717e87feb39 (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
#!/usr/bin/perl -w

=head1 NAME

dh_runit - install/enable runit runscripts

=cut

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

=head1 SYNOPSIS

B<dh_runit> [S<I<debhelper options>>] [B<--no-enable>]

=head1 DESCRIPTION

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

For runit, every unit of supervision, simply speaking program, is
represented by directory under F</etc/sv>, containing at least F<run>
executable file. Every enabled program is represented by symbolic link
under F</etc/services> pointing to some directory under F</etc/sv>.

If F<debian/I<package>.runit> is not executable, but contains shebang
(#!) in first line, it is installed as F</etc/sv/I<package>/run> and
enabled.

Alternatively, F<debian/I<package>.runit> is a list of lines, where
every line is either starts with hash symbol and considered comment,
or contains two or three space-separated words.

  # In this case file is installed as 'run' script. Directory name under
  # /etc/sv is derived from file basename
  enable path/to/file/to/be/installed/as/run/script

  # Same, but install directory as whole. It is your responsibility
  # to ensure is contains everything required.
  enable path/to/directory

  # Same as above, but do not create symlink under /etc/service
  disable path/to/directory

  # Also, you can explicitly specify name of directory under /etc/sv
  enable path/to/directory my-preferred-name

=head1 OPTIONS

=over 4

=item B<--no-enable>

Do not enable any runscripts. Useful, when F<debian/I<package>.runit>
is the only runscript, in which case you have no other ways to
specify, that it should not be enabled.

=back

=cut

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

init(option => { 'no-enable' => \$dh{NO_ENABLE} });

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

    my $tmp = tmpdir($pkg);
    my $sv_dir = "$tmp/etc/sv";
    my $runit = pkgfile($pkg, 'runit');
    next unless $runit;

    doit('install', '-d', $sv_dir);

    for my $words (filedoublearray($runit)) {
        (my $enable, my $path, my $name) = @{$words};
        $name = $name || basename($path);

        error("can't read `$path'") unless -r $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);

        if ($enable eq 'enable' && !$dh{NO_ENABLE}) {
            autoscript($pkg, 'postinst', 'postinst-runit', "s/#NAME#/$name/");
        }
        autoscript($pkg, 'prerm', 'prerm-runit', "s/#NAME#/$name/");
    }
}

# PROMISE: DH NOOP WITHOUT runit