diff options
author | Dmitry Bogatov <KAction@debian.org> | 2019-05-28 21:10:02 +0000 |
---|---|---|
committer | Dmitry Bogatov <KAction@debian.org> | 2019-05-28 21:30:40 +0000 |
commit | e4b8e832194949b228db4fd00eab558dfc8b7d27 (patch) | |
tree | a3a3dccdf1ba568686411f769857758372c33165 /dh_runit | |
parent | 8eddb040f95b7e6a2ebb4700c4518e76b7c5b294 (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.
Diffstat (limited to 'dh_runit')
-rwxr-xr-x | dh_runit | 38 |
1 files changed, 27 insertions, 11 deletions
@@ -7,9 +7,33 @@ use Debian::Debhelper::Dh_Lib; | |||
7 | use File::Find; | 7 | use File::Find; |
8 | use File::Path qw(make_path); | 8 | use File::Path qw(make_path); |
9 | use File::stat; | 9 | use File::stat; |
10 | use Text::Hogan::Compiler; | ||
11 | use File::Slurp qw(read_file write_file); | ||
10 | use feature 'signatures'; | 12 | use feature 'signatures'; |
11 | no warnings 'experimental'; | 13 | no 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. | ||
22 | sub 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 | |||
13 | sub parse_options($opts) { | 37 | sub 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 | ||
105 | chown -R runit-log:adm '$logdir' | ||
106 | chmod 750 '$logdir' | ||
107 | chmod u+rw,g+r,o-rwx '$logdir'/* | ||
108 | exec chpst -u runit-log svlogd -tt '$logdir' | ||
109 | HERE | ||
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"); |