| 1 | package Mojo::Command::Generate::InitScript::Debian; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use File::Spec; | ||
| 6 | |||
| 7 | #use base 'Mojo::Command::Generate::InitScript::Base'; | ||
| 8 | use base 'Mojo::Command'; | ||
| 9 | use Getopt::Long 'GetOptions'; | ||
| 10 | use File::Spec; | ||
| 11 | use IO::File; | ||
| 12 | use List::Util qw(first); | ||
| 13 | |||
| 14 | __PACKAGE__->attr(usage => <<"EOF"); | ||
| 15 | Debian initscript related options: | ||
| 16 | --should-start <applist> defines facilities which, if present, | ||
| 17 | should be available during startup of this service | ||
| 18 | --should-stop <applist> facilities which should be available | ||
| 19 | during shutdown of this service. | ||
| 20 | --runlevels <runlevels> which run levels should by default run the init script | ||
| 21 | with a start (stop) argument to start (stop) | ||
| 22 | (Default: 2 3 4 5) | ||
| 23 | EOF | ||
| 24 | |||
| 25 | sub run | ||
| 26 | { | ||
| 27 | my ( $self, $opt ) = @_; | ||
| 28 | |||
| 29 | $opt->{'should-start'} = []; | ||
| 30 | $opt->{'should-stop'} = []; | ||
| 31 | $opt->{'runlevels'} = [2, 3, 4, 5]; | ||
| 32 | $opt->{'stoplevels'} = []; | ||
| 33 | |||
| 34 | GetOptions($opt, | ||
| 35 | 'should-start=s{,}', 'should-stop=s{,}', 'runlevels=i{,}', | ||
| 36 | ); | ||
| 37 | |||
| 38 | for my $i (0..6) | ||
| 39 | { | ||
| 40 | if ( !first { $i == $_ } @{ $opt->{'runlevels'} } ) | ||
| 41 | { | ||
| 42 | push @{ $opt->{'stoplevels'} }, $i; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | # init script | ||
| 47 | my $file = $opt->{'deploy'} | ||
| 48 | ? '/etc/init.d/'. $opt->{'name'} | ||
| 49 | : File::Spec->join($opt->{'output'}, $opt->{'name'}); | ||
| 50 | $self->render_to_file( 'initscript', $file, $opt ); | ||
| 51 | $self->chmod_file( $file, 0755 ); | ||
| 52 | |||
| 53 | # config file | ||
| 54 | $file = $opt->{'deploy'} | ||
| 55 | ? '/etc/default/'. $opt->{'name'} | ||
| 56 | : File::Spec->join($opt->{'output'}, 'etc_default_'.$opt->{'name'}); | ||
| 57 | $self->render_to_file( 'config', $file, $opt ); | ||
| 58 | $self->chmod_file( $file, 0644 ); | ||
| 59 | |||
| 60 | if ( $opt->{'deploy'} ) | ||
| 61 | { | ||
| 62 | system('/usr/sbin/update-rc.d', $opt->{'name'}, | ||
| 63 | 'start', '20', (@{ $opt->{'runlevels'} }), '.', | ||
| 64 | 'stop', '20', (@{ $opt->{'stoplevels'} }), '.' | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | 1; | ||
| 69 | __DATA__ |