Mercurial > hg > SupervisorAdmin
view supervisoradmin/add.py @ 3:350e4298e116
add stopwaitsec
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 09 Feb 2015 13:11:09 -0800 |
parents | ca51502dc8d3 |
children | 8a6ee9df8ae5 |
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 stopwaitsecs=180 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()