requirements: Require OP 1.4.3.
[ganeti_webmgr.git] / fabfile.py
blob04e1db809035c7107f19c7aaaf8369cf397c04ec
1 import os
3 import pkg_resources
5 from fabric.api import env, abort
6 from fabric.context_managers import settings, hide, lcd
7 from fabric.contrib.console import confirm
8 from fabric.contrib.files import exists
9 from fabric.operations import local, require, prompt
11 # Required dependencies
13 # PIP_INSTALL - install packages from pip: name:version
15 # GIT_INSTALL - install packages from git:
17 # url - git url for checkouts
18 # development - head to checkout for dev. Defaults to master
19 # production - head to checkout for prod. defaults to master.
20 # symlink - directory to symlink into project. uses project
21 # name if blank
22 # Packages from git are given preference for dev environment. PIP is given
23 # preference for production environments
25 PIP_INSTALL = dict((r.project_name, str(r)) for r in
26 pkg_resources.parse_requirements(open("requirements.txt").read()))
28 GIT_INSTALL = {
29 'noVNC':{
30 'url':'git://github.com/kanaka/noVNC.git',
31 "checkout": "commit",
32 'development':'3859e1d35cf',
33 'production':'3859e1d35cf',
35 'django_object_permissions':{
36 'url':'git://git.osuosl.org/gitolite/django/django_object_permissions',
37 'development':'develop',
39 'django_object_log':{
40 'url':'git://git.osuosl.org/gitolite/django/django_object_log',
41 'development':'develop',
43 'django_muddle_users':{
44 'url':'git://git.osuosl.org/gitolite/django/django_muddle_users',
45 'development':'develop',
47 'muddle':{
48 'url':'git://git.osuosl.org/gitolite/django/muddle',
49 'development':'develop',
51 'twisted_vncauthproxy':{
52 'url':'git://git.osuosl.org/gitolite/ganeti/twisted_vncauthproxy',
53 'development':'develop',
58 # default environment settings - override these in environment methods if you
59 # wish to have an environment that functions differently
60 env.doc_root = '.'
61 env.remote = False
64 def dev():
65 """
66 Configure development deployment.
67 """
69 env.environment = 'development'
72 def prod():
73 """
74 Configure production deployment.
75 """
77 env.environment = 'production'
80 # List of stuff to include in the tarball, recursive.
81 env.MANIFEST = [
82 # Directories
83 "deprecated",
84 "django_test_tools",
85 "ganeti_web",
86 "locale",
87 "twisted",
88 # Files
89 "AUTHORS",
90 "CHANGELOG",
91 "COPYING",
92 "LICENSE",
93 "README",
94 "UPGRADING",
95 "__init__.py",
96 "fabfile.py",
97 "manage.py",
98 "requirements.txt",
99 "search_sites.py",
100 "settings.py.dist",
101 "urls.py",
105 def deploy():
107 Install all dependencies from git and pip.
110 install_dependencies_pip()
111 install_dependencies_git()
113 def clean():
115 In a development environment, remove all installed packages and symlinks.
117 require('environment', provided_by=[dev, prod])
119 if env.environment != 'development':
120 abort('Must be in a development environment.')
121 else:
122 with lcd('%(doc_root)s' % env):
123 gitcmd = 'git clean -%sdx -e \!settings.py'
124 print('Files to be removed:')
125 local(gitcmd % 'n')
126 if confirm('Are you certain you would like to remove these files?'):
127 local(gitcmd % 'f')
128 else:
129 abort('Aborting clean.')
132 def update():
134 In a development environment, update all develop branches.
136 require('environment', provided_by=[dev, prod])
138 if env.environment != 'development':
139 raise Exception('must be in a development environment in order to'
140 'update develop branches.')
141 else:
142 with lcd('%(doc_root)s/dependencies' % env):
143 for git_dir, opts in GIT_INSTALL.items():
144 env.git_repo = git_dir
145 if (_exists('%(doc_root)s/dependencies/%(git_repo)s' % env) and
146 'development' in opts and 'checkout' not in opts):
147 with lcd(git_dir):
148 print 'Updating git repo: %(git_repo)s' % env
149 local('git pull --ff')
152 def _exists(path):
154 A helper function to determine whether a path exists.
156 This function does the right thing in both remote and local environments.
159 if env.remote:
160 return exists(path)
161 else:
162 return os.path.exists(path)
165 def create_virtualenv(virtualenv=None, force=False):
167 Create a virtualenv for pip installations.
169 By default, the environment will be placed in the document root. Pass a
170 path to override the location.
172 If ``force`` is False, then the environment will not be recreated if it
173 already exists.
176 require('environment', provided_by=[dev, prod])
177 env.virtualenv = virtualenv if virtualenv else env.doc_root
179 with lcd(env.doc_root):
180 if not force and _exists('%(virtualenv)s/lib' % env):
181 print 'virtualenv already created'
182 else:
183 # XXX does this actually create a new environment if one already
184 # exists there?
185 local('virtualenv %(virtualenv)s --distribute' % env)
187 # now lets make sure the virtual env has the the newest pip
188 local('%(virtualenv)s/bin/pip install --upgrade pip' % env)
191 def create_env():
193 Setup environment for git dependencies.
196 require('environment', provided_by=[dev, prod])
198 with lcd(env.doc_root):
199 if _exists('dependencies'):
200 print 'dependencies directory exists already'
201 else:
202 local('mkdir dependencies')
205 def install_dependencies_pip():
207 Install all dependencies available from pip.
210 require('environment', provided_by=[dev, prod])
211 create_virtualenv()
213 with lcd(env.doc_root):
214 # Run the installation with pip, passing in our requirements.txt.
215 local('%(virtualenv)s/bin/pip install -r requirements.txt' % env)
218 def install_dependencies_git():
220 Install all dependencies available from git.
223 require('environment', provided_by=[dev, prod])
225 if env.environment != 'development':
226 # If we can satisfy all of our dependencies from pip alone, then don't
227 # bother running the git installation.
228 if all(p in PIP_INSTALL for p in GIT_INSTALL):
229 print 'No git repos to install'
230 return
232 create_env()
234 for name, opts in GIT_INSTALL.items():
236 # check for required values
237 if 'url' not in opts:
238 raise Exception('missing required argument "url" for git repo: %s' % name)
240 # set git head to check out
241 if env.environment in opts:
242 opts['head'] = opts[env.environment]
243 elif env.environment == 'development' and 'production' in opts:
244 opts['head'] = opts['production']
245 else:
246 opts['head'] = 'master'
248 # clone repo
249 with lcd('%(doc_root)s/dependencies' % env):
250 env.git_repo = name
251 if not _exists('%(doc_root)s/dependencies/%(git_repo)s' % env):
252 local('git clone %(url)s' % opts)
254 # create branch if not using master
255 if opts['head'] != 'master':
256 with lcd(name):
257 local('git fetch')
259 # If we're supposed to affix ourselves to a certain
260 # commit, then do so. Otherwise, attempt to create a
261 # tracked branch and update it.
262 if opts.get("checkout", "") == "commit":
263 local("git checkout %(head)s" % opts)
264 else:
265 with settings(hide('warnings','stderr'),
266 warn_only=True):
267 local('git checkout -t origin/%(head)s' % opts)
268 local('git pull')
270 # install to virtualenv using setup.py if it exists. Some repos might
271 # not have it and will need to be symlinked into the project
272 if _exists('%(doc_root)s/dependencies/%(git_repo)s/setup.py' % env):
273 with lcd(env.doc_root):
274 local('%(virtualenv)s/bin/pip install -e dependencies/%(git_repo)s' % env)
276 else:
277 # else, configure and create symlink to git repo
278 with lcd (env.doc_root):
279 if 'symlink' in opts:
280 env.symlink = opts['symlink']
281 env.symlink_path = '%(doc_root)s/dependencies/%(git_repo)s/%(symlink)s' % env
282 else:
283 env.symlink = name
284 env.symlink_path = '%(doc_root)s/dependencies/%(git_repo)s' % env
286 with settings(hide('warnings','stderr'), warn_only=True):
287 local('ln -sf %(symlink_path)s %(doc_root)s' % env)
291 def tarball():
293 Package a release tarball.
296 tarball = prompt('tarball name', default='ganeti-webmgr.tar.gz')
297 files = ['ganeti_webmgr/%s' % file for file in env.MANIFEST]
298 files = ' '.join(files)
300 with lcd('..'):
301 data = dict(
302 tarball=tarball,
303 files=files
305 local('tar zcf %(tarball)s %(files)s --exclude=*.pyc' % data)
306 local('mv %(tarball)s ./ganeti_webmgr/' % data)