diff --git a/debian/cloud-init.postinst b/debian/cloud-init.postinst index b97c905531a..10957460d13 100644 --- a/debian/cloud-init.postinst +++ b/debian/cloud-init.postinst @@ -51,6 +51,80 @@ with open(fname, "w") as fp: fp.write(yaml.dump(cfg))' "$@" } +handle_preseed_maas() { + local cfg_file="/etc/cloud/cloud.cfg.d/90_dpkg_maas.cfg" + local md_url="" creds_all="" c_key="" t_key="" t_sec="" c_sec=""; + + db_get "cloud-init/maas-metadata-url" && md_url="$RET" || : + db_get "cloud-init/maas-metadata-credentials" && creds_all="$RET" || : + + # nothing to do + [ -n "$md_url" -o -n "$creds_all" ] || return 0 + + # change a url query string format into : delimited + if [ -n "$creds_all" -a "${creds_all#*&}" != "${creds_all}" ]; then + # the command here ends up looking like: + # python3 -c '...' 'oauth_consumer_key=v1&oauth_token_key=v2...' \ + # oauth_consumer_key oauth_token_key oauth_token_secret + creds_all=$(python3 -c 'from six.moves.urllib.parse import parse_qs; +import sys; +keys = parse_qs(sys.argv[1]) +for k in sys.argv[2:]: + sys.stdout.write("%s:" % keys.get(k,[""])[0])' "$creds_all" \ + oauth_consumer_key oauth_token_key oauth_token_secret +) + fi + + # now, if non-empty creds_all is: consumer_key:token_key:token_secret + if [ -n "$creds_all" ]; then + OIFS="$IFS"; IFS=:; set -- $creds_all; IFS="$OIFS" + c_key=$1; t_key=$2; t_sec=$3 + fi + + if [ "$md_url" = "_" -a "${c_key}:${t_key}:${t_sec}" = "_:_:_" ]; then + # if all these values were '_', the delete value, just delete the file. + rm -f "$cfg_file" + else + local header="# written by cloud-init debian package per preseed entries +# cloud-init/{maas-metadata-url,/maas-metadata-credentials}" + + local pair="" k="" v="" pload="" orig_umask="" + for pair in "metadata_url:$md_url" "consumer_key:${c_key}" \ + "token_key:${t_key}" "token_secret:$t_sec"; do + k=${pair%%:*} + v=${pair#${k}:} + [ -n "$v" ] && pload="${pload} $k: \"$v\"," + done + + # '_' would indicate "delete", otherwise, existing entries are left + orig_umask=$(umask) + umask 066 + : >> "$cfg_file" && chmod 600 "$cfg_file" + update_cfg "$cfg_file" "$header" "datasource: { MAAS: { ${pload%,} } }" _ + umask ${orig_umask} + fi + + # now clear the database of the values, as they've been consumed + db_unregister "cloud-init/maas-metadata-url" || : + db_unregister "cloud-init/maas-metadata-credentials" || : +} + +handle_preseed_local_cloud_config() { + local ccfg="" debconf_name="cloud-init/local-cloud-config" + local cfg_file="/etc/cloud/cloud.cfg.d/90_dpkg_local_cloud_config.cfg" + local header="# written by cloud-init debian package per preseed entry +# $debconf_name" + + db_get "${debconf_name}" && ccfg="$RET" || : + + if [ "$ccfg" = "_" ]; then + rm -f "$cfg_file" + elif [ -n "$ccfg" ]; then + { echo "$header"; echo "$ccfg"; } > "$cfg_file" + fi + db_unregister "${debconf_name}" || : +} + fix_1336855() { ### Begin fix for LP: 1336855 # fix issue where cloud-init misidentifies the location of grub and @@ -191,6 +265,12 @@ datasource_list: [ $values ] EOF fi + # if there are maas settings pre-seeded apply them + handle_preseed_maas + + # if there is generic cloud-config preseed, apply them + handle_preseed_local_cloud_config + # fix issue where cloud-init misidentifies the location of grub fix_1336855