blob: 83455e3638f2534611928bdc65de86758eafd169 [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
9and so is limited to Bash (version 3 and up) and compatible shells.
10Shell 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
13DevStack's official repository is located on GitHub at
14https://github.com/openstack-dev/devstack.git. Besides the master branch that
15tracks 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`__
20contains the usual links for blueprints, bugs, tec.
21
22__ contribute_
Roger Luethi473b6282014-04-13 13:47:42 +020023.. _contribute: http://wiki.openstack.org/HowToContribute
Dean Troyer6d04fd72012-12-21 11:03:37 -060024
25__ lp_
26.. _lp: https://launchpad.net/~devstack
27
Dean Troyer07c35572012-03-05 07:15:30 -060028The primary script in DevStack is ``stack.sh``, which performs the bulk of the
29work for DevStack's use cases. There is a subscript ``functions`` that contains
30generally useful shell functions and is used by a number of the scripts in
31DevStack.
32
Dean Troyercc6b4432013-04-08 15:38:03 -050033The ``lib`` directory contains sub-scripts for projects or packages that ``stack.sh``
34sources to perform much of the work related to those projects. These sub-scripts
35contain configuration defaults and functions to configure, start and stop the project
36or package. These variables and functions are also used by related projects,
37such as Grenade, to manage a DevStack installation.
38
Dean Troyer07c35572012-03-05 07:15:30 -060039A number of additional scripts can be found in the ``tools`` directory that may
Dean Troyercc6b4432013-04-08 15:38:03 -050040be useful in supporting DevStack installations. Of particular note are ``info.sh``
Adam Spiersd9034762013-10-04 23:20:24 +010041to collect and report information about the installed system, and ``install_prereqs.sh``
Dean Troyercc6b4432013-04-08 15:38:03 -050042that handles installation of the prerequisite packages for DevStack. It is
43suitable, for example, to pre-load a system for making a snapshot.
Dean Troyer07c35572012-03-05 07:15:30 -060044
45
46Scripts
47-------
48
49DevStack scripts should generally begin by calling ``env(1)`` in the shebang line::
50
51 #!/usr/bin/env bash
52
53Sometimes the script needs to know the location of the DevStack install directory.
54``TOP_DIR`` should always point there, even if the script itself is located in
55a subdirectory::
56
Dean Troyerb8dd27b2013-10-17 12:03:55 -050057 # Keep track of the current DevStack directory.
Dean Troyer07c35572012-03-05 07:15:30 -060058 TOP_DIR=$(cd $(dirname "$0") && pwd)
59
60Many scripts will utilize shared functions from the ``functions`` file. There are
61also rc files (``stackrc`` and ``openrc``) that are often included to set the primary
62configuration of the user environment::
63
Dean Troyerb8dd27b2013-10-17 12:03:55 -050064 # Keep track of the current DevStack directory.
Dean Troyer51fb4542012-03-09 22:21:59 -060065 TOP_DIR=$(cd $(dirname "$0") && pwd)
Dean Troyer07c35572012-03-05 07:15:30 -060066
67 # Import common functions
Dean Troyer51fb4542012-03-09 22:21:59 -060068 source $TOP_DIR/functions
Dean Troyer07c35572012-03-05 07:15:30 -060069
70 # Import configuration
Dean Troyer51fb4542012-03-09 22:21:59 -060071 source $TOP_DIR/openrc
Dean Troyer07c35572012-03-05 07:15:30 -060072
73``stack.sh`` is a rather large monolithic script that flows through from beginning
Dean Troyercc6b4432013-04-08 15:38:03 -050074to end. It has been broken down into project-specific subscripts (as noted above)
75located in ``lib`` to make ``stack.sh`` more manageable and to promote code reuse.
Dean Troyer05f23652012-08-29 15:20:21 -050076
77These library sub-scripts have a number of fixed entry points, some of which may
78just be stubs. These entry points will be called by ``stack.sh`` in the
79following order::
80
81 install_XXXX
82 configure_XXXX
83 init_XXXX
84 start_XXXX
85 stop_XXXX
86 cleanup_XXXX
87
88There is a sub-script template in ``lib/templates`` to be used in creating new
89service sub-scripts. The comments in ``<>`` are meta comments describing
90how to use the template and should be removed.
Dean Troyer07c35572012-03-05 07:15:30 -060091
Dean Troyer6d04fd72012-12-21 11:03:37 -060092In order to show the dependencies and conditions under which project functions
93are executed the top-level conditional testing for things like ``is_service_enabled``
94should be done in ``stack.sh``. There may be nested conditionals that need
95to be in the sub-script, such as testing for keystone being enabled in
96``configure_swift()``.
97
Dean Troyer07c35572012-03-05 07:15:30 -060098
Dean Troyer2b7ce5a2013-01-10 13:22:45 -060099stackrc
100-------
101
102``stackrc`` is the global configuration file for DevStack. It is responsible for
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500103calling ``local.conf`` (or ``localrc`` if it exists) so local user configuration
104is recognized.
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600105
106The criteria for what belongs in ``stackrc`` can be vaguely summarized as
107follows:
108
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500109* All project repositories and branches handled directly in ``stack.sh``
110* Global configuration that may be referenced in ``local.conf``, i.e. ``DEST``, ``DATA_DIR``
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600111* Global service configuration like ``ENABLED_SERVICES``
112* Variables used by multiple services that do not have a clear owner, i.e.
113 ``VOLUME_BACKING_FILE_SIZE`` (nova-volumes and cinder) or ``PUBLIC_NETWORK_NAME``
Mark McClainb05c8762013-07-06 23:29:39 -0400114 (nova-network and neutron)
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600115* Variables that can not be cleanly declared in a project file due to
116 dependency ordering, i.e. the order of sourcing the project files can
117 not be changed for other reasons but the earlier file needs to dereference a
118 variable set in the later file. This should be rare.
119
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500120Also, variable declarations in ``stackrc`` before ``local.conf`` is sourced
121do NOT allow overriding (the form
122``FOO=${FOO:-baz}``); if they did then they can already be changed in ``local.conf``
Dean Troyer2b7ce5a2013-01-10 13:22:45 -0600123and can stay in the project file.
124
Dean Troyercc6b4432013-04-08 15:38:03 -0500125
Dean Troyer07c35572012-03-05 07:15:30 -0600126Documentation
127-------------
128
129The official DevStack repo on GitHub does not include a gh-pages branch that
130GitHub uses to create static web sites. That branch is maintained in the
131`CloudBuilders DevStack repo`__ mirror that supports the
132http://devstack.org site. This is the primary DevStack
133documentation along with the DevStack scripts themselves.
134
135__ repo_
136.. _repo: https://github.com/cloudbuilders/devstack
137
138All of the scripts are processed with shocco_ to render them with the comments
139as text describing the script below. For this reason we tend to be a little
140verbose in the comments _ABOVE_ the code they pertain to. Shocco also supports
141Markdown formatting in the comments; use it sparingly. Specifically, ``stack.sh``
142uses Markdown headers to divide the script into logical sections.
143
Dean Troyerb8dd27b2013-10-17 12:03:55 -0500144.. _shocco: https://github.com/dtroyer/shocco/tree/rst_support
145
146The script used to drive <code>shocco</code> is <code>tools/build_docs.sh</code>.
Dean Troyer07c35572012-03-05 07:15:30 -0600147
148
149Exercises
150---------
151
152The scripts in the exercises directory are meant to 1) perform basic operational
153checks on certain aspects of OpenStack; and b) document the use of the
154OpenStack command-line clients.
155
156In addition to the guidelines above, exercise scripts MUST follow the structure
157outlined here. ``swift.sh`` is perhaps the clearest example of these guidelines.
158These scripts are executed serially by ``exercise.sh`` in testing situations.
159
160* Begin and end with a banner that stands out in a sea of script logs to aid
161 in debugging failures, particularly in automated testing situations. If the
162 end banner is not displayed, the script ended prematurely and can be assumed
163 to have failed.
164
165 ::
166
167 echo "**************************************************"
168 echo "Begin DevStack Exercise: $0"
169 echo "**************************************************"
170 ...
171 set +o xtrace
172 echo "**************************************************"
173 echo "End DevStack Exercise: $0"
174 echo "**************************************************"
175
176* The scripts will generally have the shell ``xtrace`` attribute set to display
177 the actual commands being executed, and the ``errexit`` attribute set to exit
178 the script on non-zero exit codes::
179
180 # This script exits on an error so that errors don't compound and you see
Joe Gordon46400262013-06-30 04:32:27 -0700181 # only the first error that occurred.
Dean Troyer07c35572012-03-05 07:15:30 -0600182 set -o errexit
183
184 # Print the commands being run so that we can see the command that triggers
185 # an error. It is also useful for following allowing as the install occurs.
186 set -o xtrace
187
Dean Troyer51fb4542012-03-09 22:21:59 -0600188* Settings and configuration are stored in ``exerciserc``, which must be
189 sourced after ``openrc`` or ``stackrc``::
190
191 # Import exercise configuration
192 source $TOP_DIR/exerciserc
193
Dean Troyer07c35572012-03-05 07:15:30 -0600194* There are a couple of helper functions in the common ``functions`` sub-script
195 that will check for non-zero exit codes and unset environment variables and
196 print a message and exit the script. These should be called after most client
197 commands that are not otherwise checked to short-circuit long timeouts
198 (instance boot failure, for example)::
199
200 swift post $CONTAINER
201 die_if_error "Failure creating container $CONTAINER"
202
203 FLOATING_IP=`euca-allocate-address | cut -f2`
204 die_if_not_set FLOATING_IP "Failure allocating floating IP"
205
Chmouel Boudjnah408b0092012-03-15 23:21:55 +0000206* If you want an exercise to be skipped when for example a service wasn't
207 enabled for the exercise to be run, you can exit your exercise with the
208 special exitcode 55 and it will be detected as skipped.
209
Dean Troyer07c35572012-03-05 07:15:30 -0600210* The exercise scripts should only use the various OpenStack client binaries to
211 interact with OpenStack. This specifically excludes any ``*-manage`` tools
212 as those assume direct access to configuration and databases, as well as direct
213 database access from the exercise itself.
214
215* If specific configuration needs to be present for the exercise to complete,
216 it should be staged in ``stack.sh``, or called from ``stack.sh`` (see
217 ``files/keystone_data.sh`` for an example of this).
218
219* The ``OS_*`` environment variables should be the only ones used for all
220 authentication to OpenStack clients as documented in the CLIAuth_ wiki page.
221
222.. _CLIAuth: http://wiki.openstack.org/CLIAuth
223
224* The exercise MUST clean up after itself if successful. If it is not successful,
225 it is assumed that state will be left behind; this allows a chance for developers
226 to look around and attempt to debug the problem. The exercise SHOULD clean up
227 or graciously handle possible artifacts left over from previous runs if executed
228 again. It is acceptable to require a reboot or even a re-install of DevStack
229 to restore a clean test environment.
Sean Dague6db28922013-11-22 12:16:02 -0500230
231
232Bash Style Guidelines
233~~~~~~~~~~~~~~~~~~~~~
Roger Luethi473b6282014-04-13 13:47:42 +0200234DevStack defines a bash set of best practices for maintaining large
Sean Dague6db28922013-11-22 12:16:02 -0500235collections of bash scripts. These should be considered as part of the
236review process.
237
238We have a preliminary enforcing script for this called bash8 (only a
239small number of these rules are enforced).
240
241Whitespace Rules
242----------------
243
244- lines should not include trailing whitespace
245- there should be no hard tabs in the file
246- indents are 4 spaces, and all indentation should be some multiple of
247 them
248
249Control Structure Rules
250-----------------------
251- then should be on the same line as the if
252- do should be on the same line as the for
253
254Example::
255
256 if [[ -r $TOP_DIR/local.conf ]]; then
257 LRC=$(get_meta_section_files $TOP_DIR/local.conf local)
258 for lfile in $LRC; do
259 if [[ "$lfile" == "localrc" ]]; then
260 if [[ -r $TOP_DIR/localrc ]]; then
261 warn $LINENO "localrc and local.conf:[[local]] both exist, using localrc"
262 else
263 echo "# Generated file, do not edit" >$TOP_DIR/.localrc.auto
264 get_meta_section $TOP_DIR/local.conf local $lfile >>$TOP_DIR/.localrc.auto
265 fi
266 fi
267 done
268 fi
269
270Variables and Functions
271-----------------------
272- functions should be used whenever possible for clarity
273- functions should use ``local`` variables as much as possible to
274 ensure they are isolated from the rest of the environment
275- local variables should be lower case, global variables should be
276 upper case
277- function names should_have_underscores, NotCamelCase.
Ian Wienandaee18c72014-02-21 15:35:08 +1100278- functions should be declared as per the regex ^function foo {$
279 with code starting on the next line