controller.py 2.16 KB
Newer Older
1
#!/usr/bin/env python
2
# -*- encoding; utf-8 -*-
3
4
5
6
7
8
9
10
11
12
13
14
#
# Author: Christian Rebischke <chris.rebischke@archlinux.org>
# This file is licensed under GPLv3

import urllib.request
import json
import datetime
import sys
import subprocess
import os.path

API_URL = 'https://app.vagrantup.com/api/v1/box/archlinux/archlinux'
Christian Rebischke's avatar
Christian Rebischke committed
15
NOW = datetime.date.today()
16
17
LEN_RELEASES = 2
CWD = '/srv/arch-boxes/arch-boxes'
18
ISO_PATH = '/srv/ftp/iso/latest/archlinux-' + NOW.strftime(
19
20
21
    "%Y.%m") + '.01-x86_64.iso'
ISO_CHECKSUM_PATH = '/srv/ftp/iso/latest/sha1sums.txt'
PACKER_CMD_TEMPLATE = [
Christian Rebischke's avatar
Christian Rebischke committed
22
    "/usr/bin/packer", "build", "-parallel=false", "-var", "'headless=true'",
23
    "-var", "'write_zeroes=yes'", "-except=vmware-iso", "vagrant.json"
24
25
]

26

27
def main():
28
    exit_if_resources_present()
29
30
31
32
33
34
35
36
37
38
39
    with urllib.request.urlopen(API_URL) as response:
        data = json.load(response)
        release_version = data['current_version']['version']
        release_providers = data['current_version']['providers']
        if not is_latest(release_version):
            subprocess.call(PACKER_CMD_TEMPLATE, cwd=CWD)
        else:
            if not all_released(release_providers):
                determine_missing_release(release_providers)


40
def exit_if_resources_present():
41
42
43
    if os.path.exists(ISO_PATH) and os.path.exists(ISO_CHECKSUM_PATH):
        pass
    else:
44
        sys.exit(0)
45
46
47


def build_packer_call(provider):
48
    provider_map = {"virtualbox": "virtualbox", "libvirt": "qemu"}
49
50
51
52
    packer = PACKER_CMD_TEMPLATE.copy()
    packer[7] += ","
    packer[7] += provider_map[provider]
    return packer
53
54


55
56
57
def determine_missing_release(release_providers):
    subprocess.call(build_packer_call(release_providers[0]['name']), cwd=CWD)

58

59
def is_latest(release_version):
Christian Rebischke's avatar
Christian Rebischke committed
60
61
62
63
64
65
    # we need to use .date() here, otherwise the compare is going to fail
    release = datetime.datetime.strptime(release_version, "%Y.%m.%d").date()
    # set the day to 1, because we only want to check for month and year
    release = release.replace(day=1)
    current_release = NOW.replace(day=1)
    return current_release <= release
66
67
68
69
70
71
72
73


def all_released(release_providers):
    return LEN_RELEASES <= len(release_providers)


if __name__ == '__main__':
    main()