~andreserl/maas/fix_lp1273197_1.7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
.. -*- mode: rst -*-

************
Hacking MAAS
************


Coding style
============

MAAS follows the `Launchpad Python Style Guide`_, except where it gets
Launchpad specific, and where it talks about `method naming`_. MAAS
instead adopts `PEP-8`_ naming in all cases, so method names should
usually use the ``lowercase_with_underscores`` form.

.. _Launchpad Python Style Guide:
  https://dev.launchpad.net/PythonStyleGuide

.. _method naming:
  https://dev.launchpad.net/PythonStyleGuide#Naming

.. _PEP-8:
  http://www.python.org/dev/peps/pep-0008/


Prerequisites
=============

You can grab MAAS's code manually from Launchpad but Bazaar_ makes it
easy to fetch the last version of the code. First of all, install
Bazaar::

    $ sudo apt-get install bzr

.. _Bazaar: http://bazaar.canonical.com/

Then go into the directory where you want the code to reside and run::

    $ bzr branch lp:maas maas && cd maas

MAAS depends on Postgres 9.1, RabbitMQ, Apache 2, daemontools,
pyinotify, and many other packages. To install everything that's needed
for running and developing MAAS, run::

    $ make install-dependencies

Careful: this will ``apt-get install`` many packages on your system,
via ``sudo``. It may prompt you for your password.

This will install ``bind9``. As a result you will have an extra daemon
running. If you are a developer and don't intend to run BIND locally,
you can disable the daemon by inserting ``exit 1`` at the top of
``/etc/default/bind9``. The package still needs to be installed for
tests though.

You may also need to install ``python-django-piston``, but installing
it seems to cause import errors for ``oauth`` when running the test
suite.

All other development dependencies are pulled automatically from
`PyPI`_ when ``buildout`` runs. See `First time using buildout?`_ and
`Running tests`_.

.. _PyPI:
  http://pypi.python.org/


First time using buildout?
==========================

Buildout_ is used to develop MAAS. Buildout, if configured so, can
cache downloaded files and built eggs. If you've not already done
something similar, the following snippet will massively improve build
times::

    [buildout]
    download-cache = /home/<your-user-name>/.buildout/cache
    eggs-directory = /home/<your-user-name>/.buildout/eggs

Put this in ``~/.buildout/default.cfg`` and create the ``cache``
directory::

    $ mkdir /home/<your-user-name>/.buildout/cache

The ``eggs`` directory will be created automatically.

.. _Buildout:
  http://www.buildout.org/


Running tests
=============

To run the whole suite::

    $ make test

To run tests at a lower level of granularity::

    $ ./bin/test.maas test src/maasserver/tests/test_api.py
    $ ./bin/test.maas test src/maasserver/tests/test_api.py:AnonymousEnlistmentAPITest

The test runner is `nose`_, so you can pass in options like
``--with-coverage`` and ``--nocapture`` (short option: ``-s``). The
latter is essential when using ``pdb`` so that stdout is not
adulterated.

.. _nose: http://readthedocs.org/docs/nose/en/latest/


Running JavaScript tests
^^^^^^^^^^^^^^^^^^^^^^^^

The JavaScript tests are run using Selenium_. Firefox is the default
browser but any browser supported by Selenium can be used to run the
tests. Note that you might need to download the appropriate driver and
make it available in the path. You can then choose which browsers to use by
setting the environment variable ``MAAS_TEST_BROWSERS`` to a comma-separated
list of the names of the browsers to use. For instance, to run the tests
with Firefox and Chrome::

    $ export MAAS_TEST_BROWSERS="Firefox, Chrome"

.. _Selenium: http://seleniumhq.org/


Running JavaScript tests with browsers on other platforms
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The JavaScript tests can be run using the `SauceLabs' OnDemand`_
service. There is a free version of this that provides 45 minutes a
month of testing. To get started, `sign up`_ and go to your `account
page`_, select the *Account* tab, and click *View my API Key*. Now
save your credentials::

  $ mkdir -p ~/.saucelabs/connect
  $ chmod go-rwx ~/.saucelabs/connect
  $ echo "${username} ${api_key}" > ~/.saucelabs/connect/credentials

(You need to substitute your SauceLabs username and API key above.)

Next, like when running Selenium tests locally, you need to specify
the browsers to run on. At the time of writing there are four to
choose from, all running on a Windows host::

  $ export MAAS_REMOTE_TEST_BROWSERS="IE7, IE8, IE9, Chrome"

By default, when ``MAAS_REMOTE_TEST_BROWSERS`` is not specified,
testing via OnDemand is *not* attempted.

.. _SauceLabs' OnDemand: http://saucelabs.com/

.. _sign up: http://saucelabs.com/pricing

.. _account page: https://saucelabs.com/account


Development MAAS server setup
=============================

Access to the database is configured in ``src/maas/development.py``.

The ``Makefile`` or the test suite sets up a development database
cluster inside your branch. It lives in the ``db`` directory, which
gets created on demand. You'll want to shut it down before deleting a
branch; see below.

First, set up the project. This fetches all the required dependencies
and sets up some useful commands in ``bin/``::

    $ make

Create the database cluster and initialise the development database::

    $ make syncdb

Optionally, populate your database with the sample data::

    $ make sampledata

By default, the snippet ``maas_proxy`` includes a definition for an http
proxy running on port 8000 on the same host as the MAAS server. This
means you can *either* install ``squid-deb-proxy``::

    $ sudo apt-get install squid-deb-proxy

*or* you can edit ``contrib/snippets_v2/generic`` to remove the proxy
definition.

Set the iSCSI config to include the MAAS configs::

    $ sudo tee -a /etc/tgt/targets.conf < contrib/tgt.conf

The http_proxy variable is only needed if you're downloading through a
proxy; "sudo" wouldn't pass it on to the script without the assignment.
Or if you don't have it set but do want to download through a proxy, pass
your proxy's URL: "http_proxy=http://proxy.example.com/"

Run the development webserver and watch all the logs go by::

    $ make run

Point your browser to http://localhost:5240/

If you've populated your instance with the sample data, you can login as a
simple user using the test account (username: 'test', password: 'test') or the
admin account (username: 'admin', password: 'test').

At this point you may also want to `download PXE boot resources`_.

.. _`download PXE boot resources`: `Downloading PXE boot resources`_

To shut down the database cluster and clean up all other generated files in
your branch::

    $ make distclean


Downloading PXE boot resources
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To use PXE booting, each cluster controller needs to download several
files relating to PXE booting. This process is automated, but it does
not start by default.

First create a superuser and start all MAAS services::

    $ bin/maas-region-admin createadmin
    $ make run

Substitute your own email.  The command will prompt for a choice of password.

Next, get the superuser's API key on the `account preferences`_ page in the
web UI, and use it to log into MAAS at the command-line::

    $ bin/maas login dev http://localhost:5240

.. _`account preferences`: http://localhost:5240/account/prefs/

Start downloading PXE boot resources::

    $  bin/maas dev node-groups import-boot-images

This sends jobs to each cluster controller, asking each to download
the boot resources they require. This may download dozens or hundreds
of megabytes, so it may take a while. To save bandwidth, set an HTTP
proxy beforehand::

    $ bin/maas dev maas set-config name=http_proxy value=http://...


Running the built-in TFTP server
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You will need to run the built-in TFTP server on the real TFTP port (69) if
you want to boot some real hardware. By default, it's set to start up on
port 5244 for testing purposes. Make these changes::

    * Edit ``etc/maas/pserv.yaml`` to change the tftp/port setting to 69
    * Install the ``authbind``package:

      $ sudo apt-get install authbind

    * Create a file ``/etc/authbind/byport/69`` that is *executable* by the
      user running MAAS.

      $ sudo touch /etc/authbind/byport/69
      $ sudo chmod a+x /etc/authbind/byport/69

Now when starting up the MAAS development webserver, "make run" and "make
start" will detect authbind's presence and use it automatically.


Running the BIND daemon for real
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

There's a BIND daemon that is started up as part of the development service
but it runs on port 5246 by default. If you want to make it run as a real
DNS server on the box then edit ``services/dns/run`` and change the port
declaration there so it says::

    port=53

Then as for TFTP above, create an authbind authorisation::

    $ sudo touch /etc/authbind/byport/53
    $ sudo chmod a+x /etc/authbind/byport/53

and run as normal.


Running the cluster worker
^^^^^^^^^^^^^^^^^^^^^^^^^^

The cluster also needs authbind as it needs to bind a socket on UDP port
68 for DHCP probing::

    $ sudo touch /etc/authbind/byport/68
    $ sudo chmod a+x /etc/authbind/byport/68

If you omit this, nothing else will break, but you will get an error in
the cluster log because it can't bind to the port.


Configuring DHCP
^^^^^^^^^^^^^^^^

MAAS requires a properly configured DHCP server so it can boot machines using
PXE. MAAS can work with its own instance of the ISC DHCP server, if you
install the maas-dhcp package::

    $ sudo apt-get install maas-dhcp

If you choose to run your own ISC DHCP server, there is a bit more
configuration to do. First, run this tool to generate a configuration that
will work with MAAS::

    $ maas-provision generate-dhcp-config [options]

Run ``maas-provision generate-dhcp-config -h`` to see the options. You will
need to provide various IP details such as the range of IP addresses to assign
to clients. You can use the generated output to configure your system's ISC
DHCP server, by inserting the configuration in the ``/etc/dhcp/dhcpd.conf``
file.

Also, edit /etc/default/isc-dhcp-server to set the INTERFACES variable to just
the network interfaces that should be serviced by this DHCP server.

Now restart dhcpd::

    $ sudo service isc-dhcp-server restart

None of this work is needed if you let MAAS run its own DHCP server by
installing ``maas-dhcp``.


Development services
====================

The development environment uses *daemontools* to manage the various
services that are required. These are all defined in subdirectories in
``services/``.

There are familiar service-like commands::

  $ make start
  $ make status
  $ make restart
  $ make stop

The latter is a dependency of ``distclean`` so just running ``make
distclean`` when you've finished with your branch is enough to stop
everything.

Individual services can be manipulated too::

  $ make services/pserv/@start

The ``@<action>`` pattern works for any of the services.

There's an additional special action, ``run``::

  $ make run

This starts all services up and tails their log files. When you're
done, kill ``tail`` (e.g. Ctrl-c), and all the services will be
stopped.

However, when used with individual services::

  $ make services/webapp/@run

it does something even cooler. First it shuts down the service, then
it restarts it in the foreground so you can see the logs in the
console. More importantly, it allows you to use ``pdb``, for example.

A note of caution: some of the services have slightly different
behaviour when run in the foreground:

* Django (the *webapp* service) will be run with its auto-reloading
  enabled.

* Apache (the *web* service) will run with ``-X``, which puts it in
  debug mode: only one worker will be started and the server will not
  detach from the console.

There's a convenience target for hacking Django that starts everything
up, but with Django in the foreground::

  $ make run+webapp

Apparently Django needs a lot of debugging ;)


Adding new dependencies
=======================

Since MAAS is distributed mainly as an Ubuntu package, all runtime
dependencies should be packaged, and we should develop with the
packaged version if possible. All dependencies, from a package or not,
need to be added to ``setup.py`` and ``buildout.cfg``, and the version
specified in ``versions.cfg`` (``allowed-picked-version`` is disabled,
hence ``buildout`` must be given precise version information).

If it is a development-only dependency (i.e. only needed for the test suite, or
for developers' convenience), simply running ``buildout`` like this will make
the necessary updates to ``versions.cfg``::

    $ ./bin/buildout -v buildout:allow-picked-versions=true


Adding new source files
=======================

When creating a new source file, a Python module or test for example,
always start with the appropriate template from the ``templates``
directory.


Database schema changes
=======================

MAAS uses South_ to manage changes to the database schema.

.. _South: http://south.aeracode.org

Be sure to have a look at `South's documentation`_ before you make any change.

.. _South's documentation: http://south.aeracode.org/docs/


Changing the schema
^^^^^^^^^^^^^^^^^^^

Once you've made a model change (i.e. a change to a file in
``src/<application>/models/*.py``) you have to run South's `schemamigration`_
command to create a migration file that will be stored in
``src/<application>/migrations/``.

Note that if you want to add a new model class you'll need to import it
in ``src/<application>/models/__init__.py``

.. _schemamigration: http://south.aeracode.org/docs/commands.html#schemamigration

Once you've changed the code, run::

    $ ./bin/maas-region-admin schemamigration maasserver --auto description_of_the_change

This will generate a migration module named
``src/maasserver/migrations/<auto_number>_description_of_the_change.py``. Don't
forget to add that file to the project with::

    $ bzr add src/maasserver/migrations/<auto_number>_description_of_the_change.py

To apply that migration, run::

    $ make syncdb


Performing data migration
^^^^^^^^^^^^^^^^^^^^^^^^^

If you need to perform data migration, very much in the same way, you will need
to run South's `datamigration`_ command. For instance, if you want to perform
changes to the ``maasserver`` application, run::

    $ ./bin/maas-region-admin datamigration maasserver description_of_the_change

.. _datamigration: http://south.aeracode.org/docs/commands.html#datamigration

This will generate a migration module named
``src/maasserver/migrations/<auto_number>_description_of_the_change.py``.
You will need to edit that file and fill the ``forwards`` and ``backwards``
methods where data should be actually migrated. Again, don't forget to
add that file to the project::

    $ bzr add src/maasserver/migrations/<auto_number>_description_of_the_change.py

Once the methods have been written, apply that migration with::

    $ make syncdb


Documentation
=============

Use `reST`_ with the `convention for headings as used in the Python
documentation`_.

.. _reST: http://sphinx.pocoo.org/rest.html

.. _convention for headings as used in the Python documentation:
  http://sphinx.pocoo.org/rest.html#sections