use
5.006;
use
vars
qw{$VERSION @ISA $NOSERVER}
;
BEGIN {
$VERSION
=
'0.41'
;
@ISA
=
'Process'
;
}
sub
new {
my
$class
=
shift
;
my
$self
=
bless
{
@_
},
$class
;
$self
->{platforms} = [];
$self
->{tasks} = [];
$self
->{cleanup} = !!
$self
->{cleanup};
unless
(
$self
->injector ) {
Carp::croak(
"Image 'injector' was not provided"
);
}
unless
( -d
$self
->injector ) {
Carp::croak(
"Image 'injector' does not exist"
);
}
unless
( -r
$self
->injector ) {
Carp::croak(
"Image 'injector' cannot be read, insufficient permissions"
);
}
unless
(
$self
->workarea ) {
$self
->{workarea} = File::Temp::tempdir();
}
unless
(
$self
->workarea ) {
Carp::croak(
"Image 'workarea' not provided and automatic detection failed"
);
}
unless
( -d
$self
->workarea ) {
Carp::croak(
"Image 'workarea' directory does not exist"
);
}
unless
( -r
$self
->workarea and -w _ ) {
Carp::croak(
"Image 'workarea' insufficient permissions"
);
}
unless
(
$self
->image_conf ) {
$self
->{image_conf} = File::Spec->catfile(
$self
->injector,
'image.conf'
,
);
}
unless
(
$self
->image_conf ) {
Carp::croak(
"Did not get an image.conf location"
);
}
unless
( -f
$self
->image_conf ) {
Carp::croak(
"Failed to find image.conf in the injector"
);
}
unless
( -r
$self
->image_conf ) {
Carp::croak(
"No permissions to read scheme.conf"
);
}
$self
;
}
sub
cleanup {
$_
[0]->{cleanup};
}
sub
injector {
$_
[0]->{injector};
}
sub
workarea {
$_
[0]->{workarea};
}
sub
image_conf {
$_
[0]->{image_conf};
}
sub
config {
$_
[0]->{config};
}
sub
perl5lib {
$_
[0]->{perl5lib};
}
sub
server_uri {
$_
[0]->{server_uri};
}
sub
add_platform {
my
$self
=
shift
;
my
$platform
= PITA::Image::Platform->new(
@_
);
push
@{
$self
->{platforms}},
$platform
;
1;
}
sub
add_task {
my
$self
=
shift
;
my
$task
= _INSTANCE(
$_
[0],
'PITA::Image::Task'
)
or
die
(
"Passed bad param to add_task"
);
push
@{
$self
->{tasks}},
$task
;
1;
}
sub
platforms {
@{
$_
[0]->{platforms}};
}
sub
tasks {
@{
$_
[0]->{tasks}};
}
sub
prepare {
my
$self
=
shift
;
my
$class
=
ref
(
$self
);
unless
(
$self
->config ) {
$self
->{config} = Config::Tiny->
read
(
$self
->image_conf );
}
unless
( _INSTANCE(
$self
->config,
'Config::Tiny'
) ) {
Carp::croak(
"Failed to load scheme.conf config file"
);
}
my
$config
=
$self
->config->{_};
unless
(
$config
->{class} and
$config
->{class} eq
$class
) {
Carp::croak(
"Config file is incompatible with PITA::Image"
);
}
unless
(
$config
->{version} and
$config
->{version} eq
$VERSION
) {
Carp::croak(
"Config file is incompatible with this version of PITA::Image"
);
}
if
(
$config
->{perl5lib} ) {
$self
->{perl5lib} = File::Spec->catdir(
$self
->injector,
split
( /\//,
$config
->{perl5lib} ),
);
unless
( -d
$self
->perl5lib ) {
Carp::croak(
"Injector lib directory does not exist"
);
}
unless
( -r
$self
->perl5lib ) {
Carp::croak(
"Injector lib directory has no read permissions"
);
}
lib->
import
(
$self
->perl5lib );
}
unless
(
$self
->server_uri ) {
$self
->{server_uri} = URI->new(
$config
->{server_uri});
}
unless
(
$self
->server_uri ) {
Carp::croak(
"Missing 'server_uri' param in image.conf"
);
}
unless
( _INSTANCE(
$self
->server_uri,
'URI::http'
) ) {
Carp::croak(
"The 'server_uri' is not a HTTP(S) URI"
);
}
unless
(
$NOSERVER
) {
my
$response
= LWP::UserAgent->new->request( GET
$self
->server_uri );
unless
(
$response
and
$response
->is_success ) {
Carp::croak(
"Failed to contact SupportServer at $config->{server_uri}"
);
}
}
unless
(
$self
->config->{task} ) {
Carp::croak(
"Missing [task] section in image.conf"
);
}
unless
(
$self
->config->{task}->{task} ) {
Carp::croak(
"Missing task.task value in image.conf"
);
}
my
$taskname
=
$self
->config->{task}->{task};
if
(
$taskname
eq
'Ping'
) {
}
elsif
(
$taskname
eq
'Discover'
) {
$self
->add_task(
PITA::Image::Discover->new(
%{
$self
->config->{task}},
platforms
=> [
$self
->platforms ],
),
);
}
elsif
(
$taskname
eq
'Test'
) {
$self
->add_task(
PITA::Image::Test->new(
%{
$self
->config->{task}},
injector
=>
$self
->injector,
workarea
=>
$self
->workarea,
),
);
}
else
{
Carp::croak(
"Unknown task.task value in image.conf"
);
}
$self
;
}
sub
run {
my
$self
=
shift
;
$self
->prepare
unless
$self
->config;
foreach
my
$task
(
$self
->tasks ) {
$task
->run;
}
1;
}
sub
report {
my
$self
=
shift
;
foreach
my
$task
(
$self
->tasks ) {
$self
->report_task(
$task
);
}
1;
}
sub
report_task {
my
$self
=
shift
;
my
$task
=
shift
;
my
$agent
= LWP::UserAgent->new;
my
$request
=
$self
->report_task_request(
$task
);
unless
( _INSTANCE(
$request
,
'HTTP::Request'
) ) {
Carp::croak(
"Did not generate proper report HTTP::Request"
);
}
unless
(
$NOSERVER
) {
my
$response
=
$agent
->request(
$request
);
unless
(
$response
and
$response
->is_success ) {
Carp::croak(
"Failed to send result report to server"
);
}
}
1;
}
sub
report_task_request {
my
(
$self
,
$task
) =
@_
;
unless
(
$task
->result ) {
Carp::croak(
"No Result Report created to PUT"
);
}
my
$xml
=
''
;
$task
->result->
write
( \
$xml
);
unless
(
length
(
$xml
) ) {
Carp::croak(
"Failed to serialize report"
);
}
PUT
$self
->report_task_uri(
$task
),
content_type
=>
'application/xml'
,
content_length
=>
length
(
$xml
),
content
=>
$xml
;
}
sub
report_task_uri {
my
(
$self
,
$task
) =
@_
;
my
$uri
=
$self
->server_uri;
my
$job
=
$task
->job_id;
my
$path
= File::Spec->catfile(
$uri
->path ||
'/'
,
$job
);
$uri
->path(
$path
);
$uri
;
}
sub
DESTROY {
if
(
defined
$_
[0]->{tasks} ) {
foreach
my
$i
(
reverse
0 .. $
undef
$_
[0]->{tasks}->[
$i
];
}
delete
$_
[0]->{tasks};
}
if
(
defined
$_
[0]->{platforms} ) {
foreach
my
$i
(
reverse
0 .. $
undef
$_
[0]->{platforms}->[
$i
];
}
delete
$_
[0]->{platforms};
}
if
(
$_
[0]->{cleanup} and
$_
[0]->{workarea} and -d
$_
[0]->{workarea} ) {
File::Remove::remove( \1,
$_
[0]->{workarea} );
}
}
1;
1;