OK, I've combined Chad's clean-up and then emitted that as the value, like this:


LISTVAL="$(sed -e "s/\s//g" -e "/^$key:/"'!'d \
               -e "s/$key:\[//;s/]//;s/,/, /g" "$file")"
RET="$key: [ ${LISTVAL} ]"

In action looks like this:

# Edge-case, single entry
root@x1:~# cat /etc/cloud/cloud.cfg.d/90_dpkg.cfg
# to update this file, run dpkg-reconfigure cloud-init
datasource_list: [ ConfigDrive ]
root@x1:~# ./test3.sh /etc/cloud/cloud.cfg.d/90_dpkg.cfg
datasource_list: [ ConfigDrive ]

# Edge-case, empty
root@x1:~# cat /etc/cloud/cloud.cfg.d/90_dpkg.cfg.empty 
# to update this file, run dpkg-reconfigure cloud-init
datasource_list: []
root@x1:~# ./test3.sh /etc/cloud/cloud.cfg.d/90_dpkg.cfg.empty 
datasource_list: [ ]

# the bug case
root@x1:~# cat /etc/cloud/cloud.cfg.d/90_dpkg.cfg.bad 
# to update this file, run dpkg-reconfigure cloud-init
datasource_list: [ConfigDrive, None]
root@x1:~# ./test3.sh /etc/cloud/cloud.cfg.d/90_dpkg.cfg.bad 
datasource_list: [ ConfigDrive, None ]

# the default case
root@x1:~# cat /etc/cloud/cloud.cfg.d/90_dpkg.cfg.new 
s file, run dpkg-reconfigure cloud-init
datasource_list: [ NoCloud, ConfigDrive, OpenNebula, DigitalOcean, Azure, 
AltCloud, OVF, MAAS, GCE, OpenStack, CloudSigma, SmartOS, Bigstep, Scaleway, 
AliYun, Ec2, CloudStack, Hetzner, IBMCloud, Oracle, Exoscale, None] 
root@x1:~# ./test3.sh /etc/cloud/cloud.cfg.d/90_dpkg.cfg.new 
datasource_list: [ NoCloud, ConfigDrive, OpenNebula, DigitalOcean, Azure, 
AltCloud, OVF, MAAS, GCE, OpenStack, CloudSigma, SmartOS, Bigstep, Scaleway, 
AliYun, Ec2, CloudStack, Hetzner, IBMCloud, Oracle, Exoscale, None ]


Diff comments:

> diff --git a/debian/cloud-init.config b/debian/cloud-init.config
> index 6e9c6f7..adaabb8 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.
> -     RET=$(sed -n -e "/^$key:/"'!'d -e "s/$key:[ \[]*//"\
> -             -e "s, \]$,," -e p "$file")
> +     # strip all whitespace, delete lines not matching key:,
> +     # strip key: and [] and replace ',' with ', '
> +     RET=$(sed -e "s/\s//g" -e "/^$key:/"'!'d\
> +               -e "s/$key:\[//;s/]//;s/,/, /g" $file)

And it doesn't quite work; should have tested first:

RET=$(sed -e "s/\s//g" -e "/^$key:/"'!'d -e "s/$key:\[//;s/]//;s/,/, /g" 
"$file")

produces:

# ./test3.sh /etc/cloud/cloud.cfg.d/90_dpkg.cfg.bad 
+ get_yaml_list /etc/cloud/cloud.cfg.d/90_dpkg.cfg.bad datasource_list NOTFOUND
+ local file=/etc/cloud/cloud.cfg.d/90_dpkg.cfg.bad key=datasource_list 
default=NOTFOUND
+ [ -f /etc/cloud/cloud.cfg.d/90_dpkg.cfg.bad ]
+ sed -e s/\s//g -e /^datasource_list:/!d -e s/datasource_list:\[//;s/]//;s/,/, 
/g /etc/cloud/cloud.cfg.d/90_dpkg.cfg.bad
+ RET=ConfigDrive, None
+ [ -n ConfigDrive, None ]
+ echo ConfigDrive, None
ConfigDrive, None

So, well spaced list, but missing key and brackets.

>       [ -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.

_______________________________________________
Mailing list: https://launchpad.net/~cloud-init-dev
Post to     : cloud-init-dev@lists.launchpad.net
Unsubscribe : https://launchpad.net/~cloud-init-dev
More help   : https://help.launchpad.net/ListHelp

Reply via email to