From e4b8e832194949b228db4fd00eab558dfc8b7d27 Mon Sep 17 00:00:00 2001 From: Dmitry Bogatov Date: Tue, 28 May 2019 21:10:02 +0000 Subject: 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. --- Makefile | 5 ++++- data/logscript | 5 +++++ debian/control | 2 ++ debian/dh-runit.install | 1 + dh_runit | 38 +++++++++++++++++++++++++++----------- t/928935.t | 7 +++++-- 6 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 data/logscript diff --git a/Makefile b/Makefile index 2f77795..0a6c500 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ all: check: - PATH=$(CURDIR):$(PATH) DH_AUTOSCRIPTDIR=$(CURDIR) prove -I. + PATH=$(CURDIR):$(PATH) \ + DH_AUTOSCRIPTDIR=$(CURDIR) \ + DH_RUNIT_DATADIR=$(CURDIR)/data \ + prove -I. autopkgtest: prove -I. .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 @@ +#!/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 }}' diff --git a/debian/control b/debian/control index 946f548..9b889e1 100644 --- a/debian/control +++ b/debian/control @@ -7,6 +7,7 @@ Build-Depends: perl, perl-doc, libfile-copy-recursive-perl, + libtext-hogan-perl, Standards-Version: 4.3.0 Vcs-Browser: https://salsa.debian.org/runit-team/dh-runit Vcs-Git: https://salsa.debian.org/runit-team/dh-runit.git @@ -15,6 +16,7 @@ Homepage: https://salsa.debian.org/runit-team/dh-runit Package: dh-runit Architecture: all Depends: debhelper (>= 9), + libtext-hogan-perl, ${misc:Depends}, ${shlibs:Depends} Description: 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 postrm-runit /usr/share/debhelper/autoscripts postinst-runit /usr/share/debhelper/autoscripts runit.pm /usr/share/perl5/Debian/Debhelper/Sequence +data/* /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; use File::Find; use File::Path qw(make_path); use File::stat; +use Text::Hogan::Compiler; +use File::Slurp qw(read_file write_file); use feature 'signatures'; no warnings 'experimental'; +# 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); + + make_path(dirname($dest)); + write_file($dest, $output); + + if (defined $perm) { + chmod $perm, $dest; + } +} + sub parse_options($opts) { my $conf = { enable => 1, since => '0.0-0' }; for my $opt (split(/,/, $opts)) { @@ -98,17 +122,9 @@ PKG: foreach my $pkg (@{$dh{DOPACKAGES}}) { 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); + template_from_data_directory('logscript', "$sv_dir/$name/log/run", + { logdir => $logdir }, 0755); + make_symlink("/etc/sv/$name/log/supervise", "/var/lib/runit/log/supervise/$name", $tmp); 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 @@ #!/usr/bin/perl use strict; use warnings; -use Test::More tests => 2; +use Test::More tests => 4; use T; -system_ok('dh_runit', 'debian/test.runscript', 'name=test,noreplace'); +system_ok('dh_runit', 'debian/test.runscript', 'name=test,noreplace,logscript'); my $noreplace = 'debian/dh-runit-test/usr/share/runit/meta/test/noreplace'; ok(-f $noreplace, 'noreplace file correctly created'); +my $logscript = 'debian/dh-runit-test/etc/sv/test/log/run'; +ok(-f $logscript, 'logscript correctly created'); +ok(-x $logscript, 'logscript is executable'); -- cgit v1.2.3