blib/lib/App/Netdisco/Transport/Python.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 130 CC 6 SCAR 19.6
CC
6
Cov
0%
py_worklet
20.0
init
12.0
BEGIN
2.0
CRAP 7.1
1 package App::Netdisco::Transport::Python;
2  
3 use Dancer qw/:syntax :script/;
4  
5 use base 'Dancer::Object::Singleton';
6 use aliased 'App::Netdisco::Worker::Status';
7 use App::Netdisco::Util::Python 'py_cmd';
8  
9 use IPC::Run 'harness';
10 use MIME::Base64 'decode_base64';
11 use Path::Class;
12 use File::ShareDir 'dist_dir';
13 use File::Slurper qw/read_text write_text/;
14 use File::Temp ();
15 use JSON::PP ();
16 use YAML::XS ();
17 use Try::Tiny;
18  
19 =head1 NAME
20  
21 App::Netdisco::Transport::Python
22  
23 =head1 DESCRIPTION
24  
25 Not really a transport, but has similar behaviour to a Transport.
26  
27 Returns an object which has a live Python subprocess expecting
28 instruction to run worklets.
29  
30 my $runsub = App::Netdisco::Transport::Python->py_worklet();
31  
32 =cut
33  
34 __PACKAGE__->attributes(qw/ runner stdin stdout context /);
35  
36 sub init {
37 my ( $class, $instance ) = @_;
38  
39 my ( $stdin, $stdout );
40 $instance->stdin( \$stdin );
41 $instance->stdout( \$stdout );
42 $instance->context( File::Temp->new() );
43  
44 my $cmd = [ py_cmd('run_worklet'), $instance->context->filename ];
45 debug "\N{SNAKE} starting persistent Python worklet subprocess";
46  
47 $instance->runner( harness(
48 ($ENV{ND2_PYTHON_HARNESS_DEBUG} ? (debug => 1) : ()),
49 $cmd,
50 '<', \$stdin,
51 '1>', \$stdout,
52 '2>', sub { debug $_[0] },
53 ) );
54  
55 debug $instance->context if $ENV{ND2_PYTHON_HARNESS_DEBUG};
56 return $instance;
57 }
58  
59 =head1 py_worklet( )
60  
61 Contacts a live Python worklet runner to run a job and retrieve output.
62  
63 =cut
64  
65 sub py_worklet {
66 my ($self, $job, $workerconf) = @_;
67 my $action = $workerconf->{action};
68  
69 my $coder = JSON::PP->new->utf8(1)
70 ->allow_nonref(1)
71 ->allow_unknown(1)
72 ->allow_blessed(1)
73 ->allow_bignum(1);
74  
75 # this is only really used the first time (pump calls start)
76 $ENV{'ND2_JOB_METADATA'} = $coder->encode( { %$job, device => (($job->device || '') .'') } );
77 $ENV{'ND2_CONFIGURATION'} = $coder->encode( config() );
78 $ENV{'ND2_FSM_TEMPLATES'} = Path::Class::Dir->new( dist_dir('App-Netdisco') )
79 ->subdir('python')->subdir('tfsm')->stringify;
80  
81 my $inref = $self->stdin;
82 my $outref = $self->stdout;
83
84 # copy latest vars to the worklet
85 write_text($self->context->filename, $coder->encode( { vars => vars() } ));
86 # necessary before running, but do first (instead of after) to aid debugging
87 $$outref = '';
88  
89 $$inref = $workerconf->{pyworklet} ."\n";
90 $self->runner->pump until ($$outref and $$outref =~ /^\.\Z/m);
91  
92 my $context = read_text($self->context->filename);
93 truncate($self->context, 0); # do not leave things lying around on disk
94  
95 my $retdata = try { YAML::XS::Load(decode_base64($context)) }; # might explode
96 $retdata = {} if not ref $retdata or 'HASH' ne ref $retdata;
97  
98 # use DDP;
99 # p $$outref;
100 # p $retdata;
101  
102 my $status = $retdata->{status} || '';
103 my $log = $retdata->{log}
104 || ($status eq 'done' ? (sprintf '%s exit OK', $action)
105 : (sprintf '%s exit with status "%s"', $action, $status));
106  
107 var($_ => $retdata->{stash}->{$_}) for keys %{ $retdata->{stash} || {} };
108 var(live_python => true);
109  
110 return ($status ? Status->$status($log) : Status->info('worklet returned no status'));
111 }
112  
113 true;