dockeraur/pkg-install

72 lines
2 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
from argparse import ArgumentParser
from getpass import getuser
from os import getuid, getgid, getlogin
2019-12-29 12:10:08 -05:00
from shutil import which
import subprocess
parser = ArgumentParser(description="Install AUR packages into a Docker container")
parser.add_argument("package_name", type=str, help="name of the AUR package")
parser.add_argument("-x", type=str, metavar='EXECUTABLE', help="name of executable")
2020-01-01 23:27:39 -05:00
parser.add_argument("-e", type=str, help="add extra step in dockerfile")
args = vars(parser.parse_args())
package_name = args['package_name']
command = args['x']
edit_flag = args['e']
user = getlogin()
uid = getuid()
gid = getgid()
if command is None:
print(f"Assuming executable shares the same name as package_name {package_name}")
command = package_name
dockerfile = f"""FROM archlinux:latest
# Install latest build packages and git
RUN pacman -Sy --noconfirm base-devel git
# Set up new user {user} with passwordless sudo
RUN useradd {user} -u {uid} -m && \\
passwd -d {user} && \\
set -o pipefail && \\
printf '{user} ALL=(ALL) ALL\\n' | tee -a /etc/sudoers
# Set up AUR helper yay
USER {user}
WORKDIR /home/{user}
RUN git clone https://aur.archlinux.org/yay.git
WORKDIR /home/{user}/yay
RUN makepkg -si --noconfirm
#add extra step
2020-01-01 23:23:45 -05:00
{"RUN " + edit_flag if edit_flag is not None else ""}
# Now you can play with whatever package you'd like
RUN yay -S --noconfirm {package_name}
ENTRYPOINT [\"/usr/bin/env\", \"{command}\"]"
"""
2019-12-28 19:38:58 -05:00
# check if either docker or podman are installed
2019-12-28 14:10:43 -05:00
try:
if which("docker") is None:
print("Docker not installed, building with Podman")
engine = "podman"
elif which("podman") is None:
engine = "docker"
else:
print("Podman/Docker are not installed! Exiting...")
exit(1)
subprocess.run([engine, "build", "-t", command, "-"], input = dockerfile, encoding = 'UTF-8')
except FileNotFoundError:
print("An Error has occured while attempting to build the image.")
2019-12-29 12:10:08 -05:00
exit(1)