Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync-IcingaRepository for Linux #666

Open
adn77 opened this issue Sep 29, 2023 · 2 comments
Open

Sync-IcingaRepository for Linux #666

adn77 opened this issue Sep 29, 2023 · 2 comments

Comments

@adn77
Copy link

adn77 commented Sep 29, 2023

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 😄

@Al2Klimov
Copy link
Member

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')

@Al2Klimov
Copy link
Member

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants