|
| 1 | +import toml |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import logging |
| 5 | +import errno |
| 6 | + |
| 7 | +methods = { |
| 8 | + "apt": "$APT_GET", |
| 9 | + "yum": "$YUM", |
| 10 | + "dnf": "$DNF", |
| 11 | + "apk": "$APK", |
| 12 | + "pacman": "$PACMAN", |
| 13 | + "git": "$GIT" |
| 14 | +} |
| 15 | + |
| 16 | +def get_method_case(method): |
| 17 | + if method in methods: |
| 18 | + return "[ ! -z "+methods[method]+"_CMD ]; then\n" |
| 19 | + else: |
| 20 | + logging.error('Unpupported method in the TOML file, method: '+method) |
| 21 | + exit(1) |
| 22 | + |
| 23 | +def parse_line(line): |
| 24 | + line = line.replace('@sudo', '$SUDO') |
| 25 | + return line |
| 26 | + |
| 27 | +def generate(path): |
| 28 | + |
| 29 | + installer_toml_path = path+"/installer.toml" |
| 30 | + installer_sh_path = path+"/installer.sh" |
| 31 | + |
| 32 | + installer_toml = open(installer_toml_path, "r") |
| 33 | + parsed_toml = toml.loads(installer_toml.read()) |
| 34 | + try: |
| 35 | + with open(installer_sh_path, "w") as installer_sh: |
| 36 | + |
| 37 | + installer_sh.write("""#!/bin/sh |
| 38 | + |
| 39 | +YUM_CMD=$(which yum) # yum package manager for RHEL & CentOS |
| 40 | +DNF_CMD=$(which dnf) # dnf package manager for new RHEL & CentOS |
| 41 | +APT_GET_CMD=$(which apt-get) # apt package manager for Ubuntu & other Debian based distributions |
| 42 | +PACMAN_CMD=$(which pacman) # pacman package manager for ArchLinux |
| 43 | +APK_CMD=$(which apk) # apk package manager for Alpine |
| 44 | +GIT_CMD=$(which git) # to build from source pulling from git |
| 45 | +SUDO_CMD=$(which sudo) # check if sudo command is there |
| 46 | +
|
| 47 | +USER="$(id -un 2>/dev/null || true)" |
| 48 | +SUDO='' |
| 49 | +if [ "$USER" != 'root' ]; then |
| 50 | + if $SUDO_CMD; then |
| 51 | + SUDO='sudo' |
| 52 | + else |
| 53 | + cat >&2 <<-'EOF' |
| 54 | + Error: this installer needs the ability to run commands as root. |
| 55 | + We are unable to find "sudo". Make sure its available to make this happen |
| 56 | + EOF |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +fi |
| 60 | +
|
| 61 | +""") |
| 62 | + |
| 63 | + seperator = "if" |
| 64 | + |
| 65 | + for section in parsed_toml: |
| 66 | + lines = parsed_toml[section]['sh'] |
| 67 | + installer_sh.write(seperator+" "+get_method_case(section)) |
| 68 | + for line in lines.split("\n"): |
| 69 | + step = parse_line(line) |
| 70 | + installer_sh.write(" "+step+"\n") |
| 71 | + seperator = "elif" |
| 72 | + |
| 73 | + installer_sh.write(""" |
| 74 | +else |
| 75 | + echo "Couldn't install package" |
| 76 | + exit 1; |
| 77 | +fi |
| 78 | + """.strip()) |
| 79 | + |
| 80 | + installer_sh.close() |
| 81 | + |
| 82 | + except IOError as x: |
| 83 | + if x.errno == errno.EACCES: |
| 84 | + logging.error('No enough permissions to write to '+installer_sh_path) |
| 85 | + exit(1) |
| 86 | + else: |
| 87 | + logging.error('Something went wrong when trying to write to '+installer_sh_path) |
| 88 | + exit(1) |
| 89 | + |
| 90 | +for path in sys.argv[1:]: |
| 91 | + if os.path.exists(path+'/installer.toml'): |
| 92 | + logging.info('Generating installer.sh for '+path) |
| 93 | + generate(path) |
| 94 | + else: |
| 95 | + logging.warn('Could not find an installer.toml in '+path) |
0 commit comments