comparison 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
comparison
equal deleted inserted replaced
0:e58db0f06ed8 1:ca51502dc8d3
6 """ 6 """
7 7
8 # imports 8 # imports
9 import argparse 9 import argparse
10 import os 10 import os
11 import shlex
11 import subprocess 12 import subprocess
12 import sys 13 import sys
13 import time 14 import time
14 15
15 # module globals 16 # module globals
16 __all__ = ['main', 'Parser'] 17 __all__ = ['main', 'SupervisorAdminParser']
17 here = os.path.dirname(os.path.realpath(__file__)) 18 SUPERVISOR_CONF_DIR = '/etc/supervisor/conf.d'
18 string = (str, unicode)
19 19
20 def ensure_dir(directory): 20 template = """[program:{name}]
21 """ensure a directory exists""" 21 command={command}
22 if os.path.exists(directory): 22 autorestart=true
23 if not os.path.isdir(directory): 23 redirect_stderr=true
24 raise OSError("Not a directory: '{}'".format(directory)) 24 startretries=3
25 return directory 25 stopasgroup=true
26 os.makedirs(directory) 26 user={user}
27 return directory 27 """
28 28
29 29 class SupervisorAdminParser(argparse.ArgumentParser):
30 class Parser(argparse.ArgumentParser):
31 """CLI option parser""" 30 """CLI option parser"""
32 def __init__(self, **kwargs): 31 def __init__(self, **kwargs):
33 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) 32 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
34 kwargs.setdefault('description', __doc__) 33 kwargs.setdefault('description', __doc__)
35 argparse.ArgumentParser.__init__(self, **kwargs) 34 argparse.ArgumentParser.__init__(self, **kwargs)
36 self.add_argument('--monitor', dest='monitor', 35 self.add_argument('command',
37 type=float, metavar='SLEEP', 36 help="command to add")
38 help="run in monitor mode") 37 self.add_argument('-n', '--name', dest='name',
38 help="name of program; by default taken from command")
39 self.add_argument('-u', '--user', dest='user', default='ubuntu',
40 help="run program as this user [DEFAULT: %(default)s]")
39 self.options = None 41 self.options = None
40 42
41 def parse_args(self, *args, **kw): 43 def parse_args(self, *args, **kw):
42 options = argparse.ArgumentParser.parse_args(self, *args, **kw) 44 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
43 self.validate(options) 45 self.validate(options)
49 51
50 def main(args=sys.argv[1:]): 52 def main(args=sys.argv[1:]):
51 """CLI""" 53 """CLI"""
52 54
53 # parse command line options 55 # parse command line options
54 parser = Parser() 56 parser = SupervisorAdminParser()
55 options = parser.parse_args(args) 57 options = parser.parse_args(args)
56 58
57 try: 59 # get command
58 while True: 60 command = shlex.split(options.command)
59 if options.monitor: 61 if not command:
60 time.sleep(options.monitor) 62 parser.error("Please supply a command")
61 else: 63
62 break 64 # name
63 except KeyboardInterrupt: 65 name = options.name or os.path.basename(command[0])
64 pass 66
67 print (template.format(name=name,
68 user=options.user,
69 command=options.command))
65 70
66 if __name__ == '__main__': 71 if __name__ == '__main__':
67 main() 72 main()
68 73
69 74