Colin Copeland avatar Colin Copeland committed 2954011

add support (now required) for loading modules off the Python path; add cloud command to list providers, images, sizes, etc.

Comments (0)

Files changed (2)

 
 def chown(path, user):    
     call(['chown', '%s:%s' % (user, user), path])
+
+
+def get_mallet_template(path):
+    try:
+        dot = path.rindex('.')
+    except ValueError:
+        raise ValueError('Please provice a template on the Python path')
+    module_name, class_name = path[:dot], path[dot+1:]
+    __import__(module_name)
+    module = sys.modules[module_name]
+    class_ = getattr(module, class_name)
+    return class_()
 import ConfigParser
 import string
 import getpass
+import pprint
 from optparse import OptionParser
 
 from random import choice
 from libcloud.providers import get_driver
 from libcloud.base import ConnectionKey, ConnectionUserAndKey, NodeAuthPassword
 
-from mallet.utils import call
+from mallet.utils import call, get_mallet_template
 from mallet.bootstrap import apt
 from mallet.bootstrap.apt import lenny
 from mallet import colors
     if options.password:
         env.password = getpass.getpass()
     env.cfg = DriversConfig('drivers.cfg')
-    
-    from client_vm import SampleTemplate
-    template = SampleTemplate()
-    
+
+    if command == 'cloud':
+        if len(positional_args) == 1:
+            print "Available drivers:"
+            for driver in env.cfg.drivers():
+                print driver.name
+            sys.exit()
+        driver_name = positional_args[1]
+        for driver in env.cfg.drivers():
+            if driver_name == driver.name:
+                print 'Using %s' % driver.name
+                if len(positional_args) == 3:
+                    list_cmd = positional_args[2]
+                    items = list(getattr(driver, 'list_' + list_cmd)())
+                    pprint.pprint(items)        
+        sys.exit()
+
+    template = get_mallet_template(positional_args[1])
+    logger.info('Loaded template %s' % template)
+
     node_wrapper = None
-    if len(positional_args) > 1:
+    if len(positional_args) > 2:
         server_cfg = ConfigParser.RawConfigParser()
-        server_cfg.read(positional_args[1])
+        server_cfg.read(positional_args[2])
         if server_cfg.has_option('server', 'uuid'):
             uuid = server_cfg.get('server', 'uuid')
             node_wrapper = template.find_node(uuid)
         template.create_instance(options.name)
     elif command == 'install' and node_wrapper:
         command_args = []
-        if len(positional_args) > 2:
-            command_args = positional_args[2:]
+        if len(positional_args) > 3:
+            command_args = positional_args[3:]
         template.install_packages(node_wrapper.node, *command_args)
     elif node_wrapper:
         command_args = []
-        if len(positional_args) > 2:
-            command_args = positional_args[2:]
+        if len(positional_args) > 3:
+            command_args = positional_args[3:]
         if not hasattr(node_wrapper, command):
             logging.error('Unknown command %s' % command)
             sys.exit(-1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.