changeset 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 1792871b4302
files supervisoradmin/add.py
diffstat 1 files changed, 30 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/supervisoradmin/add.py	Tue Jan 27 14:01:52 2015 -0800
+++ b/supervisoradmin/add.py	Tue Jan 27 15:33:25 2015 -0800
@@ -8,34 +8,36 @@
 # imports
 import argparse
 import os
+import shlex
 import subprocess
 import sys
 import time
 
 # module globals
-__all__ = ['main', 'Parser']
-here = os.path.dirname(os.path.realpath(__file__))
-string = (str, unicode)
+__all__ = ['main', 'SupervisorAdminParser']
+SUPERVISOR_CONF_DIR = '/etc/supervisor/conf.d'
 
-def ensure_dir(directory):
-    """ensure a directory exists"""
-    if os.path.exists(directory):
-        if not os.path.isdir(directory):
-            raise OSError("Not a directory: '{}'".format(directory))
-        return directory
-    os.makedirs(directory)
-    return directory
+template = """[program:{name}]
+command={command}
+autorestart=true
+redirect_stderr=true
+startretries=3
+stopasgroup=true
+user={user}
+"""
 
-
-class Parser(argparse.ArgumentParser):
+class SupervisorAdminParser(argparse.ArgumentParser):
     """CLI option parser"""
     def __init__(self, **kwargs):
         kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
         kwargs.setdefault('description', __doc__)
         argparse.ArgumentParser.__init__(self, **kwargs)
-        self.add_argument('--monitor', dest='monitor',
-                          type=float, metavar='SLEEP',
-                          help="run in monitor mode")
+        self.add_argument('command',
+                          help="command to add")
+        self.add_argument('-n', '--name', dest='name',
+                          help="name of program; by default taken from command")
+        self.add_argument('-u', '--user', dest='user', default='ubuntu',
+                          help="run program as this user [DEFAULT: %(default)s]")
         self.options = None
 
     def parse_args(self, *args, **kw):
@@ -51,17 +53,20 @@
     """CLI"""
 
     # parse command line options
-    parser = Parser()
+    parser = SupervisorAdminParser()
     options = parser.parse_args(args)
 
-    try:
-        while True:
-            if options.monitor:
-                time.sleep(options.monitor)
-            else:
-                break
-    except KeyboardInterrupt:
-        pass
+    # get command
+    command = shlex.split(options.command)
+    if not command:
+        parser.error("Please supply a command")
+
+    # name
+    name = options.name or os.path.basename(command[0])
+
+    print (template.format(name=name,
+                           user=options.user,
+                           command=options.command))
 
 if __name__ == '__main__':
     main()