summaryrefslogtreecommitdiffstats
path: root/python-scripts/build-nss.py
blob: 70a9533a807fd595bc40d1442332abca9c89c37c (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
#!/usr/bin/python 
import sys
import os
import argparse
import json
import build_functions
print os.environ.get("USER")
#######################################################

parser = argparse.ArgumentParser(description='Program to automate nss builds using mock.', 
    version = '0.9 beta', 
    add_help=True, 
    conflict_handler='resolve', 
    epilog='This program is designed to be used both on the command line or by cron') 

#parser = argparse.ArgumentParser(description='Program to automate nss builds using mock.', version = '0.9 beta')#, add_help=True)


parser.add_argument("-d", 
                    "--debug", 
                    action="store_true", 
                    default=True, 
                    help="show debug output")

parser.add_argument("-t", 
                    "--test", 
                    action="store_true", 
                    default=False,
                    help="test mode (for debugging)")
parser.add_argument(
                    '-f', 
                    '--flavor', 
                    action="store", 
                    dest="flavor", 
                    default='nss', 
                    help='specify the flavor of the build. [default=nss]')
#parser.add_argument(
#                    '-p',
#                    '--platform', 
#                    action="store", 
#                    dest="platform", 
#                    default=None, 
#                    help='specify a platform to use for mock config.  [default=None] NOT IMPLEMENTED')
parser.add_argument(
                    '-r', 
                    '--rel',
                    action="store", 
                    dest="rel", 
                    default=None,
                    choices=("6", "7" ),
                    help='specify a release to use for mock config.  [default=None]')
parser.add_argument(
                    '-a', 
                    '--arch', 
                    action="store", 
                    dest="arch", 
                    default=None,
                    choices=("i386", "x86_64"),
                    help='specify an arch to use for mock config.  [default=None]')
parser.add_argument(
                    '-l', 
                    '--local', 
                    action="store_true", 
                    dest="localBuild", 
                    default=False, 
                    help='perform a build in the current working diretory. [default=False]')
parser.add_argument(
                    '-mm', 
                    '--mailme', 
                    action="store_true", 
                    dest="mailMe", 
                    #default=True,  
                    default=False,
                    help='send email to kwright@redhat.com. [default=False')
parser.add_argument(
                    '-sb', 
                    '--skipbuild', 
                    action="store_true", 
                    dest="skipBuild", 
                    default=False, 
                    help='skip the build portion of the program. [default=False] ')
parser.add_argument(
                    '-sc', 
                    '--skipcopy', 
                    action="store_true", 
                    dest="skipCopy", 
                    default=False, 
                    help='skip the copy portion of the program. [default=False] ')
parser.add_argument(
                    '-sw', 
                    '--skipwget', 
                    action="store_true", 
                    dest="skipWget", 
                    default=False, 
                    help='skip the wget portion of the program. [default=False] ')
parser.add_argument(
                    '-b', 
                    '--build', 
                    action='store',
                    dest='nssPackageList',
                    nargs='+',
                    default=['nspr', 'nss-util', 'nss-softokn', 'nss', 'certmonger', 'crypto-utils', 'curl', 'mod_nss', 'mod_revocator' ],
                    help='specify a list of one of more builds to performm. ',
                    choices=('nspr', 'nss-util', 'nss-softokn', 'nss'))
parser.add_argument(
                    '-m', 
                    '--mock_cfg', 
                    action="store", 
                    dest="mockCfgList", 
                    nargs='+', 
                    default=["rhel-6-i386", "rhel-6-x86_64"],
                    help='spcify a list of one or more mock configs to use.  [default = rhel-6-i386", "rhel-6-x86_64]',
                    choices=("rhel-6-i386", "rhel-6-x86_64"))
options  = parser.parse_args()
#if args.servername is None and not args.dry_run:


def print_debug(msg):
    if options.debug:
        print "DEBUG: %s" % (msg)
        
#TODO: figure out some logic about which parameters can be used together
#######################################################
workbase =  os.environ['HOME']
scriptsDir = workbase + "/scripts"
pythonDir=scriptsDir + "/build"
sys.path.append(pythonDir)
from build_functions import *
#######################################################
if options.test:
    options.debug = True
    options.mailMe = True
 
if options.skipCopy:
    options.skipWget = True

if options.skipBuild and options.nssPackageList:
    print "INFO: --skipBuild and --build cannot be used together"
   
print_debug("test = %s " % options.test)
print_debug("debug = %s " % options.debug)
#print_debug("platform = %s " % options.platform)
print_debug("release = %s " % options.rel)
print_debug("arch = %s " % options.arch)
print_debug("flavor = %s " % options.flavor)
print_debug("mailMe = %s " % options.mailMe)
print_debug("localBuild = %s " % options.localBuild)
print_debug("skipBuild = %s " % options.skipBuild)
print_debug("skipCopy = %s " % options.skipCopy)
print_debug("skipWget = %s " % options.skipWget)
print_debug("nssPackageList = %s " % options.nssPackageList)
print_debug("mockCfgList = %s " % options.mockCfgList)
#######################################################
mockCfgList=[]
for mock_cfg in options.mockCfgList:
    [platform, rel, arch] = mock_cfg.split("-")
    if options.rel is not None:
        print_debug("Release = %s" % options.rel)
        mockCfgList.append("-".join([platform, options.rel, arch]))
    else:
        print_debug("Release not defind")
        mockCfgList = options.mockCfgList
    print_debug("mockCfgList = %s" % mockCfgList)


for mock_cfg in options.mockCfgList:
    [platform, rel, arch] = mock_cfg.split("-")
    if options.arch is not None:
        print_debug("arch = %s" % options.arch)
        mockCfgList = [("-".join([platform, rel, options.arch]))]
    else:
        print_debug("arch not defind")
        mockCfgList = options.mockCfgList

print_debug("mockCfgList = %s" % mockCfgList)

#######################################################
#TODO use MAILTO or mailTo or receiver to pass as a arg
#test to see if a variable is defined
#try:
  #thevariable
#except NameError:
  #print "well, it WASN'T defined after all!"
#else:
  #print "sure, it was defined."
#######################################################
#TODO use MAILTO or mailTo or receiver to pass as a arg
if options.mailMe:
    mailTo="emaldona@redhat.com"
else:
    #mailTo="rhcs-dev-list@redhat.com"
    mailTo="nss-nspr-devel@redhat.com"
#######################################################
if options.localBuild is False:
    import datetime
    now = datetime.datetime.now()
    DT=now.strftime('%Y%m%d-%H%M%S')
    str(DT)
    buildDir = workbase + "/" +  DT
    print_debug("DT = %s" % DT)
else:
    buildDir = os.getcwd()
    DT=os.path.basename(buildDir)
    #need the DT for later when copying files. 
    print_debug("DT = %s" % DT)
print_debug("buildDir = %s" % buildDir)

nssPackageList = options.nssPackageList
flavor = options.flavor
#######################################################
progname = os.path.basename(__file__)
message = "Subject: INFO: starting a build of from %s\n\nThe build directory is %s " % (progname, buildDir)
#TODO. figure out how to send this email only to me. (ugh!)
#email(message, mailTo)
#######################################################

print_debug("Verifying the existence of the mock config file")
mockDir = '/etc/mock'
fileNameExt = '.cfg'
for mock_cfg in mockCfgList:
    if os.path.isfile(os.path.join(mockDir, mock_cfg + fileNameExt)):
        print_debug("%s" % os.path.join(mockDir, mock_cfg + fileNameExt))
    else:
        print "ERROR: %s doesn't exist." % os.path.join(mockDir, mock_cfg + fileNameExt)
        message = "Subject: ERROR: %s missing from /etc/mock\n\nERROR: %s missing from /etc/mock. Killing the build." % (mock_cfg + fileNameExt, mock_cfg)
        email(message, mailTo)
        sys.exit()
#######################################################

buildStatusDict={}
if options.skipBuild is False:
    for mock_cfg in mockCfgList:
        [platform, rel, arch] = mock_cfg.split("-")
        if rel == "18":
            nssPackageList.append("pki-tps")
            print_debug("Adding pki-tps to list of packages to build for %s %s" % (platform, rel))
            print_debug("nssPackageList = %s" % nssPackageList)
        for package in nssPackageList:
            #status = build_pki(package, buildDir, mock_cfg, flavor, scriptsDir, options.debug, mailTo)
            status = build_generic_pkg(package, buildDir, mock_cfg, flavor, scriptsDir, options.debug, mailTo)
            buildStatusDict[(package,mock_cfg)]=status
####################
#hope this line works
            print_debug("%s" % json.dumps(str(buildStatusDict), sort_keys=True, indent=4))
####################
            print_debug("package %s completed with status %s for %s" % (package, status, mock_cfg))



# TODO fix issue with failed build not stopping creating the repo
# NOTE the problem appears to be if the x86_64 bit builds all complete
# successfully, then the dict only shows those builds then it thinks the
# build is okay. also, dogtag uses build_pki, whereas nss
# uses build_generic_pkg -- this may not apply to nss
#print_debug("%s" % buildStatusDict)
print_debug("%s" % json.dumps(str(buildStatusDict), sort_keys=True, indent=4))
#TODO write the output of buildStatusDict to a file in the home directory.
#This way, I can easily tell if anything failed.

################################################################################
#NOTE: handle whether or not to copy the files over and create the rpm repo. 
#The logic is as follows: 
#if one of the packages failed to build, skipCopy is enalbed, don't copy
#if the commandline option skipCopy is enalbled, don't copy
#else skipCopy is disabled and the files get copied then the yum repos are created.
################################################################################
skipCopy=False
for mock_cfg in mockCfgList:
    for (package, status) in buildStatusDict:
        print_debug("%s" % buildStatusDict[package,mock_cfg])
        if buildStatusDict[package,mock_cfg]=="FAILED":
            skipCopy=True 
            print "ERROR: skipCopy is %s (enabled) due to a failure of package %s on %s. The repo will NOT be created)" % (skipCopy, package, mock_cfg)
            #TODO Add the code here to copy the build logs over possibly only for the packages the failed?
    
    if options.skipCopy is True:
        message = "Subject: INFO: skipCopy was enabled from the commandline. Skipping copying to and/or creating the repo.\n\nThe build directory is %s." % (buildDir)
        print message
        email(message, mailTo)
    elif skipCopy is True:
        status_message=""
        for (package, status) in buildStatusDict:
            status_message += "build status of package: %s on %s: %s\n" % (package, status, buildStatusDict[package,mock_cfg])
        message = "Subject: ERROR: %s skipping copying to and/or creating the repo.\n\nERROR: The repo was not created due to a failure of one or more packages.\n\nHere's the list of builds and their status:\n\n%s\n" % (flavor, status_message)
#TODO add code to copy the log files over somewhere when the build failes.
        createRepoDirs(mock_cfg, flavor, DT, buildDir, options.test, options.debug, mailTo)

        print_debug("%s" %  message)
        email(message, mailTo)
    elif skipCopy is False:
        #for mock_cfg in mockCfgList:
        print_debug("copy the files to the repo for %s" % mock_cfg)
        copyToRepo(mock_cfg, flavor, DT, buildDir, options.test, options.debug, options.skipWget,  mailTo)

sys.exit()


if __name__ == '__main__':
    import sys
    try:
        main(sys.argv[1])
    except IndexError:
        main()