Skip to content

Commit

Permalink
chore(debian): Clean up postinst and preinst scripts
Browse files Browse the repository at this point in the history
Drop version gated code from preinst.
Add version gates to old code in preinst and postinst.
  • Loading branch information
holmanb committed Aug 5, 2024
1 parent cf13fd1 commit 2e53fe3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 273 deletions.
64 changes: 13 additions & 51 deletions debian/cloud-init.postinst
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,12 @@ set -e
set -f # disable pathname expansion
db_capb escape # to support carriage return / multi-line values

debug() {
[ "${_CI_UPGRADE_DEBUG:-0}" = "0" ] && return 0
echo "$@" 1>&2 || :
}

update_cfg() {
# takes filename, header, new object (in yaml), optionally 'remover'
# and merges new into existing object in filename, and then updates file
# remover a string that means "delete existing entry"
python3 -c '
import sys, yaml
def update(src, cand):
if not (isinstance(src, dict) and isinstance(cand, dict)):
return cand
for k, v in cand.items():
# if the candidate has _ as value, delete source
if v == REMOVER:
if k in src:
del src[k]
continue
if k not in src:
src[k] = v
else:
src[k] = update(src[k], v)
return src
(fname, header, newyaml) = sys.argv[1:4]
REMOVER = object
if len(sys.argv) == 5:
REMOVER = sys.argv[4]
newcfg = yaml.safe_load(newyaml)
with open(fname, "r") as fp:
cfg = yaml.safe_load(fp)
if not cfg: cfg = {}
cfg = update(cfg, newcfg)
with open(fname, "w") as fp:
fp.write(header + "\n")
fp.write(yaml.dump(cfg))' "$@"
}

fix_1336855() {
### Begin fix for LP: 1336855
# fix issue where cloud-init misidentifies the location of grub and
# fix issue where cloud-init misidentified the location of grub and
# where grub misidentifies the location of the device
local oldver="$1" last_bad_ver="24.3~1g6e4153b3-0ubuntu1"
dpkg --compare-versions "$oldver" le-nl "$last_bad_ver" || return 0

# if cloud-init's grub module did not run, then it did not break anything.
[ -f /var/lib/cloud/instance/sem/config_grub_dpkg ] || return 0
Expand Down Expand Up @@ -169,15 +127,18 @@ fix_lp1889555() {
}

rename_hook_hotplug_udev_rule() {
local oldver="$1" last_bad_ver="24.3~1g6e4153b3-0ubuntu1"
dpkg --compare-versions "$oldver" le-nl "$last_bad_ver" || return 0
# Avoids LP: #1946003 see commit: b519d861aff8b44a0610c176cb34adcbe28df144
if [ -f /etc/udev/rules.d/10-cloud-init-hook-hotplug.rules ]; then
mv -f /etc/udev/rules.d/10-cloud-init-hook-hotplug.rules \
/etc/udev/rules.d/90-cloud-init-hook-hotplug.rules
fi
}


if [ "$1" = "configure" ]; then
update_maas_spelling(){
local oldver="$1" last_bad_ver="24.3~1g6e4153b3-0ubuntu1"
dpkg --compare-versions "$oldver" le-nl "$last_bad_ver" || return 0
if db_get cloud-init/datasources; then
values="$RET"
if [ "${values#*MaaS}" != "${values}" ]; then
Expand All @@ -190,13 +151,14 @@ if [ "$1" = "configure" ]; then
datasource_list: [ $values ]
EOF
fi
}

# fix issue where cloud-init misidentifies the location of grub
fix_1336855
if [ "$1" = "configure" ]; then

update_maas_spelling "$2"
fix_1336855 "$2"
fix_lp1889555 "$2"

rename_hook_hotplug_udev_rule
rename_hook_hotplug_udev_rule "$2"

fi

Expand Down
246 changes: 24 additions & 222 deletions debian/cloud-init.preinst
Original file line number Diff line number Diff line change
@@ -1,237 +1,39 @@
#!/bin/sh
set -e

# Remove a no-longer used conffile
rm_conffile() {
local PKGNAME="$1"
local CONFFILE="$2"

[ -e "$CONFFILE" ] || return 0

local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PKGNAME | \
sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
if [ "$md5sum" != "$old_md5sum" ]; then
echo "Obsolete conffile $CONFFILE has been modified by you."
echo "Saving as $CONFFILE.dpkg-bak ..."
mv -f "$CONFFILE" "$CONFFILE".dpkg-bak
else
echo "Removing obsolete conffile $CONFFILE ..."
rm -f "$CONFFILE"
fi
}

# move_sem(src,targets)
# rename sem/* items named $src to $targets
# (with hard links if more than one)
move_sem() {
local src=$1 f="" targ="" freqid="" dir=/var/lib/cloud/sem
shift
# link the remaining targets to src, if it exists
for f in "${dir}/${src}."*; do
# if there were no src entries, nothing to do
[ -f "${f}" ] || return 0
freqid=${f#${dir}/${src}.} # 'i-abcdefg' or 'always'
for targ in "$@"; do
[ -e "${dir}/${targ}.${freqid}" ] && continue
ln -f "${f}" "${dir}/${targ}.${freqid}"
done
rm "${f}"
done
return 0
}

fix_ephemeral0_micro() {
# make ephemeral0 entries in /etc/fstab written by cloudconfig
# 'nobootwait', so they do not block subsequent boots (LP: #634102)
local out="" oldver=$1 dev="" adop="nobootwait"
local s="[[:space:]]" ns="[^[:space:]]" # space and "not space"
local remain="${s}\+.*" first4=""
for dev in /dev/sda2 /dev/sdb; do
first4="${dev}$s\+$ns\+$s\+$ns\+$s\+$ns\+"
out=$(awk '$1 == dev && $4 ~ /cloudconfig/ && $4 !~ op { print $1 ; }' \
dev="${dev}" "op=${adop}" /etc/fstab) || return 0
[ -n "${out}" ] || continue
echo "making ephemeral ${dev} in /etc/fstab ${adop} (LP: #634102)" 1>&2
sed -i "s|^\(${first4}\)\(${remain}\)|\1,${adop}\2|" /etc/fstab
done
}

convert_varlib_05x_06x() {
local url="http://169.254.169.254/2009-04-04/meta-data/instance-id"
local tout="--connect-timeout .5 --read-timeout .5"
local iid="" f="" uptime="" bn=""
iid=$(wget "${url}" ${tout} --tries 1 -O - 2>/dev/null) || iid=""

[ -n "${iid}" -a -d /var/lib/cloud ] || return 0
cd /var/lib/cloud
mkdir -p "instances/${iid}" "instances/${iid}/sem"
[ -e instance ] || ln -sf "instances/${iid}" instance
for f in data/*; do
[ -f "$f" ] || continue
case "${f#*/}" in
user-data.txt.i|user-data.txt|cloud-config.txt)
mv "${f}" instance/
;;
esac
done
[ -f data/cache/obj.pkl ] && mv data/cache/obj.pkl instance/
for f in sem/*.${iid}; do
[ -f "${f}" ] || continue
bn=${f#*/}; bn=${bn%.${iid}}
case "${bn}" in
set_defaults) mv "${f}" "instance/sem/config-locale";;
set_hostname) mv "${f}" "instance/sem/config-set_hostname";;
*) mv "${f}" "instance/sem/${bn}";;
esac
done
[ ! -f sem/update_hostname.always ] ||
mv sem/update_hostname.always sem/config-update_hostname.always
rmdir data/cache 2>/dev/null || :
rm -f data/available.build

mkdir -p instance/scripts
if [ -d data/scripts ]; then
mv data/scripts/* instance/scripts || :
rmdir data/scripts || :
fi
[ -d data/scripts/* ] && mv data/scripts instance/

[ ! -e instance/boot-finished ] &&
[ -f /proc/uptime ] && read uptime other </proc/uptime &&
echo "${uptime}" > instance/boot-finished || :

return 0
}

azure_apply_new_instance_id_1506187() {
# With LP: #1506187, the Azure instance ID detection method was changed
# to use the DMI data. In order to prevent existing instances from thinking
# they are new instances, the instance ID needs to be updated here.

if grep DataSourceAzure /var/lib/cloud/instance/datasource > /dev/null 2>&1; then

product_id_f="/sys/devices/virtual/dmi/id/product_uuid"
instance_id_f="/var/lib/cloud/data/instance-id"

if [ ! -e "${product_id_f}" -o ! -e "${instance_id_f}" ]; then
return 0
fi

# Get the current instance ID's (new and old)
new_instance_id="$(cat ${product_id_f})"
old_instance_id="$(cat ${instance_id_f})"

if [ "${new_instance_id}" = "${old_instance_id}" ]; then
# this may have been applied for a prior version, i.e. upgrading
# from 14.04 to 16.04
return 0

elif [ -z "${new_instance_id}" -o -z "${old_instance_id}" ]; then
cat <<EOM
WARNING: Failed to migrate old instance ID to new instance ID.
Cloud-init may detect this instance as a new instance upon reboot.
Please see: https://bugs.launchpad.net/bug/1506187
EOM

elif [ "${new_instance_id}" != "${old_instance_id}" ]; then
cat <<EOM
AZURE: this instance uses an unstable instance ID. Cloud-init will
migrate the instance ID from:
${old_instance_id}
to:
${new_instance_id}
For more information about this change, please see:
https://bugs.launchpad.net/bug/1506187
https://azure.microsoft.com/en-us/blog/accessing-and-using-azure-vm-unique-id
EOM

# Write the new instance id
echo "${new_instance_id}" > /var/lib/cloud/data/instance-id

# Remove the symlink for the instance
rm /var/lib/cloud/instance

# Rename the old instance id to the new one
mv /var/lib/cloud/instances/${old_instance_id} \
/var/lib/cloud/instances/${new_instance_id}

# Link the old id to the new one, just incase
ln -s /var/lib/cloud/instances/${new_instance_id} \
/var/lib/cloud/instances/${old_instance_id}

# Make the active instance the new id
ln -s /var/lib/cloud/instances/${new_instance_id} \
/var/lib/cloud/instance
fi
fi
}

cleanup_lp1552999() {
local oldver="$1" last_bad_ver="0.7.7~bzr1178"
dpkg --compare-versions "$oldver" le "$last_bad_ver" || return 0
local hdir="/var/lib/systemd/deb-systemd-helper-enabled"
hdir="$hdir/multi-user.target.wants"
local edir="/etc/systemd/system/multi-user.target.wants"
rm -f "$hdir/cloud-config.service" "$hdir/cloud-final.service" \
"$hdir/cloud-init-local.service" "$hdir/cloud-init.service"
}

cleanup_oci_network_lp1956788() {
# Remove vestigial /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
# from Oracle now that datasource honors system cfg above datasource cfg.
local oldver="$1" last_bad_ver="24.3~1g6e4153b3-0ubuntu1"
dpkg --compare-versions "$oldver" le "$last_bad_ver" || return 0 # from Oracle now that datasource honors system cfg above datasource cfg.
grep DataSourceOracle /var/lib/cloud/instance/datasource > /dev/null 2>&1 || return 0
rm -f /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
}

cleanup_user_scripts() {
local oldver="$1" last_bad_ver="24.3~1g6e4153b3-0ubuntu1"
dpkg --compare-versions "$oldver" le "$last_bad_ver" || return 0 # from Oracle now that datasource honors system cfg above datasource cfg.
# 0.6.0 changed 'user-scripts' to config-scripts-user (LP: #1049146)
if [ -e /var/lib/cloud/instance/sem/user-scripts ]; then
ln -sf user-scripts /var/lib/cloud/instance/sem/config-scripts-user
fi
}

cleanup_rename_config() {
local oldver="$1" last_bad_ver="24.3~1g6e4153b3-0ubuntu1"
dpkg --compare-versions "$oldver" le "$last_bad_ver" || return 0 # from Oracle now that datasource honors system cfg above datasource cfg.
d=/etc/cloud/
if [ -f "$d/distro.cfg" ] && [ ! -f "$d/cloud.cfg.d/90_dpkg.cfg" ]; then
echo "moving $d/distro.cfg -> $d/cloud.cfg.d/90_dpkg.cfg"
[ -d "${d}/cloud.cfg.d" ] || mkdir "${d}/cloud.cfg.d"
mv "$d/distro.cfg" "$d/cloud.cfg.d/90_dpkg.cfg"
fi
}
case "$1" in
install|upgrade)
if dpkg --compare-versions "$2" le "0.5.11-0ubuntu1"; then
# rename the config entries in sem/ so they're not run again

# transition names in 0.5.11 had only short name (no config- prefix)
# so create config- entries for each
for name in apt-update-upgrade disable-ec2-metadata mounts \
puppet runcmd ssh updates-check; do
move_sem ${name} config-${name}
done

# 0.5.11 split 'config-misc' into 'updates-check' and 'runcmd'
move_sem config-misc config-updates-check config-runcmd
fi

if dpkg --compare-versions "$2" le "0.5.14-0ubuntu5"; then
fix_ephemeral0_micro
fi

if dpkg --compare-versions "$2" le 0.6.0-0ubuntu1; then
# convert /var/lib/cloud from 0.5.x layout to 0.6.x
convert_varlib_05x_06x
fi

# 0.6.0 changed 'user-scripts' to config-scripts-user (LP: #1049146)
if [ -e /var/lib/cloud/instance/sem/user-scripts ]; then
ln -sf user-scripts /var/lib/cloud/instance/sem/config-scripts-user
fi

# 0.7.7-bzr1556 introduced new instance ID source for Azure
if dpkg --compare-versions "$2" le "0.7.7~bzr1556-0ubuntu1"; then
azure_apply_new_instance_id_1506187
fi

d=/etc/cloud/
if [ -f "$d/distro.cfg" ] && [ ! -f "$d/cloud.cfg.d/90_dpkg.cfg" ]; then
echo "moving $d/distro.cfg -> $d/cloud.cfg.d/90_dpkg.cfg"
[ -d "${d}/cloud.cfg.d" ] || mkdir "${d}/cloud.cfg.d"
mv "$d/distro.cfg" "$d/cloud.cfg.d/90_dpkg.cfg"
fi

cleanup_lp1552999 "$oldver"
cleanup_oci_network_lp1956788
cleanup_user_scripts "$2"
cleanup_rename_config "$2"
cleanup_oci_network_lp1956788 "$2"

esac

Expand Down

0 comments on commit 2e53fe3

Please sign in to comment.