1. Mikhail Korobov
  2. django-fab-deploy

Commits

Mikhail Korobov  committed 91e8ffd

define_host decorator

  • Participants
  • Parent commits c2b420b
  • Branches default

Comments (0)

Files changed (5)

File docs/CHANGES.rst

View file
  • Ignore whitespace
 - backports for Ubuntu 10.04 and 10.10;
 - docs are now using default theme;
 - remote django management command errors are no longer silinced;
+- invoking ``create_linux_account`` with non-default username is fixed;
+- ``define_host`` decorator for easier host definition;
 - default nginx config uses ``INSTANCE_NAME`` for logs.
 
 0.7.1 (2011-04-21)

File docs/fabfile.rst

View file
  • Ignore whitespace
   each server configuration.
 * Call :func:`update_env() <fab_deploy.utils.update_env>` at the end of
   each function.
+* It is possible to reduce boilerplate by using
+  :func:`define_host <fab_deploy.utils.define_host>` decorator::
+
+      from fab_deploy import *
+
+      @define_host('my_site@example.com')
+      def my_site():
+          return {
+              # ...
+          }
+
 * In order to specify configuration the fab commands should use, run the
   configuring function as a first fab command::
 
 
       def my_site():
           env.hosts = ['my_site@example.com']
+          env.conf = {
+              # ...
+          }
           # ...
+          update_env()
 
       my_site()
 
 .. autofunction:: fab_deploy.utils.inside_src
 
 .. autofunction:: fab_deploy.utils.run_as_sudo
+
+.. autofunction:: fab_deploy.utils.define_host

File docs/guide.rst

View file
  • Ignore whitespace
 
         my_site()
 
+   There is a simpler syntax for the code above::
+
+        from fab_deploy import *
+
+        @define_host('my_site@example.com')
+        def my_site():
+            return dict(
+                DB_USER = 'my_site',
+                DB_PASSWORD = 'password',
+            )
+
+        my_site()
+
    In order to make things simple set the username in :attr:`env.hosts` string
    to your project name. It should be a valid python identifier.
    Don't worry if there is no such user on server, django-fab-deploy can

File fab_deploy/utils.py

View file
  • Ignore whitespace
 
 
 __all__ = ['run_as', 'update_env', 'inside_project', 'inside_virtualenv',
-           'inside_src', 'delete_pyc', 'print_env', 'detect_os']
+           'inside_src', 'delete_pyc', 'print_env', 'detect_os', 'define_host']
 
 DETECTION_ERROR_MESSAGE = """
 OS detection failed. This probably means your OS is not
         if env.conf.VCS == vcs:
             env.conf.VCS = 'fab_deploy.vcs.' + vcs
 
+def define_host(host_string):
+    """
+    This decorator populates :attr:`env.hosts`, :attr:`env.conf` and
+    calls :func:`update_env() <fab_deploy.utils.update_env>`::
+
+        from fab_deploy import *
+
+        @define_host('my_site@example.com')
+        def my_site():
+            return {
+                'DB_USER': 'my_site',
+                'DB_PASSWORD': 'password',
+            }
+
+    Decorated function should return a dict with desired :attr:`env.conf`
+    values.
+
+    """
+    def decorator(func):
+        @wraps(func)
+        def inner(*args, **kwargs):
+            env.hosts = [host_string]
+            env.conf = func(*args, **kwargs)
+            update_env()
+        return inner
+    return decorator
+
 
 def virtualenv():
     """

File fab_deploy_tests/test_project/fabfile.py

View file
  • Ignore whitespace
 from fab_deploy import *
 
+@define_host('foo@127.0.0.1:2222')
 def foo_site():
-    env.hosts = ['foo@127.0.0.1:2222']
-    env.conf = dict(
+    return dict(
         DB_PASSWORD = '123',
         VCS = 'none',
         SERVER_NAME = 'foo.example.com'
     )
-    update_env()
 
 def bar_site():
     env.hosts = ['foo@127.0.0.1:2222']
     )
     update_env()
 
+@define_host('foo@127.0.0.1:2222')
 def invalid_site():
-    env.hosts = ['foo@127.0.0.1:2222']
-    env.conf = dict(
+    return dict(
         DB_PASSWORD = '123',
         VCS = 'none',
         SERVER_NAME = 'invalid.example.com',
         INSTANCE_NAME = 'invalid',
         EXTRA = 'raise Exception()'
     )
-    update_env()