Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.4 kB)

1
.. _snf-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 <snf-asterias>`.
11

    
12
It assumes thorough familiarity with the :ref:`snf-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
  (synnefo-env)$ snf-manage syndb
110
  (synnefo-env)$ snf-manage migrate
111
  (synnefo-env)$ snf-manage loaddata users flavors images
112

    
113
Development tips
114
****************
115

    
116
* Running a development web server:
117

    
118
  .. code-block:: console
119

    
120
     (synnefo-env)$ snf-manage runserver
121

    
122
  or, if you have the django_extensions and werkzeug packages installed:
123

    
124
  .. code-block:: console
125

    
126
     (synnefo-env)$ snf-manage runserver_plus
127

    
128
* Opening a python console with the synnefo environment initialized:
129

    
130
  .. code-block:: console
131

    
132
     (synnefo-env)$ snf-manage shell
133

    
134
  or, with the django_extensions package installed:
135

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

    
140

    
141
South Database Migrations
142
-------------------------
143

    
144
.. _snf-asterias-dev-initialmigration:
145

    
146
Initial Migration
147
*****************
148

    
149
To initialize south migrations in your database the following commands must be
150
executed:
151

    
152
.. code-block:: console
153

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

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

    
161
For example:
162

    
163

    
164
.. code-block:: console
165

    
166
   $ snf-manage migrate db 0001 --fake
167

    
168
To be sure that all migrations are applied use:
169

    
170
.. code-block:: console
171

    
172
   $ snf-manage migrate db --list
173

    
174
All starred migrations are applied.
175

    
176
Schema migrations
177
*****************
178

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

    
183

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

    
187
.. code-block:: console
188
   
189
   $ snf-manage schemamigration db --auto
190

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

    
194
.. code-block:: console
195

    
196
   $ snf-manage migrate db
197

    
198
Consider this example (adding a field to the ``SynnefoUser`` model):
199

    
200
.. code-block:: console
201

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

    
205
   Created 0002_auto__add_field_synnefouser_new_south_test_field.py.
206

    
207
You can now apply this migration with:
208

    
209
.. code-block:: console
210

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

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

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

    
224
.. code-block:: console
225

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

    
234
Data migrations
235
***************
236

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

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

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

    
246
   .. code-block:: console
247

    
248
      $ snf-manage datamigration <migration_name_here>
249

    
250
   For example:
251

    
252
   .. code-block:: console
253

    
254
      $ ./bin/python manage.py datamigration db rename_credit_wallet
255
      Created 0003_rename_credit_wallet.py.
256

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

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

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

    
267
   .. code-block:: python
268

    
269
      class Migration(DataMigration):
270

    
271
      def forwards(self, orm):
272
          orm.SynnefoUser.objects.all()
273

    
274
4. To migrate the database to the latest version, run:
275

    
276
   .. code-block:: console     
277
     
278
      $ snf-manage migrate db
279

    
280
   To see which migrations are applied:
281

    
282
   .. code-block:: console
283

    
284
      $ snf-manage migrate db --list
285

    
286
      db
287
        (*) 0001_initial
288
        (*) 0002_auto__add_field_synnefouser_new_south_test_field
289
        (*) 0003_rename_credit_wallet
290

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

    
295
Test coverage
296
-------------
297

    
298
In order to get code coverage reports you need to install django-test-coverage
299

    
300
.. code-block:: console
301

    
302
   $ pip install django-test-coverage
303

    
304
Then configure the test runner inside Django settings:
305

    
306
.. code-block:: python
307

    
308
   TEST_RUNNER = 'django-test-coverage.runner.run_tests'
309

    
310
.. include:: i18n.rst
311

    
312

    
313
Building Synnefo package
314
------------------------
315

    
316
To create a python package from the Synnefo source code run
317

    
318
.. code-block:: bash
319

    
320
    $ cd snf-app
321
    $ python setup.py sdist
322

    
323
this command will create a ``tar.gz`` python source package inside ``dist`` directory.
324

    
325

    
326
Building Synnefo documentation
327
------------------------------
328

    
329
Make sure you have ``sphinx`` installed.
330

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

    
336
.. note::
337

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

    
342
html files are generated in the ``snf-app/docs/_build/html`` directory.
343

    
344
.. include:: ci.rst