MiddleArch/main.py

77 lines
2.7 KiB
Python
Raw Normal View History

2021-06-08 23:08:11 +02:00
#!/usr/bin/env python3
2021-06-09 21:05:24 +02:00
import argparse
2021-06-09 21:17:04 +02:00
import logging, sys, os
2021-06-08 23:08:11 +02:00
import requests as r
2021-06-09 20:40:00 +02:00
from datetime import date
2021-06-08 23:08:11 +02:00
from utils import *
2021-06-09 21:05:24 +02:00
def main(arch):
2021-06-08 23:08:11 +02:00
packages = load_config()
init_directories()
2021-06-08 23:08:11 +02:00
commands = []
diffs = 0
2021-06-08 23:08:11 +02:00
# 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:
2021-06-09 21:05:24 +02:00
p['tag'] = get_package_version(p['name'], arch)
2021-06-08 23:08:11 +02:00
# Retreive current package version
2021-06-09 21:05:24 +02:00
current_tag = get_package_version(p['name'], arch)
2021-06-08 23:08:11 +02:00
# 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
2021-06-08 23:08:11 +02:00
else:
2021-06-09 21:05:24 +02:00
tarball, url = get_package_tarball(p['name'], arch)
2021-06-08 23:08:11 +02:00
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")
2021-06-09 20:21:26 +02:00
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)
2021-06-08 23:08:11 +02:00
# docker_build()
2021-06-09 20:21:26 +02:00
2021-06-09 21:17:04 +02:00
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
2021-06-09 21:17:04 +02:00
# - clean the 2 lines above
2021-06-08 23:08:11 +02:00
if __name__ == "__main__":
2021-06-09 21:05:24 +02:00
parser = argparse.ArgumentParser(description='Build some packages.')
parser.add_argument('-c', '--config-dir', default="/home/eldeberen/Programmation/MiddleArch/")
2021-06-09 21:05:24 +02:00
parser.add_argument('--arch', default="x86_64", help='target architecture')
parser.add_argument('--logs', default="/tmp", help='logs destination') # TODO: add clean path filter
2021-06-09 21:05:24 +02:00
args = parser.parse_args()
2021-06-08 23:08:11 +02:00
logging.basicConfig(format="[%(asctime)s] %(levelname)s (%(filename)s.%(funcName)s): %(message)s",
2021-06-09 21:05:24 +02:00
filename=os.path.join(args.logs, date.today().isoformat()), level=logging.INFO)
main(args.arch)