annotate supervisoradmin/add.py @ 7:aa411d9ff772 default tip

we dont really need which; it is a pain in the ass dep anyway
author Jeff Hammel <k0scist@gmail.com>
date Mon, 13 Apr 2015 15:47:46 -0700
parents a1e06f4a8076
children
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']
4
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
18 SUPERVISOR_CONF = '/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
6
a1e06f4a8076 last minute tweaks
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
24 startretries={retries}
1
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
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
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 """CLI option parser"""
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 def __init__(self, **kwargs):
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 kwargs.setdefault('description', __doc__)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
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")
6
a1e06f4a8076 last minute tweaks
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
43 self.add_argument('-u', '--user', dest='user', default='root',
1
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]")
6
a1e06f4a8076 last minute tweaks
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
45 self.add_argument('--retries', dest='retries',
a1e06f4a8076 last minute tweaks
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
46 type=int, default=100,
a1e06f4a8076 last minute tweaks
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
47 help="number of retries [DEFAULT: %(default)s]")
4
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
48 self.add_argument('-o', '--output', dest='output',
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
49 type=argparse.FileType('w'),
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
50 nargs='?',
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
51 const=sys.stdout,
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
52 help="output configuration and exit [DEFAULT: %(default)s]")
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 self.options = None
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 def parse_args(self, *args, **kw):
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 self.validate(options)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 self.options = options
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 return options
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 def validate(self, options):
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 """validate options"""
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 def main(args=sys.argv[1:]):
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 """CLI"""
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 # parse command line options
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
68 parser = SupervisorAdminParser()
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 options = parser.parse_args(args)
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70
1
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
71 # get command
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
72 command = shlex.split(options.command)
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
73 if not command:
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
74 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
75
ca51502dc8d3 basic template interpolation; we could do so much more!
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
76 # name
4
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
77 name = options.name or basename(command[0])
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
78
8a6ee9df8ae5 moving towards service restart
Jeff Hammel <k0scist@gmail.com>
parents: 3
diff changeset
79 # output configuration file
5
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
80 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
81
5
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
82 output.write(template.format(name=name,
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
83 user=options.user,
6
a1e06f4a8076 last minute tweaks
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
84 retries=options.retries,
5
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
85 command=options.command))
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
86 output.close()
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
87
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
88 if options.output is None:
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
89 # update the supervisor state
d638b8578c78 as a service
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
90 subprocess.check_call(['sudo', 'supervisorctl', 'update'])
0
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
92 if __name__ == '__main__':
e58db0f06ed8 initial commit (stub)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
93 main()