Mercurial > hg > SupervisorAdmin
view supervisoradmin/add.py @ 1:ca51502dc8d3
basic template interpolation; we could do so much more!
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 27 Jan 2015 15:33:25 -0800 |
parents | e58db0f06ed8 |
children | 350e4298e116 |
line wrap: on
line source
#!/usr/bin/env python # -*- coding: utf-8 -*- """ add supervisor jobs """ # imports import argparse import os import shlex import subprocess import sys import time # module globals __all__ = ['main', 'SupervisorAdminParser'] SUPERVISOR_CONF_DIR = '/etc/supervisor/conf.d' template = """[program:{name}] command={command} autorestart=true redirect_stderr=true startretries=3 stopasgroup=true user={user} """ class SupervisorAdminParser(argparse.ArgumentParser): """CLI option parser""" def __init__(self, **kwargs): kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) kwargs.setdefault('description', __doc__) argparse.ArgumentParser.__init__(self, **kwargs) self.add_argument('command', help="command to add") self.add_argument('-n', '--name', dest='name', help="name of program; by default taken from command") self.add_argument('-u', '--user', dest='user', default='ubuntu', help="run program as this user [DEFAULT: %(default)s]") self.options = None def parse_args(self, *args, **kw): options = argparse.ArgumentParser.parse_args(self, *args, **kw) self.validate(options) self.options = options return options def validate(self, options): """validate options""" def main(args=sys.argv[1:]): """CLI""" # parse command line options parser = SupervisorAdminParser() options = parser.parse_args(args) # get command command = shlex.split(options.command) if not command: parser.error("Please supply a command") # name name = options.name or os.path.basename(command[0]) print (template.format(name=name, user=options.user, command=options.command)) if __name__ == '__main__': main()