#!/usr/bin/perl
use
JSON
qw( from_json )
;
if
(
@ARGV
== 0)
{
$uri
->path(
"/server.json"
);
my
$ua
= LWP::UserAgent->new;
my
$res
=
$ua
->get(
$uri
);
die
$res
->status_line
unless
$res
->is_success;
my
$data
= from_json(
$res
->decoded_content);
my
$table
= Text::Table->new(
'id'
,
'command'
);
foreach
my
$server
(
sort
keys
%$data
)
{
foreach
my
$id
(
sort
keys
%{
$data
->{
$server
} })
{
$table
->load([
"$server:$id"
,
join
(
' '
,
'%'
, @{
$data
->{
$server
}->{
$id
}}) ]);
}
}
print
$table
;
print
"\n"
;
print
"usage: $0 [ id ]\n"
;
exit
;
}
my
(
$server
,
$id
) =
split
/:/,
shift
@ARGV
;
unless
(
defined
$server
&&
defined
$id
)
{
print
"usage: $0 [ id ]\n"
;
exit
2;
}
$uri
->path(
"/job/$server/$id/json"
);
my
$ua
= LWP::UserAgent->new;
my
$res
=
$ua
->get(
$uri
);
die
$res
->status_line
unless
$res
->is_success;
my
$job
= from_json(
$res
->decoded_content);
my
$done
= AnyEvent->condvar;
print
(
join
(
' '
,
'%'
, @{
$job
->{command} }),
"\n"
);
process_message(
$_
)
for
@{
$job
->{events} };
sub
process_message
{
my
$msg
=
shift
;
if
(
$msg
->{type} eq
'out'
)
{
print
$msg
->{data},
"\n"
;
}
elsif
(
$msg
->{type} eq
'err'
)
{
print
$msg
->{data},
"\n"
;
}
elsif
(
$msg
->{type} eq
'exit'
)
{
print
"EXIT: "
.
$msg
->{
exit
};
print
" SIGNAL: "
.
$msg
->{signal}
if
$msg
->{signal};
print
"\n"
;
}
elsif
(
$msg
->{type} eq
'error'
)
{
print
"ERROR: "
.
$msg
->{data},
"\n"
;
}
elsif
(
$msg
->{type} eq
'closed'
)
{
print
"CONNECTION CLOSED"
,
"\n"
;
$done
->
send
;
}
}
my
$ws
= AnyEvent::WebSocket::Client->new->
connect
(
$job
->{event_url})->
recv
;
$ws
->on(
each_message
=>
sub
{
my
$msg
= from_json(
pop
->decoded_body );
process_message(
$msg
);
});
$done
->
recv
;