77 lines
2.7 KiB
Python
Executable File
77 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import logging, sys, os
|
|
import requests as r
|
|
from datetime import date
|
|
from utils import *
|
|
|
|
def main(arch):
|
|
packages = load_config()
|
|
init_directories()
|
|
|
|
commands = []
|
|
diffs = 0
|
|
|
|
# Manage each package
|
|
for p in packages:
|
|
# Retreive tag to apply
|
|
if 'tag' in p:
|
|
logging.debug(f"{p['name']} forced to {p['tag']}")
|
|
elif 'repo' in p:
|
|
p['tag'] = get_tags_from_git(p['repo'])
|
|
else:
|
|
p['tag'] = get_package_version(p['name'], arch)
|
|
|
|
# Retreive current package version
|
|
current_tag = get_package_version(p['name'], arch)
|
|
|
|
# Check if update is required
|
|
if p['tag'] != current_tag:
|
|
logging.info(f"{p['name']} will be updated: {current_tag} → {p['tag']}")
|
|
render_pkgbuild(p['name'], p['tag'])
|
|
commands.append(f"su -c '/tmp/build.sh /tmp/pkgbuilds/{p['name']}' user")
|
|
diffs += 1
|
|
else:
|
|
tarball, url = get_package_tarball(p['name'], arch)
|
|
logging.info(f"{p['name']} will be retreived from {url}")
|
|
content = r.get(url).content
|
|
with open(f"packages/{tarball}", "wb") as file:
|
|
file.write(content)
|
|
commands.append(f"pacman -U --noconfirm /tmp/packages/{tarball}")
|
|
|
|
# Write the installation script
|
|
with open("scripts/run.sh", "w") as file:
|
|
file.write("#!/usr/bin/env bash\n")
|
|
file.write("useradd user\n")
|
|
file.write("echo 'user ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers\n")
|
|
file.write(" && \\\n".join(commands))
|
|
|
|
if diffs == 0:
|
|
logging.info("no updates planned")
|
|
sys.exit(0)
|
|
|
|
# docker_build()
|
|
|
|
|
|
os.popen(f"rsync output/*.pkg.* khazad-dum:/var/lib/nginx/http/arch.middleearth.fr/{arch}/")
|
|
os.popen(f"ssh khazad-dum 'cd /var/lib/nginx/http/arch.middleearth.fr/{arch} && repose -z casio'")
|
|
|
|
# TODO:
|
|
# - create a systemd timer to run this script
|
|
# - setup a web server to publish logs in case of failure
|
|
# - clean the 2 lines above
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description='Build some packages.')
|
|
parser.add_argument('-c', '--config-dir', default="/home/eldeberen/Programmation/MiddleArch/")
|
|
parser.add_argument('--arch', default="x86_64", help='target architecture')
|
|
parser.add_argument('--logs', default="/tmp", help='logs destination') # TODO: add clean path filter
|
|
|
|
args = parser.parse_args()
|
|
|
|
logging.basicConfig(format="[%(asctime)s] %(levelname)s (%(filename)s.%(funcName)s): %(message)s",
|
|
filename=os.path.join(args.logs, date.today().isoformat()), level=logging.INFO)
|
|
|
|
main(args.arch)
|