| 1 | package PkgForge::Builder; # -*-perl-*- | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # $Id: Builder.pm.in 16589 2011-04-05 10:42:02Z squinney@INF.ED.AC.UK $ | ||
| 6 | # $Source:$ | ||
| 7 | # $Revision: 16589 $ | ||
| 8 | # $HeadURL: https://svn.lcfg.org/svn/source/tags/PkgForge-Server/PkgForge_Server_1_1_10/lib/PkgForge/Builder.pm.in $ | ||
| 9 | # $Date: 2011-04-05 11:42:02 +0100 (Tue, 05 Apr 2011) $ | ||
| 10 | |||
| 11 | our $VERSION = '1.1.10'; | ||
| 12 | |||
| 13 | use English qw(-no_match_vars); | ||
| 14 | use File::Path (); | ||
| 15 | use File::Spec (); | ||
| 16 | use File::Temp (); | ||
| 17 | use PkgForge::BuildTopic (); | ||
| 18 | use PkgForge::SourceUtils (); | ||
| 19 | |||
| 20 | use Moose::Role; | ||
| 21 | use Moose::Util::TypeConstraints; | ||
| 22 | use MooseX::Types::Moose qw(Bool Int Str); | ||
| 23 | use Readonly; | ||
| 24 | |||
| 25 | requires 'run', 'submit_packages', 'verify_environment'; | ||
| 26 | |||
| 27 | enum 'PkgForgeErrorPolicy' => [qw/immediate retry/]; | ||
| 28 | |||
| 29 | Readonly my %SOURCE_TYPES => | ||
| 30 | map { $_ => 1 } PkgForge::SourceUtils::list_source_types(); | ||
| 31 | |||
| 32 | subtype 'PkgForgeSourceType' | ||
| 33 | => as Str, | ||
| 34 | => where { exists $SOURCE_TYPES{$_} }; | ||
| 35 | |||
| 36 | subtype 'PkgForgeSourceTypeList' | ||
| 37 | => as 'ArrayRef[PkgForgeSourceType]'; | ||
| 38 | |||
| 39 | coerce 'PkgForgeSourceTypeList' | ||
| 40 | => from Str | ||
| 41 | => via { [$_] }; | ||
| 42 | |||
| 43 | has 'name' => ( | ||
| 44 | is => 'ro', | ||
| 45 | isa => Str, | ||
| 46 | required => 0, | ||
| 47 | builder => 'build_name', | ||
| 48 | lazy => 1, | ||
| 49 | documentation => 'The name of the platform', | ||
| 50 | ); | ||
| 51 | |||
| 52 | has 'platform' => ( | ||
| 53 | is => 'ro', | ||
| 54 | isa => Str, | ||
| 55 | required => 1, | ||
| 56 | documentation => 'The platform to build on', | ||
| 57 | ); | ||
| 58 | |||
| 59 | has 'architecture' => ( | ||
| 60 | is => 'ro', | ||
| 61 | isa => Str, | ||
| 62 | required => 0, | ||
| 63 | predicate => 'has_architecture', | ||
| 64 | documentation => 'The architecture to build on', | ||
| 65 | ); | ||
| 66 | |||
| 67 | has 'timeout' => ( | ||
| 68 | is => 'ro', | ||
| 69 | isa => Int, | ||
| 70 | default => 600, # 10 minutes | ||
| 71 | documentation => 'Time after which a build job should be killed', | ||
| 72 | ); | ||
| 73 | |||
| 74 | has 'accepts' => ( | ||
| 75 | traits => ['Array'], | ||
| 76 | is => 'ro', | ||
| 77 | isa => 'PkgForgeSourceTypeList', | ||
| 78 | required => 1, | ||
| 79 | coerce => 1, | ||
| 80 | handles => { | ||
| 81 | list_acceptable_types => 'elements', | ||
| 82 | }, | ||
| 83 | documentation => 'The type of source packages accepted by the builder', | ||
| 84 | ); | ||
| 85 | |||
| 86 | has 'error_policy' => ( | ||
| 87 | is => 'ro', | ||
| 88 | isa => 'PkgForgeErrorPolicy', | ||
| 89 | default => 'retry', | ||
| 90 | documentation => 'Response type on package build failure', | ||
| 91 | ); | ||
| 92 | |||
| 93 | has 'debug' => ( | ||
| 94 | is => 'rw', | ||
| 95 | isa => Bool, | ||
| 96 | default => 0, | ||
| 97 | documentation => 'Turn on the printing of debug statements', | ||
| 98 | ); | ||
| 99 | |||
| 100 | has 'logdir' => ( | ||
| 101 | is => 'ro', | ||
| 102 | isa => Str, | ||
| 103 | required => 1, | ||
| 104 | documentation => 'The directory for log files', | ||
| 105 | ); | ||
| 106 | |||
| 107 | has 'resultsdir' => ( | ||
| 108 | is => 'ro', | ||
| 109 | isa => Str, | ||
| 110 | required => 1, | ||
| 111 | documentation => 'The directory for build job results', | ||
| 112 | ); | ||
| 113 | |||
| 114 | subtype 'FileTempDir' => as class_type('File::Temp::Dir'); | ||
| 115 | |||
| 116 | has 'tmpdir' => ( | ||
| 117 | is => 'ro', | ||
| 118 | isa => 'Str|FileTempDir', | ||
| 119 | required => 1, | ||
| 120 | default => sub { | ||
| 121 | return File::Temp->newdir( 'pkgforge-XXXXX', | ||
| 122 | TMPDIR => 1, | ||
| 123 | CLEANUP => 1 ); | ||
| 124 | }, | ||
| 125 | documentation => 'Temporary directory' | ||
| 126 | ); | ||
| 127 | |||
| 128 | no Moose::Role; | ||
| 129 | |||
| 130 | sub stringify { | ||
| 131 | my ($self) = @_; | ||
| 132 | return $self->name; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub build_name { | ||
| 136 | my ($self) = @_; | ||
| 137 | |||
| 138 | my $name; | ||
| 139 | if ( $self->has_architecture ) { | ||
| 140 | $name = join q{-}, $self->platform, $self->architecture; | ||
| 141 | } else { | ||
| 142 | $name = $self->platform; | ||
| 143 | } | ||
| 144 | |||
| 145 | return $name; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub filter_sources { | ||
| 149 | my ( $self, $job ) = @_; | ||
| 150 | |||
| 151 | my @sources; | ||
| 152 | for my $type ($self->list_acceptable_types) { | ||
| 153 | my @matches = $job->filter_packages( sub { $_->type eq $type } ); | ||
| 154 | push @sources, @matches; | ||
| 155 | } | ||
| 156 | |||
| 157 | return @sources; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub new_topic { | ||
| 161 | my ( $self, $job ) = @_; | ||
| 162 | |||
| 163 | my $job_resultsdir = PkgForge::Utils::job_resultsdir( $self->resultsdir, | ||
| 164 | $job->id ); | ||
| 165 | |||
| 166 | my $topic_resultsdir = File::Spec->catdir( $job_resultsdir, $self->name ); | ||
| 167 | |||
| 168 | if ( !-d $topic_resultsdir ) { | ||
| 169 | my $ok = eval { File::Path::mkpath($topic_resultsdir) }; | ||
| 170 | if ( !$ok || $EVAL_ERROR ) { | ||
| 171 | die "Failed to create directory $topic_resultsdir: $EVAL_ERROR\n"; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | my $topic_logdir | ||
| 176 | = File::Spec->catdir( $self->logdir, $self->name, $job->id ); | ||
| 177 | |||
| 178 | if ( !-d $topic_logdir ) { | ||
| 179 | my $ok = eval { File::Path::mkpath($topic_logdir) }; | ||
| 180 | if ( !$ok || $EVAL_ERROR ) { | ||
| 181 | die "Failed to create directory $topic_logdir: $EVAL_ERROR\n"; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | my @sources = $self->filter_sources($job); | ||
| 186 | |||
| 187 | my $topic = PkgForge::BuildTopic->new( job => $job, | ||
| 188 | resultsdir => $topic_resultsdir, | ||
| 189 | logdir => $topic_logdir, | ||
| 190 | debug => $self->debug, | ||
| 191 | sources => [@sources] ); | ||
| 192 | |||
| 193 | return $topic; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub build { | ||
| 197 | my ( $self, $job ) = @_; | ||
| 198 | |||
| 199 | my $topic = $self->new_topic($job); | ||
| 200 | |||
| 201 | my $run_result = eval { $self->run($topic) }; | ||
| 202 | if ( !$run_result || $EVAL_ERROR ) { | ||
| 203 | $topic->finish(); | ||
| 204 | if ($EVAL_ERROR) { | ||
| 205 | die $EVAL_ERROR; # rethrow | ||
| 206 | } else { | ||
| 207 | return 0; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | my $submit_result = eval { $self->submit_packages($topic) }; | ||
| 212 | if ( !$submit_result || $EVAL_ERROR ) { | ||
| 213 | $topic->finish(); | ||
| 214 | if ($EVAL_ERROR) { | ||
| 215 | die $EVAL_ERROR; # rethrow | ||
| 216 | } else { | ||
| 217 | return 0; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | $topic->finish(); | ||
| 222 | |||
| 223 | return 1; | ||
| 224 | } | ||
| 225 | |||
| 226 | 1; | ||
| 227 | __END__ |