#!/bin/sh -e . /usr/share/debconf/confmodule set -f # disable pathname expansion db_capb escape # to support carriage return / multi-line values 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" python -c ' import sys, yaml def update(src, cand): if not (isinstance(src, dict) and isinstance(cand, dict)): return cand for k, v in cand.iteritems(): # 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.load(newyaml) with open(fname, "r") as fp: cfg = yaml.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))' "$@" } 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 creds_all=$(python -c 'from urlparse 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}" || : } if [ "$1" = "configure" ] && [ -z "$2" ]; then # 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 fi # Automatically added by dh_systemd_enable/13.3.4 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then # This will only remove masks created by d-s-h on package removal. deb-systemd-helper unmask 'cloud-config.service' >/dev/null || true # was-enabled defaults to true, so new installations run enable. if deb-systemd-helper --quiet was-enabled 'cloud-config.service'; then # Enables the unit on first installation, creates new # symlinks on upgrades if the unit file has changed. deb-systemd-helper enable 'cloud-config.service' >/dev/null || true else # Update the statefile to add new symlinks (if any), which need to be # cleaned up on purge. Also remove old symlinks. deb-systemd-helper update-state 'cloud-config.service' >/dev/null || true fi fi # End automatically added section # Automatically added by dh_systemd_enable/13.3.4 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then # This will only remove masks created by d-s-h on package removal. deb-systemd-helper unmask 'cloud-final.service' >/dev/null || true # was-enabled defaults to true, so new installations run enable. if deb-systemd-helper --quiet was-enabled 'cloud-final.service'; then # Enables the unit on first installation, creates new # symlinks on upgrades if the unit file has changed. deb-systemd-helper enable 'cloud-final.service' >/dev/null || true else # Update the statefile to add new symlinks (if any), which need to be # cleaned up on purge. Also remove old symlinks. deb-systemd-helper update-state 'cloud-final.service' >/dev/null || true fi fi # End automatically added section # Automatically added by dh_systemd_enable/13.3.4 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then # This will only remove masks created by d-s-h on package removal. deb-systemd-helper unmask 'cloud-init-local.service' >/dev/null || true # was-enabled defaults to true, so new installations run enable. if deb-systemd-helper --quiet was-enabled 'cloud-init-local.service'; then # Enables the unit on first installation, creates new # symlinks on upgrades if the unit file has changed. deb-systemd-helper enable 'cloud-init-local.service' >/dev/null || true else # Update the statefile to add new symlinks (if any), which need to be # cleaned up on purge. Also remove old symlinks. deb-systemd-helper update-state 'cloud-init-local.service' >/dev/null || true fi fi # End automatically added section # Automatically added by dh_systemd_enable/13.3.4 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then # This will only remove masks created by d-s-h on package removal. deb-systemd-helper unmask 'cloud-init.service' >/dev/null || true # was-enabled defaults to true, so new installations run enable. if deb-systemd-helper --quiet was-enabled 'cloud-init.service'; then # Enables the unit on first installation, creates new # symlinks on upgrades if the unit file has changed. deb-systemd-helper enable 'cloud-init.service' >/dev/null || true else # Update the statefile to add new symlinks (if any), which need to be # cleaned up on purge. Also remove old symlinks. deb-systemd-helper update-state 'cloud-init.service' >/dev/null || true fi fi # End automatically added section # Automatically added by dh_python3 if which py3compile >/dev/null 2>&1; then py3compile -p cloud-init fi if which pypy3compile >/dev/null 2>&1; then pypy3compile -p cloud-init || true fi # End automatically added section # Automatically added by dh_installinit/13.3.4 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then if [ -x "/etc/init.d/cloud-init-local" ]; then update-rc.d cloud-init-local defaults >/dev/null || exit 1 fi fi # End automatically added section # Automatically added by dh_installinit/13.3.4 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then if [ -x "/etc/init.d/cloud-init" ]; then update-rc.d cloud-init defaults >/dev/null || exit 1 fi fi # End automatically added section # Automatically added by dh_installinit/13.3.4 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then if [ -x "/etc/init.d/cloud-config" ]; then update-rc.d cloud-config defaults >/dev/null || exit 1 fi fi # End automatically added section # Automatically added by dh_installinit/13.3.4 if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then if [ -x "/etc/init.d/cloud-final" ]; then update-rc.d cloud-final defaults >/dev/null || exit 1 fi fi # End automatically added section