root / snf-app / docs / src / develop.rst @ df285bb3
History | View | Annotate | Download (8.6 kB)
1 |
Developers guide |
---|---|
2 |
================ |
3 |
|
4 |
Information on how to setup a development environment. |
5 |
|
6 |
This file documents the installation of a development environment for Synnefo. |
7 |
It should be read alongside :ref:`installation guide <installation>`. |
8 |
|
9 |
It contains development-specific ammendments to the basic installation steps |
10 |
outlined in `installation guide <installation>`, and development-specific notes. |
11 |
|
12 |
|
13 |
Prerequisites |
14 |
------------- |
15 |
|
16 |
For a basic development environment you need to follow steps |
17 |
of `installation guide <installation>`, which should be read |
18 |
in its entirety *before* this document. |
19 |
|
20 |
|
21 |
Setting up development environment |
22 |
---------------------------------- |
23 |
|
24 |
Although not necessary it is suggested that you use a virtualenv as a base for |
25 |
your development environment:: |
26 |
|
27 |
$ virtualenv synnefo-env |
28 |
$ cd synnefo-dev |
29 |
$ source bin/activate |
30 |
|
31 |
* Clone Synnefo repository:: |
32 |
|
33 |
(synnefo-env)$ git clone https://code.grnet.gr/git/synnefo synnefo |
34 |
|
35 |
* Install synnefo depedencies:: |
36 |
|
37 |
(synnefo-env)$ pip install -r synnefo/requirements.pip |
38 |
|
39 |
* Install development helpers:: |
40 |
|
41 |
(synnefo-env)$ pip install django_extensions |
42 |
|
43 |
* Install synnefo package:: |
44 |
|
45 |
(synnefo-env)$ cd synnefo/snf-app |
46 |
(synnefo-env)$ python setup.py develop -N |
47 |
|
48 |
* Create a custom development settings module and set environment variable to |
49 |
use this module as the entry point of synnefo settings:: |
50 |
|
51 |
(synnefo-env)$ touch settings_dev.py |
52 |
(synnefo-env)$ export DJANGO_SETTINGS_MODULE=settings_dev |
53 |
(synnefo-env)$ export PYTHONPATH=`pwd`:$PYTHONPATH |
54 |
(synnefo-env)$ vi settings_dev.py |
55 |
|
56 |
paste the following sample development settings: |
57 |
|
58 |
.. code-block:: python |
59 |
|
60 |
import os |
61 |
|
62 |
from synnefo.settings.common import * |
63 |
|
64 |
# uncomment this if have django-extensions installed (pip install django_extensions) |
65 |
#INSTALLED_APPS = list(INSTALLED_APPS) + ['django_extensions'] |
66 |
|
67 |
DEV_PATH = os.path.abspath(os.path.dirname(__file__)) |
68 |
DATABASES['default']['NAME'] = os.path.join(DEV_PATH, "synnefo.sqlite") |
69 |
|
70 |
|
71 |
# development rabitmq configuration |
72 |
RABBIT_HOST = "" |
73 |
RABBIT_USERNAME = "" |
74 |
RABBIT_PASSWORD = "" |
75 |
RABBIT_VHOST = "/" |
76 |
|
77 |
# development ganeti settings |
78 |
GANETI_MASTER_IP = "" |
79 |
GANETI_CLUSTER_INFO = (GANETI_MASTER_IP, 5080, "<username>", "<password>") |
80 |
GANETI_CREATEINSTANCE_KWARGS['disk_template'] = 'plain' |
81 |
|
82 |
# This prefix gets used when determining the instance names |
83 |
# of Synnefo VMs at the Ganeti backend. |
84 |
# The dash must always appear in the name! |
85 |
BACKEND_PREFIX_ID = "<my commit name>-" |
86 |
|
87 |
IGNORE_FLAVOR_DISK_SIZES = True |
88 |
|
89 |
# do not actually send emails |
90 |
# save them as files in /tmp/synnefo-mails |
91 |
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' |
92 |
EMAIL_FILE_PATH = '/tmp/synnefo-mails' |
93 |
|
94 |
# for UI developers |
95 |
UI_HANDLE_WINDOW_EXCEPTIONS = False |
96 |
|
97 |
# allow login using /?test url |
98 |
BYPASS_AUTHENTICATION = True |
99 |
|
100 |
* Initialize database:: |
101 |
|
102 |
(synnefo-env)$ snf-manage syndb |
103 |
(synnefo-env)$ snf-manage migrate |
104 |
(synnefo-env)$ snf-manage loaddata users flavors images |
105 |
|
106 |
|
107 |
Development tips |
108 |
**************** |
109 |
|
110 |
* Running a development web server:: |
111 |
|
112 |
(synnefo-env)$ snf-manage runserver |
113 |
|
114 |
or, if you have django_extensions and werkzeug packages installed:: |
115 |
|
116 |
(synnefo-env)$ snf-manage runserver_plus |
117 |
|
118 |
|
119 |
* Opening a python console with synnefo environment initialized:: |
120 |
|
121 |
(synnefo-env)$ snf-manage shell |
122 |
|
123 |
or, if you have django_extensions package installed:: |
124 |
|
125 |
(synnefo-env)$ snf-manage shell_plus |
126 |
|
127 |
|
128 |
South Database Migrations |
129 |
------------------------- |
130 |
|
131 |
Initial Migration |
132 |
***************** |
133 |
|
134 |
To initialise south migrations in your database the following commands must be |
135 |
executed:: |
136 |
|
137 |
$ snf-manage syncdb # Create / update the database with the south tables |
138 |
$ snf-manage migrate db # Perform migration in the database |
139 |
|
140 |
Note that syncdb will create the latest models that exist in the db app, so some |
141 |
migrations may fail. If you are sure a migration has already taken place you |
142 |
must use the "--fake" option, to apply it. |
143 |
|
144 |
For example:: |
145 |
|
146 |
$ snf-manage migrate db 0001 --fake |
147 |
|
148 |
To be sure that all migrations are applied type:: |
149 |
|
150 |
$ snf-manage migrate db --list |
151 |
|
152 |
All starred migrations are applied. |
153 |
|
154 |
Remember, the migration is performed mainly for the data, not for the database |
155 |
schema. If you do not want to migrate the data, a syncdb and fake migrations for |
156 |
all the migration versions will suffice. |
157 |
|
158 |
Schema migrations |
159 |
***************** |
160 |
|
161 |
Do not use the syncdb management command. It can only be used the first time |
162 |
and/or if you drop the database and must recreate it from scratch. See |
163 |
"Initial Migration" section. |
164 |
|
165 |
Every time you make changes to the database and data migration is not required |
166 |
(WARNING: always perform this with extreme care):: |
167 |
|
168 |
$ snf-manage schemamigration db --auto |
169 |
|
170 |
The above will create the migration script. Now this must be applied to the live |
171 |
database:: |
172 |
|
173 |
$ snf-manage migrate db |
174 |
|
175 |
Consider this example (adding a field to the SynnefoUser model):: |
176 |
|
177 |
$ ./bin/python manage.py schemamigration db --auto |
178 |
+ Added field new_south_test_field on db.SynnefoUser |
179 |
|
180 |
Created 0002_auto__add_field_synnefouser_new_south_test_field.py. |
181 |
|
182 |
You can now apply this migration with:: |
183 |
|
184 |
$ ./manage.py migrate db |
185 |
Running migrations for db: |
186 |
- Migrating forwards to 0002_auto__add_field_synnefouser_new_south_test_field. |
187 |
> db:0002_auto__add_field_synnefouser_new_south_test_field |
188 |
- Loading initial data for db. |
189 |
|
190 |
Installing json fixture 'initial_data' from '/home/bkarak/devel/synnefo/../synnefo/db/fixtures'. |
191 |
Installed 1 object(s) from 1 fixture(s) |
192 |
|
193 |
South needs some extra definitions to the model to preserve and migrate the |
194 |
existing data, for example, if we add a field in a model, we should declare its |
195 |
default value. If not, South will propably fail, after indicating the error:: |
196 |
|
197 |
$ ./bin/python manage.py schemamigration db --auto |
198 |
? The field 'SynnefoUser.new_south_field_2' does not have a default specified, yet is NOT NULL. |
199 |
? Since you are adding or removing this field, you MUST specify a default |
200 |
? value to use for existing rows. Would you like to: |
201 |
? 1. Quit now, and add a default to the field in models.py |
202 |
? 2. Specify a one-off value to use for existing columns now |
203 |
? Please select a choice: 1 |
204 |
|
205 |
Data migrations |
206 |
*************** |
207 |
|
208 |
If we need to do data migration as well, for example rename a field, we use the |
209 |
'datamigration' management command. |
210 |
|
211 |
In contrast with schemamigration, to perform complex data migration, we must |
212 |
write the script manually. The process is the following: |
213 |
|
214 |
1. Introduce the changes in the code and fixtures (initial data). |
215 |
2. Execute:: |
216 |
|
217 |
$ ./bin/python manage.py datamigration <migration_name_here> |
218 |
|
219 |
For example:: |
220 |
|
221 |
$ ./bin/python manage.py datamigration db rename_credit_wallet |
222 |
Created 0003_rename_credit_wallet.py. |
223 |
|
224 |
3. We edit the generated script. It contains two methods: forwards and |
225 |
backwards. |
226 |
|
227 |
For database operations (column additions, alter tables etc) we use the |
228 |
South database API (http://south.aeracode.org/docs/databaseapi.html). |
229 |
|
230 |
To access the data, we use the database reference (orm) provided as |
231 |
parameter in forwards, backwards method declarations in the migration |
232 |
script. For example:: |
233 |
|
234 |
.. code-block:: python |
235 |
|
236 |
class Migration(DataMigration): |
237 |
|
238 |
def forwards(self, orm): |
239 |
orm.SynnefoUser.objects.all() |
240 |
|
241 |
4. To migrate the database to the latest version, we execute:: |
242 |
|
243 |
$ snf-manage migrate db |
244 |
|
245 |
To see which migrations are applied:: |
246 |
|
247 |
$ snf-manage migrate db --list |
248 |
|
249 |
db |
250 |
(*) 0001_initial |
251 |
(*) 0002_auto__add_field_synnefouser_new_south_test_field |
252 |
(*) 0003_rename_credit_wallet |
253 |
|
254 |
.. seealso:: |
255 |
More information and more thorough examples can be found in the South web site. |
256 |
http://south.aeracode.org/ |
257 |
|
258 |
|
259 |
Test coverage |
260 |
------------- |
261 |
|
262 |
In order to get code coverage reports you need to install django-test-coverage:: |
263 |
|
264 |
$ ./bin/pip install django-test-coverage |
265 |
|
266 |
Then edit your settings.py and configure the test runner:: |
267 |
|
268 |
TEST_RUNNER = 'django-test-coverage.runner.run_tests' |
269 |
|
270 |
|
271 |
.. include:: i18n.rst |
272 |
|
273 |
|
274 |
Building Synnefo package |
275 |
------------------------ |
276 |
|
277 |
To create a python package from the Synnefo source code run:: |
278 |
|
279 |
$ cd snf-app |
280 |
$ python setup.py sdist |
281 |
|
282 |
this command will create a ``tar.gz`` python source package inside ``dist`` directory. |
283 |
|
284 |
|
285 |
Building Synnefo documentation |
286 |
------------------------------ |
287 |
|
288 |
Make sure you have ``sphinx`` installed. |
289 |
|
290 |
.. code-block:: bash |
291 |
|
292 |
$ cd snf-app/docs |
293 |
$ make html |
294 |
|
295 |
html files are generated in ``snf-app/docs/_build/html`` directory |
296 |
|
297 |
.. include:: ci.rst |