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 | |
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.
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | data/logscript | 5 | ||||
-rw-r--r-- | debian/control | 2 | ||||
-rw-r--r-- | debian/dh-runit.install | 1 | ||||
-rwxr-xr-x | dh_runit | 38 | ||||
-rw-r--r-- | t/928935.t | 7 |
6 files changed, 44 insertions, 14 deletions
@@ -1,6 +1,9 @@ | |||
1 | all: | 1 | all: |
2 | check: | 2 | check: |
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. | ||
4 | autopkgtest: | 7 | autopkgtest: |
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 | ||
2 | chown -R runit-log:adm '{{ logdir }}' | ||
3 | chmod 750 '{{ logdir }}' | ||
4 | chmod u+rw,g+r,o-rwx '{{ logdir }}'/* | ||
5 | 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: | |||
7 | perl, | 7 | perl, |
8 | perl-doc, | 8 | perl-doc, |
9 | libfile-copy-recursive-perl, | 9 | libfile-copy-recursive-perl, |
10 | libtext-hogan-perl, | ||
10 | Standards-Version: 4.3.0 | 11 | Standards-Version: 4.3.0 |
11 | Vcs-Browser: https://salsa.debian.org/runit-team/dh-runit | 12 | Vcs-Browser: https://salsa.debian.org/runit-team/dh-runit |
12 | Vcs-Git: https://salsa.debian.org/runit-team/dh-runit.git | 13 | Vcs-Git: https://salsa.debian.org/runit-team/dh-runit.git |
@@ -15,6 +16,7 @@ Homepage: https://salsa.debian.org/runit-team/dh-runit | |||
15 | Package: dh-runit | 16 | Package: dh-runit |
16 | Architecture: all | 17 | Architecture: all |
17 | Depends: debhelper (>= 9), | 18 | Depends: debhelper (>= 9), |
19 | libtext-hogan-perl, | ||
18 | ${misc:Depends}, | 20 | ${misc:Depends}, |
19 | ${shlibs:Depends} | 21 | ${shlibs:Depends} |
20 | Description: debhelper add-on to handle runit runscripts | 22 | 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 | |||
2 | postrm-runit /usr/share/debhelper/autoscripts | 2 | postrm-runit /usr/share/debhelper/autoscripts |
3 | postinst-runit /usr/share/debhelper/autoscripts | 3 | postinst-runit /usr/share/debhelper/autoscripts |
4 | runit.pm /usr/share/perl5/Debian/Debhelper/Sequence | 4 | runit.pm /usr/share/perl5/Debian/Debhelper/Sequence |
5 | data/* /usr/share/dh-runit/data/ | ||
@@ -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"); |
@@ -1,10 +1,13 @@ | |||
1 | #!/usr/bin/perl | 1 | #!/usr/bin/perl |
2 | use strict; | 2 | use strict; |
3 | use warnings; | 3 | use warnings; |
4 | use Test::More tests => 2; | 4 | use Test::More tests => 4; |
5 | use T; | 5 | use T; |
6 | 6 | ||
7 | system_ok('dh_runit', 'debian/test.runscript', 'name=test,noreplace'); | 7 | system_ok('dh_runit', 'debian/test.runscript', 'name=test,noreplace,logscript'); |
8 | 8 | ||
9 | my $noreplace = 'debian/dh-runit-test/usr/share/runit/meta/test/noreplace'; | 9 | my $noreplace = 'debian/dh-runit-test/usr/share/runit/meta/test/noreplace'; |
10 | ok(-f $noreplace, 'noreplace file correctly created'); | 10 | ok(-f $noreplace, 'noreplace file correctly created'); |
11 | my $logscript = 'debian/dh-runit-test/etc/sv/test/log/run'; | ||
12 | ok(-f $logscript, 'logscript correctly created'); | ||
13 | ok(-x $logscript, 'logscript is executable'); | ||