| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Haineko::CLI; |
|
2
|
5
|
|
|
5
|
|
6979
|
use feature ':5.10'; |
|
|
5
|
|
|
|
|
15
|
|
|
|
5
|
|
|
|
|
1202
|
|
|
3
|
5
|
|
|
5
|
|
34
|
use strict; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
193
|
|
|
4
|
5
|
|
|
5
|
|
28
|
use warnings; |
|
|
5
|
|
|
|
|
8
|
|
|
|
5
|
|
|
|
|
200
|
|
|
5
|
5
|
|
|
5
|
|
5895
|
use IO::File; |
|
|
5
|
|
|
|
|
66570
|
|
|
|
5
|
|
|
|
|
716
|
|
|
6
|
5
|
|
|
5
|
|
55
|
use Fcntl qw(:flock); |
|
|
5
|
|
|
|
|
12
|
|
|
|
5
|
|
|
|
|
767
|
|
|
7
|
5
|
|
|
5
|
|
6709
|
use Sys::Syslog; |
|
|
5
|
|
|
|
|
149619
|
|
|
|
5
|
|
|
|
|
607
|
|
|
8
|
5
|
|
|
5
|
|
5776
|
use Time::Piece; |
|
|
5
|
|
|
|
|
96376
|
|
|
|
5
|
|
|
|
|
40
|
|
|
9
|
5
|
|
|
5
|
|
5588
|
use Class::Accessor::Lite; |
|
|
5
|
|
|
|
|
6017
|
|
|
|
5
|
|
|
|
|
35
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $rwaccessors = [ |
|
12
|
|
|
|
|
|
|
'runfile', # (String) plackup commands in production environment |
|
13
|
|
|
|
|
|
|
'logging', # (Ref->Hash) syslog configuration |
|
14
|
|
|
|
|
|
|
'verbose', # (Integer) Verbose level |
|
15
|
|
|
|
|
|
|
'runmode', # (Integer) Run mode of the command |
|
16
|
|
|
|
|
|
|
'params', # (Ref->Hash) Parameters for each command |
|
17
|
|
|
|
|
|
|
]; |
|
18
|
|
|
|
|
|
|
my $roaccessors = [ |
|
19
|
|
|
|
|
|
|
'started', # (Time::Piece) Command started at |
|
20
|
|
|
|
|
|
|
'pidfile', # (String) process id file |
|
21
|
|
|
|
|
|
|
'command', # (String) Command line |
|
22
|
|
|
|
|
|
|
'stream', # (Ref->Hash) STDIN, STDOUT, STDERR |
|
23
|
|
|
|
|
|
|
]; |
|
24
|
|
|
|
|
|
|
my $woaccessors = []; |
|
25
|
|
|
|
|
|
|
Class::Accessor::Lite->mk_accessors( @$rwaccessors ); |
|
26
|
|
|
|
|
|
|
Class::Accessor::Lite->mk_ro_accessors( @$roaccessors ); |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub new { |
|
29
|
5
|
|
|
5
|
1
|
89
|
my $class = shift; |
|
30
|
5
|
|
|
|
|
20
|
my $argvs = { @_ }; |
|
31
|
5
|
|
|
|
|
10
|
my $param = {}; |
|
32
|
5
|
|
|
|
|
12
|
my $thing = undef; |
|
33
|
|
|
|
|
|
|
|
|
34
|
5
|
100
|
|
|
|
22
|
return $class if ref $class eq __PACKAGE__; |
|
35
|
|
|
|
|
|
|
|
|
36
|
4
|
50
|
50
|
|
|
57
|
$param = { |
|
|
|
50
|
100
|
|
|
|
|
|
|
|
50
|
50
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
37
|
|
|
|
|
|
|
'started' => Time::Piece->new, |
|
38
|
|
|
|
|
|
|
'runfile' => $argvs->{'runfile'} || q(), |
|
39
|
|
|
|
|
|
|
'pidfile' => $argvs->{'pidfile'} || q(), |
|
40
|
|
|
|
|
|
|
'verbose' => $argvs->{'verbose'} || 0, |
|
41
|
|
|
|
|
|
|
'command' => $argvs->{'command'} || [ caller ]->[1], |
|
42
|
|
|
|
|
|
|
'runmode' => $argvs->{'runmode'} || 1, |
|
43
|
|
|
|
|
|
|
'logging' => $argvs->{'logging'} || { 'disabled' => 1, 'facility' => 'local2', 'file' => '' }, |
|
44
|
|
|
|
|
|
|
'stream' => { |
|
45
|
|
|
|
|
|
|
'stdin' => -t STDIN ? 1 : 0, |
|
46
|
|
|
|
|
|
|
'stdout' => -t STDOUT ? 1 : 0, |
|
47
|
|
|
|
|
|
|
'stderr' => -t STDERR ? 1 : 0, |
|
48
|
|
|
|
|
|
|
}, |
|
49
|
|
|
|
|
|
|
}; |
|
50
|
4
|
|
|
|
|
719
|
return bless( $param, $class ); |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub version { |
|
54
|
1
|
|
|
1
|
1
|
3
|
my $class = shift; |
|
55
|
5
|
|
|
5
|
|
3565
|
use Haineko; |
|
|
5
|
|
|
|
|
54
|
|
|
|
5
|
|
|
|
|
7784
|
|
|
56
|
1
|
|
|
|
|
27
|
printf( STDERR "Haineko %s\n", $Haineko::VERSION ); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub which { |
|
60
|
5
|
|
|
5
|
1
|
6189
|
my $class = shift; |
|
61
|
5
|
|
100
|
|
|
30
|
my $cname = shift || return q(); |
|
62
|
4
|
|
|
|
|
30
|
my $paths = [ split( ':', $ENV{'PATH'} ) ]; |
|
63
|
4
|
|
|
|
|
10
|
my $cpath = q(); |
|
64
|
|
|
|
|
|
|
|
|
65
|
4
|
50
|
|
|
|
14
|
return q() unless scalar @$paths; |
|
66
|
4
|
|
|
|
|
9
|
for my $e ( @$paths ) { |
|
67
|
25
|
100
|
|
|
|
490
|
next unless -d $e; |
|
68
|
|
|
|
|
|
|
|
|
69
|
24
|
|
|
|
|
62
|
my $f = $e.'/'.$cname; |
|
70
|
24
|
100
|
|
|
|
394
|
next unless -f $f; |
|
71
|
3
|
50
|
|
|
|
58
|
next unless -x $f; |
|
72
|
3
|
|
|
|
|
8
|
$cpath = $f; |
|
73
|
3
|
|
|
|
|
7
|
last; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
4
|
|
|
|
|
33
|
return $cpath; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub stdin { |
|
79
|
1
|
|
|
1
|
1
|
1399
|
my $self = shift; |
|
80
|
1
|
|
|
|
|
10
|
return $self->{'stream'}->{'stdin'}; |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub stdout { |
|
84
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
|
85
|
1
|
|
|
|
|
9
|
return $self->{'stream'}->{'stdout'}; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub stderr { |
|
89
|
3
|
|
|
3
|
1
|
5
|
my $self = shift; |
|
90
|
3
|
|
|
|
|
20
|
return $self->{'stream'}->{'stderr'}; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub r { |
|
94
|
11
|
|
|
11
|
1
|
1411
|
my $self = shift; |
|
95
|
11
|
|
|
|
|
15
|
my $argv = shift; |
|
96
|
|
|
|
|
|
|
|
|
97
|
11
|
100
|
|
|
|
103
|
$self->{'runmode'} = $argv if defined $argv; |
|
98
|
11
|
|
|
|
|
65
|
return $self->{'runmode'}; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub v { |
|
102
|
6
|
|
|
6
|
1
|
1128
|
my $self = shift; |
|
103
|
6
|
|
|
|
|
108
|
my $argv = shift; |
|
104
|
|
|
|
|
|
|
|
|
105
|
6
|
100
|
|
|
|
20
|
$self->{'verbose'} = $argv if defined $argv; |
|
106
|
6
|
|
|
|
|
23
|
return $self->{'verbose'}; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub e { |
|
110
|
2
|
|
|
2
|
1
|
317
|
my $self = shift; |
|
111
|
2
|
100
|
|
|
|
5
|
my $mesg = shift; return 0 unless length $mesg; |
|
|
2
|
|
|
|
|
14
|
|
|
112
|
1
|
|
50
|
|
|
4
|
my $cont = shift || 0; |
|
113
|
|
|
|
|
|
|
|
|
114
|
1
|
50
|
|
|
|
7
|
$self->l( $mesg, 'e' ) unless $self->{'logging'}->{'disabled'}; |
|
115
|
1
|
50
|
|
|
|
4
|
if( $self->stderr ) { |
|
116
|
0
|
|
|
|
|
0
|
printf( STDERR " * error0: %s\n", $mesg ); |
|
117
|
0
|
0
|
|
|
|
0
|
printf( STDERR " * error0: ******** ABORT ********\n" ) unless $cont; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
1
|
50
|
|
|
|
7
|
$cont ? return 1 : exit(1); |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub p { |
|
123
|
2
|
|
|
2
|
1
|
5
|
my $self = shift; |
|
124
|
2
|
100
|
|
|
|
6
|
my $mesg = shift; return 0 unless length $mesg; |
|
|
2
|
|
|
|
|
10
|
|
|
125
|
1
|
|
50
|
|
|
5
|
my $rung = shift // 1; |
|
126
|
|
|
|
|
|
|
|
|
127
|
1
|
50
|
|
|
|
4
|
return 0 unless $self->stderr; |
|
128
|
|
|
|
|
|
|
|
|
129
|
0
|
0
|
|
|
|
0
|
if( $rung > -1 ) { |
|
130
|
0
|
0
|
|
|
|
0
|
return 0 unless $self->v; |
|
131
|
0
|
0
|
|
|
|
0
|
return 0 unless $self->v >= $rung; |
|
132
|
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
0
|
chomp $mesg; |
|
134
|
0
|
|
|
|
|
0
|
printf( STDERR " * debug%d: %s\n", $rung, $mesg ); |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
} else { |
|
137
|
0
|
|
|
|
|
0
|
printf( STDERR "%s\n", $mesg ); |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
0
|
return 1; |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub makerf { |
|
145
|
1
|
|
|
1
|
0
|
1157
|
my $self = shift; |
|
146
|
1
|
|
|
|
|
2
|
my $argv = shift; |
|
147
|
1
|
|
|
|
|
1
|
my $file = undef; |
|
148
|
1
|
|
|
|
|
3
|
my $text = ''; |
|
149
|
|
|
|
|
|
|
|
|
150
|
1
|
50
|
|
|
|
4
|
return 0 unless ref $argv; |
|
151
|
1
|
50
|
|
|
|
4
|
return 0 unless ref $argv eq 'ARRAY'; |
|
152
|
1
|
50
|
|
|
|
3
|
return 0 unless scalar @$argv; |
|
153
|
|
|
|
|
|
|
|
|
154
|
1
|
50
|
33
|
|
|
9
|
if( $self->{'pidfile'} && ! $self->{'runfile'} ) { |
|
155
|
|
|
|
|
|
|
# Generate file name of ``runfile'' |
|
156
|
1
|
|
|
|
|
2
|
$self->{'runfile'} = $self->{'pidfile'}; |
|
157
|
1
|
|
|
|
|
6
|
$self->{'runfile'} =~ s|[.]pid|.sh|; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
1
|
50
|
|
|
|
3
|
return 0 unless $self->{'runfile'}; |
|
160
|
|
|
|
|
|
|
|
|
161
|
1
|
50
|
|
|
|
63
|
$self->removerf if -e $self->{'runfile'}; |
|
162
|
1
|
|
50
|
|
|
8
|
$file = IO::File->new( $self->{'runfile'}, 'w' ) || return 0; |
|
163
|
1
|
|
|
|
|
123
|
$text = sprintf( "%s\nexec %s\n", '#!/bin/sh', join( ' ', @$argv ) ); |
|
164
|
|
|
|
|
|
|
|
|
165
|
1
|
50
|
|
|
|
11
|
flock( $file, LOCK_EX ) ? $file->print( $text ) : return 0; |
|
166
|
1
|
50
|
|
|
|
45
|
flock( $file, LOCK_UN ) ? $file->close : return 0; |
|
167
|
1
|
|
|
|
|
47
|
chmod( 0755, $self->{'runfile'} ); |
|
168
|
1
|
|
|
|
|
5
|
return 1; |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub removerf { |
|
172
|
1
|
|
|
1
|
0
|
304
|
my $self = shift; |
|
173
|
1
|
50
|
|
|
|
24
|
return 0 unless -f $self->{'runfile'}; |
|
174
|
1
|
|
|
|
|
92
|
unlink $self->{'runfile'}; |
|
175
|
1
|
|
|
|
|
4
|
return 1; |
|
176
|
|
|
|
|
|
|
} |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub makepf { |
|
179
|
1
|
|
|
1
|
1
|
278
|
my $self = shift; |
|
180
|
1
|
|
|
|
|
2
|
my $file = undef; |
|
181
|
1
|
|
|
|
|
3
|
my $text = ''; |
|
182
|
|
|
|
|
|
|
|
|
183
|
1
|
50
|
|
|
|
7
|
return 0 unless $self->{'pidfile'}; |
|
184
|
1
|
50
|
|
|
|
97
|
return 0 if -e $self->{'pidfile'}; |
|
185
|
|
|
|
|
|
|
|
|
186
|
1
|
|
50
|
|
|
56
|
$file = IO::File->new( $self->{'pidfile'}, 'w' ) || return 0; |
|
187
|
1
|
|
|
|
|
223
|
$text = sprintf( "%d\n%s\n", $$, $self->{'command'} ); |
|
188
|
|
|
|
|
|
|
|
|
189
|
1
|
50
|
|
|
|
21
|
flock( $file, LOCK_EX ) ? $file->print( $text ) : return 0; |
|
190
|
1
|
50
|
|
|
|
99
|
flock( $file, LOCK_UN ) ? $file->close : return 0; |
|
191
|
1
|
|
|
|
|
28
|
return 1; |
|
192
|
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub removepf { |
|
195
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
|
196
|
1
|
50
|
|
|
|
26
|
return 0 unless -f $self->{'pidfile'}; |
|
197
|
1
|
|
|
|
|
164
|
unlink $self->{'pidfile'}; |
|
198
|
1
|
|
|
|
|
4
|
return 1; |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub readpf { |
|
202
|
1
|
|
|
1
|
1
|
1271
|
my $self = shift; |
|
203
|
|
|
|
|
|
|
|
|
204
|
1
|
50
|
|
|
|
38
|
return undef unless -e $self->{'pidfile'}; |
|
205
|
1
|
50
|
|
|
|
20
|
return undef unless -f $self->{'pidfile'}; |
|
206
|
1
|
50
|
|
|
|
20
|
return undef unless -s $self->{'pidfile'}; |
|
207
|
|
|
|
|
|
|
|
|
208
|
1
|
|
50
|
|
|
10
|
my $file = IO::File->new( $self->{'pidfile'}, 'r' ) || return undef; |
|
209
|
1
|
|
|
|
|
151
|
my $pid1 = $file->getline; |
|
210
|
|
|
|
|
|
|
|
|
211
|
1
|
|
|
|
|
51
|
chomp $pid1; |
|
212
|
1
|
|
|
|
|
5
|
$file->close; |
|
213
|
1
|
|
|
|
|
25
|
return $pid1; |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
|
|
216
|
0
|
|
|
0
|
0
|
0
|
sub optionparser {} |
|
217
|
|
|
|
|
|
|
sub help { |
|
218
|
7
|
|
|
7
|
0
|
635
|
my $class = shift; |
|
219
|
7
|
|
100
|
|
|
24
|
my $argvs = shift || q(); |
|
220
|
|
|
|
|
|
|
|
|
221
|
7
|
|
|
|
|
15
|
my $commoption = [ '-v, --verbose' => 'Verbose mode.' ]; |
|
222
|
7
|
|
|
|
|
13
|
my $subcommand = [ 'help' => 'This screen.' ]; |
|
223
|
7
|
|
|
|
|
10
|
my $forexample = []; |
|
224
|
|
|
|
|
|
|
|
|
225
|
7
|
100
|
100
|
|
|
42
|
return $commoption if $argvs eq 'o' || $argvs eq 'option'; |
|
226
|
5
|
100
|
100
|
|
|
28
|
return $subcommand if $argvs eq 's' || $argvs eq 'subcommand'; |
|
227
|
3
|
100
|
100
|
|
|
26
|
return $forexample if $argvs eq 'e' || $argvs eq 'example'; |
|
228
|
|
|
|
|
|
|
} |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
1; |
|
231
|
|
|
|
|
|
|
__END__ |