Commits

Brent Tubbs committed 4058ef7

cleaning up stuff

Comments (0)

Files changed (2)

     """
     env.local_root = silk.lib.get_site_root(os.getcwd())
     env.config = silk.lib.get_site_config(env.local_root)
-    if len(env.roles) == 0:
-        sys.exit("ERROR: you must define a role with -R <rolename>")
-    elif len(env.roles) > 1:
-        sys.exit("ERROR: Silk only permits passing in one role at a time")
-    else:
+    if len(env.roles) == 1:
         env.config.update(silk.lib.get_role_config(env.roles[0]))
-    env.site = env.config['site']
-    env.remote_root = '/'.join([SRV_ROOT, env.site])
-    env.envdir = '/'.join([env.remote_root, 'env'])
-    env.workdir = '/'.join(['/tmp', env.site])
-    env.rollbackdir = '/'.join([SRV_ROOT, 'rollbacks'])
-    env.deploytime = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
-    env.config['bind'] = env.config['gunicorn'].get('bind', 'unix:/tmp/%s.sock'
-                                                    % env.site)
-    env.config['silk_deps'] = _get_silk_deps()
-    env.config['site_deps'] = _get_site_deps(env.local_root)
+        env.site = env.config['site']
+        env.remote_root = '/'.join([SRV_ROOT, env.site])
+        env.envdir = '/'.join([env.remote_root, 'env'])
+        env.workdir = '/'.join(['/tmp', env.site])
+        env.rollbackdir = '/'.join([SRV_ROOT, 'rollbacks'])
+        env.deploytime = datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
+        env.config['bind'] = env.config['gunicorn'].get('bind', 'unix:/tmp/%s.sock'
+                                                        % env.site)
+        env.config['silk_deps'] = _get_silk_deps()
+        env.config['site_deps'] = _get_site_deps(env.local_root)
 
 _set_vars()
 
     args[0] = 'fab'
     run_til_you_die(args, SIGTERM)
 
-def run_devserver(config, root):
+def run_devserver():
     # Overwrite the wsgi_app config var to point to our internal app that will
     # also mount the static dirs.
+    root = os.environ['SILK_ROOT']
+    role = os.environ['SILK_ROLE']
+    config = silk.lib.get_config(root, role)
     config['wsgi_app'] = 'silk.devserver:app'
     
     cmd = silk.lib.get_gunicorn_cmd(config)
 
 def install_skel(sitename):
     """Copies the contents of site_templates into the named directory (within cwd)"""
+    root = os.environ['SILK_ROOT']
     #get the dir from pkg_resources
     src = pkg_resources.resource_filename('silk', 'site_templates')
     try:
     deps = yaml.safe_load(txt)
     print '\n'.join(deps['python_packages'])
 
-def local_python_deps(config, root):
+def local_python_deps():
     """Write a requirements.txt from deps.yaml file for pip, then run pip on it."""
+    root = os.environ['SILK_ROOT']
+    role = os.environ['SILK_ROLE']
+    config = silk.lib.get_config(root, role)
     depfile = os.path.join(silk.lib.get_site_root(os.getcwd()), 'deps.yaml')
     txt = open(depfile).read()
     deps = yaml.safe_load(txt)
     'deps': local_python_deps
 }
 
-config_required = (
-    run_devserver,
-    local_python_deps,
-)
-
 def cmd_dispatcher():
     """wraps 'fab', handles 'silk run'"""
     args = sys.argv
     try:
         cmd = args[1]
+
+        # If a command is provided by cmd_map, use that.  Else pass through to
+        # fabric.
+        if cmd in cmd_map:
+            # Stick some information about the role and root into the current env,
+            # then call the local function in cmd_map.
+            os.environ['SILK_ROLE'] = silk.lib.get_role() or '' 
+            os.environ['SILK_ROOT'] = silk.lib.get_site_root(os.getcwd()) or ''
+            cmd_map[cmd]()
+        else:
+            # Use project-provided fabfile if present, else the one built into
+            # Silk.  We'll have to trust that the project file imports ours.
+            root = silk.lib.get_site_root(os.getcwd())
+            site_fab = os.path.join(root, 'fabfile.py')
+            if os.path.isfile(site_fab):
+                fabfile = site_fab
+            else:
+                fabfile = pkg_resources.resource_filename('silk', 'fabfile.py')
+            args.extend(['--fabfile', fabfile])
+            run_fab(args)
+
     except IndexError:
-        run_fab(*args)
+        # Print out help text.  Currently just prints it for the cmds specified
+        # in the fabfile, which isn't great because it omits things like 'silk
+        # run' and 'silk deps'.  Would be better to inspect the fabfile and
+        # list the cmds/docstrings myself, right along the non-fabfile cmds
+        # that Silk provides.  Or I could just make all the things I provide as
+        # fab cmds.  That might be the simpler approach.
+        run_fab(['fab', '-l'])
 
-    if cmd in cmd_map:
-        cmd_func = cmd_map[cmd]
-        if cmd_func in config_required:
-            role = silk.lib.get_role()
-            root = silk.lib.get_site_root(os.getcwd())
-            config = silk.lib.get_config(root, role)
-            cmd_func(config, root)
-        else:
-            cmd_func(*args[2:])
-    else:
-        run_fab(args)
+# OK there's a problem with cmds that require arguments.  Some do, some don't,
+# and it's unclear whether I should be sending those on all or none or
+# distinguishing.