Skip to content

Commit

Permalink
version 0.5.6
Browse files Browse the repository at this point in the history
- option to abort jobs on non-zero exit codes
- option to store job commands as strings instead of list of args
- add --list arg to list jobs
- add --push flag
- add -b flag to git status when run with baka --diff
- add --docker flag for running docker-compose commands in each docker folder without a .ignore file
  • Loading branch information
elesiuta committed Aug 29, 2021
1 parent e8a451d commit 38e029e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ Baka Admin's Kludge Assistant
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
--init open config, init git repo, add files then commit
--commit msg git add -u and commit your changes to tracked files
--push git push (caution, can expose sensitive data)
--install ... install package(s) and commit changes
--remove ... remove package(s) and commit changes
--upgrade upgrade packages on system and commit changes
--docker cmd run docker up|down|pull for each compose file
--job name run commands for job with name
--list show list of jobs
--status run commands to track status of various things
--verify run commands to verify system integrity
--diff show git diff --color-words
Expand Down
45 changes: 42 additions & 3 deletions baka.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,32 @@
import sys
import time

VERSION = "0.5.6"


def init_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Baka Admin's Kludge Assistant",
usage="%(prog)s [--dry-run] <argument>")
parser.add_argument("--version", action="version", version=VERSION)
maingrp = parser.add_mutually_exclusive_group()
maingrp.add_argument("--init", dest="init", action="store_true",
help="open config, init git repo, add files then commit")
maingrp.add_argument("--commit", dest="commit", type=str, metavar="msg",
help="git add -u and commit your changes to tracked files")
maingrp.add_argument("--push", dest="push", action="store_true",
help="git push (caution, can expose sensitive data)")
maingrp.add_argument("--install", dest="install", nargs=argparse.REMAINDER,
help="install package(s) and commit changes")
maingrp.add_argument("--remove", dest="remove", nargs=argparse.REMAINDER, default=None,
help="remove package(s) and commit changes")
maingrp.add_argument("--upgrade", dest="upgrade", action="store_true",
help="upgrade packages on system and commit changes")
maingrp.add_argument("--docker", dest="docker", type=str.lower, metavar="cmd", choices=["up", "down", "pull"],
help="run docker up|down|pull for each compose file")
maingrp.add_argument("--job", dest="job", type=str, metavar="name",
help="run commands for job with name")
maingrp.add_argument("--list", dest="list", action="store_true",
help="show list of jobs")
maingrp.add_argument("--status", dest="status", action="store_true",
help="run commands to track status of various things")
maingrp.add_argument("--verify", dest="verify", action="store_true",
Expand Down Expand Up @@ -86,7 +95,9 @@ def __init__(self):
"to": "[email protected] or null",
"subject": "example subject"
},
"exit_non_zero": False,
"interactive": False,
"shlex_split": False,
"verbosity": "one of: debug (default if null), info, error, silent",
"write": "./jobs/example job %Y-%m-%d %H:%M.log (supports strftime format codes) or null"
}
Expand Down Expand Up @@ -190,6 +201,10 @@ def main() -> int:
*rsync_and_git_add_all(config),
["git", "commit", "-m", "baka commit " + args.commit]
]
elif args.push:
cmds = [
["git", "push"]
]
elif args.install:
cmds = [
*rsync_and_git_add_all(config),
Expand All @@ -214,8 +229,21 @@ def main() -> int:
*rsync_and_git_add_all(config),
["git", "commit", "-m", "baka upgrade"]
]
elif args.docker:
cmd = "up -d" if args.docker == "up" else args.docker
cmds = []
for path in os.listdir("docker"):
if not os.path.exists(os.path.join("docker", path, ".ignore")):
cmds.append(["bash", "-c", "cd docker/%s && sudo docker-compose %s" % (path, cmd)])
elif args.job:
cmds = config.jobs[args.job]["commands"]
elif args.list:
cmds = [
["echo", "Prompt\tExit!0\tJob Name\n========================"],
*[["echo", "%s\t%s\t%s" % ("interactive" in config.jobs[job] and config.jobs[job]["interactive"],
"exit_non_zero" in config.jobs[job] and config.jobs[job]["exit_non_zero"],
job)] for job in config.jobs]
]
elif args.status:
assert ("history" not in config.status_checks)
assert all([key not in config.system_integrity for key in config.status_checks])
Expand All @@ -241,7 +269,7 @@ def main() -> int:
elif args.diff:
cmds = [
*rsync_and_git_add_all(config),
["git", "status", "-s"],
["git", "status", "-sb"],
["git", "diff", "--color-words", "--cached", "--minimal"]
]
elif args.log:
Expand All @@ -257,6 +285,8 @@ def main() -> int:
return_code = 0
try:
for cmd in cmds:
if args.job and "shlex_split" in config.jobs[args.job] and config.jobs[args.job]["shlex_split"]:
cmd = shlex.split(cmd)
if args.dry_run:
print(shlex.join(cmd))
command_output.append("dry-run")
Expand Down Expand Up @@ -295,7 +325,14 @@ def main() -> int:
proc_err = sys.stderr
proc = subprocess.run(cmd, stdout=proc_out, stderr=proc_err)
if proc.returncode != 0:
return_code += 1
if "exit_non_zero" in config.jobs[args.job] and config.jobs[args.job]["exit_non_zero"]:
return_code = proc.returncode
error_message = "Error: baka job encountered a non-zero return code for `%s`, exiting" % shlex.join(cmd)
command_output.append(error_message)
print(error_message, file=sys.stderr)
break
else:
return_code += 1
if capture_output:
if verbosity in ["debug", "info"]:
sys.stdout.buffer.write(proc.stdout)
Expand All @@ -316,7 +353,9 @@ def main() -> int:
print(line)
else:
# run command normally
subprocess.run(cmd)
proc = subprocess.run(cmd)
if proc.returncode != 0:
return_code += 1
except Exception as e:
error_message = "Error baka line: %s For: %s %s %s" % (sys.exc_info()[2].tb_lineno, shlex.join(cmd), type(e).__name__, e.args)
command_output.append(error_message)
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import setuptools
import baka

with open("README.md", "r") as f:
long_description = f.read()

setuptools.setup(
name="bakabakabaka",
version="0.5.5",
version=baka.VERSION,
description="Baka Admin's Kludge Assistant",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 38e029e

Please sign in to comment.