annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 add supervisor jobs
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import os
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
11 import shlex
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import subprocess
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import sys
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 import time
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 # module globals
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
17 __all__ = ['main', 'SupervisorAdminParser']
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
18 SUPERVISOR_CONF_DIR = '/etc/supervisor/conf.d'
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
20 template = """[program:{name}]
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
21 command={command}
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
22 autorestart=true
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
23 redirect_stderr=true
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
24 startretries=3
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
25 stopasgroup=true
3
350e4298e116 add stopwaitsec
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
26 stopwaitsecs=180
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
27 user={user}
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
28 """
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
30 class SupervisorAdminParser(argparse.ArgumentParser):
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 """CLI option parser"""
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 def __init__(self, **kwargs):
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 kwargs.setdefault('description', __doc__)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 argparse.ArgumentParser.__init__(self, **kwargs)
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
36 self.add_argument('command',
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
37 help="command to add")
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
38 self.add_argument('-n', '--name', dest='name',
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
39 help="name of program; by default taken from command")
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
40 self.add_argument('-u', '--user', dest='user', default='ubuntu',
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
41 help="run program as this user [DEFAULT: %(default)s]")
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 self.options = None
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 def parse_args(self, *args, **kw):
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 self.validate(options)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 self.options = options
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 return options
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 def validate(self, options):
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 """validate options"""
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 def main(args=sys.argv[1:]):
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 """CLI"""
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 # parse command line options
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
57 parser = SupervisorAdminParser()
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 options = parser.parse_args(args)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
60 # get command
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
61 command = shlex.split(options.command)
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
62 if not command:
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
63 parser.error("Please supply a command")
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
64
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
65 # name
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
66 name = options.name or os.path.basename(command[0])
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
67
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
68 print (template.format(name=name,
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
69 user=options.user,
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
70 command=options.command))
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 if __name__ == '__main__':
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 main()
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75