Mercurial > hg > SupervisorAdmin
annotate supervisoradmin/add.py @ 5:d638b8578c78
as a service
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 13 Apr 2015 12:33:44 -0700 |
parents | 8a6ee9df8ae5 |
children | a1e06f4a8076 |
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'] |
4
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
18 SUPERVISOR_CONF = '/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 |
3 | 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 | 29 |
4
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
30 def basename(filename): |
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
31 return os.path.splitext(os.path.basename(filename))[0] |
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
32 |
1
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
33 class SupervisorAdminParser(argparse.ArgumentParser): |
0 | 34 """CLI option parser""" |
35 def __init__(self, **kwargs): | |
36 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) | |
37 kwargs.setdefault('description', __doc__) | |
38 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
|
39 self.add_argument('command', |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
40 help="command to add") |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
41 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
|
42 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
|
43 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
|
44 help="run program as this user [DEFAULT: %(default)s]") |
4
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
45 self.add_argument('-o', '--output', dest='output', |
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
46 type=argparse.FileType('w'), |
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
47 nargs='?', |
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
48 const=sys.stdout, |
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
49 help="output configuration and exit [DEFAULT: %(default)s]") |
0 | 50 self.options = None |
51 | |
52 def parse_args(self, *args, **kw): | |
53 options = argparse.ArgumentParser.parse_args(self, *args, **kw) | |
54 self.validate(options) | |
55 self.options = options | |
56 return options | |
57 | |
58 def validate(self, options): | |
59 """validate options""" | |
60 | |
61 def main(args=sys.argv[1:]): | |
62 """CLI""" | |
63 | |
64 # parse command line options | |
1
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
65 parser = SupervisorAdminParser() |
0 | 66 options = parser.parse_args(args) |
67 | |
1
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
68 # get command |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
69 command = shlex.split(options.command) |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
70 if not command: |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
71 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
|
72 |
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
73 # name |
4
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
74 name = options.name or basename(command[0]) |
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
75 |
8a6ee9df8ae5
moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents:
3
diff
changeset
|
76 # output configuration file |
5 | 77 output = options.output or open(os.path.join(SUPERVISOR_CONF, '{}.conf'.format(name)), 'w') |
1
ca51502dc8d3
basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents:
0
diff
changeset
|
78 |
5 | 79 output.write(template.format(name=name, |
80 user=options.user, | |
81 command=options.command)) | |
82 output.close() | |
83 | |
84 if options.output is None: | |
85 # update the supervisor state | |
86 subprocess.check_call(['sudo', 'supervisorctl', 'update']) | |
0 | 87 |
88 if __name__ == '__main__': | |
89 main() |