Statistics
| Branch: | Tag: | Revision:

root / snf-app / docs / src / asterias-dev-guide.rst @ 442b6550

History | View | Annotate | Download (9.5 kB)

1
.. _asterias-developer-guide:
2

    
3
===============
4
Developer Guide
5
===============
6

    
7
This is the asterias developer guide.
8

    
9
It is intended for developers, wishing to implement new functionality
10
inside :ref:`asterias <asterias>`.
11

    
12
It assumes thorough familiarity with the :ref:`asterias-admin-guide`.
13

    
14
It contains development-specific ammendments to the basic installation steps
15
outlined in `installation guide <installation>`, and development-specific
16
notes.
17

    
18
Building a dev environment
19
--------------------------
20

    
21
virtualenv
22
**********
23

    
24
The easiest method to deploy a development environment is using
25
:command:`virtualenv`. Alternatively, you can use your system's package manager
26
to install any dependencies of synnefo components (e.g. Macports has them all).
27

    
28
   .. code-block:: console
29
   
30
      $ virtualenv ~/synnefo-env
31
      $ . ~/synnefo-env/bin/activate
32
      (synnefo-env)$ 
33
   
34
Any :command:`pip` commands executed from now on, affect the ``synnefo-env``
35
virtualenv.
36

    
37
* It is also recommended to install development helpers:
38

    
39
  .. code-block:: console
40
 
41
     (synnefo-env)$ pip install django_extensions
42

    
43
* Create a custom settings directory for :ref:`snf-common <snf-common>` and set
44
  the ``SYNNEFO_SETTINGS_DIR`` environment variable to use development-specific
45
  file:`*.conf` files inside this directory.
46

    
47
  (synnefo-env)$ mkdir ~/synnefo-settings-dir
48
  (synnefo-env)$ export SYNNEFO_SETTINGS_DIR=~/synnefo-settings-dir
49
    
50
  Insert your custom settings in a file such as :file:`$SYNNEFO_SETTINGS_DIR/99-local.conf`:
51

    
52
  .. code-block:: python
53
    
54
        # uncomment this if have django-extensions installed (pip install django_extensions)
55
        #INSTALLED_APPS = list(INSTALLED_APPS) + ['django_extensions']
56

    
57
        DEV_PATH = os.path.abspath(os.path.dirname(__file__))
58
        DATABASES['default']['NAME'] = os.path.join(DEV_PATH, "synnefo.sqlite")
59

    
60
        # development rabitmq configuration
61
        RABBIT_HOST = "<RabbitMQ_host>"
62
        RABBIT_USERNAME = "<RabbitMQ_username>"
63
        RABBIT_PASSWORD = "<RabbitMQ_password>"
64
        RABBIT_VHOST = "/"
65

    
66
        # development ganeti settings
67
        GANETI_MASTER_IP = "<Ganeti_master_IP>"
68
        GANETI_CLUSTER_INFO = (GANETI_MASTER_IP, 5080, "<username>", "<password>")
69
        GANETI_CREATEINSTANCE_KWARGS['disk_template'] = 'plain'
70

    
71
        # This prefix gets used when determining the instance names
72
        # of Synnefo VMs at the Ganeti backend.
73
        # The dash must always appear in the name!
74
        BACKEND_PREFIX_ID = "<your_commit_name>-"
75

    
76
        IGNORE_FLAVOR_DISK_SIZES = True
77

    
78
        # do not actually send emails
79
        # save them as files in /tmp/synnefo-mails
80
        EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
81
        EMAIL_FILE_PATH = '/tmp/synnefo-mails'
82

    
83
        # for UI developers
84
        UI_HANDLE_WINDOW_EXCEPTIONS = False
85

    
86
        # allow login using /?test url
87
        BYPASS_AUTHENTICATION = True 
88

    
89
synnefo source
90
**************
91

    
92
* Clone the repository of the synnefo software components you wish
93
  to work on, e.g.:
94

    
95
   .. code-block:: console
96
   
97
     (synnefo-env)$ git clone https://code.grnet.gr/git/synnefo synnefo
98
   
99
* Install the software components you wish to work on inside the
100
  virtualenv, in development mode:
101

    
102
   .. code-block:: console
103
   
104
      (synnefo-env)$ cd snf-asterias-app
105
      (synnefo-env)$ python setup.py develop -N
106
   
107
* Initialize database:
108

    
109
   .. code-block:: console
110
     
111
      (synnefo-env)$ snf-manage syndb
112
      (synnefo-env)$ snf-manage migrate
113
      (synnefo-env)$ snf-manage loaddata users flavors images
114
  
115
Development tips
116
****************
117

    
118
* Running a development web server:
119

    
120
  .. code-block:: console
121

    
122
     (synnefo-env)$ snf-manage runserver
123

    
124
  or, if you have the django_extensions and werkzeug packages installed:
125

    
126
  .. code-block:: console
127

    
128
     (synnefo-env)$ snf-manage runserver_plus
129

    
130
* Opening a python console with the synnefo environment initialized:
131

    
132
  .. code-block:: console
133

    
134
     (synnefo-env)$ snf-manage shell
135

    
136
  or, with the django_extensions package installed:
137

    
138
  .. code-block:: console
139
     
140
     (synnefo-env)$ snf-manage shell_plus
141

    
142

    
143
South Database Migrations
144
-------------------------
145

    
146
.. _asterias-dev-initialmigration:
147

    
148
Initial Migration
149
*****************
150

    
151
To initialize south migrations in your database the following commands must be
152
executed:
153

    
154
.. code-block:: console
155

    
156
   $ snf-manage syncdb       # Create / update the database with the south tables
157
   $ snf-manage migrate      # Perform migration in the database
158

    
159
Note that syncdb will create the latest models that exist in the db app, so some
160
migrations may fail.  If you are sure a migration has already taken place you
161
must use the ``--fake`` option, to apply it.
162

    
163
For example:
164

    
165

    
166
.. code-block:: console
167

    
168
   $ snf-manage migrate db 0001 --fake
169

    
170
To be sure that all migrations are applied use:
171

    
172
.. code-block:: console
173

    
174
   $ snf-manage migrate db --list
175

    
176
All starred migrations are applied.
177

    
178
Schema migrations
179
*****************
180

    
181
Do not use the syncdb management command. It can only be used the first time
182
and/or if you drop the database and must recreate it from scratch. See
183
:ref:`asterias-dev-initialmigration`.
184

    
185

    
186
Every time you make changes to the database and data migration is not required
187
(WARNING: always perform this with extreme care):
188

    
189
.. code-block:: console
190
   
191
   $ snf-manage schemamigration db --auto
192

    
193
The above will create the migration script. Now this must be applied to the live
194
database:
195

    
196
.. code-block:: console
197

    
198
   $ snf-manage migrate db
199

    
200
Consider this example (adding a field to the ``SynnefoUser`` model):
201

    
202
.. code-block:: console
203

    
204
   $ ./bin/python manage.py schemamigration db --auto
205
   + Added field new_south_test_field on db.SynnefoUser
206

    
207
   Created 0002_auto__add_field_synnefouser_new_south_test_field.py.
208

    
209
You can now apply this migration with:
210

    
211
.. code-block:: console
212

    
213
   $ ./manage.py migrate db
214
   Running migrations for db:
215
   - Migrating forwards to 0002_auto__add_field_synnefouser_new_south_test_field.
216
   > db:0002_auto__add_field_synnefouser_new_south_test_field
217
   - Loading initial data for db.
218

    
219
   Installing json fixture 'initial_data' from '/home/bkarak/devel/synnefo/../synnefo/db/fixtures'.
220
   Installed 1 object(s) from 1 fixture(s)
221

    
222
South needs some extra definitions to the model to preserve and migrate the
223
existing data, for example, if we add a field in a model, we should declare its
224
default value. If not, South will propably fail, after indicating the error:
225

    
226
.. code-block:: console
227

    
228
   $ ./bin/python manage.py schemamigration db --auto
229
   ? The field 'SynnefoUser.new_south_field_2' does not have a default specified, yet is NOT NULL.
230
   ? Since you are adding or removing this field, you MUST specify a default
231
   ? value to use for existing rows. Would you like to:
232
   ?  1. Quit now, and add a default to the field in models.py
233
   ?  2. Specify a one-off value to use for existing columns now
234
   ? Please select a choice: 1
235

    
236
Data migrations
237
***************
238

    
239
To do data migration as well, for example rename a field, use the
240
``datamigration`` management command.
241

    
242
In contrast with ``schemamigration``, to perform complex data migration, we
243
must write the script manually. The process is the following:
244

    
245
1. Introduce the changes in the code and fixtures (initial data).
246
2. Execute:
247

    
248
   .. code-block:: console
249

    
250
      $ snf-manage datamigration <migration_name_here>
251

    
252
   For example:
253

    
254
   .. code-block:: console
255

    
256
      $ ./bin/python manage.py datamigration db rename_credit_wallet
257
      Created 0003_rename_credit_wallet.py.
258

    
259
3. Edit the generated script. It contains two methods, ``forwards`` and
260
   ``backwards``.
261

    
262
   For database operations (column additions, alter tables etc), use the
263
   South database API (http://south.aeracode.org/docs/databaseapi.html).
264

    
265
   To access the data, use the database reference (``orm``) provided as
266
   parameter in ``forwards``, ``backwards`` method declarations in the
267
   migration script. For example:
268

    
269
   .. code-block:: python
270

    
271
      class Migration(DataMigration):
272

    
273
      def forwards(self, orm):
274
          orm.SynnefoUser.objects.all()
275

    
276
4. To migrate the database to the latest version, run:
277

    
278
   .. code-block:: console     
279
     
280
      $ snf-manage migrate db
281

    
282
   To see which migrations are applied:
283

    
284
   .. code-block:: console
285

    
286
      $ snf-manage migrate db --list
287

    
288
      db
289
        (*) 0001_initial
290
        (*) 0002_auto__add_field_synnefouser_new_south_test_field
291
        (*) 0003_rename_credit_wallet
292

    
293
.. seealso::
294
    More information and more thorough examples can be found in the South web site,
295
    http://south.aeracode.org/
296

    
297
Test coverage
298
-------------
299

    
300
In order to get code coverage reports you need to install django-test-coverage
301

    
302
.. code-block:: console
303

    
304
   $ pip install django-test-coverage
305

    
306
Then configure the test runner inside Django settings:
307

    
308
.. code-block:: python
309

    
310
   TEST_RUNNER = 'django-test-coverage.runner.run_tests'
311

    
312
.. include:: i18n.rst
313

    
314

    
315
Building Synnefo package
316
------------------------
317

    
318
To create a python package from the Synnefo source code run
319

    
320
.. code-block:: bash
321

    
322
    $ cd snf-app
323
    $ python setup.py sdist
324

    
325
this command will create a ``tar.gz`` python source package inside ``dist`` directory.
326

    
327

    
328
Building Synnefo documentation
329
------------------------------
330

    
331
Make sure you have ``sphinx`` installed.
332

    
333
.. code-block:: bash
334
    
335
    $ cd snf-app/docs
336
    $ make html
337

    
338
.. note::
339

    
340
   The theme define in the Sphinx configuration file ``conf.py`` is ``nature``,
341
   not available in the version of Sphinx shipped with Debian Squeeze. Replace
342
   it with ``default`` to build with a Squeeze-provided Sphinx.
343

    
344
html files are generated in the ``snf-app/docs/_build/html`` directory.
345

    
346
.. include:: ci.rst