]> git.donarmstrong.com Git - debhelper.git/blob - dh_installinit
dh_installinit: rework upstart handling to comply with new policy proposal; packages...
[debhelper.git] / dh_installinit
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_installinit - install upstart jobs and/or init scripts into package build directories
6
7 =cut
8
9 use strict;
10 use Debian::Debhelper::Dh_Lib;
11
12 =head1 SYNOPSIS
13
14 B<dh_installinit> [S<I<debhelper options>>] [B<--name=>I<name>] [B<-n>] [B<-R>] [B<-r>] [B<-d>] [S<B<--> I<params>>]
15
16 =head1 DESCRIPTION
17
18 B<dh_installinit> is a debhelper program that is responsible for installing
19 upstart job files and init scripts with associated defaults files into
20 package build directories.
21
22 It also automatically generates the F<postinst> and F<postrm> and F<prerm> commands
23 needed to set up the symlinks in F</etc/rc*.d/> to start and stop the init
24 scripts.
25
26 =head1 FILES
27
28 =over 4
29
30 =item debian/I<package>.upstart
31
32 If this exists, it is installed into etc/init/I<package>.conf in the package
33 build directory.
34
35 =item debian/I<package>.init
36
37 If this exists, it is installed into etc/init.d/I<package> in the package
38 build directory.
39
40 =item debian/I<package>.default
41
42 If this exists, it is installed into etc/default/I<package> in the package
43 build directory.
44
45 =back
46
47 =head1 OPTIONS
48
49 =over 4
50
51 =item B<-n>, B<--noscripts>
52
53 Do not modify F<postinst>/F<postrm>/F<prerm> scripts.
54
55 =item B<-o>, B<--onlyscripts>
56
57 Only modify F<postinst>/F<postrm>/F<prerm> scripts, do not actually install any init
58 script, default files, or upstart job. May be useful if the init script or
59 upstart job is shipped and/or installed by upstream in a way that doesn't
60 make it easy to let B<dh_installinit> find it.
61
62 =item B<-R>, B<--restart-after-upgrade>
63
64 Do not stop the init script until after the package upgrade has been
65 completed. This is different than the default behavior, which stops the
66 script in the F<prerm>, and starts it again in the F<postinst>.
67
68 This can be useful for daemons that should not have a possibly long
69 downtime during upgrade. But you should make sure that the daemon will not
70 get confused by the package being upgraded while it's running before using
71 this option.
72
73 =item B<-r>, B<--no-restart-on-upgrade>
74
75 Do not stop init script on upgrade.
76
77 =item B<--no-start>
78
79 Do not start the init script on install or upgrade, or stop it on removal.
80 Only call B<update-rc.d>. Useful for rcS scripts.
81
82 =item B<-d>, B<--remove-d>
83
84 Remove trailing B<d> from the name of the package, and use the result for the
85 filename the upstart job file is installed as in F<etc/init/> , and for the
86 filename the init script is installed as in etc/init.d and the default file
87 is installed as in F<etc/default/> . This may be useful for daemons with names
88 ending in B<d>. (Note: this takes precedence over the B<--init-script> parameter
89 described below.)
90
91 =item B<-u>I<params> B<--update-rcd-params=>I<params>
92
93 =item B<--> I<params>
94
95 Pass I<params> to L<update-rc.d(8)>. If not specified, B<defaults> will be
96 passed to L<update-rc.d(8)>.
97
98 =item B<--name=>I<name>
99
100 Install the upstart job file and init script (and default file) using the
101 filename I<name> instead of the default filename, which is the package name.
102 When this parameter is used, B<dh_installinit> looks for and installs files
103 named F<debian/package.name.upstart>, F<debian/package.name.init> and
104 F<debian/package.name.default>, instead of the usual F<debian/package.upstart>,
105 F<debian/package.init> and F<debian/package.default>.
106
107 =item B<--init-script=>I<scriptname>
108
109 Use I<scriptname> as the filename the init script is installed as in
110 F<etc/init.d/> (and also use it as the filename for the defaults file, if it
111 is installed). If you use this parameter, B<dh_installinit> will look to see
112 if a file in the F<debian/> directory exists that looks like
113 F<package.scriptname> and if so will install it as the init script in
114 preference to the files it normally installs.
115
116 This parameter is deprecated, use the B<--name> parameter instead. This
117 parameter is incompatible with the use of upstart jobs.
118
119 =item B<--error-handler=>I<function>
120
121 Call the named shell I<function> if running the init script fails. The
122 function should be provided in the F<prerm> and F<postinst> scripts, before the
123 B<#DEBHELPER#> token.
124
125 =back
126
127 =head1 NOTES
128
129 Note that this command is not idempotent. L<dh_prep(1)> should be called
130 between invocations of this command. Otherwise, it may cause multiple
131 instances of the same text to be added to maintainer scripts.
132
133 =cut
134
135 init(options => {
136         "r" => \$dh{R_FLAG},
137         "no-restart-on-upgrade" => \$dh{R_FLAG},
138         "no-start" => \$dh{NO_START},
139         "R|restart-after-upgrade" => \$dh{RESTART_AFTER_UPGRADE},
140         "init-script=s" => \$dh{INIT_SCRIPT},
141         "update-rcd-params=s", => \$dh{U_PARAMS},
142         "remove-d" => \$dh{D_FLAG},
143 });
144
145 foreach my $package (@{$dh{DOPACKAGES}}) {
146         my $tmp=tmpdir($package);
147
148         # Figure out what filename to install it as.
149         my $script;
150         my $jobfile=$package;
151         if (defined $dh{NAME}) {
152                 $jobfile=$script=$dh{NAME};
153         }
154         elsif ($dh{D_FLAG}) {
155                 # -d on the command line sets D_FLAG. We will 
156                 # remove a trailing 'd' from the package name and 
157                 # use that as the name.
158                 $script=$package;
159                 if ($script=~m/(.*)d$/) {
160                         $jobfile=$script=$1;
161                 }
162                 else {
163                         warning("\"$package\" has no final d' in its name, but -d was specified.");
164                 }
165         }       
166         elsif ($dh{INIT_SCRIPT}) {
167                 $script=$dh{INIT_SCRIPT};
168         }
169         else {
170                 $script=$package;
171         }       
172
173         my $job=pkgfile($package,"upstart");
174
175         if ($job ne '' && ! $dh{ONLYSCRIPTS}) {
176                 if (! -d "$tmp/etc/init") {
177                         doit("install","-d","$tmp/etc/init");
178                 }
179                 
180                 doit("install","-p","-m644",$job,"$tmp/etc/init/$jobfile.conf");
181         }
182
183         my $default=pkgfile($package,'default');
184         if ($default ne '' && ! $dh{ONLYSCRIPTS}) {
185                 if (! -d "$tmp/etc/default") {
186                         doit("install","-d","$tmp/etc/default");
187                 }
188                 doit("install","-p","-m644",$default,"$tmp/etc/default/$script");
189         }
190
191         my $init=pkgfile($package,$script) || pkgfile($package,"init") ||
192                 pkgfile($package,"init.d");
193         if ($init ne '' && ! $dh{ONLYSCRIPTS}) {
194                 if (! -d "$tmp/etc/init.d") {
195                         doit("install","-d","$tmp/etc/init.d");
196                 }
197                 
198                 doit("install","-p","-m755",$init,"$tmp/etc/init.d/$script");
199         }
200
201         if ($dh{INIT_SCRIPT} && $job ne '' && $init ne '') {
202                 error("Can't use --init-script with an upstart job");
203         }
204
205         if ($job ne '' || $init ne '' || $dh{ONLYSCRIPTS}) {
206                 # This is set by the -u "foo" command line switch, it's
207                 # the parameters to pass to update-rc.d. If not set,
208                 # we have to say "defaults".
209                 my $params='';
210                 if (defined($dh{U_PARAMS})) {
211                         $params=join(' ',@{$dh{U_PARAMS}});
212                 }       
213                 if ($params eq '') {
214                         $params="defaults";
215                 }
216                 
217                 if (! $dh{NOSCRIPTS}) {
218                         if (! $dh{NO_START}) {
219                                 if ($dh{RESTART_AFTER_UPGRADE}) {
220                                         # update-rc.d, and restart (or
221                                         # start if new install) script
222                                         autoscript($package,"postinst", "postinst-init-restart",
223                                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
224                                 }
225                                 else {
226                                         # update-rc.d, and start script
227                                         autoscript($package,"postinst", "postinst-init",
228                                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
229                                 }
230                         
231                                 if ($dh{R_FLAG} || $dh{RESTART_AFTER_UPGRADE}) {
232                                         # stops script only on remove
233                                         autoscript($package,"prerm","prerm-init-norestart",
234                                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
235                                 }
236                                 else {
237                                         # always stops script
238                                         autoscript($package,"prerm","prerm-init",
239                                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
240                                 }
241                         }
242                         else {
243                                 # just update-rc.d
244                                 autoscript($package,"postinst", "postinst-init-nostart",
245                                         "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
246                         }
247
248                         # removes rc.d links
249                         autoscript($package,"postrm","postrm-init",
250                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
251                 }
252         }
253 }
254
255 =head1 SEE ALSO
256
257 L<debhelper(7)>
258
259 This program is a part of debhelper.
260
261 =head1 AUTHORS
262
263 Joey Hess <joeyh@debian.org>
264
265 Steve Langasek <steve.langasek@canonical.com>
266
267 =cut