-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_settings
executable file
·206 lines (185 loc) · 9.33 KB
/
build_settings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
## Runs `cockroach` locally on your system to get the very latest cluster settings via:
## cockroach gen settings-list --format=rawhtml
## Then performs the following:
## 1. Adds anchor link targets to all cluster settings
## - Links are added in the following format:
## - `admission.epoch_lifo.enabled` becomes <a name="setting-admission-epoch_lifo-enabled"></a>admission.epoch_lifo.enabled
## - `version` becomes <a name="setting-version"></a>version
## 2. For settings descriptions only, fixes non-rendering characters:
## - Removes all backticks (actually this is applies to the entire line, but only encountered in desc)
## - Replaces any instances of `<` or `>` with HTML representations `<` and `>`
## 3. Compares generated list against current include file
## - If differences, replaces current with generated.
## - If -q is NOT set to this script, additionally report diff
## - If identical, takes no action
##
## Optionally supports a -v flag to specify a specific cluster settings version.
## Supports the same local symlinking scheme for versioned `cockroach`
## that my `spinup` program does. See NOTE ON VERSION there:
## https://github.com/andf-crl/tools/blob/main/spinup
##
## To run in quiet mode, with no output, use -q
## Research:
## - Generated content lives here: cockroachdb/cockroach/docs/generated
## - Bazel finds: cockroachdb/cockroach/docs/generated/bazel_targets.txt
## - And builds:
## - docs/generated/settings
## - docs/generated/settings:settings_for_tenants
## - The build itself is (almost) as simple as:
## - settings: `cockroach gen settings-list --format=rawhtml`
## - settings:settings_for_tenants: `cockroach gen settings-list --without-system-only`
## - Actual upstream code that handles this for us seems to be here:
## https://github.com/cockroachdb/cockroach/blob/7191d8119c4f75b9008e7c8943b46e99468eb9be/pkg/cli/clisqlexec/format_table.go#L471-L520
## - We could get upstream to edit it, or we could just tweak the output it gives us ourselves.
## This script explores the latter option.
##
## TODO:
## 1. Consider alternate build target for tenants found here:
## settings:settings_for_tenants: `cockroach gen settings-list --without-system-only
############################################################
## BEGIN SCRIPT ##
##################
TMPSPACE=/tmp/build_settings$$.tmp
VERSION=""
STABLE=""
QUIET=0
## Check for arguments passed to this script:
while getopts ":v:q" opt; do
case ${opt} in
v)
VERSION=`echo $OPTARG | sed 's%^v%%g'`
;;
q)
QUIET=1
;;
\? )
echo -e "\nERROR: Invalid option: $OPTARG. The following are the supported flags:" 1>&2
echo " -v: Specific version of cockroach to use. Expects a \`cockroach-21.2\` or"
echo " similar in path if so."
echo " -q: Quiet mode: no output"
exit;
;;
: )
echo -e "\nERROR: If you specify -$OPTARG, you must provide a value." 1>&2
;;
esac
done
## Sanity check on version selection:
if [[ ! -z $VERSION && `echo $VERSION | egrep -c '^[0-9]{1,2}\.[0-9]$'` -lt 1 ]]; then
echo "ERROR: Malformed version specified to -v"
echo " It must be something like \`21.2\` or \`21.1\`."
echo " Leave off the -v flag to use your environment's /`cockroach/`."
exit;
elif [[ -z $VERSION && ! `which cockroach` ]]; then
echo "ERROR: No matching cockroach binary found in \$PATH."
echo " This script expects either \`cockroach\` or \`cockroach-21.2\`"
echo " (or similar other version) to be findable in your \$PATH."
exit;
elif [[ ! -z $VERSION && ! `which cockroach-$VERSION` ]]; then
echo "ERROR: No matching cockroach binary found in \$PATH for requested version $VERSION."
echo " This script expects either \`cockroach\` or \`cockroach-21.2\`"
echo " (or similar other version) to be findable in your \$PATH."
exit;
elif [[ ! -z $VERSION && $VERSION != `cockroach --version | grep 'Build Tag:' | egrep -o '[0-9]{1,2}\.[0-9]'` ]]; then
## Please see NOTE ON VERSION in spinup, as mentioned in intro comments to this script:
COCKROACH="cockroach-$VERSION"
else
## Use your system's `cockroach`, as found via $PATH
COCKROACH="cockroach"
## Determine value for $STABLE from local docs repo:
STABLE=`grep 'stable:' _config_cockroachdb.yml | awk -F ': ' '{print $2}' | sed 's%^v%%g'`
fi
## Exit if not in a valid git repo:
GITROOT=`git rev-parse --show-toplevel 2>&1`
if [ `echo $GITROOT | grep -c '^fatal: not a git repository'` -gt 0 ]; then
echo -e "\nERROR: Not in a git repo!"
echo -e " You must run this program from within the git branch you wish to edit.\n"
exit;
fi
## Get current settings list directly from cockroach, pruning off table header and footer,
## which we will add back later. Please forgive my sed | sed syntax, as our macOS workstations
## don't include gnu head for -n -2 syntax, so this is our ugly-but-portable alternative
## (i.e. should work on our workstations && on a theoretical ubuntu build host):
if [[ $QUIET -eq 0 ]]; then
echo "Getting settings-list from your local cockroach instance ..."
fi
$COCKROACH gen settings-list --format=rawhtml | tail -n +4 | sed '$d' | sed '$d' >> $TMPSPACE.rawhtml
touch $TMPSPACE.now_with_anchors
IFS="
"
if [[ $QUIET -eq 0 ]]; then
echo "Building new cluster settings listing and comparing to current ..."
fi
## Inject anchor links into settings rows:
for LINE in `cat $TMPSPACE.rawhtml`; do
## We seem to do <a name=""></a>name rather than <a name="">name</a>
## elsewhere in the docs for table element anchor links; keeping format here same:
SETTING_NAME=`echo $LINE | sed 's%^<tr><td><code>%%g' | sed 's%</code></td><td>.*$%%g'`
SETTING_ANCHOR=`echo $SETTING_NAME | sed 's%\.%-%g'`
SETTING_NAME_W_ANCHOR=`echo "<a name=\"setting-$SETTING_ANCHOR\"></a>$SETTING_NAME"`
## NOTE: we are also stripping backticks here, as they do not render in the resulting HTML table:
echo $LINE | sed "s%<code>$SETTING_NAME</code>%<code>$SETTING_NAME_W_ANCHOR</code>%g" | sed 's%\`%%g' >> $TMPSPACE.now_with_anchors
done
## Build replacement settings table, with table header and footer:
## Start with comment to team: autogen, don't edit manually, see ticket:
echo "{% comment %}" >> $TMPSPACE.tweaked
echo "NOTE: This is auto-generated content populated directly from the cockroach binary. Do not edit manually!" >> $TMPSPACE.tweaked
echo " Last auto-generated `date` | See DOC-5103 for details" >> $TMPSPACE.tweaked
echo -e "{% endcomment %}\n" >> $TMPSPACE.tweaked
## Then wrap in table element, and copy in anchor-ified settings list:
echo -e "<table>\n<thead><tr><th>Setting</th><th>Type</th><th>Default</th><th>Description</th></tr></thead>\n<tbody>" >> $TMPSPACE.tweaked
cat $TMPSPACE.now_with_anchors >> $TMPSPACE.tweaked
echo -e "</tbody>\n</table>" >> $TMPSPACE.tweaked
## Loop through our generated replacement, fixing descriptions that use either
## the `<` or `>` character to use '<' and '>' instead, so that they render
## properly. Carefully limit replacement only to setting descriptions:
for LINE in `cat $TMPSPACE.tweaked`; do
if [ `echo $LINE | egrep -c '^<tr><td><code><a name=".*"></a>.*</code></td><td>.*</td><td><code>.*</code></td><td>.*[<>].*</td></tr>$'` -gt 0 ]; then
FIRST_PART=`echo $LINE | grep -o '^<tr><td><code><a name=".*"></a>.*</code></td><td>.*</td><td><code>.*</code></td><td>'`
SECOND_PART=`echo $LINE | sed -n -e "s%^$FIRST_PART%%p" | sed 's%</td></tr>$%%g'`
FIXED_SECOND_PART=`echo $SECOND_PART | sed 's%\<%\<%g' | sed 's%\>%\>%g'`
LAST_PART="</td></tr>"
FIXED_WHOLE_LINE="$FIRST_PART$FIXED_SECOND_PART$LAST_PART"
echo $FIXED_WHOLE_LINE >> $TMPSPACE.tweaked2
else
echo $LINE >> $TMPSPACE.tweaked2
fi
done
## Use $STABLE unless $VERSION was passed:
if [[ -z $VERSION ]]; then
CLUSTER_SETTINGS_FILE="$GITROOT/_includes/v$STABLE/cluster-settings-table.md"
else
CLUSTER_SETTINGS_FILE="$GITROOT/_includes/v$VERSION/cluster-settings-table.md"
fi
## Compare our generated table to the current state of
## $CLUSTER_SETTINGS_FILE, omitting timestamp comment portion:
diff -w <( tail -n +6 $TMPSPACE.tweaked2 ) <( tail -n +6 $CLUSTER_SETTINGS_FILE ) > $TMPSPACE.differences
## If differences are detected, update $CLUSTER_SETTINGS_FILE and print
## diff to user. Else report no action:
if [[ -z $VERSION && ! -s $TMPSPACE.differences ]]; then
if [[ $QUIET -eq 0 ]]; then
echo "DONE: Nothing to do: cluster-settings-table.md is already up to date for v$STABLE."
fi
elif [[ ! -z $VERSION && ! -s $TMPSPACE.differences ]]; then
if [[ $QUIET -eq 0 ]]; then
echo "DONE: Nothing to do: cluster-settings-table.md is already up to date for v$VERSION."
fi
elif [[ -z $VERSION && -s $TMPSPACE.differences ]]; then
cp $TMPSPACE.tweaked2 $CLUSTER_SETTINGS_FILE
if [[ $QUIET -eq 0 ]]; then
echo "DONE: Successfully updated $CLUSTER_SETTINGS_FILE"
echo -e "The following changes were performed:\n"
cat $TMPSPACE.differences
fi
elif [[ ! -z $VERSION && -s $TMPSPACE.differences ]]; then
cp $TMPSPACE.tweaked2 $CLUSTER_SETTINGS_FILE
if [[ $QUIET -eq 0 ]]; then
echo "DONE: Successfully updated $CLUSTER_SETTINGS_FILE"
echo -e "The following changes were performed:\n"
cat $TMPSPACE.differences
fi
fi
## Cleanup:
rm -rf $TMPSPACE.*
exit;