File: dh_nginx

package info (click to toggle)
nginx 1.18.0-6.1%2Bdeb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,344 kB
  • sloc: ansic: 250,653; perl: 7,548; sh: 1,408; ruby: 879; python: 358; makefile: 338; awk: 36; cpp: 18
file content (280 lines) | stat: -rwxr-xr-x 7,079 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#! /usr/bin/perl

# dh_nginx - Nginx configuration helper
# Copyright (C) 2016 Christos Trochalakis <ctrochalakis@debian.org>
#
# This program is licensed under the terms of the GNU General
# Public License veserion 2+
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

use strict;
use File::Find;
use Debian::Debhelper::Dh_Lib;


=head1 NAME

dh_nginx - register configuration snippets to the nginx web server

=cut

sub nginx_depends
{
	return 'nginx-common (= ${source:Version})'
}

sub nginx_api_installdir
{
	return "/usr/lib/nginx/modules/";
}

sub nginx_modules_conf_installdir
{
	return "usr/share/nginx/modules-available/"
}

=head1 SYNOPSIS

B<dh_nginx> [S<I<debhelper options>>] [B<-n>|B<--noscripts>]

=head1 DESCRIPTION

B<dh_nginx> is a debhelper program that is responsible for correctly installing
Nginx configuration snippets and setting postinst, prerm and dependencies in
Nginx web server modules and web applications.

It supports the following configuration types

=over 4

=item *
Nginx modules

=head1 INVOCATION

  %:
     dh $@ --with nginx

=head1 FILES

=over 4

=item debian/I<package>.nginx

=item debian/nginx

=back

Lists files to be registered with the Nginx HTTP server. The file is
interpreted as line separated list of installation stanzas, where each entry
consists of whitespace separated values conforming to the file semantics below.

=head2 FILE SEMANTICS

Each line consists of a triple

I<type> I<file> [I<arguments>]

where the values are interpreted as follows:


=head3 I<type>

Denotes the type of file to be installed. Recognized values are B<mod> for
Nginx modules.

=head3 I<file>

Is interpreted as existing file name within the source package. No path
expansion is effectuated. Just like L<dh_install(1)>, B<dh_nginx> can not
rename files.

=head3 I<arguments>

Is inrerpreted as optional arguments if any, currently not used.

=head2 MODULES

Modules are handled specially and are determined by the B<mod> type. Modules must
have a I<.conf> suffix. In that case the file is interpreted as module load
file and is installed to I</etc/nginx/modules-available>. If the file is ending
with a I<.so> suffix it is interpreted as actual module shared object and is
installed to the Nginx module directory, an optional numeric priority can be
set as the last argument to handle module dependencies.

=head1 OPTIONS

=over 4

=item B<-e>, B<--noenable>

Install maintainer scripts accordingly, but do not enable the scripts or
configuration by default.

=item B<-n>, B<--noscripts>

Do not modify F<postinst>/F<postrm>/F<prerm> maintainer scripts.


=back

=head1 NOTES

Note that this command is not idempotent. L<dh_prep(1)> should be called
between invocations of this command. Otherwise, it may cause multiple
instances of the same text to be added to maintainer scripts.

=head1 AUTHOR

This manual and L<dh_nginx> was written by Christos Trochalakis.
dh_nginx is heavily influnced by dh_apache2 written by Arno Toell
<debian@toell.net>.

=cut


##
## main code starts here
##

init(options => {
	"e|noenable" => \$dh{NOENABLE},
});

foreach my $package ((@{$dh{DOPACKAGES}}))
{
	my %PACKAGE_TYPE = (
		has_a_module => [],
	);

	my $file = pkgfile($package, "nginx");
	my $tmp  = tmpdir($package);

	my @files_to_register = filedoublearray($file, ".") if $file;
	foreach my $line (@files_to_register)
	{
		my $type = lc(shift @{$line}) if $line->[0];
		my $source = shift @{$line} if $line->[0];
		my @arguments = @{$line};
		my $destination;

		$type = "modules" if $type eq "mod";
		my $installdir = $tmp . "/" . nginx_modules_conf_installdir();

		verbose_print("$type -- $source -- @arguments\n\n");

		if ($type eq "modules")
		{
			my $basesource = basename($source);

			if ($type eq "modules")
			{
				if ($basesource =~ m/\.conf$/)
				{
					my $enablename = $basesource;
					my $prio = $#arguments >= 0 ? $arguments[0] : 50;
					$destination = "$prio-$basesource";
					push @{$PACKAGE_TYPE{'has_a_module'}}, "$enablename:$destination";
					verbose_print("Installing module configuration $enablename into $installdir prio:$prio\n");
				}
				elsif ($basesource =~ m/\.so$/)
			{
					my $modinstalldir = $tmp . "/" . nginx_api_installdir();
					verbose_print("Installing module binary $source into $modinstalldir\n");
					if (! -d $modinstalldir)
					{
						complex_doit("mkdir","-p", $modinstalldir);
						complex_doit("chmod","755","$modinstalldir");
					}
					complex_doit("cp", $source, $modinstalldir);
					next;
				}

				# TODO
				error("module: \"$basesource\" needs .conf, .so or suffix") if $basesource !~ m/\.(conf|so)/;
			}

			if (! -d $installdir)
			{
				complex_doit("mkdir","-p",$installdir);
				complex_doit("chmod","755","$installdir");
			}
			complex_doit("cp",$source,$installdir);
			complex_doit("chmod","644","$installdir/$basesource");

		}
		else
		{
			error("Unknown parameter: $type\n");
		}

	}

        my @postinst_autoscripts;

        if ($#{$PACKAGE_TYPE{'has_a_module'}} >= 0)
        {
                if ($package !~ m/libnginx-mod-\w+?/)
                {
                        warning("Package $package appears to be an Nginx module. It should comply to the package naming scheme libnginx-mod-<modulename>\n");
                }
                addsubstvar($package, "misc:Depends", nginx_depends());

                my $modules = "";
                foreach my $module (@{$PACKAGE_TYPE{'has_a_module'}})
                {
                        $modules .= "$module ";
                }

                push @postinst_autoscripts, ["module", $modules];
        }

	if (! $dh{NOSCRIPTS})
	{
		foreach my $ref (@postinst_autoscripts)
		{
			for my $script_type (qw/postinst prerm postrm/)
			{
				if ($script_type eq "postinst" &&  $dh{NOENABLE})
				{
					next
				}

				my %replacements = (
					NAMES  => $ref->[1],
				);

				my $sed_command = "";
				foreach my $key (sort keys %replacements)
				{
					my $val = $replacements{$key};
					# Use a control char as separator for sed, to
					# reduce escaping issues. Everything else is
					# passed verbatim, i.e. it must not contain any
					# shell or sed special characters.
					my $sep = "\x17";
					$sed_command .= "s" . $sep . "#$key#" .
							      $sep . $val .
							      $sep . "g; ";
				}

				autoscript($package, "$script_type", "$script_type-nginx", $sed_command);
			}
		}
	}
}

# vim: syntax=perl sw=8 sts=8 sr noet