Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | import shutil |
| 5 | import signal |
| 6 | import string |
| 7 | import subprocess |
| 8 | import sys |
Maxim Kuvyrkov | 56cc7fe | 2018-10-09 15:23:12 +0000 | [diff] [blame] | 9 | import xml.etree.ElementTree |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 10 | from distutils.spawn import find_executable |
| 11 | |
Vishal Bhoj | afb8736 | 2016-08-22 19:31:08 +0530 | [diff] [blame] | 12 | |
| 13 | def findparentfiles(fname): |
| 14 | filelist = [] |
| 15 | newlist = [] |
| 16 | args = ['grep', '-rl', '--exclude-dir=.git', fname] |
| 17 | proc = subprocess.Popen(args, |
| 18 | stdin=subprocess.PIPE, |
| 19 | stdout=subprocess.PIPE, |
| 20 | universal_newlines=False, |
| 21 | preexec_fn=lambda: |
| 22 | signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
| 23 | data = proc.communicate()[0] |
| 24 | if proc.returncode != 0: |
| 25 | return filelist |
| 26 | for filename in data.splitlines(): |
| 27 | if filename.endswith('.yaml') and '/' not in filename: |
| 28 | filelist.append(filename) |
| 29 | else: |
| 30 | newlist = findparentfiles(filename) |
| 31 | for tempname in newlist: |
| 32 | filelist.append(tempname) |
| 33 | return filelist |
| 34 | |
| 35 | |
Fathi Boudra | 340fc63 | 2017-04-06 12:30:46 +0300 | [diff] [blame] | 36 | jjb_cmd = find_executable('jenkins-jobs') or sys.exit('jenkins-jobs is not found.') |
Fathi Boudra | c6fdc73 | 2017-04-07 14:56:54 +0300 | [diff] [blame] | 37 | jjb_args = [jjb_cmd] |
Fathi Boudra | f25a3db | 2017-04-07 07:25:33 +0300 | [diff] [blame] | 38 | |
| 39 | jjb_user = os.environ.get('JJB_USER') |
| 40 | jjb_password = os.environ.get('JJB_PASSWORD') |
| 41 | if jjb_user is not None and jjb_password is not None: |
| 42 | jenkins_jobs_ini = ('[job_builder]\n' |
| 43 | 'ignore_cache=True\n' |
| 44 | 'keep_descriptions=False\n' |
| 45 | '\n' |
| 46 | '[jenkins]\n' |
Fathi Boudra | a85ff5f | 2017-04-07 10:16:50 +0300 | [diff] [blame] | 47 | 'user=%s\n' |
| 48 | 'password=%s\n' |
| 49 | 'url=https://ci.linaro.org/\n' % (jjb_user, jjb_password)) |
Fathi Boudra | f25a3db | 2017-04-07 07:25:33 +0300 | [diff] [blame] | 50 | with open('jenkins_jobs.ini', 'w') as f: |
| 51 | f.write(jenkins_jobs_ini) |
Fathi Boudra | c6fdc73 | 2017-04-07 14:56:54 +0300 | [diff] [blame] | 52 | jjb_args.append('--conf=jenkins_jobs.ini') |
Fathi Boudra | f25a3db | 2017-04-07 07:25:33 +0300 | [diff] [blame] | 53 | |
Maxim Kuvyrkov | 56cc7fe | 2018-10-09 15:23:12 +0000 | [diff] [blame] | 54 | jjb_test_args = list(jjb_args) |
| 55 | jjb_delete_args = list(jjb_args) |
| 56 | |
| 57 | # !!! "update" below and through out this file is replaced by "test" (using sed) |
| 58 | # !!! in the sanity-check job. |
| 59 | main_action = 'update' |
| 60 | jjb_args.extend([main_action, 'template.yaml']) |
| 61 | jjb_test_args.extend(['test', '-o', 'out/', 'template.yaml']) |
| 62 | jjb_delete_args.extend(['delete']) |
| 63 | |
| 64 | if main_action == 'test': |
| 65 | # Dry-run, don't delete jobs. |
| 66 | jjb_delete_args.insert(0, 'echo') |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 67 | |
| 68 | try: |
Siddhesh Poyarekar | 8d25b3d | 2019-08-08 19:42:21 +0530 | [diff] [blame^] | 69 | git_args = ['git', 'diff', '--raw', |
Fathi Boudra | c6fdc73 | 2017-04-07 14:56:54 +0300 | [diff] [blame] | 70 | os.environ.get('GIT_PREVIOUS_COMMIT'), |
| 71 | os.environ.get('GIT_COMMIT')] |
| 72 | proc = subprocess.Popen(git_args, |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 73 | stdin=subprocess.PIPE, |
| 74 | stdout=subprocess.PIPE, |
| 75 | universal_newlines=False, |
| 76 | preexec_fn=lambda: |
| 77 | signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
| 78 | except (OSError, ValueError) as e: |
| 79 | raise ValueError("%s" % e) |
| 80 | |
| 81 | data = proc.communicate()[0] |
| 82 | if proc.returncode != 0: |
| 83 | raise ValueError("command has failed with code '%s'" % proc.returncode) |
| 84 | |
Vishal Bhoj | afb8736 | 2016-08-22 19:31:08 +0530 | [diff] [blame] | 85 | filelist = [] |
| 86 | files = [] |
Siddhesh Poyarekar | 8d25b3d | 2019-08-08 19:42:21 +0530 | [diff] [blame^] | 87 | for line in data.splitlines(): |
| 88 | # Format of the git-diff; we only need OPERATION and FILE1 |
| 89 | # |
| 90 | # :<OLD MODE> <NEW MODE> <OLD REF> <NEW REF> <OPERATION> <FILE1> <FILE2> |
| 91 | elems = line.split() |
| 92 | operation = elems[4][0] |
| 93 | filename = elems[5] |
| 94 | |
Vishal Bhoj | afb8736 | 2016-08-22 19:31:08 +0530 | [diff] [blame] | 95 | if filename.endswith('.yaml') and '/' not in filename: |
Siddhesh Poyarekar | 8d25b3d | 2019-08-08 19:42:21 +0530 | [diff] [blame^] | 96 | # No point trying to test deleted jobs because they don't exist any |
| 97 | # more. |
| 98 | if operation == 'D': |
| 99 | continue |
Vishal Bhoj | afb8736 | 2016-08-22 19:31:08 +0530 | [diff] [blame] | 100 | filelist.append(filename) |
| 101 | else: |
| 102 | files = findparentfiles(filename) |
| 103 | for tempname in files: |
| 104 | filelist.append(tempname) |
| 105 | |
Riku Voipio | c0e6b13 | 2019-06-27 13:52:04 +0300 | [diff] [blame] | 106 | # Remove duplicate entries in the list |
Vishal Bhoj | afb8736 | 2016-08-22 19:31:08 +0530 | [diff] [blame] | 107 | filelist = list(set(filelist)) |
| 108 | |
| 109 | for conf_filename in filelist: |
| 110 | with open(conf_filename) as f: |
| 111 | buffer = f.read() |
| 112 | template = string.Template(buffer) |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 113 | buffer = template.safe_substitute( |
Fathi Boudra | fdd9f21 | 2015-04-18 19:28:55 +0300 | [diff] [blame] | 114 | AUTH_TOKEN=os.environ.get('AUTH_TOKEN'), |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 115 | LT_QCOM_KEY=os.environ.get('LT_QCOM_KEY'), |
| 116 | LAVA_USER=os.environ.get('LAVA_USER'), |
| 117 | LAVA_TOKEN=os.environ.get('LAVA_TOKEN')) |
| 118 | with open('template.yaml', 'w') as f: |
| 119 | f.write(buffer) |
| 120 | try: |
Fathi Boudra | c6fdc73 | 2017-04-07 14:56:54 +0300 | [diff] [blame] | 121 | proc = subprocess.Popen(jjb_args, |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 122 | stdin=subprocess.PIPE, |
| 123 | stdout=subprocess.PIPE, |
| 124 | universal_newlines=False, |
| 125 | preexec_fn=lambda: |
| 126 | signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
| 127 | except (OSError, ValueError) as e: |
| 128 | raise ValueError("%s" % e) |
| 129 | |
| 130 | data = proc.communicate()[0] |
| 131 | if proc.returncode != 0: |
| 132 | raise ValueError("command has failed with code '%s'" % proc.returncode) |
| 133 | |
Maxim Kuvyrkov | 56cc7fe | 2018-10-09 15:23:12 +0000 | [diff] [blame] | 134 | try: |
| 135 | shutil.rmtree('out/', ignore_errors=True) |
| 136 | |
| 137 | proc = subprocess.Popen(jjb_test_args, |
| 138 | stdin=subprocess.PIPE, |
| 139 | stdout=subprocess.PIPE, |
| 140 | universal_newlines=False, |
| 141 | preexec_fn=lambda: |
| 142 | signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
| 143 | data = proc.communicate()[0] |
| 144 | if proc.returncode != 0: |
| 145 | raise ValueError("command has failed with code '%s'" % proc.returncode) |
| 146 | |
| 147 | proc = subprocess.Popen(['ls', 'out/'], |
| 148 | stdin=subprocess.PIPE, |
| 149 | stdout=subprocess.PIPE, |
| 150 | universal_newlines=False, |
| 151 | preexec_fn=lambda: |
| 152 | signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
| 153 | data = proc.communicate()[0] |
| 154 | if proc.returncode != 0: |
| 155 | raise ValueError("command has failed with code '%s'" % proc.returncode) |
| 156 | |
| 157 | for filename in data.splitlines(): |
Riku Voipio | c0e6b13 | 2019-06-27 13:52:04 +0300 | [diff] [blame] | 158 | conf_name=os.path.splitext(conf_filename)[0] |
| 159 | if not filename.startswith(conf_name): |
| 160 | raise ValueError("Job name %s does not match the file it is in: %s" % (filename, conf_name)) |
Maxim Kuvyrkov | 56cc7fe | 2018-10-09 15:23:12 +0000 | [diff] [blame] | 161 | try: |
| 162 | xmlroot = xml.etree.ElementTree.parse('out/' + filename).getroot() |
| 163 | disabled = next(xmlroot.iterfind('disabled')).text |
| 164 | if disabled != 'true': |
| 165 | continue |
| 166 | displayName = next(xmlroot.iterfind('displayName')).text |
| 167 | if displayName != 'DELETE ME': |
| 168 | continue |
| 169 | except: |
| 170 | continue |
| 171 | |
| 172 | delete_args = list(jjb_delete_args) |
| 173 | delete_args.extend([filename]) |
| 174 | proc = subprocess.Popen(delete_args, |
| 175 | stdin=subprocess.PIPE, |
| 176 | stdout=subprocess.PIPE, |
| 177 | universal_newlines=False, |
| 178 | preexec_fn=lambda: |
| 179 | signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
| 180 | data = proc.communicate()[0] |
| 181 | if proc.returncode != 0: |
| 182 | raise ValueError("command has failed with code '%s'" % proc.returncode) |
| 183 | print data |
| 184 | except (OSError, ValueError) as e: |
| 185 | raise ValueError("%s" % e) |
| 186 | |
| 187 | shutil.rmtree('out/', ignore_errors=True) |
Paul Sokolovsky | b1d8657 | 2015-03-05 22:48:18 +0200 | [diff] [blame] | 188 | os.remove('template.yaml') |
Fathi Boudra | f25a3db | 2017-04-07 07:25:33 +0300 | [diff] [blame] | 189 | |
| 190 | if os.path.exists('jenkins_jobs.ini'): |
| 191 | os.remove('jenkins_jobs.ini') |