File Coverage

blib/lib/Catalyst/Helper/InitScript/FreeBSD.pm
Criterion Covered Total %
statement 21 63 33.3
branch 0 12 0.0
condition 0 12 0.0
subroutine 7 11 63.6
pod 1 1 100.0
total 29 99 29.2


line stmt bran cond sub pod time code
1             package Catalyst::Helper::InitScript::FreeBSD;
2              
3 1     1   21032 use warnings;
  1         2  
  1         29  
4 1     1   5 use strict;
  1         2  
  1         29  
5              
6 1     1   3277 use English;
  1         8423  
  1         7  
7 1     1   1441 use File::Spec::Functions;
  1         774  
  1         90  
8 1     1   890 use Term::Prompt;
  1         26761  
  1         72  
9 1     1   1409 use Getopt::Long;
  1         20224  
  1         7  
10              
11             our $VERSION = '0.01';
12              
13             =head1 NAME
14              
15             Catalyst::Helper::InitScript::FreeBSD - /usr/local/etc/rc.d/yourapp.sh generator.
16              
17             =head1 SYNOPSIS
18              
19             % ./script/yourapp_create.pl InitScript::FreeBSD -- --help
20             usage: ./script/yourapp_create.pl
21             -? -help display this help and exits.
22             -user The real uid of fastcgi process. [default is USERNAME]
23             -group The real gid of fastcgi process. [default is GROUP]
24             -p -pidfile specify filename for pid file.
25             [default is /var/run/yourapp.pid]
26             -l -listen Socket path to listen on can be HOST:PORT, :PORT or a filesystem path.
27             [default is /var/run/yourapp.sockets]
28             -n -nproc specify number of processes to keep to serve requests.
29             [default is 4]
30             -mysql run after init mysql. [default is no]
31             -postgresql run after init postgresql. [default is no]
32              
33             % ./script/yourapp_create.pl InitScript::FreeBSD -- -nproc 2 -mysql
34             /usr/home/bokutin/svk/YourApp/trunk/script/../yourapp.sh.sample is exist.
35             overwrite? (y or n) [default n] y
36             /usr/home/bokutin/svk/YourApp/trunk/script/../yourapp.sh.sample was created.
37              
38             The following commands were needed to complete setting up.
39             % sudo cp /usr/home/bokutin/svk/YourApp/trunk/script/../yourapp.sh.sample /usr/local/etc/rc.d/yourapp.sh
40             % sudo chmod 755 /usr/local/etc/rc.d/yourapp.sh
41             % sudo touch /var/run/yourapp.pid
42             % sudo touch /var/run/yourapp.sockets
43              
44             =cut
45              
46             =head2 mk_stuff
47              
48             =cut
49              
50             sub mk_stuff {
51 0     0 1   my ( $class, $helper, @args ) = @_;
52              
53             # vars
54 0   0       my $vars = {
      0        
      0        
55             app => lc($helper->{app}) || die,
56             base => $helper->{base},
57             user => getpwuid($UID) || "",
58             group => getgrgid($GID) || "",
59             nproc => 4,
60             use_socket => 1,
61             };
62 0           $vars->{pidfile} = "/var/run/$vars->{app}.pid";
63 0           $vars->{listen} = "/var/run/$vars->{app}.sockets";
64 0           my $output = canonpath(catfile($vars->{base}, "$vars->{app}.sh.sample"));
65              
66             # parse args
67             {
68 1     1   405 no warnings 'uninitialized';
  1         3  
  1         683  
  0            
69 0           my $opts = {};
70 0           local @ARGV = @args;
71 0           my $ret = GetOptions(
72             'help|?' => \$opts->{help},
73             'user=s' => \$opts->{user},
74             'group=s' => \$opts->{group},
75             'pidfile|p=s' => \$opts->{pidfile},
76             'listen|l=s' => \$opts->{listen},
77             'nproc|n=i' => \$opts->{nproc},
78             'mysql' => \$opts->{mysql},
79             'postgresql' => \$opts->{postgresql},
80             );
81 0 0 0       if (!$ret or $opts->{help}) {
82 0           $class->_usage($vars);
83 0           return 0;
84             }
85 0 0         if ($opts->{listen} =~ m/:\d+$/) {
86 0           $opts->{use_socket} = 0;
87             }
88 0           delete $opts->{$_} for (grep { ! length $opts->{$_} } keys %$opts);
  0            
89 0           $vars = { %$vars, %$opts };
90             }
91            
92             # processing
93 0 0 0       if (-f $output and !$class->_ask_overwite($vars, $output)) {
94 0           print "cancelled.\n";
95 0           return 0;
96             }
97             else {
98 0           $class->_render_file($helper, $vars, $output);
99              
100 0           my @msgs;
101 0           push @msgs, "$output was created.";
102 0           push @msgs, "";
103 0           push @msgs, "The following commands were needed to complete setting up.";
104 0           push @msgs, "% sudo cp $output /usr/local/etc/rc.d/$vars->{app}.sh";
105 0           push @msgs, "% sudo chmod 755 /usr/local/etc/rc.d/$vars->{app}.sh";
106 0           push @msgs, "% sudo touch $vars->{pidfile}";
107 0 0         push @msgs, "% sudo touch $vars->{listen}" if $vars->{use_socket};
108              
109 0           print join("\n", @msgs), "\n";
110             }
111              
112 0           return 1;
113             }
114              
115             sub _usage {
116 0     0     my ($class, $vars) = @_;
117              
118 0           print <
119             usage: $0
120             -? -help display this help and exits.
121             -user The real uid of fastcgi process. [default is $vars->{user}]
122             -group The real gid of fastcgi process. [default is $vars->{group}]
123             -p -pidfile specify filename for pid file.
124             [default is $vars->{pidfile}]
125             -l -listen Socket path to listen on can be HOST:PORT, :PORT or a filesystem path.
126             [default is $vars->{listen}]
127             -n -nproc specify number of processes to keep to serve requests.
128             [default is $vars->{nproc}]
129             -mysql run after init mysql. [default is no]
130             -postgresql run after init postgresql. [default is no]
131             USAGE
132             }
133              
134             sub _ask_overwite {
135 0     0     my ($class, $vars, $output) = @_;
136              
137 0           prompt('y', "$output is exist. overwrite?", "", "");
138             }
139              
140             sub _render_file {
141 0     0     my ($class, $helper, $vars, $output) = @_;
142              
143 0           my $file = 'init_script';
144              
145 0           my $t = Template->new;
146 0           my $template = $helper->get_file( __PACKAGE__, $file );
147 0 0         return 0 unless $template;
148 0 0         $t->process( \$template, $vars, $output )
149             || Catalyst::Exception->throw(
150             message => qq/Couldn't process "$file", / . $t->error() );
151              
152 0           return 1;
153             }
154              
155             =head1 AUTHOR
156              
157             Tomohiro Hosaka, C<< >>
158              
159             =head1 COPYRIGHT & LICENSE
160              
161             Copyright 2008 Tomohiro Hosaka, all rights reserved.
162              
163             This program is free software; you can redistribute it and/or modify it
164             under the same terms as Perl itself.
165              
166             =cut
167              
168             1; # End of Catalyst::Helper::InitScript::FreeBSD
169              
170             __DATA__