Commits

Darren Pearce committed 3fe7964

adding support for local or remote usage

  • Participants
  • Parent commits f9fbd50

Comments (0)

Files changed (1)

baste/__init__.py

         local,
         run,
         settings,
+        sudo,
     )
 from fabric import colors
 from fabric.contrib.project import rsync_project
     'Subversion',
 ]
 
+LOCAL_SCOPE_ENV = True
+#-------------------------------------------------------------------------------
+def run_scoped(cmd, *args , **kwargs):
+    """
+    Runs the specified command on the local machine unless LOCAL_SCOPE_ENV 
+    is False then it will run against the current fab ENV
+    """
+    if LOCAL_SCOPE_ENV:
+        local(cmd,*args, **kwargs)
+    else:
+        run(cmd, *args, **kwargs)
+
+def sudo_scoped(cmd, *args, **kwargs):
+    """
+    Runs the specified command as `sudo` on the local machine unless LOCAL_SCOPE_ENV
+    is False then it runs against the current fab ENV.
+    """
+    print LOCAL_SCOPE_ENV
+
+    if LOCAL_SCOPE_ENV:
+        cmd = """sudo -u %s sh -c "%s" """ % (kwargs['user'], cmd)
+        local(cmd, *args)
+    else:
+        sudo(cmd, *args, **kwargs)
+
 #-------------------------------------------------------------------------------
 def project_relative(path):
     return os.path.join(os.path.dirname(env.real_fabfile), path)
 
     #---------------------------------------------------------------------------
     def status(self):
-        local(self.status_command())
+        run_scoped(self.status_command())
 
     #---------------------------------------------------------------------------
     def update(self):
         print(colors.green("[update] ") + self.name)
         with hide('running'):
-            local("test -d %s && %s || %s" % (
-                    self.directory, self.update_command(), self.create_command()
-                )
-            )
+            cmd = "test -d %s && %s || %s" % (
+                        self.directory, self.update_command(), self.create_command()
+                        )
+            run_scoped(cmd)
 
 #-------------------------------------------------------------------------------
 class Subversion(Repository):
     create_symbolic_link = "ln -s %s %s" % (python_path, symlink)
     print(colors.green("[install] ") + package)
     with hide('running'):
-        local("test -L %s || %s" % (symlink, create_symbolic_link))
+        run_scoped("test -L %s || %s" % (symlink, create_symbolic_link))
 
 #-------------------------------------------------------------------------------
 class StatusCommand(object):
         commands = []
         for repo in self.repos.values():
             commands.append(repo.diff_command())
-        local("{ %s; } | less" % (" && ".join(commands)))
+        run_scoped("{ %s; } | less" % (" && ".join(commands)))
 
 #-------------------------------------------------------------------------------
 class PgRestore(object):
         """
         Uses the pg_restore command to restore the database from the given file.
         """
-        local(
+        run_scoped(
             "pg_restore --clean --no-owner --no-privileges --format=%s --host=localhost --username=%s --dbname=%s %s" % (
                 self.format, self.user, self.db, self.file
             )
     #---------------------------------------------------------------------------
     def __call__(self):
         """Uses psql to load a plain dump format"""
-        local(
+        run_scoped(
             "bzcat %s | psql --host=localhost --username=%s --dbname=%s" % (
                 self.file, self.user, self.db
             )
         """
         Uses the psql command to give someone a shell within the db.
         """
-        local("psql --host=localhost --username=%s %s" % (self.user, self.db))
+        run_scoped("psql --host=localhost --username=%s %s" % (self.user, self.db))
 
 #-------------------------------------------------------------------------------
 class MysqlLoadPlain(object):
     #---------------------------------------------------------------------------
     def __call__(self):
         """Uses mysql command line client to load a plain dump format."""
-        local(
+        run_scoped(
             "mysql -h localhost -u %s -p %s < %s" % (self.user, self.db, self.file)
         )
 
     #---------------------------------------------------------------------------
     def __call__(self):
         with settings(warn_only=True, hide=('warnings',)):
-            local("""sudo -u postgres sh -c "dropdb %s;" """ % self.db_name)
-            local("""sudo -u postgres sh -c "dropuser %s;" """ % self.db_user)
-            local("""sudo -u postgres sh -c "createuser --createdb --pwprompt --no-superuser --no-createrole %s" """ % self.db_user)
-            local("""sudo -u postgres sh -c "createdb -O %s %s" """ % (self.db_user, self.db_name))
+            sudo_scoped("dropdb %s" % self.db_name, user="postgres")
+            sudo_scoped("dropuser %s" % self.db_user, user="postgres")
+            sudo_scoped("createuser --createdb --pwprompt --no-superuser --no-createrole %s" %
+                    self.db_user, user="postgres")
+            sudo_scoped("createdb -O %s %s" % (self.db_user, self.db_name), user="postgres")