Skip to content

Commit

Permalink
d/cloud-init-base.{config,postinst}: handle migration of debconf values
Browse files Browse the repository at this point in the history
  • Loading branch information
aciba90 committed Sep 27, 2024
1 parent 5829b34 commit 8451ae6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
29 changes: 27 additions & 2 deletions debian/cloud-init-base.config
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ get_yaml_list() {
[ -n "$RET" ] || RET="$default"
}

# Migrate a debconf value from cloud-init/* to cloud-init-base/*
# cleaning up the old value.
#
# Arguments:
# Name of the debconf question
migrate_debconf_to_cloud_init_base() {
local old="cloud-init/$1" new="cloud-init-base/$1"
if db_get "$new"; then
# cloud-init-base/* win against cloud-init/* values
if db_get "$old"; then
echo "Removing ${old} in favor of ${new}" 1>&2 || :
db_unregister "$old" || :
fi
return 0
fi
if db_get "$old"; then
echo "Migrating debconf value: ${old} -> ${new}" 1>&2 || :
db_set "$new" "$RET"
db_unregister "$old" || :
fi
}

# old_dpkg_cfg is very old file that is no longer read by cloud-init.
# it gets re-named to cloud.cfg.d/90_dpkg.cfg in the preinst.
dpkg_cfg="/etc/cloud/cloud.cfg.d/90_dpkg.cfg"
Expand All @@ -52,10 +74,13 @@ if [ -f "${old_dpkg_cfg}" -a ! -f "$dpkg_cfg" ]; then
dpkg_cfg="${old_dpkg_cfg}"
echo "WARN: reading value from ${old_dpkg_cfg}" 1>&2
fi

migrate_debconf_to_cloud_init_base datasources

if [ -f "$dpkg_cfg" ]; then
if get_yaml_list "$dpkg_cfg" datasource_list NOTFOUND &&
val="$RET" && [ "$val" != "NOTFOUND" ]; then
db_set cloud-init-base/datasources $val
db_set cloud-init-base/datasources "$val"
else
echo "WARN: failed to read datasource_list from $dpkg_cfg" 1>&2
fi
Expand All @@ -64,7 +89,7 @@ elif { db_fget cloud-init-base/datasources seen || : ; } &&
# this is the first time this we've run (installation or re-install after
# purge). try to determine if the Ec2 datasource is there.
# if it is, and Ec2 was not in the default list, then add it.
db_get cloud-init/datasources
db_get cloud-init-base/datasources
def="${RET}"
case " ${def}," in
*\ Ec2,*) :;;
Expand Down
50 changes: 42 additions & 8 deletions debian/cloud-init-base.postinst
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ 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" || :
db_get "cloud-init-base/maas-metadata-url" && md_url="$RET" || :
db_get "cloud-init-base/maas-metadata-credentials" && creds_all="$RET" || :

# nothing to do
[ -n "$md_url" -o -n "$creds_all" ] || return 0
Expand Down Expand Up @@ -86,7 +86,7 @@ for k in sys.argv[2:]:
rm -f "$cfg_file"
else
local header="# written by cloud-init debian package per preseed entries
# cloud-init/{maas-metadata-url,/maas-metadata-credentials}"
# cloud-init-base/{maas-metadata-url,/maas-metadata-credentials}"

local pair="" k="" v="" pload="" orig_umask=""
for pair in "metadata_url:$md_url" "consumer_key:${c_key}" \
Expand All @@ -105,12 +105,12 @@ for k in sys.argv[2:]:
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" || :
db_unregister "cloud-init-base/maas-metadata-url" || :
db_unregister "cloud-init-base/maas-metadata-credentials" || :
}

handle_preseed_local_cloud_config() {
local ccfg="" debconf_name="cloud-init/local-cloud-config"
local ccfg="" debconf_name="cloud-init-base/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"
Expand Down Expand Up @@ -250,14 +250,48 @@ rename_hook_hotplug_udev_rule() {
fi
}

# Migrate a debconf value from cloud-init/* to cloud-init-base/*
# cleaning up the old value.
#
# Arguments:
# Name of the debconf question
migrate_debconf_to_cloud_init_base() {
local old="cloud-init/$1" new="cloud-init-base/$1"
if db_get "$new"; then
# cloud-init-base/* win against cloud-init/* values
if db_get "$old"; then
echo "Removing ${old} in favor of ${new}" 1>&2 || :
db_unregister "$old" || :
fi
return 0
fi
if db_get "$old"; then
echo "Migrating debconf value: ${old} -> ${new}" 1>&2 || :
db_set "$new" "$RET"
db_unregister "$old" || :
fi
}

upgrade_to_cloud_init_base() {
migrate_debconf_to_cloud_init_base local-cloud-config
migrate_debconf_to_cloud_init_base maas-metadata-credentials
migrate_debconf_to_cloud_init_base maas-metadata-url

local oldver="$1" last_ver="24.4~3+really24.3.1-0ubuntu4"
dpkg --compare-versions "$oldver" le-nl "$last_ver" || return 0

migrate_debconf_to_cloud_init_base datasources
}

if [ "$1" = "configure" ]; then
if db_get cloud-init/datasources; then
upgrade_to_cloud_init_base "$2"

if db_get cloud-init-base/datasources; then
values="$RET"
if [ "${values#*MaaS}" != "${values}" ]; then
# if db had old MAAS spelling, fix it.
values=$(echo "$values" | sed 's,MaaS,MAAS,g')
db_set cloud-init/datasources "$values"
db_set cloud-init-base/datasources "$values"
fi
cat > /etc/cloud/cloud.cfg.d/90_dpkg.cfg <<EOF
# to update this file, run dpkg-reconfigure cloud-init
Expand Down

0 comments on commit 8451ae6

Please sign in to comment.