blob: d5d6fbcf02a9caae231f5afa1853d443b29e5734 [file] [log] [blame]
Dean Troyer07c35572012-03-05 07:15:30 -06001Contributing to DevStack
2========================
3
4
5General
6-------
7
Dean Troyerb8dd27b2013-10-17 12:03:55 -05008DevStack is written in UNIX shell script. It uses a number of bash-isms
Dean Troyerd97ee302015-02-04 12:35:39 -06009and so is limited to Bash (version 4 and up) and compatible shells.
Dean Troyerb8dd27b2013-10-17 12:03:55 -050010Shell script was chosen because it best illustrates the steps used to
11set up and interact with OpenStack components.
Dean Troyer07c35572012-03-05 07:15:30 -060012
Adrien Cunineaff3e12014-10-21 13:46:54 +020013DevStack's official repository is located on git.openstack.org at
14https://git.openstack.org/openstack-dev/devstack. Besides the master branch that
Dean Troyer07c35572012-03-05 07:15:30 -060015tracks the OpenStack trunk branches a separate branch is maintained for all
16OpenStack releases starting with Diablo (stable/diablo).
17
Dean Troyer6d04fd72012-12-21 11:03:37 -060018Contributing code to DevStack follows the usual OpenStack process as described
19in `How To Contribute`__ in the OpenStack wiki. `DevStack's LaunchPad project`__
Roger Luethic21cfc92014-07-22 15:35:02 +020020contains the usual links for blueprints, bugs, etc.
Dean Troyer6d04fd72012-12-21 11:03:37 -060021
22__ contribute_
zhangbailinc63d9332017-09-01 19:46:16 -070023.. _contribute: https://docs.openstack.org/infra/manual/developers.html
Dean Troyer6d04fd72012-12-21 11:03:37 -060024
25__ lp_
26.. _lp: https://launchpad.net/~devstack
27
Ian Wienand6f6e2fd2015-03-20 12:16:28 +110028The `Gerrit review
29queue <https://review.openstack.org/#/q/project:openstack-dev/devstack,n,z>`__
30is used for all commits.
31
Dean Troyer07c35572012-03-05 07:15:30 -060032The primary script in DevStack is ``stack.sh``, which performs the bulk of the
33work for DevStack's use cases. There is a subscript ``functions`` that contains
34generally useful shell functions and is used by a number of the scripts in
35DevStack.
36
37A number of additional scripts can be found in the ``tools`` directory that may
Dean Troyercc6b4432013-04-08 15:38:03 -050038be useful in supporting DevStack installations. Of particular note are ``info.sh``
Adam Spiersd9034762013-10-04 23:20:24 +010039to collect and report information about the installed system, and ``install_prereqs.sh``
Dean Troyercc6b4432013-04-08 15:38:03 -050040that handles installation of the prerequisite packages for DevStack. It is
41suitable, for example, to pre-load a system for making a snapshot.
Dean Troyer07c35572012-03-05 07:15:30 -060042
Ian Wienand6f6e2fd2015-03-20 12:16:28 +110043Repo Layout
44-----------
45
46The DevStack repo generally keeps all of the primary scripts at the root
47level.
48
49``doc`` - Contains the Sphinx source for the documentation.
50``tools/build_docs.sh`` is used to generate the HTML versions of the
51DevStack scripts. A complete doc build can be run with ``tox -edocs``.
52
53``exercises`` - Contains the test scripts used to sanity-check and
54demonstrate some OpenStack functions. These scripts know how to exit
55early or skip services that are not enabled.
56
57``extras.d`` - Contains the dispatch scripts called by the hooks in
58``stack.sh``, ``unstack.sh`` and ``clean.sh``. See :doc:`the plugins
59docs <plugins>` for more information.
60
61``files`` - Contains a variety of otherwise lost files used in
62configuring and operating DevStack. This includes templates for
63configuration files and the system dependency information. This is also
64where image files are downloaded and expanded if necessary.
65
66``lib`` - Contains the sub-scripts specific to each project. This is
67where the work of managing a project's services is located. Each
68top-level project (Keystone, Nova, etc) has a file here. Additionally
69there are some for system services and project plugins. These
70variables and functions are also used by related projects, such as
71Grenade, to manage a DevStack installation.
72
73``samples`` - Contains a sample of the local files not included in the
74DevStack repo.
75
76``tests`` - the DevStack test suite is rather sparse, mostly consisting
77of test of specific fragile functions in the ``functions`` and
78``functions-common`` files.
79
80``tools`` - Contains a collection of stand-alone scripts. While these
81may reference the top-level DevStack configuration they can generally be
82run alone. There are also some sub-directories to support specific
83environments such as XenServer.
84
Dean Troyer07c35572012-03-05 07:15:30 -060085
86Scripts
87-------
88
89DevStack scripts should generally begin by calling ``env(1)`` in the shebang line::
90
91 #!/usr/bin/env bash
92
93Sometimes the script needs to know the location of the DevStack install directory.
94``TOP_DIR`` should always point there, even if the script itself is located in
95a subdirectory::
96
Dean Troyerb8dd27b2013-10-17 12:03:55 -050097 # Keep track of the current DevStack directory.
Dean Troyer07c35572012-03-05 07:15:30 -060098 TOP_DIR=$(cd $(dirname "$0") && pwd)
99
100Many scripts will utilize shared functions from the ``functions`` file. There are
101also rc files (``stackrc`` and ``openrc``) that are often included to set the primary
102configuration of the user environment::
103
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500104 # Keep track of the current DevStack directory.
Dean Troyer51fb4542012-03-09 22:21:59 -0600105 TOP_DIR=$(cd $(dirname "$0") && pwd)
Dean Troyer07c35572012-03-05 07:15:30 -0600106
107 # Import common functions
Dean Troyer51fb4542012-03-09 22:21:59 -0600108 source $TOP_DIR/functions
Dean Troyer07c35572012-03-05 07:15:30 -0600109
110 # Import configuration
Dean Troyer51fb4542012-03-09 22:21:59 -0600111 source $TOP_DIR/openrc
Dean Troyer07c35572012-03-05 07:15:30 -0600112
113``stack.sh`` is a rather large monolithic script that flows through from beginning
Dean Troyercc6b4432013-04-08 15:38:03 -0500114to end. It has been broken down into project-specific subscripts (as noted above)
115located in ``lib`` to make ``stack.sh`` more manageable and to promote code reuse.
Dean Troyer05f23652012-08-29 15:20:21 -0500116
117These library sub-scripts have a number of fixed entry points, some of which may
118just be stubs. These entry points will be called by ``stack.sh`` in the
119following order::
120
121 install_XXXX
122 configure_XXXX
123 init_XXXX
124 start_XXXX
125 stop_XXXX
126 cleanup_XXXX
127
128There is a sub-script template in ``lib/templates`` to be used in creating new
129service sub-scripts. The comments in ``<>`` are meta comments describing
130how to use the template and should be removed.
Dean Troyer07c35572012-03-05 07:15:30 -0600131
Dean Troyer6d04fd72012-12-21 11:03:37 -0600132In order to show the dependencies and conditions under which project functions
133are executed the top-level conditional testing for things like ``is_service_enabled``
134should be done in ``stack.sh``. There may be nested conditionals that need
135to be in the sub-script, such as testing for keystone being enabled in
136``configure_swift()``.
137
Dean Troyer07c35572012-03-05 07:15:30 -0600138
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600139stackrc
140-------
141
142``stackrc`` is the global configuration file for DevStack. It is responsible for
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500143calling ``local.conf`` (or ``localrc`` if it exists) so local user configuration
144is recognized.
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600145
146The criteria for what belongs in ``stackrc`` can be vaguely summarized as
147follows:
148
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500149* All project repositories and branches handled directly in ``stack.sh``
150* Global configuration that may be referenced in ``local.conf``, i.e. ``DEST``, ``DATA_DIR``
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600151* Global service configuration like ``ENABLED_SERVICES``
152* Variables used by multiple services that do not have a clear owner, i.e.
Daniel Genind4708672014-10-31 15:01:29 -0400153 ``VOLUME_BACKING_FILE_SIZE`` (nova-compute, nova-volumes and cinder) or
154 ``PUBLIC_NETWORK_NAME`` (nova-network and neutron)
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600155* Variables that can not be cleanly declared in a project file due to
156 dependency ordering, i.e. the order of sourcing the project files can
157 not be changed for other reasons but the earlier file needs to dereference a
158 variable set in the later file. This should be rare.
159
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500160Also, variable declarations in ``stackrc`` before ``local.conf`` is sourced
161do NOT allow overriding (the form
162``FOO=${FOO:-baz}``); if they did then they can already be changed in ``local.conf``
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600163and can stay in the project file.
164
Dean Troyercc6b4432013-04-08 15:38:03 -0500165
Dean Troyer07c35572012-03-05 07:15:30 -0600166Documentation
167-------------
168
Dean Troyerf5cb1ce2014-10-21 11:16:58 -0500169The DevStack repo now contains all of the static pages of devstack.org in
170the ``doc/source`` directory. The OpenStack CI system rebuilds the docs after every
171commit and updates devstack.org (now a redirect to docs.openstack.org/developer/devstack).
Dean Troyer07c35572012-03-05 07:15:30 -0600172
173All of the scripts are processed with shocco_ to render them with the comments
174as text describing the script below. For this reason we tend to be a little
175verbose in the comments _ABOVE_ the code they pertain to. Shocco also supports
176Markdown formatting in the comments; use it sparingly. Specifically, ``stack.sh``
177uses Markdown headers to divide the script into logical sections.
178
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500179.. _shocco: https://github.com/dtroyer/shocco/tree/rst_support
180
181The script used to drive <code>shocco</code> is <code>tools/build_docs.sh</code>.
Dean Troyerf5cb1ce2014-10-21 11:16:58 -0500182The complete docs build is also handled with <code>tox -edocs</code> per the
183OpenStack project standard.
Dean Troyer07c35572012-03-05 07:15:30 -0600184
185
186Exercises
187---------
188
189The scripts in the exercises directory are meant to 1) perform basic operational
190checks on certain aspects of OpenStack; and b) document the use of the
191OpenStack command-line clients.
192
193In addition to the guidelines above, exercise scripts MUST follow the structure
194outlined here. ``swift.sh`` is perhaps the clearest example of these guidelines.
195These scripts are executed serially by ``exercise.sh`` in testing situations.
196
197* Begin and end with a banner that stands out in a sea of script logs to aid
198 in debugging failures, particularly in automated testing situations. If the
199 end banner is not displayed, the script ended prematurely and can be assumed
200 to have failed.
201
202 ::
203
204 echo "**************************************************"
205 echo "Begin DevStack Exercise: $0"
206 echo "**************************************************"
207 ...
208 set +o xtrace
209 echo "**************************************************"
210 echo "End DevStack Exercise: $0"
211 echo "**************************************************"
212
213* The scripts will generally have the shell ``xtrace`` attribute set to display
214 the actual commands being executed, and the ``errexit`` attribute set to exit
215 the script on non-zero exit codes::
216
217 # This script exits on an error so that errors don't compound and you see
Joe Gordon46400262013-06-30 04:32:27 -0700218 # only the first error that occurred.
Dean Troyer07c35572012-03-05 07:15:30 -0600219 set -o errexit
220
221 # Print the commands being run so that we can see the command that triggers
igor01acdab2016-07-29 13:11:53 +0200222 # an error. It is also useful for following as the install occurs.
Dean Troyer07c35572012-03-05 07:15:30 -0600223 set -o xtrace
224
Dean Troyer51fb4542012-03-09 22:21:59 -0600225* Settings and configuration are stored in ``exerciserc``, which must be
226 sourced after ``openrc`` or ``stackrc``::
227
228 # Import exercise configuration
229 source $TOP_DIR/exerciserc
230
Dean Troyer07c35572012-03-05 07:15:30 -0600231* There are a couple of helper functions in the common ``functions`` sub-script
232 that will check for non-zero exit codes and unset environment variables and
233 print a message and exit the script. These should be called after most client
234 commands that are not otherwise checked to short-circuit long timeouts
235 (instance boot failure, for example)::
236
237 swift post $CONTAINER
238 die_if_error "Failure creating container $CONTAINER"
239
240 FLOATING_IP=`euca-allocate-address | cut -f2`
241 die_if_not_set FLOATING_IP "Failure allocating floating IP"
242
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000243* If you want an exercise to be skipped when for example a service wasn't
244 enabled for the exercise to be run, you can exit your exercise with the
245 special exitcode 55 and it will be detected as skipped.
246
Dean Troyer07c35572012-03-05 07:15:30 -0600247* The exercise scripts should only use the various OpenStack client binaries to
248 interact with OpenStack. This specifically excludes any ``*-manage`` tools
249 as those assume direct access to configuration and databases, as well as direct
250 database access from the exercise itself.
251
252* If specific configuration needs to be present for the exercise to complete,
Einst Crazyf54f60a2015-10-30 23:00:57 +0800253 it should be staged in ``stack.sh``, or called from ``stack.sh``.
Dean Troyer07c35572012-03-05 07:15:30 -0600254
255* The ``OS_*`` environment variables should be the only ones used for all
256 authentication to OpenStack clients as documented in the CLIAuth_ wiki page.
257
zhangbailinc63d9332017-09-01 19:46:16 -0700258.. _CLIAuth: https://wiki.openstack.org/CLIAuth
Dean Troyer07c35572012-03-05 07:15:30 -0600259
260* The exercise MUST clean up after itself if successful. If it is not successful,
261 it is assumed that state will be left behind; this allows a chance for developers
262 to look around and attempt to debug the problem. The exercise SHOULD clean up
263 or graciously handle possible artifacts left over from previous runs if executed
264 again. It is acceptable to require a reboot or even a re-install of DevStack
265 to restore a clean test environment.
Sean Dague6db28922013-11-22 12:16:02 -0500266
267
268Bash Style Guidelines
269~~~~~~~~~~~~~~~~~~~~~
Roger Luethi473b6282014-04-13 13:47:42 +0200270DevStack defines a bash set of best practices for maintaining large
Sean Dague6db28922013-11-22 12:16:02 -0500271collections of bash scripts. These should be considered as part of the
272review process.
273
Dean Troyerf5cb1ce2014-10-21 11:16:58 -0500274DevStack uses the bashate_ style checker
275to enforce basic guidelines, similar to pep8 and flake8 tools for Python. The
276list below is not complete for what bashate checks, nor is it all checked
277by bashate. So many lines of code, so little time.
278
279.. _bashate: https://pypi.python.org/pypi/bashate
Sean Dague6db28922013-11-22 12:16:02 -0500280
281Whitespace Rules
282----------------
283
284- lines should not include trailing whitespace
285- there should be no hard tabs in the file
286- indents are 4 spaces, and all indentation should be some multiple of
287 them
288
289Control Structure Rules
290-----------------------
Ian Wienand6f6e2fd2015-03-20 12:16:28 +1100291
Sean Dague6db28922013-11-22 12:16:02 -0500292- then should be on the same line as the if
293- do should be on the same line as the for
294
295Example::
296
297 if [[ -r $TOP_DIR/local.conf ]]; then
298 LRC=$(get_meta_section_files $TOP_DIR/local.conf local)
299 for lfile in $LRC; do
300 if [[ "$lfile" == "localrc" ]]; then
301 if [[ -r $TOP_DIR/localrc ]]; then
302 warn $LINENO "localrc and local.conf:[[local]] both exist, using localrc"
303 else
304 echo "# Generated file, do not edit" >$TOP_DIR/.localrc.auto
305 get_meta_section $TOP_DIR/local.conf local $lfile >>$TOP_DIR/.localrc.auto
306 fi
307 fi
308 done
309 fi
310
311Variables and Functions
312-----------------------
Ian Wienand6f6e2fd2015-03-20 12:16:28 +1100313
Sean Dague6db28922013-11-22 12:16:02 -0500314- functions should be used whenever possible for clarity
315- functions should use ``local`` variables as much as possible to
316 ensure they are isolated from the rest of the environment
317- local variables should be lower case, global variables should be
318 upper case
319- function names should_have_underscores, NotCamelCase.
Ian Wienandaee18c72014-02-21 15:35:08 +1100320- functions should be declared as per the regex ^function foo {$
321 with code starting on the next line
Ian Wienandc7df4df2015-03-20 12:18:52 +1100322
323
324Review Criteria
Clark Boylan2a6112e2017-05-12 10:16:33 -0700325---------------
Ian Wienandc7df4df2015-03-20 12:18:52 +1100326
327There are some broad criteria that will be followed when reviewing
328your change
329
330* **Is it passing tests** -- your change will not be reviewed
Swapnil Kulkarni (coolsvap)7f0be4f2015-11-20 10:52:59 +0530331 thoroughly unless the official CI has run successfully against it.
Ian Wienandc7df4df2015-03-20 12:18:52 +1100332
333* **Does this belong in DevStack** -- DevStack reviewers have a
334 default position of "no" but are ready to be convinced by your
335 change.
336
337 For very large changes, you should consider :doc:`the plugins system
338 <plugins>` to see if your code is better abstracted from the main
339 repository.
340
341 For smaller changes, you should always consider if the change can be
342 encapsulated by per-user settings in ``local.conf``. A common example
343 is adding a simple config-option to an ``ini`` file. Specific flags
344 are not usually required for this, although adding documentation
345 about how to achieve a larger goal (which might include turning on
346 various settings, etc) is always welcome.
347
348* **Work-arounds** -- often things get broken and DevStack can be in a
349 position to fix them. Work-arounds are fine, but should be
350 presented in the context of fixing the root-cause of the problem.
351 This means it is well-commented in the code and the change-log and
352 mostly likely includes links to changes or bugs that fix the
353 underlying problem.
354
355* **Should this be upstream** -- DevStack generally does not override
356 default choices provided by projects and attempts to not
Atsushi SAKAI20401432015-07-27 20:42:44 +0900357 unexpectedly modify behavior.
Ian Wienandc7df4df2015-03-20 12:18:52 +1100358
359* **Context in commit messages** -- DevStack touches many different
360 areas and reviewers need context around changes to make good
361 decisions. We also always want it to be clear to someone -- perhaps
362 even years from now -- why we were motivated to make a change at the
363 time.
364
365* **Reviewers** -- please see ``MAINTAINERS.rst`` for a list of people
366 that should be added to reviews of various sub-systems.
Clark Boylan2a6112e2017-05-12 10:16:33 -0700367
368
369Making Changes, Testing, and CI
370-------------------------------
371
372Changes to Devstack are tested by automated continuous integration jobs
373that run on a variety of Linux Distros using a handful of common
374configurations. What this means is that every change to Devstack is
375self testing. One major benefit of this is that developers do not
376typically need to add new non voting test jobs to add features to
377Devstack. Instead the features can be added, then if testing passes
378with the feature enabled the change is ready to merge (pending code
379review).
380
381A concrete example of this was the switch from screen based service
382management to systemd based service management. No new jobs were
383created for this. Instead the features were added to devstack, tested
384locally and in CI using a change that enabled the feature, then once
385the enabling change was passing and the new behavior communicated and
386documented it was merged.
387
388Using this process has been proven to be effective and leads to
389quicker implementation of desired features.