Toadfarm.pm

filter: Statementstmt 84.1% 197 / 234 Branchbran 65.7% 96 / 146 Conditioncond 45.0% 27 / 60 MC/DCmcdc 15.6% 5 / 32 Subroutinesub 87.8% 29 / 33 Podpod 100% 1 / 1 totaltotal 70.1% 355 / 506 CC 118 SCAR 29.8
CC
118
Cov
73%
_run_as
74.4
run_as
74.4
_mount_apps
30.1
CRAP 19.7
↑ Summary Toadfarm/Command/tail.pm »
1 package Toadfarm;
2 21 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 21 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Mojo::Util qw(class_to_path monkey_patch);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 21 ▶use constant DEBUG => $ENV{TOADFARM_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TOADFARM_DEBUG'} ? : 0 21 21
13  
14 21 our $VERSION = '0.84';
15  
16 21 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=19.2
17 21 ▶ $ENV{TOADFARM_ACTION} //= (@ARGV and $ARGV[0] =~ /^(reload|start|stop)$/) ? $1 : 'load';
Branch: 1 branch
BranchTrueFalseTotal
@ARGV && $ARGV[0] =~ /^(reload|start|stop)$/u ? : 0 19 19
Condition50%3/6
$ENV{"TOADFARM_ACTION"} //= @ARGV && $ARGV[0] =~ /^(reload|start|stop)$/u ? $1 : "load"
l!l&&r!l&&!r
2190
@ARGV and $ARGV[0] =~ /^(reload|start|stop)$/u
!ll&&!rl&&r
1900
MC/DC0%0/3
$ENV{"TOADFARM_ACTION"} //= @ARGV && $ARGV[0] =~ /^(reload|start|stop)$/u ? $1 : "load"
$ENV{"TOADFARM_ACTION"} @ARGV $ARGV[0] =~ /^(reload|start|stop)$/u
Truth table0%0/4
A //= B and C
A= $ENV{"TOADFARM_ACTION"}B= @ARGVC= $ARGV[0] =~ /^(reload|start|stop)$/u
ABCresult
1XX1
0111
00X0
0100
18 21 ▶ $ENV{MOJO_CONFIG} = $ENV{TOADFARM_CONFIG} if $ENV{TOADFARM_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TOADFARM_CONFIG"} 0 21 21
19 }
20  
21 18 ▶sub import {
Subroutine import: called CC=4 SCAR=14.4
22 18 ▶ return unless grep {/^(-dsl|-init|-test)/} @_;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^(-dsl|-init|-test)/u;} @_ 1 17 18
23  
24 17 my $class = shift;
25 17 my $caller = caller;
26 17 my $app = Toadfarm->new;
27 17 ▶ my $tf = $app->config->{tf} ||= {}; # internal
Condition50%1/2
$app->config->{"tf"} ||= {}
l!l
017
MC/DC0%0/1
$app->config->{"tf"} ||= {}
$app->config->{"tf"}
Truth table50%1/2
A
A= $app->config->{"tf"}
Aresult
11
00
28  
29 17 $_->import for qw(strict warnings utf8);
30 17 feature->import(':5.10');
31 17 unshift @{$app->commands->namespaces}, 'Toadfarm::Command';
32  
33 monkey_patch $caller, (
34 7 ▶ app => sub {$app},
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine app: called CC=1 SCAR=0.0
35 change_root => \&_change_root,
36 0 ▶ logging => sub { $tf->{logging}++; $app->_setup_log(@_) },
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine logging: called CC=1 SCAR=1.2
37 6 ▶ mount => sub { push @{$app->config->{apps}}, @_ == 2 ? @_ : ($_[0], {}); $app },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 6 0 6
38 1 ▶ plugin => sub { push @{$app->config->{tf_plugins}}, @_ == 2 ? @_ : ($_[0], {}); $app },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 1 0 1
39 run_as => \&_run_as,
40 0 ▶ secrets => sub { $tf->{secrets}++; $app->secrets([@_]) },
Subroutine __ANON__: called CC=1 SCAR=0.2
41 5 ▶ start => sub {
Subroutine __ANON__: called CC=10 SCAR=25.9
42 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 4 5
43 1 ▶ my $listen = ref $_[0] eq 'ARRAY' ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 0 1
44 0 ▶ $app->config->{hypnotoad} = @_ > 1 ? {@_} : {%{$_[0]}} if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 1 0 1
@_ > 1 ? : 1 0 1
45 1 ▶ $app->config->{hypnotoad}{listen} = $listen if $listen;
Branch: 1 branch
BranchTrueFalseTotal
if $listen 1 0 1
46 }
47  
48 5 ▶ $app->moniker($class->_moniker) if $app->moniker eq 'toadfarm';
Branch: 1 branch
BranchTrueFalseTotal
if $app->moniker eq "toadfarm" 5 0 5
49 5 ▶ $app->config->{hypnotoad}{pid_file} ||= $class->_pid_file($app);
Condition33%1/3
$app->config->{"hypnotoad"}{"pid_file"} ||= $class->_pid_file($app)
l!l&&r!l&&!r
050
MC/DC0%0/2
$app->config->{"hypnotoad"}{"pid_file"} ||= $class->_pid_file($app)
$app->config->{"hypnotoad"}{"pid_file"} $class->_pid_file($app)
Truth table33%1/3
A ||= B
A= $app->config->{"hypnotoad"}{"pid_file"}B= $class->_pid_file($app)
ABresult
1X1
011
000
50 5 ▶ $app = $class->_setup_app($app) if $ENV{TOADFARM_ACTION};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TOADFARM_ACTION"} 5 0 5
51 5 Mojo::UserAgent::Server->app($app);
52 5 warn '$config=' . Mojo::Util::dumper($app->config) if DEBUG;
53 5 ▶ $class->_die_on_insecure($app) unless $ENV{TOADFARM_INSECURE};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"TOADFARM_INSECURE"} 5 0 5
54 5 $app->start;
55 },
56 17 );
57 }
58  
59 21 ▶sub startup {
Subroutine startup: called CC=9 SCAR=22.0
Pod documented
60 21 my $self = shift;
61 21 ▶ my $config = $ENV{MOJO_CONFIG} ? $self->plugin('Config') : {};
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'MOJO_CONFIG'} ? : 5 16 21
62  
63 # remember the config when hot reloading the app
64 21 $ENV{TOADFARM_CONFIG} = delete $ENV{MOJO_CONFIG};
65  
66 21 $self->{mounted} = 0;
67 21 ▶ $self->_setup_log($config->{log}) if $config->{log}{file};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"log"}{"file"} 1 20 21
68 21 ▶ $self->_paths($config->{paths}) if $config->{paths};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"paths"} 1 20 21
69 21 ▶ $self->secrets([$config->{secret}]) if $config->{secret};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"secret"} 1 20 21
70 21 ▶ $self->secrets($config->{secrets}) if $config->{secrets};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"secrets"} 1 20 21
71 21 ▶ $self->_mount_apps(@{$config->{apps}}) if $config->{apps};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"apps"} 2 19 21
72 21 ▶ $self->_load_plugins(@{$config->{tf_plugins}}) if $config->{tf_plugins};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"tf_plugins"} 2 19 21
73 21 ▶ $self->_mount_root_app(delete $self->{root_app}) if $self->{root_app};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"root_app"} 0 21 21
74 }
75  
76 0 ▶sub _change_root {
Subroutine _change_root: not called CC=5 SCAR=24.9
77 0 my @cmd = @_;
78 0 my $exit = -2;
79  
80 0 ▶ return 1 if $<; # not root
Branch: 2 branches
BranchTrueFalseTotal
if $< 0 0 0
if $< 7 9 16
81  
82 0 ▶ unshift @cmd, $ENV{TOADFARM_CHROOT_BIN} || 'chroot';
Condition20%1/5
$ENV{'TOADFARM_CHROOT_BIN'} || 'chroot'
l!l
00
$ENV{'TOADFARM_CHROOT_BIN'} || 'chroot'
l!l
0100
MC/DC0%0/1
$ENV{'TOADFARM_CHROOT_BIN'} || 'chroot'
$ENV{'TOADFARM_CHROOT_BIN'}
MC/DC0%0/1
$ENV{'TOADFARM_CHROOT_BIN'} || 'chroot'
$ENV{'TOADFARM_CHROOT_BIN'}
Truth table0%0/2
A
A= $ENV{'TOADFARM_CHROOT_BIN'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'TOADFARM_CHROOT_BIN'}
Aresult
11
00
83 0 push @cmd, $^X;
84 0 ▶ push @cmd, -I => $INC[0] if $ENV{TOADFARM_ACTION} eq 'test';
Branch: 2 branches
BranchTrueFalseTotal
if $ENV{"TOADFARM_ACTION"} eq "test" 0 0 0
if $ENV{"TOADFARM_ACTION"} eq "test" 3 0 3
85 0 push @cmd, File::Spec->rel2abs($0), @ARGV;
86  
87 0 warn "[Toadfarm] system @cmd\n" if DEBUG;
88 0 system @cmd;
89 0 ▶ die "Could not run '@cmd' exit=$exit\n" if $exit = $? >> 8;
Branch: 2 branches
BranchTrueFalseTotal
if $exit = $? >> 8 0 0 0
if $exit = $? >> 8 3 0 3
90 0 exit $?;
91 }
92  
93 15 ▶sub _die_on_insecure {
Subroutine _die_on_insecure: called CC=7 SCAR=21.0
94 15 my ($class, $app) = @_;
95 15 my $config = $app->config;
96 15 ▶ my $plugins = $config->{tf_plugins} || [];
Condition100%2/2
$config->{'tf_plugins'} || []
l!l
213
MC/DC100%1/1
$config->{'tf_plugins'} || []
$config->{'tf_plugins'}
Truth table100%2/2
A
A= $config->{'tf_plugins'}
Aresult
11
00
97  
98 15 ▶ die "Cannot change user without TOADFARM_INSECURE=1" if $config->{hypnotoad}{user};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"hypnotoad"}{"user"} 1 14 15
99 14 ▶ die "Cannot change group without TOADFARM_INSECURE=1" if $config->{hypnotoad}{group};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"hypnotoad"}{"group"} 1 13 14
100 die "Cannot run as 'root' without TOADFARM_INSECURE=1"
101 if +($> == 0 or $< == 0)
102 0 ▶ and !grep {/\bSetUserGroup$/} @$plugins;
Branch: 1 branch
BranchTrueFalseTotal
if $> == 0 || $< == 0 and not grep({/\bSetUserGroup$/u;} @$plugins) 0 13 13
Condition33%2/6
$> == 0 || $< == 0 and not grep({/\bSetUserGroup$/u;} @$plugins)
!ll&&!rl&&r
1300
$> == 0 or $< == 0
l!l&&r!l&&!r
0013
MC/DC0%0/3
$> == 0 || $< == 0 and not grep({/\bSetUserGroup$/u;} @$plugins)
$> == 0 $< == 0 not grep({/\bSetUserGroup$/u;} @$plugins)
Truth table20%1/5
A or B and not(C)
A= $> == 0B= $< == 0C= not grep({/\bSetUserGroup$/u;} @$plugins)
ABCresult
00X0
1X00
0100
1X11
0111
103 }
104  
105 0 ▶sub _exit { say shift and exit 0 }
Subroutine _exit: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
say shift() and exit 0 0 0 0
106  
107 4 ▶sub _load_plugins {
Subroutine _load_plugins: called CC=2 SCAR=6.9
108 4 my $self = shift;
109  
110 4 unshift @{$self->plugins->namespaces}, 'Toadfarm::Plugin';
111  
112 4 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 4 8
113 4 my ($plugin, $config) = (shift @_, shift @_);
114 4 $self->log->info("Loading plugin $plugin");
115 4 $self->plugin($plugin, $config);
116 }
117 }
118  
119 14 ▶sub _moniker {
Subroutine _moniker: called CC=1 SCAR=0.0
120 14 my $moniker = basename $0;
121 14 $moniker =~ s!\W!_!g;
122 14 $moniker;
123 }
124  
125 9 ▶sub _mount_apps {
Subroutine _mount_apps: called CC=26 SCAR=34.1
126 9 my $self = shift;
127 9 my $routes = $self->routes;
128 9 my $config = $self->config;
129  
130 9 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 9 23
131 14 my ($app, $rules) = (shift @_, shift @_);
132 14 my $server = Mojo::Server->new;
133 14 my $mount_point = delete $rules->{mount_point};
134 14 my ($request_base, $tmp, @over);
135  
136 14 local $ENV{MOJO_CONFIG} = $ENV{MOJO_CONFIG};
137  
138 14 ▶ if (ref $rules->{config} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules->{"config"} eq "HASH") 6 8 14
139 6 require File::Temp;
140 6 my %config = (%{$self->config}, %{$rules->{config}});
141 6 $tmp = File::Temp->new;
142 Mojo::File->new($tmp->filename)->spurt(
143 6 do {
144 6 local $Data::Dumper::Terse = 1;
145 6 local $Data::Dumper::Deepcopy = 1;
146 6 Data::Dumper::Dumper(\%config);
147 }
148 );
149 6 $ENV{MOJO_CONFIG} = $tmp->filename;
150 }
151  
152 14 ▶ unless (ref $app and UNIVERSAL::isa($app, 'Mojolicious')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $app and UNIVERSAL::isa($app, "Mojolicious")) 13 1 14
Condition66%2/3
ref $app and UNIVERSAL::isa($app, "Mojolicious")
!ll&&!rl&&r
1301
MC/DC50%1/2
ref $app and UNIVERSAL::isa($app, "Mojolicious")
ref $app UNIVERSAL::isa($app, "Mojolicious")
Truth table66%2/3
A and B
A= ref $appB= UNIVERSAL::isa($app, "Mojolicious")
ABresult
0X0
100
111
153 13 my ($class, $path, @error) = ($app, $app);
154 13 ▶ $path = File::Which::which($path) || class_to_path($path) unless -r $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 13 0 13
Condition33%1/3
File::Which::which($path) || class_to_path($path)
l!l&&r!l&&!r
0130
MC/DC0%0/2
File::Which::which($path) || class_to_path($path)
File::Which::which($path) class_to_path($path)
Truth table33%1/3
A || B
A= File::Which::which($path)B= class_to_path($path)
ABresult
1X1
011
000
155 13 ▶ $app = eval { $server->build_app($class) } or push @error, $@ if $class =~ /^[\w:]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /^[\w:]+$/u 13 0 13
Condition33%2/6
$class =~ /^[\w:]+$/u && (($app = eval { do { $server->build_app($class) } }) || push(@error, $@))
!ll
0130
$app = eval { do { $server->build_app($class) } } or push @error, $@
l!l
1300
MC/DC0%0/3
$class =~ /^[\w:]+$/u && (($app = eval { do { $server->build_app($class) } }) || push(@error, $@))
$class =~ /^[\w:]+$/u $app = eval { do { $server->build_app($class) } } push @error, $@
Truth table0%0/4
A && B or C
A= $class =~ /^[\w:]+$/uB= $app = eval { do { $server->build_app($class) } }C= push @error, $@
ABCresult
0XX0
1000
11X1
1011
156 0 ▶ $app = eval { $server->load_app($path) } or push @error, $@ unless ref $app;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $app 0 13 13
Condition16%1/6
ref $app || (($app = eval { do { $server->load_app($path) } }) || push(@error, $@))
l!l
1300
$app = eval { do { $server->load_app($path) } } or push @error, $@
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $app || (($app = eval { do { $server->load_app($path) } }) || push(@error, $@))
ref $app $app = eval { do { $server->load_app($path) } } push @error, $@
Truth table0%0/4
A || B or C
A= ref $appB= $app = eval { do { $server->load_app($path) } }C= push @error, $@
ABCresult
1XX1
01X1
0011
0000
157 13 ▶ die join "\n", @error unless $app;
Branch: 1 branch
BranchTrueFalseTotal
unless $app 0 13 13
158 }
159  
160 14 ▶ $app->log($self->log) if $config->{log}{combined};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"log"}{"combined"} 3 11 14
161 14 ▶ $app->secrets($self->secrets) if $config->{tf}{secrets};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"tf"}{"secrets"} 4 10 14
162  
163 14 ▶ if (ref $rules->{config} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rules->{"config"} eq "HASH") 6 8 14
164 6 my $local = delete $rules->{config};
165 6 $app->config->{$_} = $local->{$_} for keys %$local;
166 }
167  
168 14 ▶ $app->config->{$_} ||= $config->{$_} for keys %$config;
Condition66%2/3
$app->config->{$_} ||= $config->{$_}
l!l&&r!l&&!r
31380
MC/DC0%0/2
$app->config->{$_} ||= $config->{$_}
$app->config->{$_} $config->{$_}
Truth table66%2/3
A ||= B
A= $app->config->{$_}B= $config->{$_}
ABresult
1X1
011
000
169  
170 14 for my $k (qw(local_port remote_address remote_port)) {
171 42 push @over, $self->_skip_if(tx => $k, delete $rules->{$k});
172 }
173  
174 14 for my $name (sort keys %$rules) {
175 9 ▶ $request_base = $rules->{$name} if $name eq 'X-Request-Base';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "X-Request-Base" 3 6 9
176 9 push @over, $self->_skip_if(header => $name, $rules->{$name});
177 }
178  
179 14 ▶ if (@over) {
Branch: 2 branches
BranchTrueFalseTotal
if (@over) { } 8 6 14
elsif ($mount_point) { } 3 3 6
180 8 $self->log->info("Mounting @{[$app->moniker]} with conditions");
181 8 unshift @over, "sub { my \$h = \$_[1]->req->headers;\nlocal \$1;";
182 8 ▶ push @over, "\$_[1]->req->url->base(Mojo::URL->new(\$1 || '$request_base'));" if $request_base;
Branch: 1 branch
BranchTrueFalseTotal
if $request_base 3 5 8
183 8 push @over, "return 1; }";
184 8 ▶ $routes->add_condition("toadfarm_condition_$self->{mounted}", => eval "@over" || die "@over: $@");
Condition50%1/2
eval join($", @over) || die("@{over}: $@")
l!l
80
MC/DC0%0/1
eval join($", @over) || die("@{over}: $@")
eval join($", @over)
Truth table50%1/2
A
A= eval join($", @over)
Aresult
11
00
185 8 ▶ $routes->any($mount_point || '/')->requires("toadfarm_condition_$self->{mounted}")->partial(1)->to(app => $app);
Condition100%2/2
$mount_point || '/'
l!l
26
MC/DC100%1/1
$mount_point || '/'
$mount_point
Truth table100%2/2
A
A= $mount_point
Aresult
11
00
186 }
187 elsif ($mount_point) {
188 3 $routes->any($mount_point)->partial(1)->to(app => $app);
189 }
190 else {
191 3 $self->{root_app} = $app;
192 }
193  
194 14 $self->{mounted}++;
195 }
196  
197 9 $self;
198 }
199  
200 1 ▶sub _mount_root_app {
Subroutine _mount_root_app: called CC=1 SCAR=0.0
201 1 my ($self, $app) = @_;
202 1 $self->log->info("Mounting @{[$app->moniker]} without conditions.");
203 1 $self->routes->any('/')->partial(1)->to(app => $app);
204 }
205  
206 1 ▶sub _paths {
Subroutine _paths: called CC=3 SCAR=11.1
207 1 my ($self, $config) = @_;
208  
209 1 for my $type (qw(renderer static)) {
210 2 ▶ my $paths = $config->{$type} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $paths = $config->{$type} or next 2 0 2
211 2 $self->$type->paths($paths);
212 }
213 }
214  
215 15 ▶sub _pid_file {
Subroutine _pid_file: called CC=2 SCAR=7.4
216 15 my ($class, $app) = @_;
217 15 my $name = basename $0;
218 15 my $dir = dirname abs_path $0;
219  
220 15 ▶ return File::Spec->catfile($dir, "$name.pid") if -w $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -w $dir 15 0 15
221 0 return File::Spec->catfile(File::Spec->tmpdir, "toadfarm-$name.pid");
222 }
223  
224 0 ▶sub _run_as {
Subroutine _run_as: not called CC=10 SCAR=43.1
225 0 ▶ my $user = shift || die "Usage: run_as('username')";
Condition0%0/2
shift() || die(q[Usage: run_as('username')])
l!l
00
MC/DC0%0/1
shift() || die(q[Usage: run_as('username')])
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
226 0 my ($exit, $uid, @sudo);
227  
228 0 ▶ $uid = $user =~ m!^\d+$! ? $user : scalar getpwnam $user;
Branch: 2 branches
BranchTrueFalseTotal
$user =~ /^\d+$/u ? : 0 0 0
$user =~ /^\d+$/u ? : 3 0 3
229 0 ▶ die "Could not find uid for user $user\n" unless $uid;
Branch: 2 branches
BranchTrueFalseTotal
unless $uid 0 0 0
unless $uid 3 0 3
230 0 ▶ return 1 if $uid == $>;
Branch: 2 branches
BranchTrueFalseTotal
if $uid == $> 0 0 0
if $uid == $> 9 1 10
231  
232 0 for my $p (File::Spec->path) {
233 0 $sudo[0] = File::Spec->catfile($p, 'sudo');
234 0 ▶ next unless -x $sudo[0];
Branch: 2 branches
BranchTrueFalseTotal
unless -x $sudo[0] 0 0 0
unless -x $sudo[0] 10 0 10
235 0 push @sudo, qw(-i -n -u), "#$uid";
236 0 last;
237 }
238  
239 0 ▶ die "Cannot change to uid=$uid: 'sudo' was not found.\n" unless @sudo > 1;
Branch: 2 branches
BranchTrueFalseTotal
unless @sudo > 1 0 0 0
unless @sudo > 1 10 0 10
240 0 push @sudo, $^X;
241 0 ▶ push @sudo, -I => $INC[0] if $ENV{TOADFARM_ACTION} eq 'test';
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TOADFARM_ACTION"} eq "test" 0 0 0
242 0 push @sudo, File::Spec->rel2abs($0), @ARGV;
243 0 warn "[Toadfarm] system @sudo\n" if DEBUG;
244 0 system @sudo;
245 0 ▶ die "Could not run '@sudo' exit=$exit\n" if $exit = $? >> 8;
Branch: 1 branch
BranchTrueFalseTotal
if $exit = $? >> 8 0 0 0
246 0 exit $?;
247 }
248  
249 15 ▶sub _setup_app {
Subroutine _setup_app: called CC=11 SCAR=24.0
250 15 my ($class, $app) = @_;
251 15 my $config = $app->config;
252  
253 15 ▶ $app->secrets([Mojo::Util::md5_sum($$ . $0 . time . rand)]) unless $config->{tf}{secrets};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"tf"}{"secrets"} 13 2 15
254 15 ▶ $app->_mount_apps(@{$config->{apps}}) if $config->{apps};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"apps"} 7 8 15
255 15 ▶ $app->_load_plugins(@{$config->{tf_plugins}}) if $config->{tf_plugins};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"tf_plugins"} 2 13 15
256  
257 15 ▶ if (my $root_app = delete $app->{root_app}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $root_app = delete $app->{"root_app"}) 3 12 15
258 3 ▶ if (@{$config->{apps} || []} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$config->{'apps'} or [];} == 2) { } 2 1 3
Condition50%1/2
$config->{'apps'} || []
l!l
30
MC/DC0%0/1
$config->{'apps'} || []
$config->{'apps'}
Truth table50%1/2
A
A= $config->{'apps'}
Aresult
11
00
259 2 ▶ my $plugins = $config->{tf_plugins} || [];
Condition100%2/2
$config->{'tf_plugins'} || []
l!l
11
MC/DC100%1/1
$config->{'tf_plugins'} || []
$config->{'tf_plugins'}
Truth table100%2/2
A
A= $config->{'tf_plugins'}
Aresult
11
00
260 2 ▶ $root_app->config(hypnotoad => $config->{hypnotoad}) if $config->{hypnotoad};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"hypnotoad"} 2 0 2
261 2 ▶ $root_app->log($app->log) if $config->{tf}{logging};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"tf"}{"logging"} 1 1 2
262 2 $root_app->plugin(shift(@$plugins), shift(@$plugins)) for @$plugins;
263 2 $root_app->secrets($app->secrets);
264 2 push @{$root_app->commands->namespaces}, 'Toadfarm::Command';
265 2 return $root_app;
266 }
267 else {
268 1 $app->_mount_root_app($root_app);
269 }
270 }
271  
272 13 return $app;
273 }
274  
275 3 ▶sub _setup_log {
Subroutine _setup_log: called CC=4 SCAR=14.0
276 3 my ($self, $config) = @_;
277 3 my $log = Mojo::Log->new;
278  
279 3 $self->config(log => $config);
280 3 ▶ $log->path($config->{path}) if $config->{path} ||= delete $config->{file};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"path"} ||= delete $config->{"file"} 1 2 3
Condition66%2/3
$config->{"path"} ||= delete $config->{"file"}
l!l&&r!l&&!r
012
MC/DC50%1/2
$config->{"path"} ||= delete $config->{"file"}
$config->{"path"} delete $config->{"file"}
Truth table66%2/3
A ||= B
A= $config->{"path"}B= delete $config->{"file"}
ABresult
1X1
011
000
281 3 ▶ $log->level($config->{level} || 'info');
Condition50%1/2
$config->{'level'} || 'info'
l!l
30
MC/DC0%0/1
$config->{'level'} || 'info'
$config->{'level'}
Truth table50%1/2
A
A= $config->{'level'}
Aresult
11
00
282 3 $self->log($log);
283 }
284  
285 51 ▶sub _skip_if {
Subroutine _skip_if: called CC=5 SCAR=16.1
286 51 my ($self, $type, $k, $value) = @_;
287 51 ▶ my $format = $type eq 'tx' ? '$_[1]->tx->%s' : $type eq 'header' ? q[$h->header('%s')] : q[INVALID(%s)];
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'tx' ? : 42 9 51
$type eq 'header' ? : 9 0 9
288  
289 51 ▶ if (!defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 40 11 51
elsif (ref $value eq 'Regexp') { } 3 8 11
290 40 return;
291 }
292 elsif (ref $value eq 'Regexp') {
293 3 $value =~ s,(?<!\\)/,\\/,g;
294 3 return sprintf "return 0 unless +($format || '') =~ /(%s)/;", $k, $value;
295 }
296 else {
297 8 return sprintf "return 0 unless +($format || '') eq '%s';", $k, $value;
298 }
299 }
300  
301 21 1;
302  
303 =encoding utf8
304  
305 =head1 NAME
306  
307 Toadfarm - One Mojolicious app to rule them all
308  
309 =head1 VERSION
310  
311 0.84
312  
313 =head1 DESCRIPTION
314  
315 Toadfarm is a module for configuring and starting your L<Mojolicious>
316 applications. You can either combine multiple applications in one script,
317 or just use it as a init script.
318  
319 Core features:
320  
321 =over 4
322  
323 =item *
324  
325 Wrapper around L<hypnotoad|Mojo::Server::Hypnotoad> that makes your
326 application L<Sys-V|https://www.debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian>
327 compatible.
328  
329 =item *
330  
331 Advanced routing and virtual host configuration. Also support routing
332 from behind another web server, such as L<nginx|http://nginx.com/>.
333 This feature is very much like L<Mojolicious::Plugin::Mount> on steroids.
334  
335 =item *
336  
337 Hijacking log messages to a common log file. There's also plugin,
338 L<Toadfarm::Plugin::AccessLog>, that allows you to log the requests sent
339 to your server.
340  
341 =back
342  
343 =head1 SYNOPSIS
344  
345 =head2 Script
346  
347 Here is an example script that sets up logging and mounts some applications
348 under different domains, as well as loading in some custom plugins.
349  
350 See L<Toadfarm::Manual::DSL> for more information about the different functions.
351  
352 #!/usr/bin/perl
353 use Toadfarm -init;
354  
355 logging {
356 combined => 1,
357 file => "/var/log/toadfarm/app.log",
358 level => "info",
359 };
360  
361 mount "MyApp" => {
362 Host => "myapp.example.com",
363 config => {
364 config_parameter_for_myapp => "foo"
365 },
366 };
367  
368 mount "/path/to/app" => {
369 Host => "example.com",
370 mount_point => "/other",
371 };
372  
373 mount "Catch::All::App";
374  
375 plugin "Toadfarm::Plugin::AccessLog";
376  
377 start; # needs to be at the last line
378  
379 =head2 Usage
380  
381 You don't have to put L</Script> in init.d, but it will work with standard
382 start/stop actions.
383  
384 $ /etc/init.d/your-script reload
385 $ /etc/init.d/your-script start
386 $ /etc/init.d/your-script stop
387  
388 See also L<Toadfarm::Manual::RunningToadfarm/Init script> for more details.
389  
390 You can also start the application with normal L<Mojolicious> commands:
391  
392 $ morbo /etc/init.d/your-script
393 $ /etc/init.d/your-script daemon
394  
395 =head1 DOCUMENTATION INDEX
396  
397 =over 4
398  
399 =item * L<Toadfarm::Manual::Intro> - Introduction.
400  
401 =item * L<Toadfarm::Manual::DSL> - Domain specific language for Toadfarm.
402  
403 =item * L<Toadfarm::Manual::Config> - Config file format.
404  
405 =item * L<Toadfarm::Manual::RunningToadfarm> - Command line options.
406  
407 =item * L<Toadfarm::Manual::BehindReverseProxy> - Toadfarm behind nginx.
408  
409 =item * L<Toadfarm::Manual::VirtualHost> - Virtual host setup.
410  
411 =back
412  
413 =head1 PLUGINS
414  
415 =over 4
416  
417 =item * L<Toadfarm::Plugin::AccessLog>
418  
419 Log each request that hit your application.
420  
421 =item * L<Mojolicious::Plugin::SizeLimit>
422  
423 Kill Hypnotoad workers if they grow too large.
424  
425 =item * L<Mojolicious::Plugin::SetUserGroup>
426  
427 Start as root, run workers as less user. See also
428 L<Toadfarm::Manual::RunningToadfarm/Listen to standard HTTP ports>.
429  
430 =back
431  
432 =head1 PREVIOUS VERSIONS
433  
434 L<Toadfarm> prior to version 0.49 used to be a configuration file loaded in
435 by the C<toadfarm> script. This resulted in all the executables to be named
436 C<toadfarm> instead of something descriptive. It also felt a bit awkward to
437 take over C<MOJO_CONFIG> and use all the crazy hacks to start C<hypnotoad>.
438  
439 It also didn't work well as an init script, so there still had to be a
440 seperate solution for that.
441  
442 The new L<Toadfarm> DSL aim to solve all of these issues. This means that
443 if you decide to still use any C<MOJO_CONFIG>, it should be for the
444 applications loaded from inside C<Toadfarm> and not the startup script.
445  
446 Note that the old solution still works, but a warning tells you to change
447 to the new L<DSL|Toadfarm::Manual::DSL> based API.
448  
449 =head1 COPYRIGHT AND LICENSE
450  
451 Copyright (C) 2014, Jan Henning Thorsen
452  
453 This program is free software, you can redistribute it and/or modify it
454 under the terms of the Artistic License version 2.0.
455  
456 =head1 AUTHOR
457  
458 Jan Henning Thorsen - C<jhthorsen@cpan.org>
459  
460 =cut
↑ Summary Toadfarm/Command/tail.pm »