]> iEval git - gruntmaster-daemon.git/blame_incremental - lib/Gruntmaster/Daemon/Format.pm
Merge branch 'master' into scaleway
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon / Format.pm
... / ...
CommitLineData
1package Gruntmaster::Daemon::Format;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
7no if $] > 5.017011, warnings => 'experimental::smartmatch';
8
9use Digest::SHA qw/sha256_hex/;
10use Expect;
11use File::Basename qw/fileparse/;
12use File::Copy qw/cp/;
13use File::Slurp qw/read_file write_file/;
14use List::MoreUtils qw/natatime/;
15use Log::Log4perl qw/get_logger/;
16use String::ShellQuote qw/shell_quote/;
17use Time::HiRes qw/time/;
18use Try::Tiny;
19
20our $VERSION = '5999.000_005';
21our @EXPORT_OK = qw/copy_from_vm copy_to_vm prepare_files stopvms/;
22
23##################################################
24
25our (%vm, %pid);
26
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
41sub runvm {
42 my ($name, $arg) = @_;
43 return unless $ENV{GRUNTMASTER_VM};
44 my $cmd = $ENV{GRUNTMASTER_VM} . ' ' . $name;
45 $cmd .= ' ' . $arg if $arg;
46 get_logger->trace("Starting VM $name ($cmd)");
47 $vm{$name} = Expect->new;
48 $vm{$name}->raw_pty(1);
49 $vm{$name}->log_stdout(0);
50 $vm{$name}->spawn($cmd);
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, '# ')
54}
55
56sub stopvms {
57 $_->hard_close for values %vm;
58 %vm = %pid = ();
59}
60
61sub execlist_finish {
62 my ($vm, $kill) = @_;
63
64 if ($vm{$vm}) {
65 warn "Cannot kill VM\n" if $kill;
66 $vm{$vm}->expect(50, '# ');
67 } else {
68 kill KILL => $pid{$vm} if $kill;
69 waitpid $pid{$vm}, 0;
70 }
71 write_file "time-stop-$vm", time;
72 return if $kill;
73
74 my $er = "exec-result-$vm";
75 copy_from_vm $er;
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}
83
84sub execlist {
85 my ($vm, @args) = @_;
86 write_file "time-start-$vm", time;
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");
91 $vm{$vm}->send($cmd, "\n");
92 } else {
93 $pid{$vm} = fork // die "Cannot fork\n";
94 unless ($pid{$vm}) {
95 open STDOUT, '>', $er or die "Cannot open $er\n";
96 get_logger->trace("Running: gruntmaster-exec @args");
97 exec 'gruntmaster-exec', @args;
98 }
99 }
100}
101
102sub mkrun{
103 my $format = shift;
104 sub{
105 local *__ANON__ = 'mkrun_runner';
106 my ($name, %args) = @_;
107 get_logger->trace("Running $name...");
108 my $basename = fileparse $name, qr/[.][^.]*/s;
109 my @args = ('--sudo');
110 push @args, '--keep-stderr' if $ENV{TEST_VERBOSE};
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";
118 }
119 execlist $basename, @args, '--', "./$basename", @{$args{args}};
120 execlist_finish $basename unless $args{nonblocking}
121 }
122}
123
124sub prepare{
125 my ($name, $format) = @_;
126 get_logger->trace("Preparing file $name...");
127
128 try {
129 execlist prog => '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
130 execlist_finish 'prog';
131 } catch {
132 my $exmsg = $_->[1];
133 die "Compile error ($exmsg)\n"
134 } finally {
135 copy_from_vm 'errors';
136 $Gruntmaster::Daemon::errors .= read_file 'errors';
137 $Gruntmaster::Daemon::errors .= "\n" if -s 'errors';
138 unlink 'errors';
139 };
140}
141
142sub prepare_files{
143 my $meta = shift;
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 }
150
151 for my $file (values %{$meta->{files}}) {
152 my ($format, $name, $content) = @{$file}{qw/format name content/};
153
154 $file->{run} = mkrun($format);
155 write_file $name, $content;
156 copy_to_vm $name;
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 }
170 }
171}
172
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.027535 seconds and 4 git commands to generate.