[19] | 1 | #!/bin/bash
|
---|
| 2 |
|
---|
[258] | 3 | # Copyright 2016 Christian 'Alloc' Illy
|
---|
| 4 | #
|
---|
| 5 | # Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 6 | # you may not use this file except in compliance with the License.
|
---|
| 7 | # You may obtain a copy of the License at
|
---|
| 8 | #
|
---|
| 9 | # http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 10 | #
|
---|
| 11 | # Unless required by applicable law or agreed to in writing, software
|
---|
| 12 | # distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 14 | # See the License for the specific language governing permissions and
|
---|
| 15 | # limitations under the License.
|
---|
| 16 |
|
---|
| 17 |
|
---|
[19] | 18 |
|
---|
[23] | 19 | #################################
|
---|
| 20 | ## Generic worker functions
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | # Query for an instance name (will be saved in $INSTANCE)
|
---|
[19] | 24 | readInstanceName() {
|
---|
| 25 | until [ $(isValidInstanceName "$INSTANCE") -eq 1 ]; do
|
---|
| 26 | read -p "Instance name: " INSTANCE
|
---|
| 27 | if [ $(isValidInstanceName "$INSTANCE") -eq 0 ]; then
|
---|
| 28 | echo "Invalid instance name, may only contain:"
|
---|
| 29 | echo " - letters (A-Z / a-z)"
|
---|
| 30 | echo " - digits (0-9)"
|
---|
| 31 | echo " - underscores (_)"
|
---|
| 32 | echo " - hyphens (-)"
|
---|
| 33 | fi
|
---|
| 34 | done
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 |
|
---|
[23] | 38 | # Check if the config template exists
|
---|
| 39 | # Returns:
|
---|
| 40 | # 0/1: no/yes
|
---|
[19] | 41 | configTemplateExists() {
|
---|
| 42 | if [ -f $SDTD_BASE/templates/config.xml ]; then
|
---|
| 43 | echo 1
|
---|
| 44 | else
|
---|
| 45 | echo 0
|
---|
| 46 | fi
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | # Get a single value from a serverconfig
|
---|
| 50 | # Params:
|
---|
| 51 | # 1: Instance name
|
---|
| 52 | # 2: Property name
|
---|
| 53 | # Returns:
|
---|
| 54 | # Property value
|
---|
| 55 | getConfigValue() {
|
---|
| 56 | local CONF=$(getInstancePath $1)/config.xml
|
---|
| 57 | $XMLSTARLET sel -t -v "/ServerSettings/property[@name='$2']/@value" $CONF
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | # Update a single value in a serverconfig
|
---|
| 61 | # Params:
|
---|
| 62 | # 1: Instance name
|
---|
| 63 | # 2: Property name
|
---|
| 64 | # 3: New value
|
---|
| 65 | setConfigValue() {
|
---|
[481] | 66 | local XMLSTARLET_OPTS="-L -P"
|
---|
[19] | 67 | local CONF=$(getInstancePath $1)/config.xml
|
---|
[481] | 68 | local XPATHBASE="//property[@name='$2']"
|
---|
| 69 |
|
---|
| 70 | if [ -z $($XMLSTARLET sel -t -v "$XPATHBASE/@name" $CONF) ]; then
|
---|
| 71 | $XMLSTARLET ed $XMLSTARLET_OPTS \
|
---|
| 72 | -s "/*[1]" -t elem -n "property" \
|
---|
| 73 | -i "//property[not(@name)]" -t attr -n "name" -v "$2" \
|
---|
| 74 | -i "$XPATHBASE" -t attr -n "value" -v "$3" \
|
---|
| 75 | $CONF
|
---|
| 76 | else
|
---|
| 77 | $XMLSTARLET ed $XMLSTARLET_OPTS -u "$XPATHBASE/@value" -v "$3" $CONF
|
---|
| 78 | fi
|
---|
[19] | 79 | }
|
---|