Skip to content

Commit

Permalink
Remove redundant generator logs (#1318)
Browse files Browse the repository at this point in the history
Summary:
- currently we log redundant data, which at a glance looks like ds-identify runs twice
- use return code for control flow rather than strings
- group templated code and add demarcation comments around it
- explicitly define generator log file
  • Loading branch information
holmanb authored Mar 14, 2022
1 parent f7a8880 commit ee5e128
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions systemd/cloud-init-generator.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@ set -f
LOG=""
DEBUG_LEVEL=1
LOG_D="/run/cloud-init"
LOG_F="/run/cloud-init/cloud-init-generator.log"
ENABLE="enabled"
DISABLE="disabled"
FOUND="found"
NOTFOUND="notfound"
RUN_ENABLED_FILE="$LOG_D/$ENABLE"
CLOUD_TARGET_NAME="cloud-init.target"
# lxc sets 'container', but lets make that explicitly a global
CONTAINER="${container}"

# start: template section
{% if variant in ["suse"] %}
CLOUD_SYSTEM_TARGET="/usr/lib/systemd/system/cloud-init.target"
{% else %}
CLOUD_SYSTEM_TARGET="/lib/systemd/system/cloud-init.target"
{% endif %}
CLOUD_TARGET_NAME="cloud-init.target"
# lxc sets 'container', but lets make that explicitly a global
CONTAINER="${container}"
{% if variant in ["almalinux", "centos", "cloudlinux", "eurolinux", "fedora",
"miraclelinux", "openEuler", "rhel", "rocky", "virtuozzo"] %}
dsidentify="/usr/libexec/cloud-init/ds-identify"
{% else %}
dsidentify="/usr/lib/cloud-init/ds-identify"
{% endif %}
# end: template section

debug() {
local lvl="$1"
shift
[ "$lvl" -gt "$DEBUG_LEVEL" ] && return
if [ -z "$LOG" ]; then
local log="$LOG_D/${0##*/}.log"
{ [ -d "$LOG_D" ] || mkdir -p "$LOG_D"; } &&
{ : > "$log"; } >/dev/null 2>&1 && LOG="$log" ||
{ : > "$LOG_F"; } >/dev/null 2>&1 && LOG="$LOG_F" ||
LOG="/dev/kmsg"
fi
echo "$@" >> "$LOG"
Expand Down Expand Up @@ -83,34 +90,24 @@ default() {

check_for_datasource() {
local ds_rc=""
{% if variant in ["almalinux", "centos", "cloudlinux", "eurolinux", "fedora",
"miraclelinux", "openEuler", "rhel", "rocky", "virtuozzo"] %}
local dsidentify="/usr/libexec/cloud-init/ds-identify"
{% else %}
local dsidentify="/usr/lib/cloud-init/ds-identify"
{% endif %}
if [ ! -x "$dsidentify" ]; then
debug 1 "no ds-identify in $dsidentify. _RET=$FOUND"
debug 1 "no ds-identify in $dsidentify"
return 0
fi
$dsidentify
ds_rc=$?
debug 1 "ds-identify rc=$ds_rc"
if [ "$ds_rc" = "0" ]; then
_RET="$FOUND"
debug 1 "ds-identify _RET=$_RET"
return 0
fi
_RET="$NOTFOUND"
debug 1 "ds-identify _RET=$_RET"
return 1
}

main() {
local normal_d="$1" early_d="$2" late_d="$3"
local target_name="multi-user.target" gen_d="$early_d"
local link_path="$gen_d/${target_name}.wants/${CLOUD_TARGET_NAME}"
local ds="$NOTFOUND"
local ds=""

debug 1 "$0 normal=$normal_d early=$early_d late=$late_d"
debug 2 "$0 $*"
Expand All @@ -133,8 +130,8 @@ main() {
if [ "$result" = "$ENABLE" ]; then
debug 1 "checking for datasource"
check_for_datasource
ds=$_RET
if [ "$ds" = "$NOTFOUND" ]; then
ds=$?
if [ "$ds" = "1" ]; then
debug 1 "cloud-init is enabled but no datasource found, disabling"
result="$DISABLE"
fi
Expand Down

0 comments on commit ee5e128

Please sign in to comment.