|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import requests |
| 4 | +import subprocess |
| 5 | +import json |
| 6 | +import sys |
| 7 | + |
| 8 | +# insiel |
| 9 | +TOKEN = '{{ openwisp2fw_uploader.token }}' |
| 10 | +BASE_URL = '{{ openwisp2fw_uploader.url }}' |
| 11 | +# key: filesystem directory |
| 12 | +# value[0]: category uuid |
| 13 | +# value[1]: flavour |
| 14 | +CATEGORIES = { |
| 15 | +{% for organization in openwisp2fw_organizations -%} |
| 16 | +{% for flavour, category_id in organization.get('categories', {}).items() -%} |
| 17 | + '{{ organization.name }}': ('{{ category_id }}', '{{ flavour }}'), |
| 18 | +{% endfor %} |
| 19 | +{% endfor %} |
| 20 | +} |
| 21 | +BASE_PATH = '{{ openwisp2fw_bin_dir }}' |
| 22 | + |
| 23 | +IMAGE_TYPES = {{ openwisp2fw_uploader.image_types | to_json }} |
| 24 | + |
| 25 | + |
| 26 | +def jsonify(data): |
| 27 | + return json.dumps(data) |
| 28 | + |
| 29 | + |
| 30 | +def find_config_line(config_name): |
| 31 | + with open('.config') as f: |
| 32 | + config_contents = f.read() |
| 33 | + lines = config_contents.split('\n') |
| 34 | + for line in lines: |
| 35 | + if line.startswith(config_name): |
| 36 | + parts = line.split('=') |
| 37 | + return parts[1].replace('"', '') |
| 38 | + else: |
| 39 | + raise ValueError(f'{config_name} not found') |
| 40 | + |
| 41 | + |
| 42 | +API_URL = f'{BASE_URL}/api/v1/firmware/' |
| 43 | +BUILD_URL = f'{API_URL}build/' |
| 44 | +AUTHORIZATION_HEADER = {'Authorization': f'Bearer {TOKEN}'} |
| 45 | +CONTENT_TYPE_HEADER = {'Content-Type': 'application/json'} |
| 46 | +HEADERS = AUTHORIZATION_HEADER.copy() |
| 47 | +HEADERS.update(CONTENT_TYPE_HEADER) |
| 48 | +VERSION_DIST = find_config_line('CONFIG_VERSION_DIST') |
| 49 | +VERSION_NUMBER = find_config_line('CONFIG_VERSION_NUMBER') |
| 50 | + |
| 51 | +out = subprocess.Popen( |
| 52 | + ['./scripts/getver.sh'], |
| 53 | + stdout=subprocess.PIPE, |
| 54 | + stderr=subprocess.STDOUT |
| 55 | +) |
| 56 | +stdout, stderr = out.communicate() |
| 57 | + |
| 58 | +REVISION = str(stdout.decode('utf8').strip()) |
| 59 | +OS_IDENTIFIER = f'{VERSION_DIST} {VERSION_NUMBER} {REVISION}' |
| 60 | +IMAGE_PREFIX = OS_IDENTIFIER.lower().replace(' ', '-') |
| 61 | + |
| 62 | +for org_slug, org_data in CATEGORIES.items(): |
| 63 | + category_id = org_data[0] |
| 64 | + flavour = org_data[1] |
| 65 | + |
| 66 | + # create build |
| 67 | + response = requests.post( |
| 68 | + BUILD_URL, |
| 69 | + headers=HEADERS, |
| 70 | + data=jsonify({ |
| 71 | + 'version': VERSION_NUMBER, |
| 72 | + 'category': category_id, |
| 73 | + 'os': OS_IDENTIFIER |
| 74 | + }), |
| 75 | + ) |
| 76 | + |
| 77 | + if response.status_code != 201: |
| 78 | + print('It was not possible to create a build.') |
| 79 | + print(response.content.decode()) |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + BUILD_ID = response.json()['id'] |
| 83 | + UPLOAD_IMAGE_URL = f'{BUILD_URL}{BUILD_ID}/image/' |
| 84 | + |
| 85 | + for image_file in IMAGE_TYPES: |
| 86 | + target = image_file.split('-')[0] |
| 87 | + BIN_PATH = f'{BASE_PATH}/{org_slug}/latest/{target}/{flavour}/{IMAGE_PREFIX}-{image_file}' |
| 88 | + |
| 89 | + try: |
| 90 | + BIN_FILE = open(BIN_PATH, 'rb') |
| 91 | + except FileNotFoundError as e: |
| 92 | + print(f'Skipping {image_file} because of the following error:\n{e}') |
| 93 | + continue |
| 94 | + |
| 95 | + # upload image |
| 96 | + response = requests.post( |
| 97 | + UPLOAD_IMAGE_URL, |
| 98 | + headers=AUTHORIZATION_HEADER, |
| 99 | + data={ |
| 100 | + 'type': image_file, |
| 101 | + 'build': BUILD_ID |
| 102 | + }, |
| 103 | + files={'file': BIN_FILE}, |
| 104 | + ) |
| 105 | + assert response.status_code == 201 |
0 commit comments