| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 1 | # lib/config - Configuration file manipulation functions | 
|  | 2 |  | 
|  | 3 | # These functions have no external dependencies and the following side-effects: | 
|  | 4 | # | 
|  | 5 | # CONFIG_AWK_CMD is defined, default is ``awk`` | 
|  | 6 |  | 
|  | 7 | # Meta-config files contain multiple INI-style configuration files | 
|  | 8 | # using a specific new section header to delimit them: | 
|  | 9 | # | 
|  | 10 | #   [[group-name|file-name]] | 
|  | 11 | # | 
|  | 12 | # group-name refers to the group of configuration file changes to be processed | 
| Sean Dague | 537d402 | 2013-10-22 07:43:22 -0400 | [diff] [blame] | 13 | # at a particular time.  These are called phases in ``stack.sh`` but | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 14 | # group here as these functions are not DevStack-specific. | 
|  | 15 | # | 
|  | 16 | # file-name is the destination of the config file | 
|  | 17 |  | 
|  | 18 | # Save trace setting | 
|  | 19 | C_XTRACE=$(set +o | grep xtrace) | 
|  | 20 | set +o xtrace | 
|  | 21 |  | 
|  | 22 |  | 
|  | 23 | # Allow the awk command to be overridden on legacy platforms | 
|  | 24 | CONFIG_AWK_CMD=${CONFIG_AWK_CMD:-awk} | 
|  | 25 |  | 
|  | 26 | # Get the section for the specific group and config file | 
|  | 27 | # get_meta_section infile group configfile | 
|  | 28 | function get_meta_section() { | 
|  | 29 | local file=$1 | 
|  | 30 | local matchgroup=$2 | 
|  | 31 | local configfile=$3 | 
|  | 32 |  | 
|  | 33 | [[ -r $file ]] || return 0 | 
|  | 34 | [[ -z $configfile ]] && return 0 | 
|  | 35 |  | 
|  | 36 | $CONFIG_AWK_CMD -v matchgroup=$matchgroup -v configfile=$configfile ' | 
|  | 37 | BEGIN { group = "" } | 
| Isaku Yamahata | bff0014 | 2013-12-20 11:55:08 +0900 | [diff] [blame] | 38 | /^\[\[.+\|.*\]\]/ { | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 39 | if (group == "") { | 
|  | 40 | gsub("[][]", "", $1); | 
|  | 41 | split($1, a, "|"); | 
|  | 42 | if (a[1] == matchgroup && a[2] == configfile) { | 
|  | 43 | group=a[1] | 
|  | 44 | } | 
|  | 45 | } else { | 
|  | 46 | group="" | 
|  | 47 | } | 
|  | 48 | next | 
|  | 49 | } | 
|  | 50 | { | 
|  | 51 | if (group != "") | 
|  | 52 | print $0 | 
|  | 53 | } | 
|  | 54 | ' $file | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 |  | 
|  | 58 | # Get a list of config files for a specific group | 
|  | 59 | # get_meta_section_files infile group | 
|  | 60 | function get_meta_section_files() { | 
|  | 61 | local file=$1 | 
|  | 62 | local matchgroup=$2 | 
|  | 63 |  | 
|  | 64 | [[ -r $file ]] || return 0 | 
|  | 65 |  | 
|  | 66 | $CONFIG_AWK_CMD -v matchgroup=$matchgroup ' | 
| Sean Dague | 537d402 | 2013-10-22 07:43:22 -0400 | [diff] [blame] | 67 | /^\[\[.+\|.*\]\]/ { | 
|  | 68 | gsub("[][]", "", $1); | 
|  | 69 | split($1, a, "|"); | 
|  | 70 | if (a[1] == matchgroup) | 
|  | 71 | print a[2] | 
|  | 72 | } | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 73 | ' $file | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 |  | 
|  | 77 | # Merge the contents of a meta-config file into its destination config file | 
|  | 78 | # If configfile does not exist it will be created. | 
|  | 79 | # merge_config_file infile group configfile | 
|  | 80 | function merge_config_file() { | 
|  | 81 | local file=$1 | 
|  | 82 | local matchgroup=$2 | 
|  | 83 | local configfile=$3 | 
|  | 84 |  | 
|  | 85 | [[ -r $configfile ]] || touch $configfile | 
|  | 86 |  | 
|  | 87 | get_meta_section $file $matchgroup $configfile | \ | 
|  | 88 | $CONFIG_AWK_CMD -v configfile=$configfile ' | 
|  | 89 | BEGIN { section = "" } | 
|  | 90 | /^\[.+\]/ { | 
|  | 91 | gsub("[][]", "", $1); | 
|  | 92 | section=$1 | 
|  | 93 | next | 
|  | 94 | } | 
|  | 95 | /^ *\#/ { | 
|  | 96 | next | 
|  | 97 | } | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 98 | /^[^ \t]+/ { | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 99 | split($0, d, " *= *") | 
|  | 100 | print "iniset " configfile " " section " " d[1] " \"" d[2] "\"" | 
|  | 101 | } | 
|  | 102 | ' | while read a; do eval "$a"; done | 
|  | 103 |  | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 |  | 
|  | 107 | # Merge all of the files specified by group | 
|  | 108 | # merge_config_group infile group [group ...] | 
|  | 109 | function merge_config_group() { | 
|  | 110 | local localfile=$1; shift | 
|  | 111 | local matchgroups=$@ | 
|  | 112 |  | 
|  | 113 | [[ -r $localfile ]] || return 0 | 
|  | 114 |  | 
|  | 115 | for group in $matchgroups; do | 
|  | 116 | for configfile in $(get_meta_section_files $localfile $group); do | 
|  | 117 | if [[ -d $(dirname $configfile) ]]; then | 
|  | 118 | merge_config_file $localfile $group $configfile | 
|  | 119 | fi | 
|  | 120 | done | 
|  | 121 | done | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 |  | 
|  | 125 | # Restore xtrace | 
|  | 126 | $C_XTRACE | 
|  | 127 |  | 
|  | 128 | # Local variables: | 
|  | 129 | # mode: shell-script | 
|  | 130 | # End: |