Commits

Mikhail Korobov committed ef1abdb

The usage of env.user is discouraged. Fix #5.

Comments (0)

Files changed (4)

 Overview
 --------
 
-* Write a function populating :attr:`env` and :attr:`env.conf` for each server
-  configuration.
+* Write a function populating :attr:`env.hosts` and :attr:`env.conf` for
+  each server configuration.
 * Call :func:`update_env() <fab_deploy.utils.update_env>` at the end of
   each function.
 * In order to specify configuration the fab commands should use, run the
       from fab_deploy import *
 
       def my_site():
-          env.hosts = ['example.com']
-          env.user = 'example'
+          env.hosts = ['my_site@example.com']
           # ...
 
       my_site()
 
 .. attribute:: env.hosts
 
-    hosts URL. See `fabric docs <http://docs.fabfile.org/1.0a/usage/env.html#hosts>`_
+    A list with host string. Example::
+
+       env.hosts = ['user@example.com']
+
+    See `fabric docs <http://docs.fabfile.org/1.0a/usage/execution.html#hosts>`_
     for explanation.
 
+    User obtained from this string will be used for ssh logins and
+    as a default value for :attr:`env.conf.INSTANCE_NAME`.
+
     .. note::
 
         multiple hosts are supported via multiple config functions, not
         via this option.
 
-.. attribute:: env.user
+    .. warning::
 
-    remote user name. Used for ssh logins and as a default value for
-    :attr:`env.conf.INSTANCE_NAME`
+        Due to bug in Fabric please don't use ``env.user`` and ``env.port``.
+        Put the username and non-standard ssh port directly into host string.
 
 .. attribute:: env.conf
 
 
 .. attribute:: env.conf.INSTANCE_NAME
 
-    Project instance name. It equals to :attr:`env.user` by default.
-    INSTANCE_NAME should be unique for server. If there are several sites
-    running as one linux user, set different INSTANCE_NAMEs for them.
+    Project instance name. It equals to username obtained from :attr:`env.hosts`
+    by default. INSTANCE_NAME should be unique for server. If there are
+    several sites running as one linux user, set different
+    INSTANCE_NAMEs for them.
 
 .. attribute:: env.conf.SERVER_NAME
 
 
 .. attribute:: env.conf.OS
 
-    Server operating system name. Supported operating systems are
-    'lenny' and 'squeeze'. Default is 'lenny'.
+    A string with server operating system name. Supported operating systems:
+
+    * lenny
+    * squeeze
+
+    Default is 'lenny'.
 
     .. warning::
 
         from fab_deploy import *
 
         def my_site():
-            env.hosts = ['example.com']
-            env.user = 'example'
+            env.hosts = ['my_site@example.com']
             env.conf = dict(
                 DB_PASSWORD = 'password',
                 PROCESSES = 2,
 
    Make sure the :attr:`env.conf.OS` is correct (it is 'lenny' by default).
 
-   In order to make things simple set :attr:`env.user` 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 create linux user and setup ssh
-   access for you, and it is preferrable to have linux user per
-   project if possible.
+   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
+   create linux user and setup ssh access for you, and it is
+   preferrable to have linux user per project if possible.
 
    .. note::
 
        There are some defaults, e.g. :attr:`DB_NAME <env.conf.DB_NAME>`
        equals to :attr:`INSTANCE_NAME <env.conf.INSTANCE_NAME>`,
        and :attr:`INSTANCE_NAME <env.conf.INSTANCE_NAME>` equals
-       to :attr:`env.user`.
+       to username obtained from :attr:`env.hosts`.
 
        Read :doc:`fabfile` for more details.
 
 Prepare the server
 ------------------
 
-1. If there is no linux account for ``env.user``
+1. If there is no linux account for user specified in :attr:`env.hosts`
    then add a new linux server user, manually or using
 
    ::
    creates a new linux user and uploads provided ssh key. Test that ssh
    login is working::
 
-       ssh example@example.com
+       ssh my_site@example.com
 
    .. note::
 
 
    :func:`mysql_install<fab_deploy.mysql.mysql_install>` does
    nothing if mysql is already installed on server. Otherwise it installs
-   mysql-server package and sets root password to ``env.conf['DB_PASSWORD']``.
+   mysql-server package and sets root password to :attr:`env.conf.DB_PASSWORD`.
 
    :func:`mysql_create_db<fab_deploy.mysql.mysql_create_db>` creates a new
-   empty database named ``env.conf['DB_NAME']`` (it equals to
-   ``env.conf['INSTANCE_NAME']`` by default, which equals to ``env.user``
-   by default).
+   empty database named :attr:`env.conf.DB_NAME` (it equals to
+   :attr:`env.conf.INSTANCE_NAME` by default, which equals to
+   the user from :attr:`env.hosts` by default).
 
    .. note::
 
-        If non-root mysql user is used then you'd better create DB and
-        grant necessary priveleges manually.
+        If non-root DB user is used or the DB enging is not mysql then
+        you'd better create DB and grant necessary priveleges manually.
 
 
 3. If you feel brave you can now run ``fab full_deploy`` from the project root

fab_deploy/deploy.py

             run('mkdir %s' % env.conf['INSTANCE_NAME'])
             with cd(env.conf['INSTANCE_NAME']):
                 run('hg init')
-    local('hg push ssh://%s@%s/src/%s/' % (env.conf['USER'], env.conf['HOST'], env.conf['INSTANCE_NAME']))
+    local('hg push ssh://%s/src/%s/' % (env.hosts[0], env.conf['INSTANCE_NAME']))
     with cd('src'):
         with cd(env.conf['INSTANCE_NAME']):
             run('hg up -C %s' % env.conf['HG_BRANCH'])
             print 'Valid arguments are: %s' % allowed_args
             return
 
-    repo = 'ssh://%s@%s/src/%s/' % (env.conf['USER'], env.conf['HOST'], env.conf['INSTANCE_NAME'])
+    repo = 'ssh://%s/src/%s/' % (env.hosts[0], env.conf['INSTANCE_NAME'])
     local('hg push %s' % repo)
     delete_pyc()
     with cd('src/'+env.conf['INSTANCE_NAME']):

fab_deploy/utils.py

 def supports_only(supported_systems):
     """ Safety decorator. Marks command as only supported for a set of
     operating systems. Aborts execution if :attr:`env.conf.OS` is not in
-    list::
+    list. Accepts string with comma-separated list of system names::
 
         @supports_only('lenny, squeeze')
         def my_command():
         from fab_deploy import *
 
         def my_site():
-            env.hosts = ['example.com']
-            env.user = 'example'
+            env.hosts = ['my_site@example.com']
             env.conf = dict(
                 DB_PASSWORD = 'password',
             )
             update_env()
     """
+    assert len(env.hosts)==1, "Multiple hosts in env.hosts are not supported now."
+    user, host, port = normalize(env.hosts[0])
 
-    HOME_DIR = '/home/%s' % env.user
+    HOME_DIR = '/home/%s' % user
     if 'INSTANCE_NAME' not in env.conf:
-        env.conf['INSTANCE_NAME'] = env.user
+        env.conf['INSTANCE_NAME'] = user
 
     defaults = _AttributeDict(
         HG_BRANCH = 'default',
         DB_USER = 'root',
         PROCESSES = 1,
         THREADS = 15,
+        SERVER_NAME = host,
+        SERVER_ADMIN = 'example@example.com',
+        OS = 'lenny',
+
+        # these options shouldn't be set by user
         HOME_DIR = HOME_DIR,
         ENV_DIR = HOME_DIR + '/envs/' + env.conf['INSTANCE_NAME'],
         SRC_DIR = HOME_DIR + '/src/' + env.conf['INSTANCE_NAME'],
-        USER = env.user,
-        HOST = env.hosts[0],
-        SERVER_NAME = env.hosts[0].split(':')[0],
-        SERVER_ADMIN = 'example@example.com',
-        OS = 'lenny',
+        USER = user,
     )
     defaults.update(env.conf)
     env.conf = defaults