]>
Commit | Line | Data |
---|---|---|
1 | package Gruntmaster::Daemon::Format; | |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Exporter/; | |
7 | no if $] > 5.017011, warnings => 'experimental::smartmatch'; | |
8 | ||
9 | use Digest::SHA qw/sha256_hex/; | |
10 | use Expect; | |
11 | use File::Basename qw/fileparse/; | |
12 | use File::Copy qw/cp/; | |
13 | use File::Slurp qw/read_file write_file/; | |
14 | use List::MoreUtils qw/natatime/; | |
15 | use Log::Log4perl qw/get_logger/; | |
16 | use String::ShellQuote qw/shell_quote/; | |
17 | use Time::HiRes qw/time/; | |
18 | use Try::Tiny; | |
19 | ||
20 | our $VERSION = '5999.000_005'; | |
21 | our @EXPORT_OK = qw/copy_from_vm copy_to_vm prepare_files stopvms/; | |
22 | ||
23 | ################################################## | |
24 | ||
25 | our (%vm, %pid); | |
26 | ||
27 | sub 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 | ||
34 | sub 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 | ||
41 | sub 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 | ||
56 | sub stopvms { | |
57 | $_->hard_close for values %vm; | |
58 | %vm = %pid = (); | |
59 | } | |
60 | ||
61 | sub 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 | ||
84 | sub 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 | ||
102 | sub 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 | ||
124 | sub 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 | ||
142 | sub 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 | ||
173 | 1; | |
174 | __END__ | |
175 | ||
176 | =encoding utf-8 | |
177 | ||
178 | =head1 NAME | |
179 | ||
180 | Gruntmaster::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 | ||
200 | Gruntmaster::Daemon::Format exports utility functions for handling source files. | |
201 | ||
202 | =over | |
203 | ||
204 | =item B<prepare_files> I<$meta> | |
205 | ||
206 | Compiles all the source files in C<< $meta->{files} >>. | |
207 | ||
208 | =back | |
209 | ||
210 | =head1 AUTHOR | |
211 | ||
212 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
213 | ||
214 | =head1 COPYRIGHT AND LICENSE | |
215 | ||
216 | Copyright (C) 2014 by Marius Gavrilescu | |
217 | ||
218 | This library is free software: you can redistribute it and/or modify | |
219 | it under the terms of the GNU Affero General Public License as published by | |
220 | the Free Software Foundation, either version 3 of the License, or | |
221 | (at your option) any later version. | |
222 | ||
223 | ||
224 | =cut |