source: main/trunk/gs-release-builder/envi/etc/tasks/snapshot/lib.pl@ 36214

Last change on this file since 36214 was 36214, checked in by kjdon, 4 months ago

initial version of snapshot task. copied from bedrock. based on other-projects/nightly-tasks/snapshot. which I would like to get rid of eventually, and just have it living in here

File size: 11.5 KB
Line 
1my $sep = $^O eq "MSWin32" ? "\\" : "/";
2
3sub get_date {
4 local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
5 local $day = $mday;
6 local $month = $mon+1;
7 local $year = $year+1900;
8 if ( length $month == 1 ) {
9 $month = join( "", "0", $month);
10 }
11 if ( length $day== 1 ) {
12 $day = join( "", "0", $day );
13 }
14 local $date = join( ".", $year,$month,$day );
15 return $date;
16}
17
18sub gen_snapshot_id {
19 if ( exists $_[0] && exists $_[1] ) {
20 return $_[0] . get_date() . $_[1];
21 }
22
23 if ( exists $_[0] ) {
24 return $_[0] . get_date();
25 }
26 return get_date();
27}
28
29sub create_release {
30
31 die "Must provide parameter to create_release\n" unless (exists $_[0]);
32
33 my $release_folder_name = $_[0];
34
35 my $release_version = $release_folder_name;
36 $release_version =~ s/^gs-?//;
37 $release_version =~ s/rc\d$//;
38 my $major_version = $release_version;
39 $major_version =~ s/^(3|2).*/$1/;
40 $release_version =~ s/^($major_version)\.?(\d*).*/$1.$2/;
41 my $rk = "rk".$major_version;
42
43 my $release_version_extra;
44 if ($release_folder_name =~ m/(rc\d)$/) {
45 $release_version_extra = $1;
46 }
47 if ($release_folder_name !~ m/^gs/) { # prefix gs
48 $release_folder_name = "gs".$release_folder_name;
49 }
50 my $release_dir = "$ENV{'DATA_DIR'}${sep}$release_folder_name";
51
52 print "major_version: $major_version\n";
53 print "version: $release_version\n";
54 print "versionextra: $release_version_extra\n";
55 print "release_folder_name: $release_folder_name\n";
56 print "release_dir: $release_dir\n";
57
58 print "about to clean up old snapshots (Ctrl-C to cancel)";
59 local $| = 1;
60 for ( my $i=0; $i<5; $i++ ) {
61 print ".";
62 sleep 1;
63 }
64 $| = 0;
65
66 print "cleaning up previous release snapshot $release_dir\n";
67
68 if(-d $release_dir) {
69 if ( $^O eq "MSWin32" ) {
70 system("rd /q /s \"$release_dir\"");
71 } else {
72 system("rm -rf \"$release_dir\"");
73 }
74 }
75
76 print "creating the release dir\n";
77 mkdir $release_dir or die "couldn't create release directory\n";
78
79 print "changing to the release dir\n";
80 chdir $release_dir;
81
82 #version property
83 print "setting up todays properties\n";
84 `echo version:$release_version> $rk-build.properties`;
85
86 if($release_version_extra) {
87 `echo version-extra:$release_version_extra>> $rk-build.properties`;
88 }
89
90 #processor propertylocal $| = 1;
91 if ( $^O eq "darwin" ) {
92 print "setting processor\n";
93 if ( `uname -p` eq "i386" ) {
94 `echo processor:intel>> $rk-build.properties`;
95 } elsif ( `uname -p` eq "powerpc" ) {
96 `echo processor:ppc>> $rk-build.properties`;
97 } else {
98 print "unable to determine processor type, using intel\n";
99 `echo processor:intel>> $rk-build.properties`;
100 }
101 } elsif ( $^O eq "linux" ) {
102 if(`uname -m` =~ m/64$/) {
103 print "Setting linux architecture to 64 bit";
104 `echo x64:true>> $rk-build.properties`;
105 }
106 }
107
108 #branch path property
109 if ( $ENV{'branch_path'} ) {
110 `echo branch.path:$ENV{'branch_path'}>> $rk-build.properties`;
111 }
112
113 #server.exe.location
114 if ( $major_version eq "2" && exists $ENV{'SERVER_EXE_LOCATION'} ) {
115 `echo server.exe.location:$ENV{'SERVER_EXE_LOCATION'}>> $rk-build.properties`;
116 }
117
118 print "creating the snapshot using $rk\n";
119 system( $rk );
120}
121
122sub create {
123
124 die "release_dir not set, cant create\n" unless $release_dir;
125
126 print "about to clean up old snapshots (Ctrl-C to cancel)";
127 local $| = 1;
128 for ( my $i=0; $i<5; $i++ ) {
129 print ".";
130 sleep 1;
131 }
132 $| = 0;
133
134 print "cleaning up previous snapshot\n";
135 local $release_parent = dirname($release_dir);
136 if ( $^O eq "MSWin32" ) {
137 system("rd /q /s \"$release_parent\"");
138 } else {
139 system("rm -rf \"$release_parent\"");
140 }
141
142 print "creating the release dir\n";
143 mkdir $release_parent or die "couldn't create release parent directory\n";
144 mkdir $release_dir or die "couldn't create release directory\n";
145
146 print "changing to the release dir\n";
147 chdir $release_dir;
148
149 #version property
150 print "setting up todays properties\n";
151 `echo version:$snapshot_id> $rk-build.properties`;
152
153 #processor propertylocal $| = 1;
154 if ( $^O eq "darwin" ) {
155 print "setting processor\n";
156 if ( `uname -p` eq "i386" ) {
157 `echo processor:intel>> $rk-build.properties`;
158 } elsif ( `uname -p` eq "powerpc" ) {
159 `echo processor:ppc>> $rk-build.properties`;
160 } else {
161 print "unable to determine processor type, using intel\n";
162 `echo processor:intel>> $rk-build.properties`;
163 }
164 }
165
166 #branch path property
167 if ( $ENV{'branch_path'} ) {
168 `echo branch.path:$ENV{'branch_path'}>> $rk-build.properties`;
169 }
170
171 #x64 bit property
172 if ( $ENV{'x64'} ) {
173 `echo x64:true>> $rk-build.properties`;
174 }
175
176 #server.exe.location
177 if ( exists $ENV{'SERVER_EXE_LOCATION'} ) {
178 `echo server.exe.location:$ENV{'SERVER_EXE_LOCATION'}>> $rk-build.properties`;
179 }
180
181 print "creating the snapshot using $rk\n";
182 system( $rk );
183
184}
185
186sub upload {
187 print "preparing files for uploading\n";
188
189 my @munges = ();
190 if ( exists $ENV{'munges'} ) {
191 @munges = split(' ', $ENV{'munges'});
192 }
193
194 #copy products to a temporary folder, giving them their new names
195 if ( -d "$release_dir${sep}uploads" ) {
196 system( "rm -rf '$release_dir${sep}uploads'" );
197 }
198 mkdir "$release_dir${sep}uploads";
199
200 my @files;
201 if ( -d "$release_dir${sep}products" ) {
202 @files = <$release_dir${sep}products${sep}*>;
203 }
204 push( @files, "$release_dir${sep}$rk.out" );
205
206 for my $file ( @files ) {
207 if ( -e $file ) {
208 my $filename = basename($file);
209 #munge
210 for my $m ( @munges ) {
211 $doit="\$filename =~ $m"; eval "$doit";
212 }
213 #upload
214 print "Will upload '" . basename($file) . "' to '$filename'\n";
215 if( $^O =~ "linux|darwin" ) {
216 system("cp \"$file\" \"${release_dir}${sep}uploads${sep}$filename\"");
217 }
218 else {
219 system("copy \"$file\" \"${release_dir}${sep}uploads${sep}$filename\"");
220 }
221 }
222
223 }
224
225# puka is no longer
226# my $command = "cd \"${release_dir}${sep}uploads\" && tar -c * | ";
227# $command .= ($^O eq "MSWin32" ? "plink" : "ssh");
228# $command .= " -T -i \"$ENV{'IDENTITY_FILE'}\" nzdl\@puka.cs.waikato.ac.nz";
229 #print "$command\n";
230# system("$command");
231
232 # ssh too old inside lsb to upload to www-internal, so we do that in a separate step later
233# $command = "cd \"${release_dir}${sep}uploads\" && tar -c * | ";
234# $command .= ($^O eq "MSWin32" ? "plink" : "ssh");
235# $command .= " -T -i \"$ENV{'IDENTITY_FILE'}\" nzdl-gsorg\@www-internal.greenstone.org";
236
237 #print "$command\n";
238# system("$command");
239}
240
241# EMAILING DOESN'T WORK, as SMTP not setup on release-kit LSB machines
242
243#sub xsend_mail_on_releasekit_fail {
244
245 # first, create your message
246 # use Email::MIME;
247# my $message = Email::MIME->create(
248# header_str => [
249# From => $ENV{'MONITOR_EMAIL'},
250# To => $ENV{'MONITOR_EMAIL'},
251# Subject => 'Trial message',
252# ],
253# attributes => {
254# encoding => 'quoted-printable',
255# charset => 'ISO-8859-1',
256# },
257# body_str => "Email test!\n",
258# );
259
260# # send the message
261# use Email::Sender::Simple qw(sendmail);
262# sendmail($message);
263
264 # %mail = ( To => $ENV{'MONITOR_EMAIL'},
265# From => $ENV{'MONITOR_EMAIL'},
266# Message => "This is a test message"
267# );
268
269# sendmail(%mail) or die $Mail::Sendmail::error;
270
271# print "OK. Log says:\n", $Mail::Sendmail::log;
272#}
273
274# EMAILING DOESN'T WORK, as SMTP not setup on release-kit LSB machines
275
276# Copied from diffcol's task.pl into here
277# Sending emails with perl: http://learn.perl.org/examples/email.html
278# Sending email attachments with perl: http://www.perlmonks.org/?node_id=19430
279# Sadly none of the packages are installed by default and use of MIME::Lite is discouraged
280sub send_mail_on_releasekit_fail
281{
282 # email greenstone_team if build failed
283
284 my $logfile="$release_dir${sep}$rk.out";
285 my $finalLinesLogOutput=`tail -n 50 $logfile`;
286
287 print STDERR "Checking if successful... \n";
288 # using reverse index to search from bottom of tail output
289 my $had_error = 0;
290
291 if (rindex($finalLinesLogOutput, "BUILD FAILED") != -1) {
292 # release-kit failed to build binary
293 $had_error = 1;
294 }
295 elsif (rindex($finalLinesLogOutput, "BUILD SUCCESSFUL") != -1) {
296 # SUCCESS!
297 $had_error = 0;
298 }
299 else {
300 # saw neither BUILD FAILED nor BUILD SUCCESSFUL in tail of log output
301 # Could be that building never completed, still want to be notified
302 $had_error = 2;
303 }
304
305
306 if(!$had_error) {
307 # everything fine, no need to email
308 return;
309 }
310
311
312 print STDERR "Build had error code: $had_error\n";
313 # let's send the last 200 lines of the log file to the user as email
314 #
315 $finalLinesLogOutput=`tail -n 200 $logfile`;
316
317 my $msg = "Last 200 lines in the log:\n$finalLinesLogOutput";
318 my $subject = "Release-kit $release_dir failed"; # mentions OS, bitness, date
319
320 # let's attach logfile besides
321
322 if($isWin) {
323 if($use_blat && $blat && $ENV{'GSDL_SMTP'}) {
324 # http://stackoverflow.com/questions/709635/sending-mail-from-batch-file
325 #blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body"
326
327 # need to install blat on windows
328 $cmd = "$blat -to $ENV{'MONITOR_EMAIL'} -server $ENV{'GSDL_SMTP'} -f $ENV{'MONITOR_EMAIL'} -attach $logfile -subject \"$subject\" -body \"$msg\"";
329 $result = system($cmd);
330 }
331 else {
332 $result = 1; # status from running mail command is 0 if success, 1 if fail
333 print STDERR "********************************************\n";
334 if ($use_blat) {
335 print STDERR "Need blat and SMTP set to send mail attachment\n" ;
336 } else {
337 print STDERR "Not set up to send mail on Windows\n";
338 }
339 print STDERR "Inspect release-kit build log at: $log_file\n";
340 print STDERR "********************************************\n";
341 }
342 } else { # linux
343
344 # try using sendmail, since mutt is not installed on lsb
345 # https://vitux.com/three-ways-to-send-email-from-ubuntu-command-line/
346 # Sending attachment with sendmail is too involved, see
347 # https://unix.stackexchange.com/questions/223636/sendmail-attachment/223650
348 # https://askubuntu.com/questions/355823/sending-file-using-sendmail
349 # Will just put the contents of the rk log file in the body.
350
351 # read in all of logfile
352 # https://stackoverflow.com/questions/206661/what-is-the-best-way-to-slurp-a-file-into-a-string-in-perl
353 my $contents = do {local (@ARGV,$/) = $logfile; <>};
354 my $email_path = "$release_dir${sep}email.txt";
355 if (open(FOUT, '>:utf8', $email_path))
356 {
357 print FOUT "Subject: $subject";
358 if($contents) {
359 print FOUT $contents;
360 } else {
361 print FOUT "Empty release kit building log: $logfile\n";
362 }
363 close(FOUT);
364 $cmd = "sendmail $ENV{'MONITOR_EMAIL'} < $email_path";
365 $result = system($cmd);
366 }
367 else
368 {
369 print STDERR "WARNING: sendmail email attempt failed. Failed to open file for writing email msg:\n\t$email_path\n";
370
371 # try using mutt to send email
372 my $status = system("command -v mutt > /dev/null 2>&1;"); #better way of doing "which mutt"
373
374 if($status != 0) { # mutt doesn't exist, can't send attachments, so send simple email
375 $cmd="echo '$message' | mail -s '$subject' $ENV{'MONITOR_EMAIL'}";
376
377 print STDERR "********************************************\n";
378 print STDERR "No mutt installed, unable to mail attachment\n";
379 print STDERR "Inspect release-kit build log at: $logfile\n";
380 print STDERR "********************************************\n";
381 } else {
382 #$cmd = "bash -c \"echo '$message' | mutt -a $logfile -s 'subject' -- $ENV{'MONITOR_EMAIL'}\"";
383 $cmd = "echo '$message' | mutt -a $logfile -s '$subject' -- $ENV{'MONITOR_EMAIL'}";
384 }
385
386 # run the mail command
387 $result = system($cmd); #&run_and_print_cmd($cmd);
388 }
389 }
390
391
392 if($result != 0) {
393 print STDERR "*** Unable to send email: $?\n";
394 }
395 else {
396 print STDERR "Sent mail with $logfile attached.\n";
397 }
398
399}
Note: See TracBrowser for help on using the repository browser.