Do not depend on bash logout and pass -n to poweroff (9p is sync,dirsync)
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon / Format.pm
CommitLineData
ddceb393
MG
1package Gruntmaster::Daemon::Format;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
9577371b 7no if $] > 5.017011, warnings => 'experimental::smartmatch';
ddceb393 8
fdb99417 9use Expect::Simple;
ddceb393 10use File::Basename qw/fileparse/;
c40a2dd0 11use File::Slurp qw/read_file write_file/;
ddceb393 12use List::MoreUtils qw/natatime/;
f5e29130 13use Log::Log4perl qw/get_logger/;
fdb99417 14use String::ShellQuote qw/shell_quote/;
c40a2dd0 15use Try::Tiny;
ddceb393 16
99d37110 17our $VERSION = '5999.000_004';
fdb99417 18our @EXPORT_OK = qw/prepare_files stopvms/;
ddceb393
MG
19
20##################################################
21
fdb99417
MG
22our (%vm);
23
24sub runvm {
0f817fa6 25 my ($name, $arg) = @_;
fdb99417 26 return unless $ENV{GRUNTMASTER_VM};
0f817fa6
MG
27 my $cmd = $ENV{GRUNTMASTER_VM};
28 $cmd .= ' ' . $arg if $arg;
29 get_logger->trace("Starting VM $name ($cmd)");
fdb99417 30 $vm{$name} = Expect::Simple->new({
0f817fa6 31 Cmd => $cmd,
fdb99417 32 Prompt => '# ',
4bc82f17 33 DisconnectCmd => '/sbin/poweroff -fn',
fdb99417
MG
34 RawPty => 1,
35 Timeout => 10,
36 });
37}
38
39sub stopvms { %vm = () }
40
c40a2dd0 41sub execlist {
fdb99417
MG
42 my ($vm, @args) = @_;
43 my $er = "exec-result-$vm";
44 if ($vm{$vm}) {
45 my $cmd = ">$er " . shell_quote 'gruntmaster-exec', @args;
46 get_logger->trace("Running in VM $vm: $cmd");
47 $vm{$vm}->send($cmd);
c40a2dd0 48 } else {
99d37110 49 my $ret = fork // die "Cannot fork\n";
fdb99417
MG
50 if ($ret) {
51 waitpid $ret, 0;
52 } else {
99d37110 53 open STDOUT, '>', $er or die "Cannot open $er\n";
e61f1a82 54 get_logger->trace("Running: gruntmaster-exec @args");
fdb99417
MG
55 exec 'gruntmaster-exec', @args;
56 }
c40a2dd0 57 }
fdb99417
MG
58
59 die "gruntmaster-exec died\n" if -z $er;
60 my ($excode, $exmsg) = read_file $er;
61 unlink $er;
99d37110 62 chomp ($excode, $exmsg); ## no critic (ProhibitParensWithBuiltins)
fdb99417 63 get_logger->trace("Exec result: $excode $exmsg");
99d37110 64 die [$excode, $exmsg] if $excode > 0; ## no critic (RequireCarping)
c40a2dd0
MG
65}
66
ddceb393
MG
67sub mkrun{
68 my $format = shift;
69 sub{
d16a999c 70 local *__ANON__ = 'mkrun_runner';
ddceb393 71 my ($name, %args) = @_;
d16a999c 72 get_logger->trace("Running $name...");
99d37110 73 my $basename = fileparse $name, qr/[.][^.]*/s;
42ce8ba9 74 my @args = ('--sudo');
13b1661a 75 push @args, '--keep-stderr' if $ENV{TEST_VERBOSE};
c40a2dd0
MG
76 push @args, '--timeout', $args{timeout} if $args{timeout};
77 push @args, '--mlimit', $args{mlimit} if $args{mlimit};
78 push @args, '--olimit', $args{olimit} if $args{olimit};
79 my @fds = exists $args{fds} ? @{$args{fds}} : ();
80 my $it = natatime 2, @fds;
81 while (my ($fd, $file) = $it->()) {
82 push @args, "--fd=$fd $file";
ddceb393 83 }
77ba721e 84 execlist $basename, @args, '--', "./$basename", @{$args{args}}
ddceb393
MG
85 }
86}
87
ad77b7d3
MG
88sub prepare{
89 my ($name, $format) = @_;
ad77b7d3
MG
90 get_logger->trace("Preparing file $name...");
91
c40a2dd0 92 try {
fdb99417 93 execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
c40a2dd0 94 } catch {
36ecc8ea
MG
95 my $exmsg = $_->[1];
96 die "Compile error ($exmsg)\n"
c40a2dd0
MG
97 } finally {
98 $Gruntmaster::Daemon::errors .= read_file 'errors';
837911b3 99 $Gruntmaster::Daemon::errors .= "\n" if -s 'errors';
c40a2dd0
MG
100 unlink 'errors';
101 };
ad77b7d3
MG
102}
103
104sub prepare_files{
105 my $meta = shift;
0f817fa6
MG
106 if ($meta->{runner} eq 'Interactive') {
107 runvm ver => '-serial unix:vm.sock,nowait,server';
108 runvm prog => '-serial unix:vm.sock,nowait';
109 } else {
110 runvm $_ for keys %{$meta->{files}};
111 }
ad77b7d3 112
759d3ca5 113 for my $file (values %{$meta->{files}}) {
ad77b7d3
MG
114 my ($format, $name, $content) = @{$file}{qw/format name content/};
115
116 $file->{run} = mkrun($format);
117 write_file $name, $content;
118 prepare $name, $format;
119 }
120}
121
bc372959
MG
1221;
123__END__
124
125=encoding utf-8
126
127=head1 NAME
128
129Gruntmaster::Daemon::Format - Utility functions for handling source files
130
131=head1 SYNOPSIS
132
133 use Gruntmaster::Daemon::Format qw/prepare_files/;
134 prepare_files { files => {
135 prog => {
136 name => 'prog.pl',
137 format => 'PERL',
138 content => 'print "Hello, world!"'
139 },
140 ver => {
141 name => 'ver.cpp',
142 format => 'CPP',
143 content => ...
144 },
145 }};
146
147=head1 DESCRIPTION
148
149Gruntmaster::Daemon::Format exports utility functions for handling source files.
150
151=over
152
153=item B<prepare_files> I<$meta>
154
155Compiles all the source files in C<< $meta->{files} >>.
156
157=back
158
159=head1 AUTHOR
160
161Marius Gavrilescu E<lt>marius@ieval.roE<gt>
162
163=head1 COPYRIGHT AND LICENSE
164
165Copyright (C) 2014 by Marius Gavrilescu
166
167This library is free software: you can redistribute it and/or modify
168it under the terms of the GNU Affero General Public License as published by
169the Free Software Foundation, either version 3 of the License, or
170(at your option) any later version.
171
172
173=cut
This page took 0.074018 seconds and 4 git commands to generate.