summaryrefslogtreecommitdiffstats
path: root/src/tools/utils/sbeGitTool.pl
blob: 44c755afcf63cc8006cf60a1d219c7cec3752f4f (plain)
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
#! /usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/tools/utils/sbeGitTool.pl $
#
# OpenPOWER sbe Project
#
# Contributors Listed Below - COPYRIGHT 2016
# [+] International Business Machines Corp.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG

use strict;
use lib "src/tools/utils/modules";
use Getopt::Long qw(:config pass_through);
use Data::Dumper;
use Cwd;
use gitUtil;

# Global variables & constants
my $debug = 0;
my $help = 0;
my %globals = ();
my @references = ();
my @patchList = ();
my @commits = ();
my $patches = "";
my $sbbase = "";
my $sbname = "";
my $sbrc = "";
my $release = "";
my $bbuild = "";
my $authorFile = "git_author_emails.csv";

# We do not want hooks to run in this tool
$ENV{MIRROR} = 1;
$ENV{DISABLEHOOKS} = 1;

my %commands = ( "extract" => \&execute_extract,
                 "get-commits" => \&execute_get_commits,
                 "discover" => \&execute_discover);

# Handle input command
GetOptions("debug!" => \$debug,
           "help" => \$help);

if ($help)
{
    execute_help();
}
else
{
    my $command = shift @ARGV;
    if ($commands{$command})
    {
        &{$commands{$command}}();
    }
    else
    {
        execute_help();
    }
}

foreach my $arg (@ARGV)
{
    print "Unprocessed arg: $arg\n" if $debug;
}

############################## Begin Actions ##################################

# sub execute_help
#
# Display the help text for the tool
#
sub execute_help
{
 my $command = shift @ARGV;

    if ($command eq "")
    {
        print "sbeGitTool\n";
        print "    Run FSP-CI from a set of git commits\n";
        print "\n";
        print "    Syntax:\n";
        print "        sbeGitTool <tool> [options]\n";
        print "\n";
        print "    Available subtools:\n";
        foreach my $key (sort keys %commands)
        {
            print "        $key\n";
        }
        print "\n";
        print "    Global options:\n";
        print "        --debug              Enable debug mode.\n";
        print "        --help               Display help on a specific tool.\n";
        print "\n";
        print "    Note: Generally a <commit> can be any git or gerrit\n";
        print "          reference.  A git commit number, tag, branch, or\n";
        print "          a gerrit change-id are all valid.\n";
    }
    elsif (not defined $commands{$command})
    {
        die "Unknown subcommand: $command.\n";
    }
    else
    {
        my %help = (
           "extract" =>
q(
    Create the SBE binaries with the patch and its GIT dependencies.

    Options:
        --patches=<changeId:patch-set>      CSV of changeId:patch-set's [required].
        --sbbase=<full-path-to-sb-base>     Sandbox base for FSP-CI [required].
        --sbname=<name>                     Sandbox name [required].
        --rc=<rc file name>                 RC file for the sandbox with absolute path [optional].
        --bbuild=<full-path-to-bb>          Backing build of sandbox [required].
),
           "get-commits" =>
q(
    Given a patch, find the corresponding commit id.

    Options:
        --patches=<changeId:patch-set>      CSV of changeId:patch-set's [required].
        --sbbase=<full-path-to-sb-base>     Sandbox base for FSP-CI [required].
        --sbname=<name>                     Sandbox name [required].
        --release=<name>                    Fips release for patch [required].
),
           "discover" =>
q(
    Discover if there are any CMVC dependencies

    Options:
        --patches=<changeId:patch-set>      CSV of changeId:patch-set's [required].
        --sbbase=<full-path-to-sb-base>     Sandbox base for FSP-CI [required].
        --sbname=<name>                     Sandbox name [required].
        --bbuild=<full-path-to-bb>          Backing build of sandbox [required].
)
    );
        print "sbeGitTool $command:";
        print $help{$command};
    }
}

# sub execute_discover
#
# Discover if there are any CMVC dependencies
#
sub execute_discover
{
    # Set GIT environment
    git_environ_init();

    # Set CI environment
    git_ci_init();

    # Use Release tool to discover possible CMVC requirements
    print "Discovering CMVC requirements for $globals{sandbox}\n" if $debug;
    my $output = `./src/tools/utils/gitRelease.pl fsp-ci --discover --level $sbname --patches $patches --branch $globals{branch} --basestr \"gerrit/$globals{branch}\" --bbuild-Rel $globals{Build_Rel} --debug`;
    die "sbeRelease tool failed\n" if($?);

    my $bbuild_tracks = `cat $bbuild/logs/tracks.txt`;
    my @lines = split /\n/, $output;
    my @tracks = ();
    foreach my $line (@lines)
    {
        # Look for lines indicating tracks (e.g. !@#%^ Need CoReq:)
        my $cmvc_line = $line;
        if ($cmvc_line =~ m/^\!\@\#\%\^/)
        {
            $cmvc_line =~ s/^.*\:\s*//;

            # Remove CMVC tracks that are already in the sb bbuild
            if ($bbuild_tracks !~ m/$cmvc_line/)
            {
                print $cmvc_line;
                print "," if(\$line != \$lines[-1]);
            }
        }
    }

    # Remove level=$sbname from config file as extract() will
    # actually create a release with this name
    system("./src/tools/utils/gitRelease.pl undef $sbname > /dev/null");
    die "sbeRelease tool failed\n" if($?);
}

# sub execute_get_commits
#
# Given a patch, find the asociated corresponding commits
#
sub execute_get_commits
{
    # Set GIT environment
    git_environ_init();

    die "Missing release" if ($release eq "");

    # Set CI environemnt
    git_ci_init($release);

    # Use gitRelease tool to discover all associated commits
    chdir $globals{tool_path};
    print `./src/tools/utils/gitRelease.pl gerrit-commit --patches $patches --branch $globals{branch}`;
    die "$?" if ($?);
}

# sub execute_extract
#
# Create the SBE binaries with the patch and its GIT dependencies.
# Copy the binaries to the sandbox.
#
sub execute_extract
{
    # Set GIT environment
    git_environ_init();

    # Set CI environment
    git_ci_init();

    print "Extracting Code for $globals{sandbox}\n";
    system("./src/tools/utils/gitRelease.pl fsp-ci --level $sbname --patches $patches --branch $globals{branch} --basestr \"gerrit/$globals{branch}\" --bbuild-Rel $globals{Build_Rel}");
    die "sbeRelease tool failed\n" if($?);

    # Compile the SBE and copy binaries to sandbox
    compileAndCopy();
}

# sub git_environ_init
#
# Fetch the parameters and setup environment for further functions
#
sub git_environ_init
{
    # Handle the i/p to the function
    GetOptions("patches:s" => \$patches,
               "sbbase:s" => \$sbbase,
               "sbname:s" => \$sbname,
               "rc:s" => \$sbrc,
               "bbuild:s" => \$bbuild,
               "release:s" => \$release);

    die "Missing patch list" if ($patches eq "");
    die "Missing sandbox base path" if ($sbbase eq "");
    die "Missing sandbox name" if ($sbname eq "");
    die "Missing bbuild path" if ($bbuild eq "" && $release eq "");
    exit "Workaround for Patch list = none" if ($patches eq "none");

    # Set global variables
    $globals{sandbox} = $sbbase."/".$sbname;
    $globals{sbe_git_root} = $globals{sandbox}."/git-ci";
    $globals{tool_path} = $globals{sb_git_root}."/src/tools/utils";

    print "Sandbox: $globals{sandbox}\n" if $debug;
    print "GIT repository path: $globals{sbe_git_root}\n" if $debug;

    chdir($globals{sbe_git_root});
    die "ERROR $?: Invalid GIT repository path in the sandbox" if $? ;
}

# sub git_ci_init
#
# Fetch the GIT branch to work with given the release
#
# @param[in] release - release to convert to branch, indicates no bbuild was
#                      passed in so the default is blank as the release is
#                      derived from the bbuild
#
sub git_ci_init
{

    my $release = shift;

    # If a release is passed in, there is no bbuild to do parsing for
    if ($release eq "")
    {
        # Get SBE release associated with bbuild
        parse_release_notes($bbuild);

        # Parse release out of bbuild path ("fipsXXX")
        ($release) = $bbuild =~ /\/esw\/(fips.*)\/Builds/;
        chomp($release);
    }

    # Convert release to gerrit branch
    release_to_branch($release);
    print "\nConverted release:\n" if $debug;
    print "    Release=$release -> branch=$globals{branch}\n\n" if $debug;
    die "Gerrit branch missing" if  $globals{branch} eq "";

    # Create commit author file
    create_author_file($patches);

}

# sub parse_release_notes
#
# Parse bbuilds SBE release notes to determine the sbeRelease in the bbuild
#
# @param[in] bbuild - bbuild of the sandbox
#
sub parse_release_notes
{
    my $bbuild = shift;

    print "Parsing Release Notes...\n" if $debug;

    # Check if a new SBE release was pulled into the sandbox first, then check the bbuild
    open(RELNOTE, $globals{sandbox}."/src/sbei/sbfw/releaseNotes.html") or
    open(RELNOTE, $bbuild."/src/sbei/sbfw/releaseNotes.html") or
    die("Cannot find releaseNotes.html in the following directories $globals{sandbox} or $bbuild; : $!");
    my $Release = "";
    while (my $line = <RELNOTE>)
    {
        if ($line =~ m/<title>/)
        {
            print $line if $debug;
            ($Release) = $line =~ /Release notes for (.*)<\/title>/;
            chomp $Release;
            last;
        }
    }
    $globals{Build_Rel} = $Release;
    close(RELNOTE);
}

# sub release_to_branch
#
# Assign global branch by converting a release name to a SBE branch
#
# @param[in] release - The fips release to convert to a branch
#
sub release_to_branch
{
    my ($release) = @_;

    die "Release missing while trying to convert to branch" if $release eq "";

    # Run conversion tool to translate a fips-release to gerrit branch
    $globals{branch} = `./src/tools/utils/conv_rel_branch.pl rtob --release $release`;
    die "Conversion from fips-release = $release to gerrit branch failed" if $globals{branch} eq "";
    chomp($globals{branch});
}

# sub create_author_file
#
# Get patches from gerrit and create a CSV file with author emails
# for FSP-CI notification purposes.
#
# @param[in] patches - List of patches to determine authors of
#
sub create_author_file
{
    my ($patches) = @_;

    # Get commits via gitRelease.pl tool
    my $commitsCSV = `./src/tools/utils/gitRelease.pl gerrit-commit --patches $patches --branch $globals{branch}`;
    chomp($commitsCSV);
    my @commits = split(',',$commitsCSV);
    print Dumper \@commits if $debug;

    # Write author email to file
    system("mkdir -p $globals{sandbox}/logs");
    my $file = $globals{sandbox}."/logs/".$authorFile;
    print "Creating CSV author file - $file\n" if $debug;
    open(FILE, ">", $file) or die("Cannot open: $file: $!");
    foreach my $commit (@commits)
    {
        my $email =  `git log -n1 --pretty="%ae" $commit`;
        chomp ($email);
        print FILE $email;
        print FILE "," if ($commit != $commits[-1]);
    }
    system("chmod 755 $file");
    close FILE;
}

# sub compileAndCopy
#
# utility that builds and copies the SBE image.
#
sub compileAndCopy
{
    my $statusFile = $globals{sbe_git_root}."/compile.status";
    my $compile_path = $globals{sbe_git_root};

    print "Compiling and copying the generated binaries to sandbox\n" if $debug;
    open  SBWORKON, " | ./sb workon";
    print SBWORKON  "cd $compile_path \n";
    print SBWORKON  "make install && echo \"Compile Passed\" > $statusFile \n";
    print SBWORKON  "export SANDBOXBASE=$globals{sandbox} \n";
    print SBWORKON  "cd $globals{sbe_git_root} \n";
    print SBWORKON  "./sb prime --sb $sbname --no_build && echo \"Prime Passed\" >> $statusFile \n";
    print SBWORKON  "exit \n";
    close SBWORKON;

    print "\nChecking compile status...\n" if $debug;
    my $compile_status = `cat $statusFile | grep "Compile"`;
    if ($compile_status =~ m/Compile/)
    {
        print "SBE compile successful\n";
    }
    else
    {
        die "ERROR: SBE compile failed\n";
    }

    print "\nChecking sandbox status...\n" if $debug;
    my $sb_status = `cat $statusFile | grep "Prime"`;
    if ($sb_status =~ m/Prime/)
    {
        print "SBE prime successful\n";
    }
    else
    {
        die "ERROR: SBE prime failed\n";
    }
}
OpenPOWER on IntegriCloud