This commit is contained in:
Xavier Pillons 2019-10-03 12:50:05 +02:00
Родитель 15b4f5280c 65edf2a2fb
Коммит 9b2e565d3c
5 изменённых файлов: 65 добавлений и 65 удалений

Просмотреть файл

@ -158,15 +158,14 @@ The scripts allow storage account key be retrieved. This is the format: `sakey.<
#### referencing variables in variables names
There are some situation where you want to use variable values inside other names like a keyvault name or a storage account name. To do this just enclose it with `[]` like this :
There are some situation where you want to use variable values inside other variables like a keyvault name or a storage account name. To do this just enclose it with double curly braces `{{}}` like this :
```json
"secret.[.variables.key_vault].CycleAdminPassword"
"secret.{{variables.key_vault}}.CycleAdminPassword"
````
In the above example, the key vault name is stored into the `.variables.key_vault` value.
> Note : Don't forget the `.` prefixing the variables keyword otherwise it won't work.
## Commands

Просмотреть файл

@ -56,53 +56,21 @@ function make_uuid_str {
fi
}
# read a read_subvalue. if formatted with brakets like [subvalue], then the subvalue is used as a new value to read from the config file
# syntax read_subvalue <variable> <value>
function read_subvalue {
value=$2
firstletter=${value:0:1}
if [ "$firstletter" == "[" ]; then
value=$(echo $value | awk -F'[][]' '{print $2}')
read_value value "$value"
fi
read $1 <<< $value
}
function read_value {
read $1 <<< $(jq -r "$2" $config_file)
if [ "${!1}" = "null" ]; then
if [ -z "$3" ]; then
error "failed to read $2 from $config_file"
else
read $1 <<< $3
debug "read_value: $1=${!1} (default)"
fi
else
debug "read_value: $1=${!1}"
fi
function process_value {
prefix=${!1%%.*}
if [ "$prefix" = "variables" ]; then
read_value $1 ".${!1}"
elif [ "$prefix" = "secret" ]; then
keyvault_str=${!1#*.}
vault_name=${keyvault_str%.*}
read_subvalue vault_name $vault_name
key_name=${keyvault_str##*.}
read_subvalue key_name $key_name
key_name=${keyvault_str#*.}
debug "read_value reading from keyvault (keyvault=$vault_name, key=$key_name)"
read $1 <<< $(az keyvault secret show --name $key_name --vault-name $vault_name -o json | jq -r '.value')
elif [ "$prefix" = "sasurl" ]; then
sasurl_storage_str=${!1#*.}
read_subvalue sasurl_storage_account $sasurl_storage_str
sasurl_storage_account=${sasurl_storage_account%.*}
value=$(echo $sasurl_storage_str | sed 's/\[[^]]*\]//')
sasurl_storage_fullpath=${value#*.}
read_subvalue sasurl_storage_container ${sasurl_storage_fullpath%%/*}
sasurl_storage_fullpath="$sasurl_storage_container/${sasurl_storage_str#*/}"
sasurl_storage_account=${sasurl_storage_str%%.*}
sasurl_storage_fullpath=${sasurl_storage_str#*.}
sasurl_storage_container=${sasurl_storage_fullpath%%/*}
sasurl_storage_url="$( \
az storage account show \
--name $sasurl_storage_account \
@ -121,27 +89,28 @@ function read_value {
sasurl_storage_full="$sasurl_storage_url$sasurl_storage_fullpath?$sasurl_storage_saskey"
debug "read_value creating a sasurl (account=$sasurl_storage_account, fullpath=$sasurl_storage_fullpath, container=$sasurl_storage_container, sasurl=$sasurl_storage_full"
read $1 <<< "$sasurl_storage_full"
fi
}
elif [ "$prefix" = "fqdn" ]; then
fqdn_str=${!1#*.}
read_subvalue resource_name $fqdn_str
debug "getting FQDN for $resource_name in $resource_group"
fqdn=$(
az network public-ip show \
--resource-group $resource_group \
--name ${resource_name}pip --query dnsSettings.fqdn \
--output tsv \
2>/dev/null \
)
read $1 <<< "$fqdn"
elif [ "$prefix" = "sakey" ]; then
sakey_str=${!1#*.}
read_subvalue storage_name $sakey_str
debug "getting storage key for $storage_name in $resource_group"
storage_key=$(az storage account keys list -g $resource_group -n $storage_name --query "[0].value" | sed 's/\"//g')
read $1 <<< "$storage_key"
function read_value {
read $1 <<< $(jq -r "$2" $config_file)
if [ "${!1}" = "null" ]; then
if [ -z "$3" ]; then
error "failed to read $2 from $config_file"
else
read $1 <<< $3
debug "read_value: $1=${!1} (default)"
fi
else
debug "read_value: $1=${!1}"
fi
}
while [[ "${!1}" =~ \{\{([^\}]*)\}\} ]]; do
local match_fullstr=${BASH_REMATCH[0]}
local match_value=${BASH_REMATCH[1]}
process_value match_value
read $1 <<< "${!1/$match_fullstr/$match_value}"
done
process_value $1
}

Просмотреть файл

@ -8,13 +8,13 @@
"projectstore": "store",
"container": "container",
"resource": "vm1",
"secret1": "secret.[.variables.key_vault].secret1",
"secret1": "secret.{{variables.key_vault}}.secret1",
"secret2": "secret.vault.secret2",
"sakey1":"sakey.[.variables.projectstore]",
"sakey1":"sakey.{{variables.projectstore}}",
"sakey2":"sakey.store",
"fqdn1":"fqdn.[.variables.resource]",
"fqdn1":"fqdn.{{variables.resource}}",
"fqdn2":"fqdn.vm2",
"sasurl1":"sasurl.account.container/path.foo/blob.data.ext",
"sasurl2":"sasurl.[.variables.projectstore].[.variables.container]/path.foo/blob.data.ext"
"sasurl2":"sasurl.{{variables.projectstore}}.{{variables.container}}/path.foo/blob.data.ext"
}
}

Просмотреть файл

@ -0,0 +1,7 @@
{
"variables": {
"foo": "foo{{variables.bar}}",
"bar": "bar{{variables.baz}}",
"baz": "baz"
}
}

Просмотреть файл

@ -0,0 +1,25 @@
#!/bin/bash
source "$azhpc_dir/libexec/common.sh"
DEBUG_ON=0
COLOR_ON=1
config_file=test.json
function test_read_value
{
read_value_str=$1
expected_val=$2
echo -n "testing read_value [ $1 = $2 ]... "
read_value val "$read_value_str"
if [ "$val" = "$expected_val" ]; then
echo "SUCCESS"
else
echo "FAILURE [ value = \"$val\" ]"
fi
}
test_read_value .variables.baz baz
test_read_value .variables.bar barbaz
test_read_value .variables.foo foobarbaz