File: dh_runit

package info (click to toggle)
dh-runit 2.16.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: perl: 302; sh: 219; haskell: 49; makefile: 22
file content (433 lines) | stat: -rwxr-xr-x 16,713 bytes parent folder | download
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/perl -w


use v5.10.1;
use strict;
use Debian::Debhelper::Dh_Lib;
use File::Find;
use File::Path qw(make_path);
use File::stat;
use Text::Hogan::Compiler;
use File::Slurp qw(read_file write_file edit_file);
use File::Copy::Recursive qw(dircopy);
use feature 'signatures';

our $VERSION = "2.16.4";

# Create empty file {dest} and all required parent directories.
sub create_empty_file {
    my ($dest) = @_;
    make_path(dirname($dest));
    write_file($dest, '') || die $!;
}

# Render mustache template {name} from data directory into {dest},
# substituting {values} hash. If {perm} argument is provided, it is used
# set permissions of {dest}. Intermediate directories to ${dest} are
# created as needed.
#
# Data directory is specified by {DH_RUNIT_DATADIR} environment
# variable, and defaults to /usr/share/dh-runit/data.
sub template_from_data_directory {
    my ($name, $dest, $values, $perm) = @_;
    my $datadir = $ENV{DH_RUNIT_DATADIR} || "/usr/share/dh-runit/data";
    my $template = read_file("${datadir}/${name}");
    my $compiler = Text::Hogan::Compiler->new;
    my $output = $compiler->compile($template)->render($values);

    create_empty_file($dest);
    write_file($dest, $output);

    if (defined $perm) {
        chmod $perm, $dest;
    }
}

sub parse_options($opts) {
    my $conf = { enable => 1, onupgrade => 'restart' };
    for my $opt (split(/,/, $opts)) {
        for($opt) {
            if (/^disable$/)            { $conf->{enable} = 0; }
            elsif (/^name=(.*)$/)       { $conf->{name} = $1; }
            elsif (/^onupgrade=(.*)$/)  { $conf->{onupgrade} = $1; }
            elsif (/^since=(.*)$/)      { $conf->{since} = $1; }
            elsif (/^logscript$/)       { $conf->{logscript} = 1}
            elsif (/^noreplace$/)       { $conf->{noreplace} = 1}
            elsif (/^noscripts$/)       { $conf->{noscripts} = 1}
            elsif (/^presubj$/)         { $conf->{presubj} = 1; }
            elsif (/^usr$/)             { $conf->{usr} = 1}
            elsif (/^finish$/)          { $conf->{finish} = 1}
            elsif (/^nofinish$/)        { $conf->{nofinish} = 1}
            elsif (/^bin=(.*)$/)        { $conf->{bin} = $1; }
            elsif (/^defaults$/)        { }  #do nothing
            else                        { error("unknown option `$opt'"); }
        }
    }
    return $conf;
}

sub ensure_executable($directory) {
    my @scripts = (
        'run',
        'finish',
        'check',
        'log/run',
        'log/finish',
        'control/c',
        'control/d',
        'control/t',
        'control/u',
        'control/x',
    );

    for my $f (@scripts) {
        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];
    }

    next unless @entries;

    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 ($conf->{usr}) {
            error("usr must be used together with bin option") unless ($conf->{bin});
        }

        my $sv_dir = "$tmp/etc/sv" unless ($conf->{usr});
        $sv_dir = "$tmp/usr/share/runit/sv" if ($conf->{usr}) ;
        my $meta_dir = "$sv_dir/$name/.meta";
        my $finish ="$sv_dir/$name/finish";

        my @upgrades = ("restart", "stop", "nostop", "reload");
        foreach my $upstring (@upgrades) {
			last if ($conf->{onupgrade} eq $upstring );
			error("unknown onupgrade string") if ($upstring eq 'reload');
        }

        if ($conf->{onupgrade} eq 'nostop' || $conf->{onupgrade} eq 'reload') {
            create_empty_file("$meta_dir/noreplace");
        }

        #Metafiles: ## note that usr requires bin but bin does not require usr
        if ($conf->{bin}) {
            # set ownership of the service in /etc, by packagename
            create_empty_file("$meta_dir/pkg");
            write_file("$meta_dir/pkg", "$pkg");
            create_empty_file("$meta_dir/bin");
            write_file("$meta_dir/bin", $conf->{bin});
            #service upgrade action; restart is the implicit default, only write non-defaults values
            if ($conf->{onupgrade} ne "restart" ) {
                write_file("$meta_dir/onupgrade", $conf->{onupgrade});
            }
            #enable/disable: enable=yes is the default value, only write non-default 'no'
            write_file("$meta_dir/enable", 'no') unless($conf->{enable});
	}
        unless($conf->{usr}) {
	    # These files allow handling of uninstalled-not-purged situation.
	    make_path("${tmp}/usr/share/runit/meta/${name}");
            create_empty_file("${tmp}/usr/share/runit/meta/${name}/installed");
            create_empty_file("${tmp}/etc/sv/${name}/.meta/installed");
        }

        #install the service: run, finish, log, supervise links
        if ( -f $path) {
            install_dir("$sv_dir/$name");
            install_prog($path, "$sv_dir/$name/run");
        } elsif ( -d $path) {
            dircopy($path, "$sv_dir/$name");
            # Unfortunately, dh_fixperms does not handle executable bit here.
            ensure_executable("$sv_dir/$name");
        }
        edit_file { s/##bin##/$conf->{bin}/g } "$sv_dir/$name/run";
        install_dir("$tmp/etc/runit/runsvdir/default");

        my $finishdatadir = $ENV{DH_RUNIT_DATADIR} || "/usr/share/dh-runit/data";
        if ($conf->{finish}) {
            install_prog("$finishdatadir/finish", "$finish") unless(-e $finish);
        }

        if ($conf->{logscript}) {
            my $logdir = "/var/log/runit/$name";
            if (-e "$sv_dir/$name/log/run") {
                error("logscript option but log/run already exists");
	    }
	        elsif  ($conf->{usr}) {
	        #create_empty_file("$meta_dir/logscript"); #use the empty log dir instead
	        install_dir("$sv_dir/$name/log");
	    }
                else {
                install_dir("$sv_dir/$name/log");
                install_dir($tmp . $logdir);
                template_from_data_directory('logscript', "$sv_dir/$name/log/run",
                                         { logdir => $logdir }, 0755);
	    }
        }

        my $substitutions = {
            NAME   => $name,
            ENABLE => $conf->{enable} ? "yes" : "no",
            ONUPGRADE => $conf->{onupgrade}
        };
        unless ($conf->{noscripts}) {
            runit_autoscript($pkg, 'postinst', $substitutions) unless ($conf->{bin});
            runit_autoscript($pkg, 'prerm', $substitutions) unless ($conf->{bin});
            runit_autoscript($pkg, 'postrm', $substitutions);
        }

        if ($conf->{presubj}) {
            if ( -e "debian/$pkg.bug-presubj") {
                warning("presubj for $pkg already exists and takes precedence, runit presubj file not installed")
            }
            else {
             template_from_data_directory('presubj', "$tmp/usr/share/bug/$pkg/presubj",
                                          { pkg => $pkg }, 0644);
            }
         }

         #opts scheduled for removal in 3.0 release
         if ($conf->{nofinish}) {
            warning("the nofinish option is deprecated and will be removed in 3.0 release of dh-runit");
	}
         if ($conf->{since}) {
            warning("the since option is deprecated and will be removed in 3.0 release of dh-runit");
        }
         if ($conf->{noreplace}) {
            warning("the noreplace option is deprecated, please use onupgrade=nostop instead");
        }
    }
    # runit=2.1.2-20 introduced 'runit-log' user
    # runit=2.1.2-23 introduced /lib/runit/invoke-run
    # runit=2.1.2-36 introduced '_runit-log' user
    # runit-helper 2.8.15 use dotlinks to mark a service as disabled, 'since' option is deprecated
    # runit-helper 2.9.0 has code for loguser transition
    # dh-runit 2.10.0 introduce the 'onupgrade' option that needs support in runit-helper
    # runit 2.1.2-46 introduces /lib/runit/make_svlinks
    # runit-helper 2.14.0 calls make_svlinks in postinst
    # runit 2.1.2-51 stops checking meta in /usr/share/runit/meta/$name (except for installed)
    addsubstvar($pkg, 'runit:Conflicts', 'runit', '<< 2.1.2-51~');
    addsubstvar($pkg, 'runit:Breaks', 'runit', '<< 2.1.2-51~');
    addsubstvar($pkg, 'misc:Depends', 'runit-helper', '>= 2.14.0~');
}

# 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 by the system administrator manually or
with 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<onupgrade>=string

This option controls the debhelper action performed during the upgrade
of the service, when runit is PID 1, and it takes one of the following
strings as argument:

F<restart>
 restart the service in postinstall, after all files of the package are
 unpacked and the upgrade is complete. This is the same as dh_installinit
 or dh_installsystemd ' --restart-after-upgrade '.
 This is the implicit default if the 'onupgrade' option is not set.

F<stop>
 stop the service in prerm and start again in postinst. This option is useful
 if the daemon get confused by the package being upgraded while it's still
 running. This is the same as dh_installinit or dh_installsystemd
 '--no-restart-after-upgrade'.

F<nostop>
 do not stop service on upgrade. This has the side-effect of not restarting the
 service as a part of the upgrade. Use this option when the service does not 
 support being restarted while it's running. Display managers (xdm, slim, ...) 
 are example of non-restartible services.
 When this option is set the B<invoke-run> interpreter, provided by I<runit>
 package, will refrain from automatically replace the sysvinit-managed 
 instance of the service with a runit-managed instance (replacement requires manual
 intervention or a system restart).
 This is the same as dh_installinit or dh_installsystemd '--no-stop-on-upgrade'.

F<reload>
 similar to nostop, but the service configuration is reloaded (HUP) during the upgrade. 
 This might be useful in cases (like dbus) where the service does not support restarting
 on upgrade and where reloading the config instead of restarting makes sense.
 When this option is set the B<invoke-run> interpreter, provided by I<runit>
 package, will refrain to automatically replace the sysvinit-managed 
 instance of the service with a runit-managed instance (replacement requires manual
 intervention or a system restart).

The F<onupgrade> option does not control whether the service will be started
after the first install of the package; by default the service is always started
if it's enabled (and stopped before removal). To not start the service after the
first install you should use the 'disable' option.
The upgrade action usually happens in postinst, however if the bin option is used,
the action is performed at the end of the upgrade process, with a noawait trigger
on runit's package side.

=item I<noscripts>

With this option dh-runit will not add its snippets to maintscripts; actions like
enable or disable, start, stop, restart and purge the service need manual editing of
maintaner scripts.

=item I<usr>

With this option dh-runit will install the service directory under /usr/share/runit/sv/
instead of the default /etc/sv/. It must be used together with the 'bin' option or 
dh-runit will exit with an error. This option is experimental, is mainly intended
for a catch-all runscript package and may produce a buggy package (that fails
the piuparts purge test).

=item I<finish>

Install a standard finish file with the service; if there is already a finish file
inside the service directory, this option does nothing.

=item I<bin>=/full/path/to/bin

The full path to the binary that is exec'd in the runscript should be provided.
When this option is used, additional metafiles are written inside the service
directory so that postinstall and prerm actions that are usually performed inside
maintainer scripts are performed with a trigger on runit package side.
When this option is used, only the postrm snippet that handles purge action is
written while postinst and prerm snippets are skipped.
Additionally, all instances (if any) of '##bin##' in the runscript are automatically
replaced with the path provided with this option; this is done so that the path of
the bin can be maintained only in the control file.
This option is experimental.

=item I<presubj>

Include presubj file for reportbug into generate package. This file contains
note that suggest including runit maintainers into copy of bug reports, related
to runit integration. If the package already include it's own presubj file, that file
takes precedence over runit presubj file.
This may make life of package maintainer who uses another init system easier.

=item I<defaults>

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

=item I<since>

Since dh-runit 2.8.15 this option is no longer needed and is deprecated.
It's retained only for backward compatibility with packages that still
use it (see #942323). It's scheduled for removal with 3.0 release.

=item I<noreplace>

Since dh-runit 2.9.0 this option is deprecated and it's retained only for backward
compatibility and it's scheduled for removal with 3.0 release.
You can use onupgrade=nostop instead.

=item I<nofinish>

This option is deprecated, does nothing and it's scheduled for removal
with 3.0 release.

=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