Icinga/icinga-powershell-framework

Sync-IcingaRepository for Linux

adn77 opened this issue · 2 comments

adn77 commented

I hacked together a small bash script which does the repo synchronisation on Linux.
https://github.com/adn77/Sync-IcingaRepository-Linux

Would it be desirable to offer such a script here as well?

Alex

PS: the script does not calculate the RepoHash for now, I could use some guidance for that 😄

FWIW, this is how https://packages.icinga.com/IcingaForWindows/stable/ifw.repo.json is assembled:

#!/usr/bin/env python3

import json
import re
from collections import OrderedDict
from datetime import datetime
from glob import glob
from hashlib import sha256
from os import chmod, link, path, unlink
from tempfile import NamedTemporaryFile

archs = (
    ('-x86_64', 'x64'),
    ('-x86', 'x86')
)

version = re.compile(r'-v?(\d+(?:\.\d+)+)')
packages = OrderedDict()
files = []
hashes = []

for pattern in ('*/*.msi', '*/*.zip'):
    for file in glob(pattern):
        files.append(file)

for file in sorted(files):
    (kind, basename) = path.split(file)
    v = version.search(basename)

    if not v:
        continue

    with open(file, 'rb') as f:
        hash = sha256()

        while True:
            data = f.read(4096)

            if not data:
                break

            hash.update(data)

        hash = hash.hexdigest().upper()

    for (pattern, arch) in archs:
        if pattern in basename:
            break
    else:
        arch = 'Multi'

    try:
        category = packages[kind]
    except KeyError:
        category = []
        packages[kind] = category

    category.append(OrderedDict((
        ('Hash', hash),
        ('Location', file),
        ('RelativePath', True),
        ('Version', v.group(1)),
        ('Snapshot', False),
        ('Architecture', arch)
    )))

    hashes.append(hash)

now = datetime.now().strftime('%Y/%m/%d %H:%M:%S')

with NamedTemporaryFile('w', dir='.') as f:
    json.dump(
        OrderedDict((
            ('Info', OrderedDict((
                ('Name', 'stable'),
                ('LocalSource', '/var/www'),
                ('RemoteSource', 'https://packages.icinga.com/IcingaForWindows/stable/'),
                ('Created', now),
                ('Updated', now),
                ('RepoHash', sha256('+'.join(hashes).encode()).hexdigest().upper())
            ))),
            ('Packages', packages)
        )),
        f,
        indent=2
    )

    f.flush()
    chmod(f.name, 0o644)

    try:
        unlink('ifw.repo.json')
    except FileNotFoundError:
        pass

    link(f.name, 'ifw.repo.json')

PS: the script does not calculate the RepoHash for now, I could use some guidance for that 😄

At least to mirror a repo 1:1, a recursive wget is enough. E.g. on https://packages.icinga.com/IcingaForWindows/stable/