]> iEval git - gruntmaster-daemon.git/blame - lib/Gruntmaster/Daemon/Format.pm
Merge branch 'master' into scaleway
[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
cd1a16d1 9use Digest::SHA qw/sha256_hex/;
61470035 10use Expect;
ddceb393 11use File::Basename qw/fileparse/;
cd1a16d1 12use File::Copy qw/cp/;
c40a2dd0 13use File::Slurp qw/read_file write_file/;
ddceb393 14use List::MoreUtils qw/natatime/;
f5e29130 15use Log::Log4perl qw/get_logger/;
fdb99417 16use String::ShellQuote qw/shell_quote/;
34cc90c9 17use Time::HiRes qw/time/;
c40a2dd0 18use Try::Tiny;
ddceb393 19
3fa65372 20our $VERSION = '5999.000_005';
cedd5d68 21our @EXPORT_OK = qw/copy_from_vm copy_to_vm prepare_files stopvms/;
ddceb393
MG
22
23##################################################
24
61470035 25our (%vm, %pid);
fdb99417 26
cedd5d68
MG
27sub copy_from_vm {
28 my ($file) = @_;
29 return unless $ENV{GRUNTMASTER_COPY_FROM_VM};
30 get_logger->trace("Copying $file from VM");
31 system $ENV{GRUNTMASTER_COPY_FROM_VM}, $file
32}
33
34sub copy_to_vm {
35 my ($file) = @_;
36 return unless $ENV{GRUNTMASTER_COPY_TO_VM};
37 get_logger->trace("Copying $file to VM");
38 system $ENV{GRUNTMASTER_COPY_TO_VM}, $file
39}
40
fdb99417 41sub runvm {
0f817fa6 42 my ($name, $arg) = @_;
fdb99417 43 return unless $ENV{GRUNTMASTER_VM};
cedd5d68 44 my $cmd = $ENV{GRUNTMASTER_VM} . ' ' . $name;
0f817fa6
MG
45 $cmd .= ' ' . $arg if $arg;
46 get_logger->trace("Starting VM $name ($cmd)");
559c4eec 47 $vm{$name} = Expect->new;
61470035 48 $vm{$name}->raw_pty(1);
559c4eec
MG
49 $vm{$name}->log_stdout(0);
50 $vm{$name}->spawn($cmd);
cedd5d68
MG
51 $vm{$name}->expect(50, '# ') or get_logger->logdie("Error while starting VM $name: ". $vm{$name}->error);
52 $vm{$name}->send("rm -rf ~/work/*\n");
53 $vm{$name}->expect(50, '# ')
fdb99417
MG
54}
55
61470035 56sub stopvms {
cedd5d68 57 $_->hard_close for values %vm;
61470035
MG
58 %vm = %pid = ();
59}
60
61sub execlist_finish {
62 my ($vm, $kill) = @_;
63
64 if ($vm{$vm}) {
7e81b08a 65 warn "Cannot kill VM\n" if $kill;
cedd5d68 66 $vm{$vm}->expect(50, '# ');
61470035
MG
67 } else {
68 kill KILL => $pid{$vm} if $kill;
69 waitpid $pid{$vm}, 0;
70 }
34cc90c9 71 write_file "time-stop-$vm", time;
61470035
MG
72 return if $kill;
73
74 my $er = "exec-result-$vm";
cedd5d68 75 copy_from_vm $er;
61470035
MG
76 die "gruntmaster-exec died\n" if -z $er;
77 my ($excode, $exmsg) = read_file $er;
78 unlink $er;
79 chomp ($excode, $exmsg); ## no critic (ProhibitParensWithBuiltins)
80 get_logger->trace("Exec result from $vm: $excode $exmsg");
81 die [$excode, $exmsg] if $excode; ## no critic (RequireCarping)
82}
fdb99417 83
c40a2dd0 84sub execlist {
fdb99417 85 my ($vm, @args) = @_;
34cc90c9 86 write_file "time-start-$vm", time;
fdb99417
MG
87 my $er = "exec-result-$vm";
88 if ($vm{$vm}) {
89 my $cmd = ">$er " . shell_quote 'gruntmaster-exec', @args;
90 get_logger->trace("Running in VM $vm: $cmd");
559c4eec 91 $vm{$vm}->send($cmd, "\n");
c40a2dd0 92 } else {
61470035
MG
93 $pid{$vm} = fork // die "Cannot fork\n";
94 unless ($pid{$vm}) {
99d37110 95 open STDOUT, '>', $er or die "Cannot open $er\n";
e61f1a82 96 get_logger->trace("Running: gruntmaster-exec @args");
fdb99417
MG
97 exec 'gruntmaster-exec', @args;
98 }
c40a2dd0
MG
99 }
100}
101
ddceb393
MG
102sub mkrun{
103 my $format = shift;
104 sub{
d16a999c 105 local *__ANON__ = 'mkrun_runner';
ddceb393 106 my ($name, %args) = @_;
d16a999c 107 get_logger->trace("Running $name...");
99d37110 108 my $basename = fileparse $name, qr/[.][^.]*/s;
42ce8ba9 109 my @args = ('--sudo');
13b1661a 110 push @args, '--keep-stderr' if $ENV{TEST_VERBOSE};
c40a2dd0
MG
111 push @args, '--timeout', $args{timeout} if $args{timeout};
112 push @args, '--mlimit', $args{mlimit} if $args{mlimit};
113 push @args, '--olimit', $args{olimit} if $args{olimit};
114 my @fds = exists $args{fds} ? @{$args{fds}} : ();
115 my $it = natatime 2, @fds;
116 while (my ($fd, $file) = $it->()) {
117 push @args, "--fd=$fd $file";
ddceb393 118 }
61470035
MG
119 execlist $basename, @args, '--', "./$basename", @{$args{args}};
120 execlist_finish $basename unless $args{nonblocking}
ddceb393
MG
121 }
122}
123
ad77b7d3
MG
124sub prepare{
125 my ($name, $format) = @_;
ad77b7d3
MG
126 get_logger->trace("Preparing file $name...");
127
c40a2dd0 128 try {
fdb99417 129 execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
61470035 130 execlist_finish 'prog';
c40a2dd0 131 } catch {
36ecc8ea
MG
132 my $exmsg = $_->[1];
133 die "Compile error ($exmsg)\n"
c40a2dd0 134 } finally {
cedd5d68 135 copy_from_vm 'errors';
c40a2dd0 136 $Gruntmaster::Daemon::errors .= read_file 'errors';
837911b3 137 $Gruntmaster::Daemon::errors .= "\n" if -s 'errors';
c40a2dd0
MG
138 unlink 'errors';
139 };
ad77b7d3
MG
140}
141
142sub prepare_files{
143 my $meta = shift;
0f817fa6
MG
144 if ($meta->{runner} eq 'Interactive') {
145 runvm ver => '-serial unix:vm.sock,nowait,server';
146 runvm prog => '-serial unix:vm.sock,nowait';
147 } else {
148 runvm $_ for keys %{$meta->{files}};
149 }
ad77b7d3 150
759d3ca5 151 for my $file (values %{$meta->{files}}) {
ad77b7d3
MG
152 my ($format, $name, $content) = @{$file}{qw/format name content/};
153
154 $file->{run} = mkrun($format);
155 write_file $name, $content;
cedd5d68 156 copy_to_vm $name;
cd1a16d1
MG
157 if ($ENV{GRUNTMASTER_CCACHE}) {
158 my $key = lc sha256_hex($content) . '-' . $format;
159 my $cachefn = "$ENV{GRUNTMASTER_CCACHE}/$key";
160 my $exefn = fileparse $name, qr/[.][^.]*/s;
161 if (cp $cachefn, $exefn) {
162 get_logger->trace("File $name found in compilation cache")
163 } else {
164 prepare $name, $format;
165 cp $exefn, $cachefn
166 }
167 } else {
168 prepare $name, $format
169 }
ad77b7d3
MG
170 }
171}
172
bc372959
MG
1731;
174__END__
175
176=encoding utf-8
177
178=head1 NAME
179
180Gruntmaster::Daemon::Format - Utility functions for handling source files
181
182=head1 SYNOPSIS
183
184 use Gruntmaster::Daemon::Format qw/prepare_files/;
185 prepare_files { files => {
186 prog => {
187 name => 'prog.pl',
188 format => 'PERL',
189 content => 'print "Hello, world!"'
190 },
191 ver => {
192 name => 'ver.cpp',
193 format => 'CPP',
194 content => ...
195 },
196 }};
197
198=head1 DESCRIPTION
199
200Gruntmaster::Daemon::Format exports utility functions for handling source files.
201
202=over
203
204=item B<prepare_files> I<$meta>
205
206Compiles all the source files in C<< $meta->{files} >>.
207
208=back
209
210=head1 AUTHOR
211
212Marius Gavrilescu E<lt>marius@ieval.roE<gt>
213
214=head1 COPYRIGHT AND LICENSE
215
216Copyright (C) 2014 by Marius Gavrilescu
217
218This library is free software: you can redistribute it and/or modify
219it under the terms of the GNU Affero General Public License as published by
220the Free Software Foundation, either version 3 of the License, or
221(at your option) any later version.
222
223
224=cut
This page took 0.072936 seconds and 4 git commands to generate.