Matthew Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Matthew Treinish | 4251685 | 2014-06-19 10:51:29 -0400 | [diff] [blame] | 3 | # 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 Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 13 | print_hint() { |
| 14 | echo "Try \`${0##*/} --help' for more information." >&2 |
| 15 | } |
| 16 | |
Matthew Treinish | 90ac914 | 2014-03-17 14:58:37 +0000 | [diff] [blame] | 17 | PARSED_OPTIONS=$(getopt -n "${0##*/}" -o hb:p:m:l:o: \ |
| 18 | --long help,base-dir:,package-name:,output-dir:,module:,library: -- "$@") |
Matthew Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 19 | |
| 20 | if [ $? != 0 ] ; then print_hint ; exit 1 ; fi |
| 21 | |
| 22 | eval set -- "$PARSED_OPTIONS" |
| 23 | |
| 24 | while 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 Treinish | 90ac914 | 2014-03-17 14:58:37 +0000 | [diff] [blame] | 34 | echo "-m, --module=MOD extra python module to interrogate for options" |
| 35 | echo "-l, --library=LIB extra library that registers options for discovery" |
Matthew Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 36 | 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 Treinish | 90ac914 | 2014-03-17 14:58:37 +0000 | [diff] [blame] | 53 | -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 Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 63 | --) |
| 64 | break |
| 65 | ;; |
| 66 | esac |
| 67 | done |
| 68 | |
| 69 | BASEDIR=${BASEDIR:-`pwd`} |
| 70 | if ! [ -d $BASEDIR ] |
| 71 | then |
| 72 | echo "${0##*/}: missing project base directory" >&2 ; print_hint ; exit 1 |
| 73 | elif [[ $BASEDIR != /* ]] |
| 74 | then |
| 75 | BASEDIR=$(cd "$BASEDIR" && pwd) |
| 76 | fi |
| 77 | |
Matthew Treinish | 90ac914 | 2014-03-17 14:58:37 +0000 | [diff] [blame] | 78 | PACKAGENAME=${PACKAGENAME:-$(python setup.py --name)} |
Matthew Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 79 | TARGETDIR=$BASEDIR/$PACKAGENAME |
| 80 | if ! [ -d $TARGETDIR ] |
| 81 | then |
| 82 | echo "${0##*/}: invalid project package name" >&2 ; print_hint ; exit 1 |
| 83 | fi |
| 84 | |
| 85 | OUTPUTDIR=${OUTPUTDIR:-$BASEDIR/etc} |
| 86 | # NOTE(bnemec): Some projects put their sample config in etc/, |
| 87 | # some in etc/$PACKAGENAME/ |
| 88 | if [ -d $OUTPUTDIR/$PACKAGENAME ] |
| 89 | then |
| 90 | OUTPUTDIR=$OUTPUTDIR/$PACKAGENAME |
| 91 | elif ! [ -d $OUTPUTDIR ] |
| 92 | then |
| 93 | echo "${0##*/}: cannot access \`$OUTPUTDIR': No such file or directory" >&2 |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | BASEDIRESC=`echo $BASEDIR | sed -e 's/\//\\\\\//g'` |
| 98 | find $TARGETDIR -type f -name "*.pyc" -delete |
| 99 | FILES=$(find $TARGETDIR -type f -name "*.py" ! -path "*/tests/*" \ |
| 100 | -exec grep -l "Opt(" {} + | sed -e "s/^$BASEDIRESC\///g" | sort -u) |
| 101 | |
Matthew Treinish | 90ac914 | 2014-03-17 14:58:37 +0000 | [diff] [blame] | 102 | RC_FILE="`dirname $0`/oslo.config.generator.rc" |
| 103 | if test -r "$RC_FILE" |
Matthew Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 104 | then |
Matthew Treinish | 90ac914 | 2014-03-17 14:58:37 +0000 | [diff] [blame] | 105 | source "$RC_FILE" |
Matthew Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 106 | fi |
| 107 | |
Matthew Treinish | 4251685 | 2014-06-19 10:51:29 -0400 | [diff] [blame] | 108 | for filename in ${TEMPEST_CONFIG_GENERATOR_EXCLUDED_FILES}; do |
| 109 | FILES="${FILES[@]/$filename/}" |
| 110 | done |
| 111 | |
Matthew Treinish | 90ac914 | 2014-03-17 14:58:37 +0000 | [diff] [blame] | 112 | for mod in ${TEMPEST_CONFIG_GENERATOR_EXTRA_MODULES}; do |
| 113 | MODULES="$MODULES -m $mod" |
| 114 | done |
| 115 | |
| 116 | for lib in ${TEMPEST_CONFIG_GENERATOR_EXTRA_LIBRARIES}; do |
| 117 | LIBRARIES="$LIBRARIES -l $lib" |
| 118 | done |
| 119 | |
Matthew Treinish | 547e843 | 2013-10-24 19:50:49 +0000 | [diff] [blame] | 120 | export EVENTLET_NO_GREENDNS=yes |
| 121 | |
| 122 | OS_VARS=$(set | sed -n '/^OS_/s/=[^=]*$//gp' | xargs) |
| 123 | [ "$OS_VARS" ] && eval "unset \$OS_VARS" |
| 124 | DEFAULT_MODULEPATH=tempest.openstack.common.config.generator |
| 125 | MODULEPATH=${MODULEPATH:-$DEFAULT_MODULEPATH} |
| 126 | OUTPUTFILE=$OUTPUTDIR/$PACKAGENAME.conf.sample |
Matthew Treinish | 90ac914 | 2014-03-17 14:58:37 +0000 | [diff] [blame] | 127 | python -m $MODULEPATH $MODULES $LIBRARIES $FILES > $OUTPUTFILE |
Matthew Treinish | 4251685 | 2014-06-19 10:51:29 -0400 | [diff] [blame] | 128 | if [ $? != 0 ] |
| 129 | then |
| 130 | echo "Can not generate $OUTPUTFILE" |
| 131 | exit 1 |
| 132 | fi |
Sean Dague | fc691e3 | 2014-01-03 08:51:54 -0500 | [diff] [blame] | 133 | |
| 134 | # Hook to allow projects to append custom config file snippets |
| 135 | CONCAT_FILES=$(ls $BASEDIR/tools/config/*.conf.sample 2>/dev/null) |
| 136 | for CONCAT_FILE in $CONCAT_FILES; do |
| 137 | cat $CONCAT_FILE >> $OUTPUTFILE |
| 138 | done |