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