summaryrefslogtreecommitdiff
path: root/dh_smf
blob: b67d0421dd8789855735237671b72ce32d2c245d (plain)
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
#!/usr/bin/perl -w

=head1 NAME

dh_smf - install SMF services into package build directories

=cut

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

=head1 SYNOPSIS

B<dh_smf> [S<I<debhelper options>>] [B<-n>] [B<-o>]

=head1 DESCRIPTION

B<dh_smf> is a debhelper program that is responsible for installing
SMF manifests and scripts.

It also automatically generates the F<postinst> and F<postrm> and F<prerm>
commands needed to manage SMF services.

=head1 FILES

=over 4

=item debian/I<package>.smf/

If this directory exists, it is installed as lib/svc/ in the package
build directory.

=back

=head1 OPTIONS

=over 4

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

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

=item B<-o>, B<--onlyscripts>

Only modify F<postinst>/F<postrm>/F<prerm> scripts, do not actually install
any files. May be useful if the file is shipped and/or installed by upstream
in a way that doesn't make it easy to let B<dh_smf> find it.

=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.

=cut

# Same as pkgfile in /usr/share/perl5/Debian/Debhelper/Dh_Lib.pm,
# but -f => -d
sub pkgdir {
    my $package  = shift;
    my $filename = shift;

    if ( defined $dh{NAME} ) {
        $filename = "$dh{NAME}.$filename";
    }

    # First, check for files ending in buildarch and buildos.
    my $match;
    foreach my $file ( glob("debian/$package.$filename.*") ) {
        next if !-d $file;
        next if $dh{IGNORE} && exists $dh{IGNORE}->{$file};
        if ( $file eq "debian/$package.$filename." . buildarch() ) {
            $match = $file;

            # buildarch files are used in preference to buildos files.
            last;
        }
        elsif ( $file eq "debian/$package.$filename." . buildos() ) {
            $match = $file;
        }
    }
    return $match if defined $match;

    my @try = ("debian/$package.$filename");
    if ( $package eq $dh{MAINPACKAGE} ) {
        push @try, "debian/$filename";
    }

    foreach my $file (@try) {
        if ( -d $file
            && ( !$dh{IGNORE} || !exists $dh{IGNORE}->{$file} ) )
        {
            return $file;
        }

    }

    return "";
}

sub mkservices {
    my ( $svc, $attrs ) = @_;
    my @services = ();
    if ( exists $attrs->{'create_default_instance'} ) {
        push @services,
          {
            'name'        => "svc:/$svc:default",
            'enabled'     => $attrs->{'create_default_instance'}->{'enabled'},
            'exec_method' => $attrs->{'exec_method'},
            'duration' =>
              $attrs->{'property_group'}->{'startd'}->{'propval'}->{'duration'}
              ->{'value'} // 'contract',
            'upgrade' =>
              $attrs->{'property_group'}->{'package'}->{'propval'}->{'upgrade'}
              ->{'value'} // 'stop',
          };
    }
    if ( exists $attrs->{'instance'} ) {
        while ( my ( $k, $v ) = each %{ $attrs->{'instance'} } ) {
            push @services,
              {
                'name'        => "svc:/$svc:$k",
                'enabled'     => $v->{'enabled'},
                'exec_method' => $v->{'exec_method'} // $attrs->{'exec_method'},
                'duration' =>
                  $v->{'property_group'}->{'startd'}->{'propval'}->{'duration'}
                  ->{'value'}
                  // $attrs->{'property_group'}->{'startd'}->{'propval'}
                  ->{'duration'}->{'value'} // 'contract',
                'upgrade' =>
                  $v->{'property_group'}->{'package'}->{'propval'}->{'upgrade'}
                  ->{'value'} // 'stop',
              };
        }
    }
    return @services;
}

init( options => {} );

foreach my $package ( @{ $dh{DOPACKAGES} } ) {
    my $tmp = tmpdir($package);

    # Copy manifests and methods into package directory
    my $smf = pkgdir( $package, 'smf' );
    if ( $smf ne '' && !$dh{'ONLYSCRIPTS'} ) {
        my $svcdir = "$tmp/lib/svc";
        if ( !-d "$svcdir" ) {
            doit( 'install', '-d', "$svcdir" );
        }
        my $cp_v = $dh{'VERBOSE'} ? '-v' : '';
        complex_doit("cp -a $cp_v -f $smf/* $svcdir/");
    }

    # Get a list of all manifests in package
    my @manifests = ();
    if ( -d "$tmp/lib/svc/manifest" ) {
        find(
            {
                wanted => sub {
                    -f $_ && /^.*\.xml\z/s && push @manifests,
                      $File::Find::name;
                },
                no_chdir => 1,
            },
            "$tmp/lib/svc/manifest"
        );
    }

    next unless @manifests;

    # TODO: validate XML?

    # Read all services from all manifests:
    my @services = ();
    foreach my $manifest (@manifests) {
        my $xml = XMLin(
            $manifest,
            ForceArray => [
                qw/service instance dependency exec_method propval property_group/
            ],
        );
        while ( my ( $svc, $attrs ) = each %{ $xml->{'service'} } ) {
            push @services, mkservices( $svc, $attrs )
              unless $svc =~ /milestone\//;
        }
    }

    # Ignore dummy services.
    @services =
      grep { $_->{'exec_method'}->{'start'}->{'exec'} ne ':true' } @services;

    error("No SMF services found in @manifests") unless @services;

    if ( !$dh{NOSCRIPTS} ) {
        my @pkg_manifests = map { s!\Q$tmp\E/*!/!; $_ } @manifests;
        my @svcs = ();
        autoscript( $package, 'postinst', 'postinst-smf-import',
            "s|#MANIFESTS#|@pkg_manifests|" );

        my @daemons = grep { $_->{'duration'} ne 'transient' } @services;
        my @daemons_stop    = grep { $_->{'upgrade'} eq 'stop' } @daemons;
        my @daemons_restart = grep { $_->{'upgrade'} eq 'restart' } @daemons;
        my @daemons_refresh = grep { $_->{'upgrade'} eq 'refresh' } @daemons;
        if (@daemons_stop) {
            @svcs = map { $_->{'name'} } @daemons_stop;
            autoscript( $package, 'prerm', 'prerm-smf-stop',
                "s|#SERVICES#|@svcs|" );
            autoscript( $package, 'postinst', 'postinst-smf-start',
                "s|#SERVICES#|@svcs|" );
        }
        if (@daemons_restart) {
            @svcs = map { $_->{'name'} } @daemons_restart;
            autoscript( $package, 'postinst', 'postinst-smf-start',
                "s|#SERVICES#|@svcs|" );
        }
        if (@daemons_refresh) {
            @svcs = map { $_->{'name'} } @daemons_refresh;
            autoscript( $package, 'postinst', 'postinst-smf-refresh',
                "s|#SERVICES#|@svcs|" );
        }

        # Disable and delete services on removal.
        # Other services (upgrade=stop) are already disabled.
        my @services_nonstop = grep { $_->{'upgrade'} ne 'stop' } @services;
        if (@services_nonstop) {
            @svcs = map { $_->{'name'} } @services_nonstop;
            autoscript( $package, 'prerm', 'prerm-smf-stop-before-remove',
                "s|#SERVICES#|@svcs|" );
        }

        # Remove all services and file hashes:
        @svcs = map { $_->{'name'} } @services;
        autoscript( $package, 'postrm', 'postrm-smf-delete',
            "s|#SERVICES#|@svcs|;s|#MANIFESTS#|@manifests|" );
    }
}

=head1 SEE ALSO

L<debhelper(7)>, L<dh_installinit(1)>, L<smf(5)>, L<svccfg(1M)>

This program is a part of debhelper.

=head1 AUTHORS

Joey Hess <joeyh@debian.org> (dh_installinit)

Igor Pashev <pashev.igor@gmail.com>

=cut