forked from perftool-incubator/rickshaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rickshaw-run
executable file
·3365 lines (3093 loc) · 154 KB
/
rickshaw-run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# -*- mode: perl; indent-tabs-mode: nil; perl-indent-level: 4 -*-
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=perl
#
# Author: Andrew Theurer
#
# Rickshaw will run a benhcmark for you. Please see README.md for instructions.
use strict;
use warnings;
use Cwd;
use Data::UUID;
use File::pushd;
use File::Basename;
use File::Temp qw(tempdir);
use File::Copy;
use File::Path qw(make_path);
use JSON::XS;
use JSON::Validator;
use Data::Dumper;
use threads;
use threads::shared;
use Thread::Queue;
use Thread::Semaphore;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Pair = ' : ';
$Data::Dumper::Useqq = 1;
$Data::Dumper::Indent = 3;
BEGIN {
if (!(exists $ENV{'TOOLBOX_HOME'} && -d "$ENV{'TOOLBOX_HOME'}/perl")) {
print "This script requires libraries that are provided by the toolbox project.\n";
print "Toolbox can be acquired from https://github.com/perftool-incubator/toolbox and\n";
print "then use 'export TOOLBOX_HOME=/path/to/toolbox' so that it can be located.\n";
exit 1;
}
}
use lib "$ENV{'TOOLBOX_HOME'}/perl";
use toolbox::json;
use toolbox::logging;
use toolbox::run;
use toolbox::jsonsettings;
$toolbox::logging::debug = 0;
my $ug = Data::UUID->new;
my %defaults = ( "num-samples" => 1, "tool-group" => "default", "test-order" => "s",
"base-run-dir" => tempdir(), "id" => $ug->create_str(),
"max-sample-failures" => 1, "max-rb-attempts" => 1,
"run-file" => "");
my @utilities = ( "packrat" );
my $jsonsettings;
my $registries_settings;
my $use_workshop = 0;
my %bench_configs;
my %bench_dirs;
my %benchmark_to_ids;
my %ids_to_benchmark;
my @endpoints;
#my %userenvs;
my %image_ids; # {$benchmark-or-tool}{$userenv}
my %run; # A multi-dimensional, nested hash, schema TBD
# This hash documents what was run.
my $redis_passwd = "flubber"; # TODO: make this cmdline setting
my $rb_bin = "roadblocker.py";
my $rb_module = "roadblock.py";
my $messages_ref;
my $default_rb_timeout;
my $collect_sysinfo_timeout;
my $endpoint_deploy_timeout;
my $engine_script_start_timeout;
my $endpoint_move_data_rb_timeout;
my $base_rb_leader_cmd = "--role=leader --redis-server=localhost --redis-password=" . $redis_passwd;
my $config_dir;
my $engine_config_dir;
my $engine_bench_cmds_dir;
my $tool_cmds_dir;
my $run_dir;
my $workshop_build_dir;
my $base_endpoint_run_dir;
my $engine_run_dir;
my $engine_logs_dir;
my $engine_archives_dir;
my $engine_run_script;
my $engine_library_script;
my $engine_roadblock_script;
my $engine_roadblock_module;
my $iterations_dir;
my $rickshaw_project_dir;
my $roadblock_msgs_dir;
my $roadblock_logs_dir;
my $roadblock_followers_dir;
my $endpoint_roadblock_opt = "";
my $workshop_roadblock_opt = "";
my %utility_configs;
my %tools_configs;
my @tools_params;
my $default_tool_userenv;
{
# Get the absolute path of the rickshaw project directory
my $pushd_dir = pushd(dirname($0));
$rickshaw_project_dir = getcwd();
}
my $bench_schema_file = $rickshaw_project_dir . "/schema/benchmark.json";
my $tool_schema_file = $rickshaw_project_dir . "/schema/tool.json";
my $run_schema_file = $rickshaw_project_dir . "/schema/run.json";
my $utility_schema_file = $rickshaw_project_dir . "/schema/utility.json";
my $bench_params_schema_file = $rickshaw_project_dir . "/schema/bench-params.json";
my $tool_params_schema_file = $rickshaw_project_dir . "/schema/tool-params.json";
my $roadblock_exit_success = 0;
my $roadblock_exit_timeout = 3;
my $roadblock_exit_abort = 4;
my $roadblock_exit_input = 2;
my $roadblock_exit_abort_waiting = 6;
my $abort_via_roadblock = 0;
my $workshop_base_cmd;
my $workshop_force_builds;
my %workshop_built_tags;
my %quay_refresh_expiration_tokens;
my $cs_conf_file;
my %cs_conf;
my @tests;
my %clients_servers;
my @rb_cs_ids; # unique IDs for roadblock
my $abort_test_id;
my @active_followers;
(my $detect_arch_cmd, my $arch, my $detect_arch_cmd_rc) = run_cmd('uname -m');
chomp($arch);
my $available_cpus = 0;
open(PROCCPUINFO, "<", "/proc/cpuinfo") || die("[ERROR] Could not open /proc/cpuinfo for reading\n");
while(<PROCCPUINFO>) {
if ($_ =~ /^processor/) {
$available_cpus++;
}
}
close(PROCCPUINFO);
if ($available_cpus == 0) {
die("[ERROR] Did not find any available cpus for job processing!\n");
}
debug_log(sprintf "Found %d available cpus for job processing.\n", $available_cpus);
$SIG{'INT'} = sub {
print "Caught a CTRL-C/SIGINT, aborting via roadblock!\n";
$abort_via_roadblock = 1;
};
sub usage {
print "\nusage:\n\n";
print "--registries-json Path to a JSON file containing container registry information\n";
print "--external-userenvs-dir Path to look for userenv definitions in that are provided by external sources\n";
print "--json-validator Path to json schema validation utility\n";
print "--engine-dir Directory where the engine project exists\n";
print "--workshop-dir Directory where workshop project exists\n";
print "--packrat-dir Directory where the packrat project exists\n";
print "--roadblock-dir Directory where workshop project exists\n";
print "--bench-dir Directory where benchmark helper project exists\n";
print "--bench-params File with benchmark parameters to use\n";
print "--tools-dir Directory where *all* tool subprojects exist (like \$CRUCIBLE_HOME/subprojects/tools)\n";
print "--tool-params File with tool parameters to use\n";
print "--num-samples The number of sample exeuctions to run for each benchmark iteration\n";
print "--max-sample-failures The total number of benchmark sample executions that are tolerated\n";
print "--test-order 's' = run all samples of an iteration first\n";
print " 'i' = run all iterations of a sample first\n";
print " 'r' = run a sample from a random iteration one at a time (ie. chaos mode)\n\n";
print "--max-rb-attempts The number of times to try a given roadblock\n";
}
sub find_index {
my $arr_ref = shift;
my $field = shift;
my $value = shift;
for (my $index = 0; $index < scalar @$arr_ref; $index++) {
if (exists $$arr_ref[$index]{$field} and $$arr_ref[$index]{$field} eq $value) {
#printf "found field: [%s] with value: [%s] at index [%d]\n", $field, $value, $index;
return $index;
}
}
# index not found
return -1;
}
sub find_files {
my $path = shift;
my @files;
if (-d $path) {
opendir(DH, $path);
my @entries = readdir(DH);
close(DH);
foreach my $entry (@entries) {
my $entry_path = $path . '/' . $entry;
if (($entry =~ /^\.$/) ||
($entry =~ /^\.\.$/) ||
($entry =~ /^\.git$/) ||
($entry =~ /^docs$/) ||
($entry =~ /\.md$/) ||
($entry =~ /^__pycache__$/) ||
($entry =~ /^\.github$/)) {
next;
}
if (-d $entry_path) {
push(@files, @{find_files($entry_path)})
} elsif (-e $entry_path) {
push (@files, $entry_path);
}
}
} elsif (-e $path) {
push (@files, $path);
}
return(\@files);
}
sub do_roadblock {
my $label = shift;
my $timeout = shift;
# $_[0] is for a reference to the messages data structure
my $rb_followers_file = $roadblock_followers_dir . "/" . $label . ".txt";
open(RB_FOLLOWERS, ">", $rb_followers_file) || die("[ERROR] Could not open the roadblock followers file for writing [" . $rb_followers_file . "]!\n");
for (my $i=1; $i<scalar(@_); $i++) {
printf RB_FOLLOWERS "%s\n", $_[$i];
}
close(RB_FOLLOWERS);
my $attempts = 0;
my $rc = 99;
my $file_rc;
my $output;
my $role = "leader";
my $uuid = $run{'id'} . ":" . $label;
my $msgs_log_file = $roadblock_msgs_dir . "/" . $label . ".json";
my $rb_log_file = $roadblock_logs_dir . "/" . $label . ".txt";
(my $date_cmd, my $date, my $date_rc) = run_cmd('date');
chomp $date;
printf "Roadblock: %s ", $date;
while ($attempts < $run{'max-rb-attempts'} and $rc != $roadblock_exit_success and $rc != $roadblock_exit_abort and $rc != $roadblock_exit_abort_waiting) {
$attempts++;
my $this_uuid = $attempts . ":" . $uuid;
printf "role: %s ", $role;
printf "attempt number: %d ", $attempts;
printf "uuid: %s\n", $this_uuid;
my $cmd = $base_rb_leader_cmd .
" --leader-id=controller" .
" --uuid=" . $this_uuid .
" --timeout=" . $timeout .
" --message-log=" . $msgs_log_file .
" --followers-file=" . $rb_followers_file;
if ($abort_via_roadblock) {
$cmd .= " --abort";
}
debug_log(sprintf "roadblock leader command:%s\n", $cmd);
($cmd, $output, $rc) = run_cmd($cmd);
debug_log(sprintf "roadblock leader rc:%s\n", $rc);
debug_log(sprintf "roadblock leader output:\n%s\n", $output);
my $rb_log_fh = open_write_text_file($rb_log_file) ||
die "[ERROR]could not open roadblock log file [" . $rb_log_file . "] for writing\n";
printf $rb_log_fh "%s", $output;
close($rb_log_fh);
($file_rc, $_[0]) = get_json_file($msgs_log_file);
if ($file_rc > 0 or ! defined $_[0]) {
printf "Could not open the messages log file: %s\n", $msgs_log_file;
exit 1;
}
if ( $rc != $roadblock_exit_success) {
printf "roadblock output BEGIN\n";
printf "%s", $output;
printf "roadblock rc: %d\n", $rc;
printf "roadblock output END\n";
}
if ( $rc == $roadblock_exit_abort or $rc == $roadblock_exit_abort_waiting ) {
printf "roadblock messages\n";
foreach my $msg (@{ $_[0]{'received'} }) {
if (exists $$msg{'payload'}{'message'}{'user-object'} and exists $$msg{'payload'}{'message'}{'user-object'}{'error'}) {
printf "\nError from %s:\n%s\n\n", $$msg{'payload'}{'sender'}{'id'}, $$msg{'payload'}{'message'}{'user-object'}{'error'};
}
}
}
}
if ($rc == $roadblock_exit_abort or $rc == $roadblock_exit_success or $rc == $roadblock_exit_abort_waiting ) {
($file_rc, $_[0]) = get_json_file($msgs_log_file);
if ($file_rc > 0 or ! defined $_[0]) {
printf "Could not open the messages log file on abort/exit: %s\n". $msgs_log_file;
exit 1;
}
return $rc;
} else {
my @dropped_followers = ();
foreach my $line (split(/\n/, $output)) {
if ($line =~ /These followers/) {
my @line_pieces = split(/: /, $line);
push @dropped_followers, split(/\s/, $line_pieces[1]);
}
}
debug_log(sprintf "roadblock dropped followers: %s\n", join(" ", @dropped_followers));
return $rc, @dropped_followers;
}
}
sub dump_params {
my $default_role = 'client';
my $params_ref = shift;
my $cs_id = shift;
my $engine = shift // $default_role;
my $params_str = "";
my $benchmark;
if (defined $cs_id) {
$benchmark = $ids_to_benchmark{$cs_id};
}
foreach my $param (@{ $params_ref }) {
my $arg = $$param{'arg'};
my $val = $$param{'val'};
my $bench = $$param{'benchmark'};
my $id;
if (exists $$param{'id'}) {
$id = $$param{'id'};
}
# fallback to client role when role is undefined in json
my $role = $$param{'role'} // $default_role;
if (! defined $id or (defined $cs_id and $id eq $cs_id)) {
if (defined $benchmark and $bench eq $benchmark) {
# only dump when role=engine or role=all
if ( $role eq $engine || $role eq 'all') {
if (defined $val && length $val) {
if (defined $cs_id) {
$val =~ s/\%client-id\%/$cs_id/;
}
$params_str .= " --" . $arg . "=" . $val;
} else {
$params_str .= " --" . $arg;
}
}
}
}
}
$params_str =~ s/^\s//;
return $params_str;
}
sub file_newer_than {
my $file = shift;
my $epoch_sec = shift;
{
(my $cmd, my $file_time, my $cmd_rc) = run_cmd("/bin/ls -l --time-style=+%s $file");
chomp($file_time);
# -rwxrwxr-x. 1 someuser somegroup 4656 1582742663 engine-script
if ($file_time =~ /\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(\d+)\s+.*/) {
if ($1 > $epoch_sec) {
return 1;
}
}
}
return 0;
}
sub add_endpoint {
my $endpoint_ref = shift;
my $type = shift;
my $opts = shift;
my $num = 1;
for my $entry (@$endpoint_ref) {
$num++ if ($$entry{'type'} eq $type);
}
my %endpoint = ( 'type' => $type, 'opts' => $opts, 'label' => $type . "-" . $num);
push(@$endpoint_ref, \%endpoint);
}
sub dump_endpoint_types {
my $endpoint_ref = shift;
my @labels;
foreach my $endpoint (@$endpoint_ref) {
push(@labels, $$endpoint{'type'})
}
return @labels;
}
sub dump_endpoint_labels {
my $endpoint_ref = shift;
my @labels;
foreach my $endpoint (@$endpoint_ref) {
push(@labels, $$endpoint{'label'})
}
return @labels;
}
sub dir_entries {
my $dir = shift;
my $pattern = shift;
my @entries;
if (! -e $dir) {
die "The directory does not exist: $dir";
}
opendir(DH, $dir);
@entries = readdir(DH);
if (defined $pattern) {
@entries = grep(/$pattern/, @entries);
}
close DH;
return @entries;
}
sub calc_image_md5 {
my $workshop_base_cmd = shift;
my $userenv_arg = shift;
die "calc_image_md5(): \$userenv_arg must be defined" if (!defined $userenv_arg);
my $req_args = shift;
my $arch_suffix = shift;
my $userenv = shift;
my $benchmark_tool = shift;
my $stage = shift;
debug_log(sprintf "calc_image_md5(): userenv=%s benchmark/tool=%s stage=%d\n", $userenv, $benchmark_tool, $stage);
my $workshop_sub_cmd;
if (defined $req_args) {
$workshop_sub_cmd = $workshop_base_cmd . " " . $userenv_arg . " " . $req_args;
} else {
$workshop_sub_cmd = $workshop_base_cmd . " " . $userenv_arg;
}
my $workshop_config_cmd = $workshop_sub_cmd . " --label config-analysis --dump-config true";
my $workshop_files_cmd = $workshop_sub_cmd . " --label files-listing --dump-files true";
debug_log(sprintf "calc_image_md5(): workshop dump-config cmd: %s\n", $workshop_config_cmd);
(my $cmd, my $cmd_output, my $cmd_rc) = run_cmd($workshop_config_cmd);
my @config_analysis_output = split(/\n/, $cmd_output);
if ($cmd_rc > 0) {
printf "Workshop dump config failed:\n";
printf "%s\n", join("\n", @config_analysis_output);
exit 1;
}
my $break_line = 0;
for (my $i=0; $i<scalar(@config_analysis_output); $i++) {
if ($config_analysis_output[$i] =~ /Config dump:/) {
$break_line = $i;
}
}
splice(@config_analysis_output, 0, $break_line+1);
debug_log("calc_image_md5(): workshop dump-config output:\n" . Dumper \@config_analysis_output);
# The second part of the hash input is the *content* of certain files
# Start with workshop code and schema, which when changed will
# trigger new builds.
my @files = ($run{'workshop-dir'} . "/workshop.pl", $run{'workshop-dir'} . "/schema.json");
# Add to the file list all the files that workshop identifies
# as being copied into the userenv
debug_log(sprintf "calc_image_md5(): workshop dump-files cmd: %s\n", $workshop_files_cmd);
($cmd, $cmd_output, $cmd_rc) = run_cmd($workshop_files_cmd);
my @files_dump_output = split(/\n/, $cmd_output);
if ($cmd_rc > 0) {
printf "Workshop dump files failed:\n";
printf "%s\n", join("\n", @files_dump_output);
exit 1;
}
$break_line = 0;
for (my $i=0; $i<scalar(@files_dump_output); $i++) {
if ($files_dump_output[$i] =~ /Files dump:/) {
$break_line = $i
}
}
splice(@files_dump_output, 0, $break_line+1);
debug_log("calc_image_md5(): workshop dump-files output:\n" . Dumper \@files_dump_output);
foreach my $dumped_file (@files_dump_output) {
if ($dumped_file !~ /^\[VERBOSE\]|^replacing/) {
debug_log(sprintf "calc_image_md5(): found file from workshop [%s]\n", $dumped_file);
my $real_path = Cwd::realpath($dumped_file);
if ($real_path ne $dumped_file) {
debug_log(sprintf "calc_image_md5(): file from workshop [%s] is a link to [%s]\n", $dumped_file, $real_path);
$dumped_file = $real_path;
}
if (-f $dumped_file) {
push(@files, $dumped_file);
} elsif (-d $dumped_file) {
debug_log(sprintf "calc_image_md5(): file from workshop [%s] is actually a directory...\n", $dumped_file);
my @found_files = @{find_files($dumped_file)};
foreach my $found_file (@found_files) {
debug_log(sprintf "calc_image_md5(): found file [%s]\n", $found_file);
push(@files, $found_file);
}
}
}
}
my $tag_calc_data = $workshop_build_dir . "tag-calc-data__" . $userenv . "__" . $benchmark_tool . "__stage-" . $stage . ".txt";
debug_log(sprintf "calc_image_md5(): logging tag calculation data to %s\n", $tag_calc_data);
my $tag_fh = open_write_text_file($tag_calc_data) || die "Failed to open " . $tag_calc_data . " for writing\n";
# compute an md5 hash of relevant information to identify the
# userenv
my $md5 = Digest::MD5->new;
my $item_header = "# Item #########################################################################\n";
my $item;
# First is the Initial hash calc on workshop reqs
print $tag_fh $item_header . "Workshop Config Output:\n" . join("", @config_analysis_output) . "\n";
$md5->add(join("", @config_analysis_output));
# Second is the hashing contents of files
for my $file (sort @files) {
debug_log(sprintf "calc_image_md5(): adding '%s' to hash\n", $file);
print $tag_fh $item_header . "File: " . $file . "\nFile Contents:\n";
open(my $fh, $file);
while(<$fh>) {
print $tag_fh $_;
}
print $tag_fh "\n";
seek $fh, 0, 0;
binmode($fh);
$md5->addfile($fh);
close($fh);
}
my $base_hash = $md5->hexdigest;
my $full_hash = $base_hash . "_" . $arch_suffix;
print $tag_fh $item_header . "Hash: " . $full_hash . "\n";
close($tag_fh);
debug_log(sprintf "calc_image_md5(): returning '%s'\n", $full_hash);
return $full_hash;
}
sub remote_image_found {
my $image = shift;
my $registry_type = shift;
my $full_url;
if ($image =~ /:/) {
$full_url = $image;
} else {
$full_url = $run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'} . ":" . $image;
}
debug_log(sprintf "Checking for remote workshop image: %s...\n", $full_url);
my $skopeo_url;
if (($full_url =~ /^dir:/) || ($full_url =~ /^docker:\/\//)) {
$skopeo_url = $full_url;
} else {
$skopeo_url = "docker://" . $full_url;
}
my $skopeo_auth = "";
if ($registry_type eq "private") {
$skopeo_auth = "--authfile=" . $run{'registries'}{$registry_type}{'pull-token'};
}
my $cmd = "skopeo inspect " . $skopeo_auth . " --tls-verify=" . $run{'registries'}{$registry_type}{'tls-verify'} . " " . $skopeo_url . " 2>&1";
debug_log(sprintf "running: %s\n", $cmd);
($cmd, my $output, my $cmd_rc) = run_cmd($cmd);
if ($cmd_rc == 0) {
debug_log(sprintf "found\n");
return 1;
} else {
debug_log(sprintf "missing\n");
return 0;
}
}
sub local_image_found {
my $image = shift;
my $registry_type = shift;
my $full_url;
if ($image =~ /:/) {
$full_url = $image;
} else {
$full_url = $run{'registries'}{$registry_type}{'url-details'}{'source-image-url'} . ":" . $image;
}
debug_log(sprintf "Checking for local workshop image: %s...\n", $full_url);
my $cmd = "buildah images " . $full_url;
debug_log(sprintf "cmd:\n%s\n\n", $cmd);
($cmd, my $output, my $cmd_rc) = run_cmd($cmd);
debug_log(sprintf "output:\n%s\n\n", $output);
if ($cmd_rc == 0) {
debug_log(sprintf "found\n");
return 1;
} else {
debug_log(sprintf "missing\n");
debug_log(sprintf "All buildah images:\n");
my $cmd = "buildah images";
($cmd, my $output, my $cmd_rc) = run_cmd($cmd);
debug_log(sprintf "output:\n%s\n\n", $output);
return 0;
}
}
sub workshop_build_image {
my $userenv = shift;
my $registry_type = shift;
my $bench_or_tool = shift;
my $workshop_base_cmd = shift;
my $stage = shift;
my $userenv_arg = shift;
die "workshop_build_image(): userenv_arg must be defined\n" if !defined $userenv_arg;
my $req_args = shift;
my $tag = shift;
my $skip_update = shift;
if (!defined $skip_update) {
printf "skip_update was not defined, so setting to false\n";
$skip_update = "false";
}
my $proj;
if (defined $run{'registries'}{$registry_type}{'url-details'}{'project'}) {
$proj = $run{'registries'}{$registry_type}{'url-details'}{'host'} . "/" . $run{'registries'}{$registry_type}{'url-details'}{'project'};
} else {
$proj = $run{'registries'}{$registry_type}{'url-details'}{'host'};
}
my $workshop_build_cmd = $workshop_base_cmd
. " --skip-update " . $skip_update
. " " . $userenv_arg
. " " . $req_args
. " --proj " . $proj
. " --label " . $run{'registries'}{$registry_type}{'url-details'}{'label'}
. " --tag " . $tag;
debug_log(sprintf "Going to generate a new engine container image with this workshop cmd:\n\n %s\n", $workshop_build_cmd);
($workshop_build_cmd, my $workshop_output, my $workshop_rc) = run_cmd($workshop_build_cmd);
my @workshop_output = split(/\n/, $workshop_output);
my $workshop_output_file = $workshop_build_dir . $userenv . "__" . $bench_or_tool . "__stage-" . $stage . "." . $tag . ".stdout.txt";
my $fh = open_write_text_file($workshop_output_file) || die "Failed to open " . $workshop_output_file . " for writing\n";
printf $fh "%s\n", join("\n", @workshop_output);
close($fh);
if ($workshop_rc > 0) {
printf "Workshop build failed: rc=%d\n", $workshop_rc;
printf "Workshop build command: %s\n", $workshop_build_cmd;
printf "Workshop build output:\n";
printf "%s\n", join("\n", @workshop_output);
exit 1;
}
debug_log(sprintf "%s\n", join("\n", @workshop_output));
# Becasue there can be a lot of non-JSON debug/info stuff in the output before the
# actual JSON, find the JSON by starting at the end and scanning backwards
my $workshop_json = "";
for (my $i = scalar @workshop_output - 1; $i > 0; $i--) {
$workshop_json = $workshop_output[$i] . $workshop_json;
# Break out if we found all of the JSON
# In this case the JSON begins with an array '['
last if ($workshop_output[$i] eq "[");
}
my $coder = JSON::XS->new;
my $workshop_ref = $coder->decode($workshop_json);
my $workshop_image_id = $$workshop_ref[0]{'id'};
return $workshop_image_id;
}
sub delete_local_image {
my $image = shift;
my $registry_type = shift;
my $full_url;
if ($image =~ /:/) {
$full_url = $image;
} else {
$full_url = $run{'registries'}{$registry_type}{'url-details'}{'source-image-url'} . ":" . $image;
}
if (!local_image_found($image, $registry_type)) {
printf "ERROR: delete_local_image(): could not find local image [%s] before delete\n", $full_url;
printf "imgae: [%s]\n", $image;
printf "full_url: [%s]\n", $full_url;
printf "source-image-url: [%s]\n", $run{'source-image-url'};
exit 1;
}
debug_log(sprintf "Deleting local image %s\n", $full_url);
my $cmd = "buildah";
$cmd .= " rmi " . " " . $full_url;
($cmd, my $output, my $cmd_rc) = run_cmd($cmd);
if ($cmd_rc != 0) {
printf "ERROR: delete_local_image(): rmi command [%s] failed with %d\nOutput:\n%s\n\n",
$cmd, $cmd_rc, $output;
exit 1;
}
}
sub push_local_image {
my $image_tag = shift;
my $registry_type = shift;
my $full_src_url = $run{'registries'}{$registry_type}{'url-details'}{'source-image-url'} . ":" . $image_tag;
my $full_dest_url = $run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'} . ":" . $image_tag;
if ($full_dest_url =~ /^dir:/) {
$full_dest_url =~ /^dir:(.*)/;
my $image_dir = $1;
if (! -d $image_dir) {
printf "Creating local registry directory: %s\n", $image_dir;
make_path($image_dir, { verbose => 1, error => \my $err } );
if ($err && @$err) {
print "make_path: encountered errors:\n";
for my $diag (@$err) {
print Dumper $diag;
}
}
}
}
if (!local_image_found($image_tag, $registry_type)) {
die "ERROR: push_local_image(): could not find local image before push ($image_tag)";
}
my $cmd = "buildah";
if (exists $run{'registries'}{$registry_type}{'push-token'}) {
$cmd .= " --authfile " . $run{'registries'}{$registry_type}{'push-token'};
}
$cmd .= " push --tls-verify=" . $run{'registries'}{$registry_type}{'tls-verify'} . " " . $full_src_url . " " . $full_dest_url;
($cmd, my $output, my $cmd_rc) = run_cmd($cmd);
if ($cmd_rc != 0) {
printf "ERROR: push_local_image(): push command [%s] failed with %d\nOutput:\n%s\n\n",
$cmd, $cmd_rc, $output;
exit 1;
}
if (!remote_image_found($image_tag, $registry_type)) {
printf "WARNING: push_local_image(): failed to find remote image after push...retrying!\n";
my $found_it = 0;
for (my $i=1; $i<=20; $i++) {
sleep 3;
if (remote_image_found($image_tag, $registry_type)) {
$found_it = 1;
last;
printf "NOTICE: push_local_image(): found image on retry attempt number %d\n", $i;
} else {
printf "NOTICE: push_local_image(): failed to find image on retry attempt number %d\n", $i;
}
}
if (!$found_it) {
die "ERROR: push_local_image(): could not find remote image after push ($image_tag)";
}
}
}
sub build_reqs {
my $req_ref = shift;
my $userenv = shift;
my $benchmark = shift;
return if (!$use_workshop);
# Build an ordered list of requirements. What order? That depends.
# We want the first requirements to be ones that are most commonly
# used by many users, while also not being updated very often, followed
# by lesser common or requirments that have their content changed more
# frequently.
#
# If there is a requirment that is widely used and its contents might
# change frequently, we should consider sourcing this requirement
# after a container image is provisioned, but that generally only works
# if the contents of the requirment is of type "files". An example of
# this is the engine-script from the engine subdirectory and various
# scripts from benchmarks and tools..
#
# Why is this order important? We build container images incrementally,
# with the smallest one containing only the userenv, and build bigger and
# bigger images, each with a new requirement. Ultimately, a user would like
# to match an existing built image with all of their requirments, but if
# that does not exist, we want to match an imagewith as many requirements
# as possible and add only what we need.
# The most common requirment is expected to be the toolbox.
my $tb_req_file = $config_dir . "/toolbox-req.json";
my %tb_req = (
'workshop' => {
'schema' => {
'version' => '2020.03.02'
}
},
'userenvs' => [
{
'name' => 'default',
'requirements' => [
'toolbox'
]
}
],
'requirements' => [
{
'name' => 'toolbox',
'type' => 'files',
'files_info' => {
'files' => [
{
'src' => $ENV{'TOOLBOX_HOME'},
'dst' => '/opt/toolbox'
}
]
}
}
]
);
if (put_json_file($tb_req_file, \%tb_req) > 0) {
printf "build_container_image(): put_json_file() failed\n";
exit 1;
}
push (@$req_ref, "--requirement " . $tb_req_file);
# The second toolbox req ensures the proper dependencies are installed
push (@$req_ref, "--requirement " . $ENV{'TOOLBOX_HOME'} . "/workshop.json");
# ensure the proper python libraies are installed that roadblock needs
push (@$req_ref, "--requirement " . $run{'roadblock-dir'} . "/workshop.json");
push (@$req_ref, "--requirement " . $rickshaw_project_dir . "/engine/workshop.json");
foreach my $utility (@utilities) {
if (exists $run{$utility . '-dir'}) {
my $utility_req_file = $run{$utility . '-dir'} . "/workshop.json";
if (-e $utility_req_file) {
push (@$req_ref, "--requirement " . $utility_req_file);
}
}
}
push (@$req_ref, "--requirement " . $bench_dirs{$benchmark} . "/workshop.json");
}
sub decode_repo_urls {
my $registry_type = shift;
if (exists $run{'registries'}{$registry_type}{'url-details'}) {
printf "%s registry repository URL (%s) details have already been decoded\n", $registry_type, $run{'registries'}{$registry_type}{'repo'};
return;
} else {
printf "Decoding %s registry repository URL (%s) details\n", $registry_type, $run{'registries'}{$registry_type}{'repo'};
}
$run{'registries'}{$registry_type}{'url-details'} = ();
if ($run{'registries'}{$registry_type}{'repo'} =~ /^(\w+:\/){0,1}([^\/]+\/){0,1}([^\/]+\/){0,1}([^\/]+)$/) {
if (defined($1)) {
$run{'registries'}{$registry_type}{'url-details'}{'protocol'} = $1;
debug_log(sprintf "protocol: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'protocol'});
} else {
$run{'registries'}{$registry_type}{'url-details'}{'protocol'} = '';
}
if (defined($2)) {
$run{'registries'}{$registry_type}{'url-details'}{'host'} = $2;
if ($run{'registries'}{$registry_type}{'url-details'}{'host'} =~ /(\w+)(:\d+)/) {
$run{'registries'}{$registry_type}{'url-details'}{'host'} = $1;
$run{'registries'}{$registry_type}{'url-details'}{'host-port'} = $2;
debug_log(sprintf "host: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'host'});
debug_log(sprintf "host-port: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'host-port'});
} else {
$run{'registries'}{$registry_type}{'url-details'}{'host'} =~ s/\/$//;
$run{'registries'}{$registry_type}{'url-details'}{'host-port'} = '';
debug_log(sprintf "host: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'host'});
}
} else {
$run{'registries'}{$registry_type}{'url-details'}{'host'} = '';
}
if (defined($3)) {
$run{'registries'}{$registry_type}{'url-details'}{'project'} = $3;
$run{'registries'}{$registry_type}{'url-details'}{'project'} =~ s/\/$//;
debug_log(sprintf "project: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'project'});
}
if (! defined $run{'registries'}{$registry_type}{'url-details'}{'host'} and defined $run{'registries'}{$registry_type}{'url-details'}{'project'}) {
$run{'registries'}{$registry_type}{'url-details'}{'host'} = $run{'registries'}{$registry_type}{'url-details'}{'project'};
debug_log(sprintf "host: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'host'});
} elsif (defined $run{'registries'}{$registry_type}{'url-details'}{'host'} and ! defined $run{'registries'}{$registry_type}{'url-details'}{'project'}) {
$run{'registries'}{$registry_type}{'url-details'}{'project'} = $run{'registries'}{$registry_type}{'url-details'}{'host'};
debug_log(sprintf "project: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'project'});
} elsif (! defined $run{'registries'}{$registry_type}{'url-details'}{'host'} and ! defined $run{'registries'}{$registry_type}{'url-details'}{'project'}) {
die "At least one of the host or the project must be present in $run{'registries'}{$registry_type}{'repo'}";
}
$run{'registries'}{$registry_type}{'url-details'}{'source-image-url'} =
$run{'registries'}{$registry_type}{'url-details'}{'host'} . "/" . $run{'registries'}{$registry_type}{'url-details'}{'project'};
debug_log(sprintf "source-image-url: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'source-image-url'});
if (defined $run{'registries'}{$registry_type}{'url-details'}{'host-port'}) {
$run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'} =
$run{'registries'}{$registry_type}{'url-details'}{'host'} .
$run{'registries'}{$registry_type}{'url-details'}{'host-port'} . "/" .
$run{'registries'}{$registry_type}{'url-details'}{'project'};
} else {
$run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'} =
$run{'registries'}{$registry_type}{'url-details'}{'host'} . "/" .
$run{'registries'}{$registry_type}{'url-details'}{'project'};
}
debug_log(sprintf "dest-image-url: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'});
if (defined $run{'registries'}{$registry_type}{'url-details'}{'protocol'}) {
$run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'} =
$run{'registries'}{$registry_type}{'url-details'}{'protocol'} .
$run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'};
debug_log(sprintf "dest-image-url: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'});
}
if (defined($4)) {
$run{'registries'}{$registry_type}{'url-details'}{'label'} = $4;
debug_log(sprintf "label: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'label'});
$run{'registries'}{$registry_type}{'url-details'}{'source-image-url'} .= "/" .
$run{'registries'}{$registry_type}{'url-details'}{'label'};
debug_log(sprintf "source-image-url: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'source-image-url'});
$run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'} .= "/" .
$run{'registries'}{$registry_type}{'url-details'}{'label'};
debug_log(sprintf "dest-image-url: [%s]\n", $run{'registries'}{$registry_type}{'url-details'}{'dest-image-url'});
} else {
print "The label/repo was not defined in \$run{'registries'}{$registry_type}{'repo'}: [%s]\n", $run{'registries'}{$registry_type}{'repo'};
}
} else {
die "The \$run{'registries'}{$registry_type}{'repo'} does not match the pattern [protocol:][host[:port]][/<project>]/<repo>: " . $run{'registries'}{$registry_type}{'repo'};
}
}
sub source_container_image {
# Ensure that the container image we need is either already in the container registry,
# or build and push the image to the registry
my $userenv = shift;
my $benchmark = shift;
my $container_arch = shift;
my $image; # What gets returned
my @local_images;
# load the userenv as early as possible so we can determine if it
# is a public or private image
# first we need to find where it is -- is it internal (to
# rickshaw) or is it provided by an external userenv library.
my $userenv_file_path;
# let's look externally first in case somebody has tried to
# override an internal version
if (exists $run{'external-userenvs-dir'}) {
if (opendir(TOP_USERENV_DIR_DH, $run{'external-userenvs-dir'})) {
my @top_userenvs_dir_entries = readdir(TOP_USERENV_DIR_DH);
close(TOP_USERENV_DIR_DH);
foreach my $userenv_dir (@top_userenvs_dir_entries) {
if (($userenv_dir eq ".") || ($userenv_dir eq "..")) {
next;
}
$userenv_dir = $run{'external-userenvs-dir'} . "/" . $userenv_dir;
if (-d $userenv_dir) {
if (opendir(BOTTOM_USERENV_DIR_DH, $userenv_dir)) {
my @bottom_userenvs_dir_entries = readdir(BOTTOM_USERENV_DIR_DH);
close(BOTTOM_USERENV_DIR_DH);
foreach my $userenv_dir_entry (@bottom_userenvs_dir_entries) {
if (($userenv_dir_entry eq ".") || ($userenv_dir_entry eq "..")) {
next;
}
if ($userenv_dir_entry eq ($userenv . ".json")) {
$userenv_file_path = $userenv_dir . "/" . $userenv_dir_entry;
printf "Found userenv %s in external userenv directory at %s\n", $userenv, $userenv_file_path;
}
}
} else {
die "ERROR: Could not open directory in the external userenvs directory: " . $userenv_dir . "\n";
}
}
}
} else {
die "ERROR: Could not open external userenvs directory: " . $run{'external-userenvs-dir'} . "\n";
}
}
my $internal_userenv_file_path = $rickshaw_project_dir . "/userenvs/" . $userenv . ".json";
if (! defined $userenv_file_path && -e $internal_userenv_file_path) {
$userenv_file_path = $internal_userenv_file_path;
printf "Found userenv %s in rickshaw directory at %s\n", $userenv, $userenv_file_path;
}
if (! defined $userenv_file_path) {
die "ERROR: Could not locate the requested userenv: " . $userenv . "\n";
}
(my $rc, my $userenv_ref) = get_json_file($userenv_file_path);
if ($rc != 0) {
die "ERROR: Could not load userenv JSON file for '" . $userenv . "' due to non-zero return code " . $rc . ". Are you sure this is a supported userenv?\n";
}
my $registry_type = "public";
if (exists $$userenv_ref{'userenv'}{'origin'}{'requires-pull-token'}) {
if ($$userenv_ref{'userenv'}{'origin'}{'requires-pull-token'} eq "true") {
$registry_type = "private";
}
}
if ($registry_type eq "public") {
printf "Userenv %s does not require a pull token so it comes from a public registry repo.\n", $userenv;
} elsif ($registry_type eq "private") {
printf "Userenv %s does require a pull token so it comes from a private registry repo.\n", $userenv;
}
if (! exists $run{'registries'}{$registry_type}) {
die "ERROR: The requested userenv is a " . $registry_type . " image but there is not a defined " . $registry_type . " engine registry!\n";
}
$workshop_base_cmd =
$run{'workshop-dir'} . "/workshop.pl" .
" --log-level verbose " .
" --config " . $cs_conf_file .
" --param %bench-dir%=" . $bench_dirs{$benchmark} .
" --param %engine-dir%=" . $rickshaw_project_dir . "/engine/" .
" --param %rickshaw-dir%=" . $rickshaw_project_dir .
" --reg-tls-verify=" . $run{'registries'}{$registry_type}{'tls-verify'} .
" --registries-json=" . $run{'registries-json'} .
" 2>&1";
printf "Sourcing container image for userenv '%s' (%s) and benchmark/tool '%s'; this may take a few minutes\n", $userenv, $registry_type, $benchmark;
my @requirements;