Mercurial > hg > SupervisorAdmin
annotate 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 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 add supervisor jobs | |
6 """ | |
7 | |
8 # imports | |
9 import argparse | |
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 | 12 import subprocess |
13 import sys | |
14 import time | |
15 | |
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 | 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 |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
26 user={user} |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
27 """ |
0 | 28 |
1
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
29 class SupervisorAdminParser(argparse.ArgumentParser): |
0 | 30 """CLI option parser""" |
31 def __init__(self, **kwargs): | |
32 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) | |
33 kwargs.setdefault('description', __doc__) | |
34 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
|
35 self.add_argument('command', |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
36 help="command to add") |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 help="run program as this user [DEFAULT: %(default)s]") |
0 | 41 self.options = None |
42 | |
43 def parse_args(self, *args, **kw): | |
44 options = argparse.ArgumentParser.parse_args(self, *args, **kw) | |
45 self.validate(options) | |
46 self.options = options | |
47 return options | |
48 | |
49 def validate(self, options): | |
50 """validate options""" | |
51 | |
52 def main(args=sys.argv[1:]): | |
53 """CLI""" | |
54 | |
55 # parse command line options | |
1
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
56 parser = SupervisorAdminParser() |
0 | 57 options = parser.parse_args(args) |
58 | |
1
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
59 # get command |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
60 command = shlex.split(options.command) |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
61 if not command: |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
62 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
|
63 |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
64 # name |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
65 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
|
66 |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
67 print (template.format(name=name, |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
68 user=options.user, |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
69 command=options.command)) |
0 | 70 |
71 if __name__ == '__main__': | |
72 main() | |
73 | |
74 |