-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.pl
executable file
·230 lines (126 loc) · 4.73 KB
/
app.pl
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
#!/usr/bin/env perl
use strict;
use Mojolicious::Lite;
use Carp;
use Data::UUID;
use File::Basename;
use Mojo::Log;
use Mojo::SQLite;
use Minion::Backend;
use Minion;
use HTML::FromANSI (); # avoid exports if using OO
plugin 'Minion' => { SQLite => 'db/minion.db'};
my $config = plugin Config => { file => '/etc/sparrowup.conf' };
require 'lib/db.pm';
helper sparrowdo_run => sub {
my ($c, $project, $server, $check_id, $params) = @_;
my $log = Mojo::Log->new();
update_check_in_db($check_id, 'proccessing');
if (-d "$config->{repo}/$project"){
my $cmd = "mkdir -p $config->{reports_dir} && cd $config->{repo}/$project && timeout -s KILL 120 sparrowdo";
$cmd.= " --http_proxy=$ENV{http_proxy}" if $ENV{http_proxy};
$cmd.= " --https_proxy=$ENV{https_proxy}" if $ENV{https_proxy};
$cmd.= " --host=$server --bootstrap";
$cmd.= " --ssh_user=".($params->{ssh_user}) if $params->{ssh_user};
$cmd.= " --ssh_port=".($params->{ssh_port}) if $params->{ssh_port};
$cmd.= " --verbose" if $params->{verbose};
$cmd.= " 1>$config->{reports_dir}/$check_id.txt 2>&1";
$log->info("sparrowdo run scheduled ... : $cmd");
my $st = system($cmd);
$log->info("sparrowdo done");
update_check_in_db($check_id, $st == 0 ? 'ok' : 'fail');
$log->info("updated database entry");
} else {
$log->warn("project $config->{repo}/$project does not exist!");
update_check_in_db($check_id,'not exist');
$log->info("updated database entry");
}
};
app->minion->add_task( check_task => sub { shift->app->sparrowdo_run(@_) } );
helper schedule_check => sub { shift->minion->enqueue( check_task => [@_]) };
# API
post '/job' => sub {
my $c = shift;
my $project = $c->param('project');
my $server = $c->param('server');
my $uid_obj = Data::UUID->new;
my $uid = $uid_obj->create();
my $check_id = $uid_obj->to_string($uid);
insert_check_into_db($check_id,$project,$server);
my $params = {};
$params->{verbose} = 1 if $c->param('verbose');
$c->schedule_check($project, $server, $check_id, $params);
$c->flash( { message => "job for $project\@$server scheduled", level => 'success' })->redirect_to('/');
};
post '/job/:project' => sub {
my $c = shift;
my $project = $c->stash('project');
my $server = $c->param('server');
my $web_ui = $c->param('web_ui');
my $uid_obj = Data::UUID->new;
my $uid = $uid_obj->create();
my $check_id = $uid_obj->to_string($uid);
insert_check_into_db($check_id,$project,$server);
my $params = {};
$params->{ssh_user} = $c->param('ssh_user') if $c->param('ssh_user');
$params->{ssh_port} = $c->param('ssh_port') if $c->param('ssh_port');
$params->{verbose} = 1 if $c->param('verbose');
$c->schedule_check($project, $server, $check_id, $params);
if ($web_ui){
$c->flash( { message => "job for $project\@$server scheduled", level => 'success' })->redirect_to('/');
}else{
return $c->render(text => "job schedulled: $check_id\n", status => 200);
}
};
get '/' => sub {
my $c = shift;
my $results = get_checks_from_db();
$c->render( template => 'index' , results => $results );
};
get '/report/:report' => sub {
my $c = shift;
my $check_id = $c->stash('report');
@HTML::FromANSI::Color = (
qw( white darkred darkgreen),
'#8b8b00',
qw(darkblue darkmagenta darkcyan gray dimgray red green yellow blue magenta cyan white)
);
my $h = HTML::FromANSI->new(
fill_cols => 1, linewrap => 1, lf_to_crlf => 1, cols => 70
);
my $log = Mojo::Log->new();
if (open REPORT, "$config->{reports_dir}/$check_id.txt"){
$h->add_text(<REPORT>);
close REPORT;
return $c->render(
template => 'report',
out => $h->html,
check_id => $check_id
);
} else {
$log->error("can't open file $config->{reports_dir}/$check_id.txt to read : $!");
return $c->render(text => "report $check_id not found", status => 404);
}
};
get '/repo/:project' => sub {
my $c = shift;
my $project = $c->stash('project');
my $log = Mojo::Log->new();
if (open SPARROWFILE, "$config->{repo}/$project/sparrowfile"){
my $source = join "", <SPARROWFILE>;
close SPARROWFILE;
return $c->render(
template => 'sparrowfile',
project => $project,
source => $source,
);
} else {
$log->error("can't open file $config->{repo}/$project/sparrowfile to read : $!");
return $c->render(text => "$project sparrowfile not found", status => 404);
}
};
sub startup {
my $self = shift;
$self->secret('sparrowup?');
}
app->start;