← Back to team overview

cloud-init-dev team mailing list archive

Re: [Merge] ~raharper/cloud-init:fix/debian-config-yaml-spaces into cloud-init:ubuntu/devel

 

fwiw, there is 'parse_yaml_array', in tools/ds-identify, which does better (at least did not have this bug). Unfortunately that uses functions 'trim' and 'unquote', so you can't just grab the single function.

Second option would be to first *try* to use a proper yaml parser (python).  There are some rules about not using dependencies in a config script I think (only essential maybe?).  Thats why we don't use cloudinit infra to load it.  So you can't *depend* on python and yaml but you could try.

Here is an example that could be added to your suggested fix:

try_python_yaml() {
    # try to load something like 'datasource_list: [xxx]' and write space delimeted
    # values to stdout
    local py out="" fname="$1"
    command -v python3 >/dev/null || return
    out=$(python3 -c '
import yaml, sys;
print(",".join(yaml.load(sys.stdin.read()).get("datasource_list")))' \
    < "$fname"
    ) || return
    echo "$out"
}




Diff comments:

> diff --git a/debian/cloud-init.config b/debian/cloud-init.config
> index 6e9c6f7..4c35e50 100644
> --- a/debian/cloud-init.config
> +++ b/debian/cloud-init.config
> @@ -32,13 +32,13 @@ hasEc2Md() {
>  get_yaml_list() {
>  	# get_yaml_list(file, key, def): return a comma delimited list with the value
>  	# for the yaml array defined in 'key' from 'file'. if not found , return 'def'
> -	# only really supports 'key: [en1, en2 ]' format.
> +	# only really supports 'key: [ en1, en2 ]' or 'key: [en1, en2]' formats.
>  	local file="$1" key="$2" default="$3"
>  	[ -f "$file" ] || return 1
>  	# any thing that didn't match the key is deleted so the final 'p' only
>  	# prints things that matched.

then just add right here:

 RET=$(try_python_yaml "$file") && return 0

> -	RET=$(sed -n -e "/^$key:/"'!'d -e "s/$key:[ \[]*//"\
> -		-e "s, \]$,," -e p "$file")
> +	RET=$(sed -n -e "/^$key:/"'!'d -e "s/$key:[[[:space:]]+\[]*//"\
> +        -e "s,[[:space:]]+\]$,," -e p "$file")
>  	[ -n "$RET" ] || RET="$default"
>  }
>  


-- 
https://code.launchpad.net/~raharper/cloud-init/+git/cloud-init/+merge/371919
Your team cloud-init commiters is requested to review the proposed merge of ~raharper/cloud-init:fix/debian-config-yaml-spaces into cloud-init:ubuntu/devel.


References