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. --- dh_runit | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'dh_runit') 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"); -- cgit v1.2.3