diff options
-rwxr-xr-x | ssp | 189 |
1 files changed, 189 insertions, 0 deletions
@@ -0,0 +1,189 @@ +#!/usr/bin/env python3 +# File: /home/bgstack15/ssp +# Author: bgstack15 +# Startdate: 2019-05-20 +# Title: Self Service Portal +# Purpose: Think "Software Center" where users can install software from a named repository without having to ask an admin +# History: +# Usage: +# Reference: +# modconf.py +# custom tostring for list https://stackoverflow.com/questions/727761/python-str-and-lists +# https://stackoverflow.com/questions/4426663/how-to-remove-the-first-item-from-a-list +# https://stackoverflow.com/questions/1770209/run-child-processes-as-different-user-from-a-long-running-python-process +# Improve: +# Documentation: +# ssp commands.xlsx + +sspversion="2019-05-20a" + +import argparse, sys, os, subprocess, re +sys.path.append("/usr/share/bgscripts/py") +from bgs import debuglev, eprint + +parser = argparse.ArgumentParser() +parser.add_argument("-d","--debug", nargs='?', default=0, type=int, choices=range(0,11), help="Set debug level.") +#parser.add_argument("-v","--verbose",help="displays output", action="store_true",default=False) +#parser.add_argument("-a","--apply", help="perform substitution", action="store_true",default=False) +parser.add_argument("-y","--assumeyes", help="accept all questions with yes", action="store_true",default=False) +parser.add_argument("-V","--version", action="version", version="%(prog)s " + sspversion) +parser.add_argument("positionals", type=str, nargs='+',help='commands and package names') +#parser.add_argument("infile", default="", help="file to use") +#parser.add_argument("action", default="", help='[ add | remove | set | gone | empty ]') +#parser.add_argument("variable", default="") +#parser.add_argument("item", default="") +args = parser.parse_args() + +# Configure variables after parameters +debuglevel=0 +if args.debug is None: + # -d was used but no value provided + debuglevel = 10 +elif args.debug: + debuglevel = args.debug + +# global variables +package_manager = "yum" +allowed_repos = [ "hosting" ] +#allowed_repos_string = ",".join(map(str, allowed_repos)) +params = args.positionals +assume = True if args.assumeyes else False + +#for repo in allowed_repos: +# for item in params: +# print("check if %s is in %s" % (item,repo)) + +# WORKHERE: check for sudo + +# wrap around yum list [*], yum list installed [*], yum list available [*] +def list_(action, pkg_mgr, args, repos): + command_array=["yum","-q","list"] + __return=[] + # add either installed or available + if action != "list": + command_array.append(action) + if action == "installed": + regex = re.compile(".*(@"+"|@".join(map(str,repos))+").*") + else: + # for list and available + regex = re.compile(".*("+"|".join(map(str,repos))+").*") + for item in args: + command_array.append(item) + if debuglev(5,debuglevel): eprint(command_array) + if pkg_mgr == "yum": + try: + __output = subprocess.check_output(command_array, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + __output = "" + for line in __output.splitlines(): + if regex.match(str(line)): + __return.append(line.decode('UTF-8','backslashreplace')) + elif pkg_mgr == "apt": + eprint("list_ for " + pkg_mgr + "not yet implemented.") + sys.exit(1) + return __return + +# wrap around yum install [*], +def install_(action, pkg_mgr, args, repos, assume): + command_array=["yum","--enablerepo="+",".join(map(str, repos)),action] + if assume: + command_array.insert(1,"-y") + for item in args: + command_array.append(item) + if debuglev(5,debuglevel): eprint(command_array) + if pkg_mgr == "yum": + __output = "" + try: + __completedprocess = subprocess.run(command_array) + pass + except subprocess.CalledProcessError as e: + pass + print(__completedprocess.stdout) + #for line in __output.splitlines(): + # if regex.match(str(line)): + # print(line.decode('UTF-8','backslashreplace')) + elif pkg_mgr == "apt": + eprint("list_ for " + pkg_mgr + "not yet implemented.") + sys.exit(1) + +# wrap around yum remove [*], +def remove_(pkg_mgr, args, repos, assume): + command_array=["yum","remove"] + if assume: + command_array.insert(1,"-y") + for item in args: + command_array.append(item) + if debuglev(5,debuglevel): eprint(command_array) + if pkg_mgr == "yum": + __output = "" + try: + __completedprocess = subprocess.run(command_array) + pass + except subprocess.CalledProcessError as e: + pass + print(__completedprocess.stdout) + #for line in __output.splitlines(): + # if regex.match(str(line)): + # print(line.decode('UTF-8','backslashreplace')) + elif pkg_mgr == "apt": + eprint("list_ for " + pkg_mgr + "not yet implemented.") + sys.exit(1) + +# wrap around yum clean cache for approved repos +def clean_(pkg_mgr, args, repos, assume): + command_array=["yum","--disablerepo=*","--enablerepo="+",".join(map(str, repos)),"clean","metadata"] + if debuglev(5,debuglevel): eprint(command_array) + if pkg_mgr == "yum": + __output = "" + try: + __output = subprocess.check_output(command_array, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + pass + #print(__output) + for line in __output.splitlines(): + print(line.decode('UTF-8','backslashreplace')) + elif pkg_mgr == "apt": + eprint("remove_ for " + pkg_mgr + "not yet implemented.") + sys.exit(1) + +# wrap around yum info [*] +def info_(pkg_mgr, args, repos, assume): + command_array=["yum","--disablerepo=*","--enablerepo="+",".join(map(str, repos)),"info"] + for item in args: + command_array.append(item) + if debuglev(5,debuglevel): eprint(command_array) + if pkg_mgr == "yum": + __output = "" + try: + __output = subprocess.check_output(command_array, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + pass + for line in __output.splitlines(): + print(line.decode('UTF-8','backslashreplace')) + elif pkg_mgr == "apt": + eprint("info_ for " + pkg_mgr + "not yet implemented.") + sys.exit(1) + +# MAIN +if len(params) >= 1: + if params[0] == "list": + if len(params) >= 2 and ( params[1] == "installed" or params[1] == "available"): + params.pop(0) + action = params.pop(0) + for item in list_(action, package_manager, params, allowed_repos): + print(item) + else: + params.pop(0) + for item in list_("list", package_manager, params, allowed_repos): + print(item) + elif params[0] in ["install","update","upgrade"]: + action = params.pop(0) + install_(action, package_manager, params, allowed_repos, assume) + elif params[0] == "remove": + params.pop(0) + remove_(package_manager, params, allowed_repos, assume) + elif params[0] == "clean": + clean_(package_manager, params, allowed_repos, assume) + elif params[0] == "info": + params.pop(0) + info_(package_manager, params, allowed_repos, assume) |