blob: d22b2f0cd0e05bce92f14f209df86df18bfa0e3b [file] [log] [blame]
Matthew Treinish547e8432013-10-24 19:50:49 +00001#!/usr/bin/env bash
2
Matthew Treinish42516852014-06-19 10:51:29 -04003# Generate sample configuration for your project.
4#
5# Aside from the command line flags, it also respects a config file which
6# should be named oslo.config.generator.rc and be placed in the same directory.
7#
8# You can then export the following variables:
9# TEMPEST_CONFIG_GENERATOR_EXTRA_MODULES: list of modules to interrogate for options.
10# TEMPEST_CONFIG_GENERATOR_EXTRA_LIBRARIES: list of libraries to discover.
11# TEMPEST_CONFIG_GENERATOR_EXCLUDED_FILES: list of files to remove from automatic listing.
12
Matthew Treinish547e8432013-10-24 19:50:49 +000013print_hint() {
14 echo "Try \`${0##*/} --help' for more information." >&2
15}
16
Matthew Treinish90ac9142014-03-17 14:58:37 +000017PARSED_OPTIONS=$(getopt -n "${0##*/}" -o hb:p:m:l:o: \
18 --long help,base-dir:,package-name:,output-dir:,module:,library: -- "$@")
Matthew Treinish547e8432013-10-24 19:50:49 +000019
20if [ $? != 0 ] ; then print_hint ; exit 1 ; fi
21
22eval set -- "$PARSED_OPTIONS"
23
24while true; do
25 case "$1" in
26 -h|--help)
27 echo "${0##*/} [options]"
28 echo ""
29 echo "options:"
30 echo "-h, --help show brief help"
31 echo "-b, --base-dir=DIR project base directory"
32 echo "-p, --package-name=NAME project package name"
33 echo "-o, --output-dir=DIR file output directory"
Matthew Treinish90ac9142014-03-17 14:58:37 +000034 echo "-m, --module=MOD extra python module to interrogate for options"
35 echo "-l, --library=LIB extra library that registers options for discovery"
Matthew Treinish547e8432013-10-24 19:50:49 +000036 exit 0
37 ;;
38 -b|--base-dir)
39 shift
40 BASEDIR=`echo $1 | sed -e 's/\/*$//g'`
41 shift
42 ;;
43 -p|--package-name)
44 shift
45 PACKAGENAME=`echo $1`
46 shift
47 ;;
48 -o|--output-dir)
49 shift
50 OUTPUTDIR=`echo $1 | sed -e 's/\/*$//g'`
51 shift
52 ;;
Matthew Treinish90ac9142014-03-17 14:58:37 +000053 -m|--module)
54 shift
55 MODULES="$MODULES -m $1"
56 shift
57 ;;
58 -l|--library)
59 shift
60 LIBRARIES="$LIBRARIES -l $1"
61 shift
62 ;;
Matthew Treinish547e8432013-10-24 19:50:49 +000063 --)
64 break
65 ;;
66 esac
67done
68
69BASEDIR=${BASEDIR:-`pwd`}
70if ! [ -d $BASEDIR ]
71then
72 echo "${0##*/}: missing project base directory" >&2 ; print_hint ; exit 1
73elif [[ $BASEDIR != /* ]]
74then
75 BASEDIR=$(cd "$BASEDIR" && pwd)
76fi
77
Matthew Treinish90ac9142014-03-17 14:58:37 +000078PACKAGENAME=${PACKAGENAME:-$(python setup.py --name)}
Matthew Treinish547e8432013-10-24 19:50:49 +000079TARGETDIR=$BASEDIR/$PACKAGENAME
80if ! [ -d $TARGETDIR ]
81then
82 echo "${0##*/}: invalid project package name" >&2 ; print_hint ; exit 1
83fi
84
85OUTPUTDIR=${OUTPUTDIR:-$BASEDIR/etc}
86# NOTE(bnemec): Some projects put their sample config in etc/,
87# some in etc/$PACKAGENAME/
88if [ -d $OUTPUTDIR/$PACKAGENAME ]
89then
90 OUTPUTDIR=$OUTPUTDIR/$PACKAGENAME
91elif ! [ -d $OUTPUTDIR ]
92then
93 echo "${0##*/}: cannot access \`$OUTPUTDIR': No such file or directory" >&2
94 exit 1
95fi
96
97BASEDIRESC=`echo $BASEDIR | sed -e 's/\//\\\\\//g'`
98find $TARGETDIR -type f -name "*.pyc" -delete
99FILES=$(find $TARGETDIR -type f -name "*.py" ! -path "*/tests/*" \
100 -exec grep -l "Opt(" {} + | sed -e "s/^$BASEDIRESC\///g" | sort -u)
101
Matthew Treinish90ac9142014-03-17 14:58:37 +0000102RC_FILE="`dirname $0`/oslo.config.generator.rc"
103if test -r "$RC_FILE"
Matthew Treinish547e8432013-10-24 19:50:49 +0000104then
Matthew Treinish90ac9142014-03-17 14:58:37 +0000105 source "$RC_FILE"
Matthew Treinish547e8432013-10-24 19:50:49 +0000106fi
107
Matthew Treinish42516852014-06-19 10:51:29 -0400108for filename in ${TEMPEST_CONFIG_GENERATOR_EXCLUDED_FILES}; do
109 FILES="${FILES[@]/$filename/}"
110done
111
Matthew Treinish90ac9142014-03-17 14:58:37 +0000112for mod in ${TEMPEST_CONFIG_GENERATOR_EXTRA_MODULES}; do
113 MODULES="$MODULES -m $mod"
114done
115
116for lib in ${TEMPEST_CONFIG_GENERATOR_EXTRA_LIBRARIES}; do
117 LIBRARIES="$LIBRARIES -l $lib"
118done
119
Matthew Treinish547e8432013-10-24 19:50:49 +0000120export EVENTLET_NO_GREENDNS=yes
121
122OS_VARS=$(set | sed -n '/^OS_/s/=[^=]*$//gp' | xargs)
123[ "$OS_VARS" ] && eval "unset \$OS_VARS"
124DEFAULT_MODULEPATH=tempest.openstack.common.config.generator
125MODULEPATH=${MODULEPATH:-$DEFAULT_MODULEPATH}
126OUTPUTFILE=$OUTPUTDIR/$PACKAGENAME.conf.sample
Matthew Treinish90ac9142014-03-17 14:58:37 +0000127python -m $MODULEPATH $MODULES $LIBRARIES $FILES > $OUTPUTFILE
Matthew Treinish42516852014-06-19 10:51:29 -0400128if [ $? != 0 ]
129then
130 echo "Can not generate $OUTPUTFILE"
131 exit 1
132fi
Sean Daguefc691e32014-01-03 08:51:54 -0500133
134# Hook to allow projects to append custom config file snippets
135CONCAT_FILES=$(ls $BASEDIR/tools/config/*.conf.sample 2>/dev/null)
136for CONCAT_FILE in $CONCAT_FILES; do
137 cat $CONCAT_FILE >> $OUTPUTFILE
138done