summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Bogatov <KAction@debian.org>2019-05-28 21:10:02 +0000
committerDmitry Bogatov <KAction@debian.org>2019-05-28 21:30:40 +0000
commite4b8e832194949b228db4fd00eab558dfc8b7d27 (patch)
treea3a3dccdf1ba568686411f769857758372c33165
parent8eddb040f95b7e6a2ebb4700c4518e76b7c5b294 (diff)
Refactor creation of logscript
Instead of using interpolation and here-strings, write function that renders mustache template. This approach makes code cleaner and more scalable. * data/logscript: add mustach template of 'log/run' script, that is installed when "logscript" option in effect. * Makefile: set DH_RUNIT_DATADIR variable in "check" target. This way, templates from ./data/ directory are used, not from system location. * debian/control: add dependency on Text::Hogan library -- implementation of "mustache" templating standard. * dh_runit(template_from_data_directory): new function * dh_runit: install 'log/run' with "template_from_data_directory" function. * t/928935.t: check that "logscript" option correctly creates 'log/run' script, with correct permissions.
-rw-r--r--Makefile5
-rw-r--r--data/logscript5
-rw-r--r--debian/control2
-rw-r--r--debian/dh-runit.install1
-rwxr-xr-xdh_runit38
-rw-r--r--t/928935.t7
6 files changed, 44 insertions, 14 deletions
diff --git a/Makefile b/Makefile
index 2f77795..0a6c500 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,9 @@
1all: 1all:
2check: 2check:
3 PATH=$(CURDIR):$(PATH) DH_AUTOSCRIPTDIR=$(CURDIR) prove -I. 3 PATH=$(CURDIR):$(PATH) \
4 DH_AUTOSCRIPTDIR=$(CURDIR) \
5 DH_RUNIT_DATADIR=$(CURDIR)/data \
6 prove -I.
4autopkgtest: 7autopkgtest:
5 prove -I. 8 prove -I.
6.PHONY: check 9.PHONY: check
diff --git a/data/logscript b/data/logscript
new file mode 100644
index 0000000..1ebfb77
--- /dev/null
+++ b/data/logscript
@@ -0,0 +1,5 @@
1#!/bin/sh
2chown -R runit-log:adm '{{ logdir }}'
3chmod 750 '{{ logdir }}'
4chmod u+rw,g+r,o-rwx '{{ logdir }}'/*
5exec chpst -u runit-log svlogd -tt '{{ logdir }}'
diff --git a/debian/control b/debian/control
index 946f548..9b889e1 100644
--- a/debian/control
+++ b/debian/control
@@ -7,6 +7,7 @@ Build-Depends:
7 perl, 7 perl,
8 perl-doc, 8 perl-doc,
9 libfile-copy-recursive-perl, 9 libfile-copy-recursive-perl,
10 libtext-hogan-perl,
10Standards-Version: 4.3.0 11Standards-Version: 4.3.0
11Vcs-Browser: https://salsa.debian.org/runit-team/dh-runit 12Vcs-Browser: https://salsa.debian.org/runit-team/dh-runit
12Vcs-Git: https://salsa.debian.org/runit-team/dh-runit.git 13Vcs-Git: https://salsa.debian.org/runit-team/dh-runit.git
@@ -15,6 +16,7 @@ Homepage: https://salsa.debian.org/runit-team/dh-runit
15Package: dh-runit 16Package: dh-runit
16Architecture: all 17Architecture: all
17Depends: debhelper (>= 9), 18Depends: debhelper (>= 9),
19 libtext-hogan-perl,
18 ${misc:Depends}, 20 ${misc:Depends},
19 ${shlibs:Depends} 21 ${shlibs:Depends}
20Description: debhelper add-on to handle runit runscripts 22Description: debhelper add-on to handle runit runscripts
diff --git a/debian/dh-runit.install b/debian/dh-runit.install
index e3f2d9d..a08eda2 100644
--- a/debian/dh-runit.install
+++ b/debian/dh-runit.install
@@ -2,3 +2,4 @@ dh_runit /usr/bin
2postrm-runit /usr/share/debhelper/autoscripts 2postrm-runit /usr/share/debhelper/autoscripts
3postinst-runit /usr/share/debhelper/autoscripts 3postinst-runit /usr/share/debhelper/autoscripts
4runit.pm /usr/share/perl5/Debian/Debhelper/Sequence 4runit.pm /usr/share/perl5/Debian/Debhelper/Sequence
5data/* /usr/share/dh-runit/data/
diff --git a/dh_runit b/dh_runit
index 3f45866..9147607 100755
--- a/dh_runit
+++ b/dh_runit
@@ -7,9 +7,33 @@ use Debian::Debhelper::Dh_Lib;
7use File::Find; 7use File::Find;
8use File::Path qw(make_path); 8use File::Path qw(make_path);
9use File::stat; 9use File::stat;
10use Text::Hogan::Compiler;
11use File::Slurp qw(read_file write_file);
10use feature 'signatures'; 12use feature 'signatures';
11no warnings 'experimental'; 13no warnings 'experimental';
12 14
15# Render mustache template {name} from data directory into {dest},
16# substituting {values} hash. If {perm} argument is provided, it is used
17# set permissions of {dest}. Intermediate directories to ${dest} are
18# created as needed.
19#
20# Data directory is specified by {DH_RUNIT_DATADIR} environment
21# variable, and defaults to /usr/share/dh-runit/data.
22sub template_from_data_directory {
23 my ($name, $dest, $values, $perm) = @_;
24 my $datadir = $ENV{DH_RUNIT_DATADIR} || "/usr/share/dh-runit/data";
25 my $template = read_file("${datadir}/${name}");
26 my $compiler = Text::Hogan::Compiler->new;
27 my $output = $compiler->compile($template)->render($values);
28
29 make_path(dirname($dest));
30 write_file($dest, $output);
31
32 if (defined $perm) {
33 chmod $perm, $dest;
34 }
35}
36
13sub parse_options($opts) { 37sub parse_options($opts) {
14 my $conf = { enable => 1, since => '0.0-0' }; 38 my $conf = { enable => 1, since => '0.0-0' };
15 for my $opt (split(/,/, $opts)) { 39 for my $opt (split(/,/, $opts)) {
@@ -98,17 +122,9 @@ PKG: foreach my $pkg (@{$dh{DOPACKAGES}}) {
98 install_dir("$sv_dir/$name/log"); 122 install_dir("$sv_dir/$name/log");
99 install_dir($tmp . $logdir); 123 install_dir($tmp . $logdir);
100 124
101 my $run_log = "$sv_dir/$name/log/run"; 125 template_from_data_directory('logscript', "$sv_dir/$name/log/run",
102 open(RUN_LOG, ">$run_log") || die $!; 126 { logdir => $logdir }, 0755);
103 print RUN_LOG << "HERE"; 127
104#!/bin/sh
105chown -R runit-log:adm '$logdir'
106chmod 750 '$logdir'
107chmod u+rw,g+r,o-rwx '$logdir'/*
108exec chpst -u runit-log svlogd -tt '$logdir'
109HERE
110 close(RUN_LOG);
111 chmod(0755, $run_log);
112 make_symlink("/etc/sv/$name/log/supervise", 128 make_symlink("/etc/sv/$name/log/supervise",
113 "/var/lib/runit/log/supervise/$name", $tmp); 129 "/var/lib/runit/log/supervise/$name", $tmp);
114 install_dir("$tmp/var/lib/runit/log/supervise/$name"); 130 install_dir("$tmp/var/lib/runit/log/supervise/$name");
diff --git a/t/928935.t b/t/928935.t
index 52774ed..5d2fea1 100644
--- a/t/928935.t
+++ b/t/928935.t
@@ -1,10 +1,13 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use strict; 2use strict;
3use warnings; 3use warnings;
4use Test::More tests => 2; 4use Test::More tests => 4;
5use T; 5use T;
6 6
7system_ok('dh_runit', 'debian/test.runscript', 'name=test,noreplace'); 7system_ok('dh_runit', 'debian/test.runscript', 'name=test,noreplace,logscript');
8 8
9my $noreplace = 'debian/dh-runit-test/usr/share/runit/meta/test/noreplace'; 9my $noreplace = 'debian/dh-runit-test/usr/share/runit/meta/test/noreplace';
10ok(-f $noreplace, 'noreplace file correctly created'); 10ok(-f $noreplace, 'noreplace file correctly created');
11my $logscript = 'debian/dh-runit-test/etc/sv/test/log/run';
12ok(-f $logscript, 'logscript correctly created');
13ok(-x $logscript, 'logscript is executable');