Merge lp:~canonical-isd-hackers/canonical-identity-provider/remove-space-shuttle-dependency into lp:canonical-identity-provider/release

Proposed by Ricardo Kirkner
Status: Merged
Approved by: Ricardo Kirkner
Approved revision: no longer in the source branch.
Merged at revision: 132
Proposed branch: lp:~canonical-isd-hackers/canonical-identity-provider/remove-space-shuttle-dependency
Merge into: lp:canonical-identity-provider/release
Diff against target: 335 lines (+210/-45)
5 files modified
fabfile.py (+2/-0)
payload/__init__.py (+11/-26)
payload/django.py (+24/-0)
payload/postgresql.py (+168/-0)
payload/virtualenv.py (+5/-19)
To merge this branch: bzr merge lp:~canonical-isd-hackers/canonical-identity-provider/remove-space-shuttle-dependency
Reviewer Review Type Date Requested Status
Anthony Lenton (community) Approve
Review via email: mp+52219@code.launchpad.net

Commit message

remove space-shuttle dependency for bootstrap

Description of the change

This branch removes the recently introduced dependency on space-shuttle, by backporting the required functions.

To post a comment you must log in.
Revision history for this message
Anthony Lenton (elachuni) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'fabfile.py'
2--- fabfile.py 1970-01-01 00:00:00 +0000
3+++ fabfile.py 2011-03-04 18:20:44 +0000
4@@ -0,0 +1,2 @@
5+# use a payload package to keep fabric code organized
6+from payload import *
7
8=== added directory 'payload'
9=== renamed file 'fabfile.py' => 'payload/__init__.py'
10--- fabfile.py 2011-03-03 19:11:04 +0000
11+++ payload/__init__.py 2011-03-04 18:20:44 +0000
12@@ -1,3 +1,7 @@
13+# Copyright 2011 Canonical Ltd. This software is licensed under the
14+# GNU Affero General Public License version 3 (see the file LICENSE).
15+from __future__ import absolute_import
16+
17 import os.path
18 import sys
19 import textwrap
20@@ -5,22 +9,8 @@
21 from fabric.api import local
22 from fabric.context_managers import cd
23
24-try:
25- import space.shuttle
26-except ImportError:
27- from _virtualenv import (
28- setup_virtualenv,
29- virtualenv,
30- )
31-
32- def install_space_shuttle():
33- # install space shuttle
34- virtualenv('pip install bzr+ssh://bazaar.launchpad.net/'
35- '~canonical-isd-hackers/space-shuttle/trunk')
36-
37- created = setup_virtualenv()
38- if created:
39- install_space_shuttle()
40+from .virtualenv import *
41+from .postgresql import *
42
43
44 # constants
45@@ -41,10 +31,6 @@
46 # environment
47 def bootstrap():
48 """Bootstrap the development environment"""
49- from space.shuttle.virtualenv import (
50- setup_virtualenv,
51- install_dependencies,
52- )
53 # run sanity checks
54 _check_psycopg2_conflicts()
55 # bootstrap
56@@ -61,6 +47,11 @@
57 local("find . -name '*.pyc' -delete")
58
59
60+def clean_coverage():
61+ """Remove coverage report files."""
62+ local("rm -rf .coverage coverage.d coverage.xml")
63+
64+
65 # development
66 def test(functional=False):
67 """Run whole test suite."""
68@@ -140,9 +131,3 @@
69 includes = config/devel.cfg
70 """ % os.path.abspath(os.curdir)))
71
72-
73-# extend fabfile with custom space.shuttle targets if available
74-try:
75- from space.shuttle import *
76-except ImportError:
77- pass
78
79=== added file 'payload/django.py'
80--- payload/django.py 1970-01-01 00:00:00 +0000
81+++ payload/django.py 2011-03-04 18:20:44 +0000
82@@ -0,0 +1,24 @@
83+# Copyright 2011 Canonical Ltd. This software is licensed under the
84+# GNU Affero General Public License version 3 (see the file LICENSE).
85+from __future__ import absolute_import
86+
87+import os
88+
89+
90+__all__ = [
91+ 'get_django_settings',
92+]
93+
94+
95+def get_django_settings(*keys):
96+ # setup the correct django environment
97+ os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
98+ # get django settings
99+ from django.conf import settings
100+
101+ result = dict.fromkeys(keys)
102+ for key in keys:
103+ result[key] = getattr(settings, key)
104+
105+ return result
106+
107
108=== added file 'payload/postgresql.py'
109--- payload/postgresql.py 1970-01-01 00:00:00 +0000
110+++ payload/postgresql.py 2011-03-04 18:20:44 +0000
111@@ -0,0 +1,168 @@
112+# Copyright 2011 Canonical Ltd. This software is licensed under the
113+# GNU Affero General Public License version 3 (see the file LICENSE).
114+from __future__ import absolute_import
115+
116+from fabric.api import (
117+ env,
118+ local,
119+ settings,
120+)
121+from fabric.context_managers import hide
122+
123+from .virtualenv import virtualenv
124+from .django import get_django_settings
125+
126+
127+__all__ = [
128+ 'createdb',
129+ 'dropdb',
130+ 'resetdb',
131+ 'setup_database',
132+ 'setup_postgresql_server',
133+ 'shutdown_postgresql_server',
134+ 'start_postgresql_server',
135+ 'stop_postgresql_server',
136+ 'syncdb',
137+]
138+
139+
140+def setup_postgresql_server():
141+ """Setup the PostgreSQL server."""
142+ _set_postgres_environment(host='/dev/shm/postgresql')
143+ local("mkdir -p %s" % env.postgres['DATA'])
144+ local("%s %s/initdb -A trust" % (env.postgres['ENV'], env.postgres['BIN']))
145+ start_postgresql_server()
146+ setup_database()
147+
148+
149+def shutdown_postgresql_server():
150+ """Shutdown the PostgreSQL server."""
151+ _set_postgres_environment(host='/dev/shm/postgresql')
152+ dropdb()
153+ stop_postgresql_server()
154+ local("rm -rf %s" % env.postgres['HOST'])
155+
156+
157+def start_postgresql_server():
158+ """Start the PostgreSQL server."""
159+ _set_postgres_environment()
160+ cmd = ['%(ENV)s', '%(BIN)s/pg_ctl', 'start', '-w', '-l',
161+ '%(HOST)s/postgresql.log', '-o', "'-F", '-k', "%(HOST)s'"]
162+ local(' '.join(cmd) % env.postgres)
163+
164+
165+def stop_postgresql_server():
166+ """Stop the PostgreSQL server."""
167+ _set_postgres_environment()
168+ cmd = ['%(ENV)s', '%(BIN)s/pg_ctl', 'stop', '-w', '-l',
169+ '%(HOST)s/postgresql.log', '-o', "'-F", '-k', "%(HOST)s'"]
170+ local(' '.join(cmd) % env.postgres)
171+
172+
173+def setup_database():
174+ """Setup the database."""
175+ success = _check_database()
176+ if not success:
177+ createdb()
178+ _createrole()
179+ syncdb()
180+
181+
182+def createdb():
183+ """Create the database."""
184+ _set_postgres_environment()
185+ local("%(ENV)s %(BIN)s/createdb %(DATABASE)s" % env.postgres)
186+
187+
188+def dropdb(warn_only=False):
189+ """Remove the database."""
190+ _set_postgres_environment()
191+ if isinstance(warn_only, basestring):
192+ warn_only = warn_only.lower() == 'yes'
193+ with settings(warn_only=warn_only):
194+ local("%(ENV)s %(BIN)s/dropdb %(DATABASE)s" % env.postgres)
195+
196+
197+def syncdb():
198+ """Sync the database."""
199+ args = '--noinput'
200+ virtualenv("python django_project/manage.py syncdb %s" % args,
201+ capture=False)
202+
203+
204+def resetdb():
205+ """Drop and recreate then sync the database."""
206+ with settings(hide='warnings'):
207+ dropdb(warn_only=True)
208+ createdb()
209+ syncdb()
210+
211+
212+# helpers
213+# =======
214+
215+
216+def _check_database():
217+ """Check the database is accessible."""
218+ with hide('aborts', 'running'):
219+ try:
220+ _set_postgres_environment()
221+ local("%(ENV)s psql -U postgres -c '\q'"
222+ % env.postgres, capture=False)
223+ success = True
224+ except SystemExit:
225+ # there was an error connecting to the db,
226+ # presumably the db didn't exist
227+ success = False
228+ return success
229+
230+
231+def _set_database_environment():
232+ """Update the environment with the database settings."""
233+ if 'database' in env:
234+ # environment already set up
235+ return
236+
237+ settings = get_django_settings('DATABASE_HOST', 'DATABASE_PORT',
238+ 'DATABASE_USER', 'DATABASE_PASSWORD', 'DATABASE_NAME')
239+ env.database = {
240+ 'HOST': settings['DATABASE_HOST'],
241+ 'PORT': settings['DATABASE_PORT'],
242+ 'NAME': settings['DATABASE_NAME'],
243+ 'USER': settings['DATABASE_USER'],
244+ 'PASSWORD': settings['DATABASE_PASSWORD'],
245+ }
246+
247+
248+def _set_postgres_environment(host=''):
249+ """Update the environment with the PostgreSQL settings."""
250+ if 'postgres' in env:
251+ # environment already set up
252+ return
253+
254+ _set_database_environment()
255+ host = host if host else env.database['HOST']
256+ port = env.database['PORT']
257+
258+ pg_env = []
259+ env.postgres = {
260+ 'BIN': '/usr/lib/postgresql/8.4/bin',
261+ 'DATABASE': env.database['NAME'],
262+ }
263+
264+ if host:
265+ data = "%s/data" % host
266+ pg_env.append("PGHOST=%s" % host)
267+ pg_env.append("PGDATA=%s" % data)
268+ env.postgres.update({'HOST': host, 'DATA': data})
269+ if port:
270+ pg_env.append("PGPORT=%s" % port)
271+ env.postgres['ENV'] = ' '.join(pg_env)
272+
273+
274+def _createrole():
275+ """Create required users/roles."""
276+ _set_postgres_environment()
277+ local("%s %s/createuser --superuser --createdb postgres" % (
278+ env.postgres['ENV'], env.postgres['BIN']))
279+
280
281=== renamed file '_virtualenv.py' => 'payload/virtualenv.py'
282--- _virtualenv.py 2011-03-03 16:45:01 +0000
283+++ payload/virtualenv.py 2011-03-04 18:20:44 +0000
284@@ -1,3 +1,7 @@
285+# Copyright 2011 Canonical Ltd. This software is licensed under the
286+# GNU Affero General Public License version 3 (see the file LICENSE).
287+from __future__ import absolute_import
288+
289 import os
290
291 from fabric.api import (
292@@ -7,14 +11,11 @@
293
294
295 __all__ = [
296- 'create_pip_cache',
297 'install_dependencies',
298 'setup_virtualenv',
299 'virtualenv',
300 ]
301
302-PIP_DOWNLOAD_CACHE = os.path.expanduser(
303- os.environ.get('PIP_DOWNLOAD_CACHE', '~/.pip/cache'))
304 VIRTUALENV = '.env'
305
306
307@@ -42,28 +43,13 @@
308 return created
309
310
311-def install_dependencies(offline=False):
312+def install_dependencies():
313 """Install all dependencies into the virtualenv."""
314 cmd = ['pip', 'install', '-r', 'requirements.txt']
315- if offline:
316- cmd.extend(['-f', "file://%s" % os.path.abspath(
317- PIP_DOWNLOAD_CACHE), '--no-index'])
318 virtualenv(' '.join(cmd), capture=False)
319 virtualenv('python setup.py develop')
320
321
322-def create_pip_cache(verbose=False):
323- """Create a pip cache."""
324- capture = True
325- local("mkdir -p %s" % PIP_DOWNLOAD_CACHE)
326- cmd = ['pip', 'install', '-r', 'requirements.txt', '--no-install',
327- '-d', PIP_DOWNLOAD_CACHE]
328- if verbose:
329- capture = False
330- cmd.append('-v')
331- virtualenv(' '.join(cmd), capture=capture)
332-
333-
334 # helpers
335 # =======
336