|
| 1 | +#!/usr/bin/env python3.6 |
| 2 | +"""CRUD for Buildkite pipelines. Reads from YAML files.""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import errno |
| 6 | +import logging |
| 7 | +import json |
| 8 | +import os |
| 9 | +import sys |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import requests |
| 14 | +import yaml |
| 15 | + |
| 16 | +L = logging.getLogger('Buildkite') |
| 17 | +L.addHandler(logging.NullHandler()) |
| 18 | + |
| 19 | +API = 'https://api.buildkite.com/v2/organizations/opx/pipelines' |
| 20 | + |
| 21 | +DEBIAN_REPOS = [ |
| 22 | + 'SAI', |
| 23 | + 'opx-alarm', |
| 24 | + 'opx-base-model', |
| 25 | + 'opx-common-utils', |
| 26 | + 'opx-cps', |
| 27 | + 'opx-db-sql', |
| 28 | + 'opx-logging', |
| 29 | + 'opx-nas-acl', |
| 30 | + 'opx-nas-common', |
| 31 | + 'opx-nas-daemon', |
| 32 | + 'opx-nas-interface', |
| 33 | + 'opx-nas-l2', |
| 34 | + 'opx-nas-l3', |
| 35 | + 'opx-nas-linux', |
| 36 | + 'opx-nas-ndi', |
| 37 | + 'opx-nas-ndi-api', |
| 38 | + 'opx-nas-qos', |
| 39 | + 'opx-northbound', |
| 40 | + 'opx-pas', |
| 41 | + 'opx-platform-config', |
| 42 | + 'opx-sai-vm', |
| 43 | + 'opx-sdi-sys', |
| 44 | + 'opx-snmp', |
| 45 | + 'opx-tmpctl', |
| 46 | + 'opx-tools', |
| 47 | +] |
| 48 | + |
| 49 | + |
| 50 | +ALL_REPOS = DEBIAN_REPOS + [ |
| 51 | + 'continuous-integration', |
| 52 | + 'github', |
| 53 | + 'opx-build', |
| 54 | + 'opx-core', |
| 55 | + 'opx-docs', |
| 56 | + 'opx-manifest', |
| 57 | + 'opx-northbound', |
| 58 | + 'opx-onie-installer', |
| 59 | + 'opx-test', |
| 60 | + 'rootfs', |
| 61 | + 'tools_opx-py', |
| 62 | +] |
| 63 | + |
| 64 | + |
| 65 | +def fatal(msg: str) -> None: |
| 66 | + """Aborts.""" |
| 67 | + L.error(msg) |
| 68 | + sys.exit(1) |
| 69 | + |
| 70 | + |
| 71 | +class Pipeline: |
| 72 | + """Represents a Buildkite pipeline.""" |
| 73 | + def __init__(self, name: str) -> None: |
| 74 | + self.name = name |
| 75 | + self.file = Path(f'buildkite/{name}.yaml') |
| 76 | + |
| 77 | + if not self.file.exists(): |
| 78 | + if name in DEBIAN_REPOS: |
| 79 | + self.file = Path('buildkite/opx-debian.yaml') |
| 80 | + else: |
| 81 | + raise FileNotFoundError(errno.ENOENT, |
| 82 | + os.strerror(errno.ENOENT), |
| 83 | + str(self.file)) |
| 84 | + |
| 85 | + def exists(self) -> bool: |
| 86 | + """Returns True if Buildkite says the pipeline exists.""" |
| 87 | + L.debug(f'checking if {self.name} pipeline exists') |
| 88 | + return requests.get(f'{API}/{self.name}').status_code == 200 |
| 89 | + |
| 90 | + def create(self) -> str: |
| 91 | + """Creates pipeline if it doesn't already exist. |
| 92 | +
|
| 93 | + Pipeline configuration is converted to json and pushed. |
| 94 | + """ |
| 95 | + if self.exists(): |
| 96 | + fatal(f'Pipeline {self.name} already exists') |
| 97 | + else: |
| 98 | + L.info(f'Running create on {self.name} with {self.file}') |
| 99 | + |
| 100 | + pipeline = yaml.safe_load(self.file.open().read()) |
| 101 | + res = requests.post(f'{API}', json=pipeline) |
| 102 | + res.raise_for_status() |
| 103 | + return json.dumps(res.json(), indent=4, sort_keys=True) |
| 104 | + |
| 105 | + def read(self) -> str: |
| 106 | + """Retrieves current pipeline configuration from Buildkite.""" |
| 107 | + if not self.exists(): |
| 108 | + fatal(f'Pipeline {self.name} does not exist') |
| 109 | + else: |
| 110 | + L.info(f'Running read on {self.name}') |
| 111 | + |
| 112 | + res = requests.get(f'{API}/{self.name}') |
| 113 | + res.raise_for_status() |
| 114 | + return json.dumps(res.json(), indent=4, sort_keys=True) |
| 115 | + |
| 116 | + def update(self) -> str: |
| 117 | + """Updates pipeline if it already exists. |
| 118 | +
|
| 119 | + Pipeline configuration is converted to json and pushed. |
| 120 | + """ |
| 121 | + if not self.exists(): |
| 122 | + fatal(f'Pipeline {self.name} does not exist') |
| 123 | + else: |
| 124 | + L.info(f'Running update on {self.name} with {self.file}') |
| 125 | + |
| 126 | + pipeline = yaml.safe_load(self.file.open().read()) |
| 127 | + res = requests.patch(f'{API}/{self.name}', json=pipeline) |
| 128 | + res.raise_for_status() |
| 129 | + return json.dumps(res.json(), indent=4, sort_keys=True) |
| 130 | + |
| 131 | + def delete(self) -> str: |
| 132 | + """Deletes pipeline if it already exists.""" |
| 133 | + if not self.exists(): |
| 134 | + fatal(f'Pipeline {self.name} does not exist') |
| 135 | + else: |
| 136 | + L.info(f'Running delete on {self.name}') |
| 137 | + |
| 138 | + res = requests.delete(f'{API}/{self.name}') |
| 139 | + res.raise_for_status() |
| 140 | + return 'deleted' |
| 141 | + |
| 142 | + |
| 143 | +def main(): |
| 144 | + """Entrypoint.""" |
| 145 | + parser = argparse.ArgumentParser(description=__doc__) |
| 146 | + |
| 147 | + parser.add_argument( |
| 148 | + '-v', '--verbose', |
| 149 | + help='log debug messages', |
| 150 | + action='store_const', |
| 151 | + dest='loglevel', |
| 152 | + const=logging.DEBUG, |
| 153 | + default=logging.INFO, |
| 154 | + ) |
| 155 | + |
| 156 | + parser.add_argument( |
| 157 | + 'action', |
| 158 | + help='action to perform', |
| 159 | + choices=['create', 'read', 'update', 'delete'], |
| 160 | + ) |
| 161 | + |
| 162 | + parser.add_argument( |
| 163 | + 'name', |
| 164 | + help='name of pipeline', |
| 165 | + ) |
| 166 | + |
| 167 | + args = parser.parse_args() |
| 168 | + logging.basicConfig(level=args.loglevel) |
| 169 | + L.debug(str(args)) |
| 170 | + |
| 171 | + print(getattr(Pipeline(args.name), args.action)()) |
| 172 | + |
| 173 | + |
| 174 | +if __name__ == '__main__': |
| 175 | + main() |
0 commit comments