Skip to content

Commit

Permalink
Clean up cli.py (#27)
Browse files Browse the repository at this point in the history
* fix import

* Add whitespace between function defs and ends

* Add more whitespace for clarity

* Remove required=False since that's the default

* Apparently pytest treats imports differently?

* Specify defaults in the argument definitions

* Remove unused import

* Since we're importing 2 things from sys, just put it on one line

* Add whitespace

* Add whitespace

* Add whitespace
  • Loading branch information
tlkamp authored and circa10a committed Oct 24, 2018
1 parent 0c33d47 commit 50fb321
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
35 changes: 23 additions & 12 deletions ouroboros/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
run_once = None
cleanup = None


def checkURI(uri):
"""Validate tcp:// regex"""
regex = re.compile( r"""(?xi) # "verbose" mode & case-insensitive
Expand Down Expand Up @@ -42,6 +43,7 @@ def checkURI(uri):
""")
return re.match(regex, uri)


def get_interval_env():
"""Attempt to convert INTERVAL environment variable to int"""
int_env = environ.get('INTERVAL')
Expand All @@ -50,25 +52,34 @@ def get_interval_env():
except:
return False


def parser(sysargs):
"""Declare command line options"""
global host, interval, monitor, loglevel, api_client, run_once, cleanup
parser = argparse.ArgumentParser(description='ouroboros', epilog='Example: python3 main.py -u tcp://1.2.3.4:5678 -i 20 -m container1 container2 -l warn')
parser.add_argument('-u', '--url', help='Url for tcp host (defaults to "unix://var/run/docker.sock")', required=False)
parser.add_argument('-i', '--interval', type=int, help='Interval in seconds between checking for updates (defaults to 300s)', required=False)
parser.add_argument('-m', '--monitor', nargs='+', help='Which container to monitor (defaults to all running).', required=False)
parser.add_argument('-l', '--loglevel', choices=['notset', 'debug', 'info', 'warn', 'error', 'critical'], help='Change logger mode (defaults to info)', required=False)
parser.add_argument('-r', '--runonce', help='Only run ouroboros once then exit', action='store_true', required=False)
parser.add_argument('-c', '--cleanup', help='Remove old images after updating', action='store_true', required=False)
parser = argparse.ArgumentParser(description='ouroboros',
epilog='Example: python3 main.py -u tcp://1.2.3.4:5678 -i 20 -m container1 container2 -l warn')
parser.add_argument('-u', '--url', help='Url for tcp host (defaults to "unix://var/run/docker.sock")')
parser.add_argument('-i', '--interval', type=int, default=get_interval_env() or defaults.INTERVAL,
help='Interval in seconds between checking for updates (defaults to 300s)')
parser.add_argument('-m', '--monitor', nargs='+', default=environ.get('MONITOR') or [],
help='Which container to monitor (defaults to all running).')
parser.add_argument('-l', '--loglevel', choices=['notset', 'debug', 'info', 'warn', 'error', 'critical'],
default=environ.get('LOGLEVEL') or 'info', help='Change logger mode (defaults to info)')
parser.add_argument('-r', '--runonce', default=environ.get('RUNONCE') or False,
help='Only run ouroboros once then exit', action='store_true')
parser.add_argument('-c', '--cleanup', default=environ.get('CLEANUP') or False,
help='Remove old images after updating', action='store_true')
args = parser.parse_args(sysargs)

if args.url:
host = args.url
if not checkURI(host):
host = defaults.LOCAL_UNIX_SOCKET
interval = args.interval or get_interval_env() or defaults.INTERVAL
monitor = args.monitor or environ.get('MONITOR') or []
loglevel = args.loglevel or environ.get('LOGLEVEL') or 'info'
run_once = args.runonce or environ.get('RUNONCE') or False
cleanup = args.cleanup or environ.get('CLEANUP') or False

interval = args.interval
monitor = args.monitor
loglevel = args.loglevel
run_once = args.runonce
cleanup = args.cleanup
api_client = docker.APIClient(base_url=host)
return args
7 changes: 4 additions & 3 deletions ouroboros/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python3
from sys import argv
from sys import exit
from sys import argv, exit
import time
import datetime
import logging
import docker
import schedule
Expand All @@ -11,6 +9,7 @@
import cli
from logger import set_logger


def main():
"""Find running containers and update them with images using latest tag"""
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -41,10 +40,12 @@ def main():
if cli.run_once:
exit(0)


if __name__ == "__main__":
cli.parser(argv[1:])
logging.basicConfig(**set_logger(cli.loglevel))
schedule.every(cli.interval).seconds.do(main)

while True:
schedule.run_pending()
time.sleep(1)

0 comments on commit 50fb321

Please sign in to comment.