| Sean Dague | e263c82 | 2014-12-05 14:25:28 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 | # | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 3 | # lib/config - Configuration file manipulation functions | 
 | 4 |  | 
 | 5 | # These functions have no external dependencies and the following side-effects: | 
 | 6 | # | 
 | 7 | # CONFIG_AWK_CMD is defined, default is ``awk`` | 
 | 8 |  | 
 | 9 | # Meta-config files contain multiple INI-style configuration files | 
 | 10 | # using a specific new section header to delimit them: | 
 | 11 | # | 
 | 12 | #   [[group-name|file-name]] | 
 | 13 | # | 
 | 14 | # 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] | 15 | # at a particular time.  These are called phases in ``stack.sh`` but | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 16 | # group here as these functions are not DevStack-specific. | 
 | 17 | # | 
 | 18 | # file-name is the destination of the config file | 
 | 19 |  | 
 | 20 | # Save trace setting | 
 | 21 | C_XTRACE=$(set +o | grep xtrace) | 
 | 22 | set +o xtrace | 
 | 23 |  | 
 | 24 |  | 
 | 25 | # Allow the awk command to be overridden on legacy platforms | 
 | 26 | CONFIG_AWK_CMD=${CONFIG_AWK_CMD:-awk} | 
 | 27 |  | 
 | 28 | # Get the section for the specific group and config file | 
 | 29 | # get_meta_section infile group configfile | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 30 | function get_meta_section { | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 31 |     local file=$1 | 
 | 32 |     local matchgroup=$2 | 
 | 33 |     local configfile=$3 | 
 | 34 |  | 
 | 35 |     [[ -r $file ]] || return 0 | 
 | 36 |     [[ -z $configfile ]] && return 0 | 
 | 37 |  | 
 | 38 |     $CONFIG_AWK_CMD -v matchgroup=$matchgroup -v configfile=$configfile ' | 
 | 39 |         BEGIN { group = "" } | 
| Isaku Yamahata | bff0014 | 2013-12-20 11:55:08 +0900 | [diff] [blame] | 40 |         /^\[\[.+\|.*\]\]/ { | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 41 |             if (group == "") { | 
 | 42 |                 gsub("[][]", "", $1); | 
 | 43 |                 split($1, a, "|"); | 
 | 44 |                 if (a[1] == matchgroup && a[2] == configfile) { | 
 | 45 |                     group=a[1] | 
 | 46 |                 } | 
 | 47 |             } else { | 
 | 48 |                 group="" | 
 | 49 |             } | 
 | 50 |             next | 
 | 51 |         } | 
 | 52 |         { | 
 | 53 |             if (group != "") | 
 | 54 |                 print $0 | 
 | 55 |         } | 
 | 56 |     ' $file | 
 | 57 | } | 
 | 58 |  | 
 | 59 |  | 
 | 60 | # Get a list of config files for a specific group | 
 | 61 | # get_meta_section_files infile group | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 62 | function get_meta_section_files { | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 63 |     local file=$1 | 
 | 64 |     local matchgroup=$2 | 
 | 65 |  | 
 | 66 |     [[ -r $file ]] || return 0 | 
 | 67 |  | 
 | 68 |     $CONFIG_AWK_CMD -v matchgroup=$matchgroup ' | 
| Sean Dague | 537d402 | 2013-10-22 07:43:22 -0400 | [diff] [blame] | 69 |         /^\[\[.+\|.*\]\]/ { | 
 | 70 |             gsub("[][]", "", $1); | 
 | 71 |             split($1, a, "|"); | 
 | 72 |             if (a[1] == matchgroup) | 
 | 73 |                 print a[2] | 
 | 74 |         } | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 75 |     ' $file | 
 | 76 | } | 
 | 77 |  | 
 | 78 |  | 
 | 79 | # Merge the contents of a meta-config file into its destination config file | 
 | 80 | # If configfile does not exist it will be created. | 
 | 81 | # merge_config_file infile group configfile | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 82 | function merge_config_file { | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 83 |     local file=$1 | 
 | 84 |     local matchgroup=$2 | 
 | 85 |     local configfile=$3 | 
 | 86 |  | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 87 |     get_meta_section $file $matchgroup $configfile | \ | 
 | 88 |     $CONFIG_AWK_CMD -v configfile=$configfile ' | 
| Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 89 |         BEGIN { | 
 | 90 |             section = "" | 
 | 91 |             last_section = "" | 
 | 92 |             section_count = 0 | 
 | 93 |         } | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 94 |         /^\[.+\]/ { | 
 | 95 |             gsub("[][]", "", $1); | 
 | 96 |             section=$1 | 
 | 97 |             next | 
 | 98 |         } | 
 | 99 |         /^ *\#/ { | 
 | 100 |             next | 
 | 101 |         } | 
| Dean Troyer | 2ac8b3f | 2013-12-04 17:20:28 -0600 | [diff] [blame] | 102 |         /^[^ \t]+/ { | 
| Fergal Mc Carthy | cc87c28 | 2014-10-09 16:16:42 -0400 | [diff] [blame] | 103 |             # get offset of first '=' in $0 | 
 | 104 |             eq_idx = index($0, "=") | 
 | 105 |             # extract attr & value from $0 | 
 | 106 |             attr = substr($0, 1, eq_idx - 1) | 
 | 107 |             value = substr($0, eq_idx + 1) | 
 | 108 |             # only need to strip trailing whitespace from attr | 
 | 109 |             sub(/[ \t]*$/, "", attr) | 
 | 110 |             # need to strip leading & trailing whitespace from value | 
 | 111 |             sub(/^[ \t]*/, "", value) | 
 | 112 |             sub(/[ \t]*$/, "", value) | 
| Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 113 |  | 
 | 114 |             # cfg_attr_count: number of config lines per [section, attr] | 
 | 115 |             # cfg_attr: three dimensional array to keep all the config lines per [section, attr] | 
 | 116 |             # cfg_section: keep the section names in the same order as they appear in local.conf | 
 | 117 |             # cfg_sec_attr_name: keep the attr names in the same order as they appear in local.conf | 
 | 118 |             if (! (section, attr) in cfg_attr_count) { | 
 | 119 |                 if (section != last_section) { | 
 | 120 |                     cfg_section[section_count++] = section | 
 | 121 |                     last_section = section | 
 | 122 |                 } | 
 | 123 |                 attr_count = cfg_sec_attr_count[section_count - 1]++ | 
 | 124 |                 cfg_sec_attr_name[section_count - 1, attr_count] = attr | 
 | 125 |  | 
 | 126 |                 cfg_attr[section, attr, 0] = value | 
 | 127 |                 cfg_attr_count[section, attr] = 1 | 
 | 128 |             } else { | 
 | 129 |                 lno = cfg_attr_count[section, attr]++ | 
 | 130 |                 cfg_attr[section, attr, lno] = value | 
 | 131 |             } | 
 | 132 |         } | 
 | 133 |         END { | 
 | 134 |             # Process each section in order | 
 | 135 |             for (sno = 0; sno < section_count; sno++) { | 
 | 136 |                 section = cfg_section[sno] | 
 | 137 |                 # The ini routines simply append a config item immediately | 
 | 138 |                 # after the section header. To keep the same order as defined | 
 | 139 |                 # in local.conf, invoke the ini routines in the reverse order | 
 | 140 |                 for (attr_no = cfg_sec_attr_count[sno] - 1; attr_no >=0; attr_no--) { | 
 | 141 |                     attr = cfg_sec_attr_name[sno, attr_no] | 
 | 142 |                     if (cfg_attr_count[section, attr] == 1) | 
| Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 143 |                         print "iniset " configfile " " section " " attr " \"" cfg_attr[section, attr, 0] "\"" | 
| Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 144 |                     else { | 
 | 145 |                         # For multiline, invoke the ini routines in the reverse order | 
 | 146 |                         count = cfg_attr_count[section, attr] | 
| Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 147 |                         print "iniset " configfile " " section " " attr " \"" cfg_attr[section, attr, count - 1] "\"" | 
| Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 148 |                         for (l = count -2; l >= 0; l--) | 
| Ian Wienand | f3bf8b6 | 2014-10-29 21:53:56 +1100 | [diff] [blame] | 149 |                             print "iniadd_literal " configfile " " section " " attr " \"" cfg_attr[section, attr, l] "\"" | 
| Robert Li | 751ad1a | 2014-10-15 21:40:53 -0400 | [diff] [blame] | 150 |                     } | 
 | 151 |                 } | 
 | 152 |             } | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 153 |         } | 
 | 154 |     ' | while read a; do eval "$a"; done | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 155 | } | 
 | 156 |  | 
 | 157 |  | 
 | 158 | # Merge all of the files specified by group | 
 | 159 | # merge_config_group infile group [group ...] | 
| Ian Wienand | aee18c7 | 2014-02-21 15:35:08 +1100 | [diff] [blame] | 160 | function merge_config_group { | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 161 |     local localfile=$1; shift | 
 | 162 |     local matchgroups=$@ | 
 | 163 |  | 
 | 164 |     [[ -r $localfile ]] || return 0 | 
 | 165 |  | 
| Dean Troyer | b1e3d0f | 2014-07-25 14:57:54 -0500 | [diff] [blame] | 166 |     local configfile group | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 167 |     for group in $matchgroups; do | 
 | 168 |         for configfile in $(get_meta_section_files $localfile $group); do | 
| Ryota MIBU | 410f5c0 | 2014-04-04 02:00:31 +0900 | [diff] [blame] | 169 |             if [[ -d $(dirname $(eval "echo $configfile")) ]]; then | 
| Dean Troyer | 893e663 | 2013-09-13 15:05:51 -0500 | [diff] [blame] | 170 |                 merge_config_file $localfile $group $configfile | 
 | 171 |             fi | 
 | 172 |         done | 
 | 173 |     done | 
 | 174 | } | 
 | 175 |  | 
 | 176 |  | 
 | 177 | # Restore xtrace | 
 | 178 | $C_XTRACE | 
 | 179 |  | 
 | 180 | # Local variables: | 
 | 181 | # mode: shell-script | 
 | 182 | # End: |