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