buildadd.py (3896B)
1 #!/usr/bin/env python3 2 # See LICENSE file for copyright and license details. 3 4 """ 5 Module to add or modify Jenkins build jobs 6 """ 7 8 import sys 9 from os import environ as env 10 from os.path import basename 11 import jenkins 12 13 from config import (jenkins_user, jenkins_pass, jenkins_host, jobtypes, 14 arches_qemusys, arches_any, architectures, dryrun, 15 allowed_distros) 16 from funcs import fill_template 17 18 19 def main(): 20 """ 21 Main function 22 """ 23 # Used to differ whether we're adding or modifying 24 progname = basename(sys.argv[0]) 25 26 if progname == 'buildadd.py': 27 print('* Requested job creation') 28 elif progname == 'buildmodify.py': 29 print('* Requested job modification') 30 else: 31 print('Invalid basename. Exiting...') 32 sys.exit(1) 33 34 print('- Connecting to Jenkins...') 35 jenk = jenkins.Jenkins(jenkins_host, jenkins_user, jenkins_pass) 36 37 try: 38 jenk.get_whoami() 39 except jenkins.JenkinsException: 40 print('Error in request. Possible authentication fail.') 41 sys.exit(1) 42 43 # the -4 cuts off '.git' from the path 44 pkgname = basename(env['SCORSH_REPO'])[:-4] 45 46 # the distro owning the job, e.g. "maemo/jessie", or "devuan/ascii-proposed" 47 distro = env['SCORSH_BRANCH'].split("/")[0] 48 if distro not in allowed_distros: 49 print('Error: This distribution is not supported. Exiting.') 50 sys.exit(1) 51 # devuan's reserved namespace 52 elif distro == 'suites': 53 distro = 'devuan' 54 55 for jobt in jobtypes: 56 # jobname = '-'.join([pkgname, jobt]) 57 jobname = distro+':'+pkgname+'-'+jobt 58 59 print('* Trying to create %s job for %s' % (jobt, pkgname)) 60 61 if jenk.job_exists(jobname) and progname == 'buildadd.py': 62 print('* %s already exists') 63 continue 64 elif not jenk.job_exists(jobname) and progname == 'buildmodify.py': 65 print('* %s does not exist in Jenkins') 66 continue 67 68 build_sequential = 'false' 69 arches = [] 70 qemusys = [] 71 jlabels = [] 72 for arg in sys.argv[1:]: 73 if arg == 'sequential': 74 build_sequential = 'true' 75 elif arg == 'pbuilder_network': 76 add_buildvar = 'export PBUILDER_USENETWORK=true' 77 elif arg == 'any': 78 arches = arches_any 79 elif arg == 'any_qemusys': 80 qemusys = arches_qemusys 81 elif arg in architectures: 82 arches.append(arg) 83 elif arg in arches_qemusys: 84 qemusys.append(arg) 85 # in original releasebot, no arch specified implies all of them 86 # not sure if we should imply this. explicitly declaring our 87 # architectures brings readability and better understanding 88 89 # set is to remove dupes 90 jlabels = list(set(arches + qemusys)) 91 92 # XXX: now there is some mention of description 93 # which seems the gitlab issue description. did we ever pass this? 94 95 # Here we use the XML template 96 pxml = fill_template(pkgname, jobt, distro, arches, 97 git_uri=env['SCORSH_GITURL'], 98 buildvar=add_buildvar, 99 sequential=build_sequential, jlabels=jlabels) 100 101 if pxml and progname == 'buildadd.py': 102 print('- Creating job') 103 if not dryrun: 104 jenk.create_job(jobname, pxml) 105 elif pxml and progname == 'buildmodify.py': 106 print('- Modifying job') 107 if not dryrun: 108 jenk.reconfig_job(jobname, pxml) 109 else: 110 print('* Internal error. Check your XML files and the exe name') 111 sys.exit(1) 112 113 114 if __name__ == '__main__': 115 if len(sys.argv) > 1: 116 main() 117 else: 118 print('Error: not enough arguments') 119 sys.exit(1)