Tests: fixing globals, fixing tabs
[ganeti_webmgr.git] / fabfile.py
blobcbd15a48f7dfc8432b9a85e7781ac46e44f83aca
1 import os
2 from fabric.api import env
3 from fabric.context_managers import cd, settings, hide, lcd
4 from fabric.contrib.files import exists
5 from fabric.operations import local, require, prompt
7 # Required dependencies
9 # PIP_INSTALL - install packages from pip: name:version
11 # GIT_INSTALL - install packages from git:
13 # url - git url for checkouts
14 # development - head to checkout for dev. Defaults to master
15 # production - head to checkout for prod. defaults to master.
16 # symlink - directory to symlink into project. uses project
17 # name if blank
18 # Packages from git are given preference for dev environment. PIP is given
19 # preference for production environments
21 PIP_INSTALL = {
22 'django' :'>=1.2',
23 'django-registration' :'',
24 'south' :'',
25 'django-haystack' :'==1.2.1',
26 'whoosh' :'>=1.8.1',
27 'django_object_permissions' :'=1.3.1',
28 'twisted' :'>=11.0.0'
31 GIT_INSTALL = {
32 'noVNC':{
33 'url':'git://github.com/kanaka/noVNC.git',
34 'development':'3859e1d35cf',
35 'production':'3859e1d35cf',
37 'django_object_permissions':{
38 'url':'git://git.osuosl.org/gitolite/django/django_object_permissions',
39 'development':'develop',
40 'symlink':'object_permissions',
42 'django_object_log':{
43 'url':'git://git.osuosl.org/gitolite/django/django_object_log',
44 'development':'develop',
45 'symlink':'object_log'
47 'django_muddle_users':{
48 'url':'git://git.osuosl.org/gitolite/django/django_muddle_users',
49 'development':'develop',
50 'symlink':'muddle_users'
55 # default environment settings - override these in environment methods if you
56 # wish to have an environment that functions differently
57 env.doc_root = '.'
58 env.remote = False
61 def dev():
62 """ configure development deployment """
63 env.environment = 'development'
66 def prod():
67 """ configure a production deployment """
68 env.environment = 'production'
71 # Files and directories that will be included in tarball when packaged
72 env.MANIFEST = [
73 "django_test_tools",
74 "ganeti",
75 "i18n",
76 "locale",
77 "media",
78 "templates",
79 "twisted",
80 "util",
81 "__init__.py",
82 "AUTHORS",
83 "CHANGELOG",
84 "COPYING",
85 "fabfile.py",
86 "LICENSE",
87 "manage.py",
88 "README",
89 "search_sites.py",
90 "settings.py.dist",
91 "UPGRADING",
92 "urls.py"
96 def deploy():
97 """
98 Install all dependencies from git and pip
99 """
100 install_dependencies_pip()
101 install_dependencies_git()
104 def _exists(path):
106 helper to see if a path exists. uses either os.exists or exists depending
107 on if the environment is remote or local
109 if env.remote:
110 return exists(path)
111 else:
112 return os.path.exists(path)
115 def create_virtualenv(virtualenv=None, force=False):
116 """ create a virtualenv for pip installs """
117 require('environment', provided_by=[dev, prod])
118 env.virtualenv = virtualenv if virtualenv else env.doc_root
120 with lcd(env.doc_root):
121 if not force and _exists('%(virtualenv)s/lib' % env):
122 print 'virtualenv already created'
123 else:
124 local('virtualenv %(virtualenv)s' % env)
127 def create_env():
129 setup environment for git dependencies
131 require('environment', provided_by=[dev, prod])
133 with lcd(env.doc_root):
134 if _exists('dependencies'):
135 print 'dependencies directory exists already'
136 else:
137 local('mkdir dependencies')
140 def install_dependencies_pip():
142 Install all dependencies available from pip
144 require('environment', provided_by=[dev, prod])
145 create_virtualenv()
147 # if this is a development install then filter out anything we have a
148 # git repo for.
149 pips_ = PIP_INSTALL.copy()
150 if env.environment == 'development':
151 map(pips_.pop, [k for k in GIT_INSTALL if k in PIP_INSTALL])
153 if not pips_:
154 print 'No git repos to install'
155 return
157 with lcd(env.doc_root):
158 #XXX create temp requirements file text from list of requirements
159 # it will be destroyed after install is complete
160 requirements = '\n'.join([''.join(p) for p in pips_.items()])
161 with settings(hide('running')):
162 local("echo '%s' > requirements.txt" % requirements)
164 local('pip install -E %(virtualenv)s -r requirements.txt' % env)
165 local('rm requirements.txt')
168 def install_dependencies_git():
169 """ Install all dependencies available from git """
170 require('environment', provided_by=[dev, prod])
172 # if this is a production install then install everything that pip that
173 # can be installed
174 gits_ = GIT_INSTALL.copy()
175 if env.environment != 'development':
176 map(gits_.pop, (k for k in PIP_INSTALL if k in GIT_INSTALL))
178 if not gits_:
179 print 'No git repos to install'
180 return
182 create_env()
184 for name, opts in GIT_INSTALL.items():
186 # check for required values
187 if 'url' not in opts:
188 raise Exception('missing required argument "url" for git repo: %s' % name)
190 # set git head to check out
191 if env.environment in opts:
192 opts['head'] = opts[env.environment]
193 elif env.environment == 'dev' and 'production' in opts:
194 opts['head'] = opts['production']
195 else:
196 opts['head'] = 'master'
198 # clone repo
199 with lcd('%(doc_root)s/dependencies' % env):
200 env.git_repo = name
201 if not _exists('%(doc_root)s/dependencies/%(git_repo)s' % env):
202 local('git clone %(url)s' % opts)
204 # create branch if not using master
205 if opts['head'] != 'master':
206 with lcd(name):
207 local('git fetch')
208 local('git checkout %(head)s' % opts)
209 with settings(hide('warnings','stderr'), warn_only=True):
210 local('git pull')
212 # configure and create symlink to git repo
213 with lcd (env.doc_root):
214 if 'symlink' in opts:
215 env.symlink = opts['symlink']
216 env.symlink_path = '%(doc_root)s/dependencies/%(git_repo)s/%(symlink)s' % env
217 else:
218 env.symlink = name
219 env.symlink_path = '%(doc_root)s/dependencies/%(git_repo)s' % env
221 with settings(hide('warnings','stderr'), warn_only=True):
222 local('ln -sf %(symlink_path)s %(doc_root)s' % env)
225 def tarball():
226 """ package a release tarball """
227 tarball = prompt('tarball name', default='ganeti-webmgr-tar.gz')
228 files = ['ganeti_webmgr/%s' % file for file in env.MANIFEST]
229 files = ' '.join(files)
231 with lcd('..'):
232 data = dict(
233 tarball=tarball,
234 files=files
236 local('tar cfz %(tarball)s %(files)s --exclude=*.pyc' % data)
237 local('mv %(tarball)s ./ganeti_webmgr/' % data)