comparison supervisoradmin/add.py @ 4:8a6ee9df8ae5

moving towards service restart
author Jeff Hammel <k0scist@gmail.com>
date Mon, 13 Apr 2015 12:04:00 -0700
parents 350e4298e116
children d638b8578c78
comparison
equal deleted inserted replaced
3:350e4298e116 4:8a6ee9df8ae5
13 import sys 13 import sys
14 import time 14 import time
15 15
16 # module globals 16 # module globals
17 __all__ = ['main', 'SupervisorAdminParser'] 17 __all__ = ['main', 'SupervisorAdminParser']
18 SUPERVISOR_CONF_DIR = '/etc/supervisor/conf.d' 18 SUPERVISOR_CONF = '/etc/supervisor/conf.d'
19 19
20 template = """[program:{name}] 20 template = """[program:{name}]
21 command={command} 21 command={command}
22 autorestart=true 22 autorestart=true
23 redirect_stderr=true 23 redirect_stderr=true
24 startretries=3 24 startretries=3
25 stopasgroup=true 25 stopasgroup=true
26 stopwaitsecs=180 26 stopwaitsecs=180
27 user={user} 27 user={user}
28 """ 28 """
29
30 def basename(filename):
31 return os.path.splitext(os.path.basename(filename))[0]
29 32
30 class SupervisorAdminParser(argparse.ArgumentParser): 33 class SupervisorAdminParser(argparse.ArgumentParser):
31 """CLI option parser""" 34 """CLI option parser"""
32 def __init__(self, **kwargs): 35 def __init__(self, **kwargs):
33 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) 36 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
37 help="command to add") 40 help="command to add")
38 self.add_argument('-n', '--name', dest='name', 41 self.add_argument('-n', '--name', dest='name',
39 help="name of program; by default taken from command") 42 help="name of program; by default taken from command")
40 self.add_argument('-u', '--user', dest='user', default='ubuntu', 43 self.add_argument('-u', '--user', dest='user', default='ubuntu',
41 help="run program as this user [DEFAULT: %(default)s]") 44 help="run program as this user [DEFAULT: %(default)s]")
45 self.add_argument('-o', '--output', dest='output',
46 type=argparse.FileType('w'),
47 nargs='?',
48 const=sys.stdout,
49 help="output configuration and exit [DEFAULT: %(default)s]")
42 self.options = None 50 self.options = None
43 51
44 def parse_args(self, *args, **kw): 52 def parse_args(self, *args, **kw):
45 options = argparse.ArgumentParser.parse_args(self, *args, **kw) 53 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
46 self.validate(options) 54 self.validate(options)
61 command = shlex.split(options.command) 69 command = shlex.split(options.command)
62 if not command: 70 if not command:
63 parser.error("Please supply a command") 71 parser.error("Please supply a command")
64 72
65 # name 73 # name
66 name = options.name or os.path.basename(command[0]) 74 name = options.name or basename(command[0])
75
76 # output configuration file
77 output = options.output or os.path.join(SUPERVISOR_CONF, '{}.conf'.format(name))
67 78
68 print (template.format(name=name, 79 print (template.format(name=name,
69 user=options.user, 80 user=options.user,
70 command=options.command)) 81 command=options.command))
71 82
72 if __name__ == '__main__': 83 if __name__ == '__main__':
73 main() 84 main()
74
75