Skip to content

Conversation

@theseal
Copy link

@theseal theseal commented Nov 26, 2025

No description provided.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

shfmt

[shfmt] reported by reviewdog 🐶

echo "Usage $0 HOSTNAME REPO TAGPATTERN"
exit 3


[shfmt] reported by reviewdog 🐶

# script is running with "set -e", use "|| true" to allow packages to not
# exist without stopping the script
apt-get -y install $pkg || true


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

echo ""
echo "test -f /etc/run-cosmos-at-boot && (bash -l cosmos -v update; bash -l cosmos -v apply && rm /etc/run-cosmos-at-boot)"
echo ""
echo "exit 0"


[shfmt] reported by reviewdog 🐶

if [ -f ${file} ]; then
sed -i 's/manage_etc_hosts: true/manage_etc_hosts: false/g' ${file}
fi


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

# When hostname pointed to loopback in /etc/hosts containers running on the
# host tried to connect to the container itself instead of the host.
host_ip=$(ip -j address show "$(ip -j route show default | jq -r '.[0].dev')" | jq -r .[0].addr_info[0].local)


[shfmt] reported by reviewdog 🐶

# shellcheck disable=SC2016
models_array+=('$COSMOS_REPO/'"${_host_type}-common/")


[shfmt] reported by reviewdog 🐶

IFS=:
echo "${models_array[*]}"


[shfmt] reported by reviewdog 🐶

date
nohup cosmos -v update && nohup cosmos -v apply && rm /etc/run-cosmos-at-boot
date


[shfmt] reported by reviewdog 🐶

local prefix=$1
local fd=${2:-$LOCK_FD}
local lock_file=$LOCKFILE_DIR/$prefix.lock


[shfmt] reported by reviewdog 🐶

# create lock file
eval "exec $fd>$lock_file"


[shfmt] reported by reviewdog 🐶

# acquier the lock
flock -n "$fd" &&
return 0 ||
return 1


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

echo "$error_str"
exit 1


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

echo "$info_str"
exit 0


[shfmt] reported by reviewdog 🐶

# In case e.g. the unit file has been removed "FragmentPath" will still
# return the old filename until daemon-reload is called, so do that here
# before we try checking for the FragmentPath.
need_reload=$(systemctl show --property NeedDaemonReload $FLEETLOCK_UNLOCK_SERVICE | awk -F= '{print $2}')
if [ "$need_reload" = "yes" ]; then
systemctl daemon-reload
fi
unit_file=$(systemctl show --property FragmentPath $FLEETLOCK_UNLOCK_SERVICE | awk -F= '{print $2}')
if [ -z "$unit_file" ]; then
# No unit file matching the service name, do nothing
return 0
fi
# Enable the service if needed
systemctl is-enabled --quiet $FLEETLOCK_UNLOCK_SERVICE || systemctl enable --quiet $FLEETLOCK_UNLOCK_SERVICE


[shfmt] reported by reviewdog 🐶

if [ ! -f $FLEETLOCK_DISABLE_FILE ] && [ -f $FLEETLOCK_CONFIG ] && [ -x $FLEETLOCK_TOOL ]; then
# Make sure the unlock service is enabled before we take a lock if
# cosmos ends up rebooting the machine before fleetlock_unlock() is
# called.
fleetlock_enable_unlock_service || return 1
local fleetlock_group=""
local optional_args=()
# shellcheck source=/dev/null
. $FLEETLOCK_CONFIG || return 1
if [ -z "$fleetlock_group" ]; then
echo "Unable to set fleetlock_group"
return 1
fi
if [ -n "$fleetlock_lock_timeout" ]; then
optional_args+=("--timeout")
optional_args+=("$fleetlock_lock_timeout")
fi
echo "Getting fleetlock lock"
$FLEETLOCK_TOOL --lock-group "$fleetlock_group" --lock "${optional_args[@]}" || return 1
fi
return 0


[shfmt] reported by reviewdog 🐶

if [ ! -f $FLEETLOCK_DISABLE_FILE ] && [ -f $FLEETLOCK_CONFIG ] && [ -x $FLEETLOCK_TOOL ]; then
local fleetlock_group=""
local optional_args=()
# shellcheck source=/dev/null
. $FLEETLOCK_CONFIG || return 1
if [ -z "$fleetlock_group" ]; then
echo "Unable to set fleetlock_group"
return 1
fi
if [ -n "$fleetlock_unlock_timeout" ]; then
optional_args+=("--timeout")
optional_args+=("$fleetlock_unlock_timeout")
fi
machine_is_healthy || return 1
echo "Releasing fleetlock lock"
$FLEETLOCK_TOOL --lock-group "$fleetlock_group" --unlock "${optional_args[@]}" || return 1
fi
return 0


[shfmt] reported by reviewdog 🐶

if [ ! -f $HEALTHCHECK_DISABLE_FILE ] && [ -x $HEALTHCHECK_TOOL ]; then
local fleetlock_healthcheck_timeout=""
local optional_args=()
# shellcheck source=/dev/null
. $FLEETLOCK_CONFIG || return 1
if [ -n "$fleetlock_healthcheck_timeout" ]; then
optional_args+=("--timeout")
optional_args+=("$fleetlock_healthcheck_timeout")
fi
echo "Running any health checks"
$HEALTHCHECK_TOOL "${optional_args[@]}" || return 1
fi
return 0


[shfmt] reported by reviewdog 🐶

if [[ $1 == '--random-sleep' ]]; then
shift
sleep=$((RANDOM % 300))


[shfmt] reported by reviewdog 🐶

echo "$0: Sleeping for ${sleep} seconds before attempting to run cosmos"
sleep $sleep
fi


[shfmt] reported by reviewdog 🐶

lock "$PROGNAME" || eexit "Only one instance of $PROGNAME can run at one time."
fleetlock_lock || eexit "Unable to acquire fleetlock lock."
cosmos "$@" update
cosmos "$@" apply


[shfmt] reported by reviewdog 🐶

if [ -f /var/run/reboot-required ] && [ -f /var/run/cosmos-reboot-in-progress ]; then
oexit "${PROGNAME}: Will not attempt fleetlock_unlock (exiting early) due to existing reboot files"
fi


[shfmt] reported by reviewdog 🐶

fleetlock_unlock || eexit "Unable to release fleetlock lock."


[shfmt] reported by reviewdog 🐶

touch /var/run/last-cosmos-ok.stamp


[shfmt] reported by reviewdog 🐶

if [ -f /cosmos-reboot ]; then
rm -f /cosmos-reboot
reboot
fi


[shfmt] reported by reviewdog 🐶

"fleetlock-unlock")
lock "$PROGNAME" || oexit "$PROGNAME appears locked by a running run-cosmos, let it handle unlocking instead."
fleetlock_unlock || eexit "Unable to release fleetlock lock."
;;
*)
main "$@"
;;


[shfmt] reported by reviewdog 🐶

SCRIPTHERDER_CMD+=('/usr/local/bin/scriptherder')
SCRIPTHERDER_CMD+=('--mode')
SCRIPTHERDER_CMD+=('wrap')
SCRIPTHERDER_CMD+=('--syslog')
SCRIPTHERDER_CMD+=('--name')
SCRIPTHERDER_CMD+=('cosmos')
SCRIPTHERDER_CMD+=('--')


[shfmt] reported by reviewdog 🐶

case "$1" in
-h)
echo "Usage: $0 [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]"
exit 0
;;
-H)
hostname="$2"
shift
;;
-s)
src_image="$2"
shift
;;
-D) dhcp="yes" ;;
-S)
size="$2"
shift
;;
-B)
bridge="$2"
shift
;;
-M)
mem="$2"
shift
;;
-C)
cpus="$2"
shift
;;
-R)
resolver="$2"
shift
;;
-i)
ip="$2"
shift
;;
-g)
gateway="$2"
shift
;;
-n)
netmask="$2"
shift
;;
-I)
ip6="$2"
shift
;;
-G)
gateway6="$2"
shift
;;
-N)
netmask6="$2"
shift
;;
--)
shift
break
;;
-*)
printf "Unknown option %s\nUsage: %s [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]" "$1" "$0"
exit 1
;;
*) break ;;
esac
shift


[shfmt] reported by reviewdog 🐶

cat >>"${meta_data}" <<EOF


[shfmt] reported by reviewdog 🐶

if [ -n "${ip}" ]; then
cat >>"${meta_data}" <<EOF


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

if [ -n "${ip6}" ]; then
cat >>"${meta_data}" <<EOF


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

test "$COSMOS_VERBOSE" = "y" &&
echo "$0: /etc/puppet/cosmos-modules.conf is present in the model, exiting"
exit 0


[shfmt] reported by reviewdog 🐶

test "$COSMOS_VERBOSE" = "y" &&
echo "$0: Updating /etc/puppet/cosmos-modules.conf with /etc/puppet/setup_cosmos_modules"
/etc/puppet/setup_cosmos_modules


[shfmt] reported by reviewdog 🐶

test -f /etc/puppet/cosmos-modules.conf && exit 0


[shfmt] reported by reviewdog 🐶

echo "$0: Creating/updating /etc/puppet/cosmos-modules.conf with defaults from this script"


[shfmt] reported by reviewdog 🐶

test "$(stat -t /root/.ssh | cut -d\ -f5)" != 0; then
chown root.root /root/.ssh


[shfmt] reported by reviewdog 🐶

test "$(stat -c %a /root/.ssh)" != 700; then
chmod 700 /root/.ssh


[shfmt] reported by reviewdog 🐶

if test "$(stat -t /root/.ssh/authorized_keys | cut -d\ -f5)" != 0; then
chown root.root /root/.ssh/authorized_keys
fi
if test "$(stat --printf=%a /root/.ssh/authorized_keys)" != 440; then
chmod 440 /root/.ssh/authorized_keys
fi


[shfmt] reported by reviewdog 🐶

test -z "$COSMOS_VERBOSE" || echo "$self: COSMOS_BASE was not found. Aborting change of permissions."
exit 0


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

# gpg on Ubuntu 16 and less is gnupg < 2, which doesn't have --import-options show-only
# but on the other hand defaults to this mode (https://dev.gnupg.org/T2943)
gnupg_show_options=("--dry-run")


[shfmt] reported by reviewdog 🐶

COSMOS_KEYS=/etc/cosmos/keys


[shfmt] reported by reviewdog 🐶

if [[ ! -s $k ]]; then
# Silently ignore empty files
continue
fi
pubkeys_in_file=$(cosmos gpg "${gnupg_show_options[@]}" \
--with-colons --with-fingerprint --quiet <"$k" |
grep "^pub:")


[shfmt] reported by reviewdog 🐶

# We only support files with one key in them
num_pub_keys=$(echo "$pubkeys_in_file" | wc -l)
if [ "$num_pub_keys" -ne 1 ]; then
echo -e "$0: ${red}Ignoring file that does not have exactly one pubkey (found $num_pub_keys): ${k}${reset}"
continue
fi


[shfmt] reported by reviewdog 🐶

expired_pubkey_in_file=$(echo "${pubkeys_in_file}" | awk -F: '$2 == "e" { print $0 }')
if [[ $expired_pubkey_in_file ]]; then
echo -e "$0: ${red}Ignoring file with expired pubkey: ${k}${reset}"
continue
fi


[shfmt] reported by reviewdog 🐶

fp=$(echo "${pubkeys_in_file}" | awk -F: '{print $5}')


[shfmt] reported by reviewdog 🐶

# Remember that we saw fingerprint $fp in file $k
SEEN[$fp]=$k


[shfmt] reported by reviewdog 🐶

# Always import a non-expired file since it may have been updated
gpg_output=$(cosmos gpg --no-tty --import <"$k" 2>&1)
# Only print output if a key is changed
echo "$gpg_output" | grep -q " not changed$" || echo "$gpg_output"


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

echo -e "$0: ${red}NO trusted keys found in directory ${COSMOS_KEYS} - aborting${reset}"
echo "(this is probably a syntax problem with the gpg commands in this script)"
exit 1


[shfmt] reported by reviewdog 🐶

if [[ ! ${SEEN[$fp]} ]]; then
echo -e "$0: ${bold}Deleting key${reset} ${fp} not present (or expired) in ${COSMOS_KEYS}"
cosmos gpg --fingerprint "$fp"
cosmos gpg --yes --batch --delete-key "$fp" || true
fi


[shfmt] reported by reviewdog 🐶

rm -rf "$CACHE_DIR/staging/$1"
git archive --format=tar --prefix="$1"/ "$2" | (cd "$CACHE_DIR/staging/" && tar xf -)


[shfmt] reported by reviewdog 🐶

if [ ! -d "$MODULES_DIR" ]; then
mkdir -p "$MODULES_DIR"
fi
if [ ! -d "$CACHE_DIR" ]; then
mkdir -p "$CACHE_DIR"/{scm,staging}
fi


[shfmt] reported by reviewdog 🐶

files=()
if [ -f "$CONFIG" ]; then
files+=("$CONFIG")
fi


[shfmt] reported by reviewdog 🐶

if [ -f "$LOCALCONFIG" ]; then
files+=("$LOCALCONFIG")
fi


[shfmt] reported by reviewdog 🐶

# First pass to clone any new modules, and update those marked for updating.
grep -h -E -v "^#" "${files[@]}" | sort | (
while read -r module src update pattern; do
# We only support git://, file:/// and https:// urls at the moment
if [ "${src:0:6}" = "git://" ] || [ "${src:0:8}" = "file:///" ] || [ "${src:0:8}" = "https://" ]; then
if [ ! -d "$CACHE_DIR/scm/$module" ]; then
git clone -q "$src" "$CACHE_DIR/scm/$module"
elif [ -d "$CACHE_DIR/scm/$module/.git" ]; then
if [ "$update" = "yes" ]; then
cd "$CACHE_DIR/scm/$module" || exit 1
if [ "$src" != "$(git config remote.origin.url)" ]; then
git config remote.origin.url "$src"
fi
# Support master branch being renamed to main
git branch --all | grep -q '^[[:space:]]*remotes/origin/main$' && git checkout main
# Update repo and clean out any local inconsistencies
git pull -q || (git fetch && git reset --hard)
else
continue
fi
else
echo -e "${red}ERROR: Ignoring non-git repository${reset}"
continue
fi
elif [[ "$src" =~ .*:// ]]; then
echo -e "${red}ERROR: Don't know how to install '${src}'${reset}"
continue
else
echo -e "${bold}WARNING - attempting UNSAFE installation/upgrade of puppet-module ${module} from ${src}${reset}"
if [ ! -d "/etc/puppet/modules/$module" ]; then
puppet module install "$src"
elif [ "$update" = "yes" ]; then
puppet module upgrade "$src"
fi
fi
done
)


[shfmt] reported by reviewdog 🐶

# Second pass to verify the signatures on all modules and stage those that
# have good signatures.
grep -h -E -v "^#" "${files[@]}" | sort | (
while read -r module src update pattern; do
# We only support git://, file:/// and https:// urls at the moment
if [ "${src:0:6}" = "git://" ] || [ "${src:0:8}" = "file:///" ] || [ "${src:0:8}" = "https://" ]; then
# Verify git tag
cd "$CACHE_DIR/scm/$module" || exit 1
TAG=$(git tag -l "${pattern:-*}" | sort | tail -1)
if [ "$COSMOS_VERBOSE" = "y" ]; then
echo -e "Checking signature on puppet-module:tag ${bold}${module}:${TAG}${reset}"
fi
if [ -z "$TAG" ]; then
echo -e "${red}ERROR: No git tag found for pattern '${pattern:-*}' on puppet-module ${module}${reset}"
continue
fi
if git tag -v "$TAG" &>/dev/null; then
#if [ "$COSMOS_VERBOSE" = "y" ]; thengg
# # short output on good signature
# git tag -v $TAG 2>&1 | grep "gpg: Good signature"
#fi
# Put archive in staging since tag verified OK
stage_module "$module" "$TAG"
else
echo -e "${red}FAILED signature check on puppet-module ${module}${reset}"
git tag -v "$TAG"
echo ''
fi
fi
done
)


[shfmt] reported by reviewdog 🐶

# Cleanup removed puppet modules from CACHE_DIR
for MODULE in "$CACHE_DIR"/staging/*; do
if ! grep -h -E -q "^$MODULE\s+" "$CONFIG" "$LOCALCONFIG"; then
rm -rf "$CACHE_DIR"/{scm,staging}/"$MODULE"
fi
done


[shfmt] reported by reviewdog 🐶

# Installing verified puppet modules
rsync --archive --delete "$CACHE_DIR/staging/" "$MODULES_DIR/"


[shfmt] reported by reviewdog 🐶

args+=('--verbose')
args+=('--show_diff')


[shfmt] reported by reviewdog 🐶

args+=('--logdest=syslog')


[shfmt] reported by reviewdog 🐶

find /etc/puppet/manifests -name \*.pp | while read -r m; do
test "$COSMOS_VERBOSE" = "y" && echo "$0: Applying Puppet manifest $m"
puppet apply "${args[@]}" "$m"
done


[shfmt] reported by reviewdog 🐶

PUPPET_REPORTS_DIR='/var/lib/puppet/reports'
if [ -d "${PUPPET_REPORTS_DIR}" ]; then
find "${PUPPET_REPORTS_DIR}" -type f -mtime +10 -print0 | xargs -0 rm -f
fi


[shfmt] reported by reviewdog 🐶

apt-get -qq update
apt-get -qq -y autoremove


[shfmt] reported by reviewdog 🐶

# May contain ALLOW_REBOOT_AT=
# Eg. ALLOW_REBOOT_AT=06
# shellcheck source=/dev/null
. /etc/cosmos-automatic-reboot


[shfmt] reported by reviewdog 🐶

if [ -n "${ALLOW_REBOOT_AT}" ]; then
if [ "${ALLOW_REBOOT_AT}" != "$(date +%H)" ]; then
echo "Scheduled to reboot at ${ALLOW_REBOOT_AT}"
exit
fi
fi


[shfmt] reported by reviewdog 🐶

if [[ $HOSTNAME =~ -tug- ]]; then
# Reboot hosts in site TUG with 15 seconds delay (enough to manually
# cancel the reboot if logged in and seeing the 'emerg' message broadcasted to console)
sleep=15
elif [[ $HOSTNAME =~ -fre- ]]; then
# reboot hosts in site FRE with 15+180 to 15+180+180 seconds delay
sleep=$((180 + (RANDOM % 180)))
elif [[ $HOSTNAME =~ -lla- ]]; then
# reboot hosts in site LLA with 15+180+180 to 15+180+180+180 seconds delay
sleep=$((375 + (RANDOM % 180)))
else
# reboot hosts in any other site with 15 to 315 seconds delay
sleep=$((15 + (RANDOM % 300)))
fi


[shfmt] reported by reviewdog 🐶

logger -p local0.emerg -i -t cosmos-automatic-reboot "Rebooting automatically in $sleep seconds (if /var/run/reboot-required still exists)"
sleep $sleep
if [ -f /var/run/reboot-required ]; then
logger -p local0.crit -i -t cosmos-automatic-reboot "Rebooting automatically"
# Signal to run-cosmos
touch /var/run/cosmos-reboot-in-progress
reboot
fi


[shfmt] reported by reviewdog 🐶

test -z "$COSMOS_VERBOSE" || echo "$self: COSMOS_BASE was not found. Aborting change of permissions."
exit 0


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

test -z "$COSMOS_VERBOSE" || echo "$self: overlay is a no-op"
exit 0


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

chown ${args} root:root "$MODEL_OVERLAY"/root
chmod ${args} 0700 "$MODEL_OVERLAY"/root


[shfmt] reported by reviewdog 🐶

chown ${args} -R root:root "$MODEL_OVERLAY"/root/.ssh
chmod ${args} 0700 "$MODEL_OVERLAY"/root/.ssh


[shfmt] reported by reviewdog 🐶

apt-get -y install vim traceroute tcpdump molly-guard less rsync git-core unattended-upgrades
update-alternatives --set editor /usr/bin/vim.basic


[shfmt] reported by reviewdog 🐶

mkdir -p "$(dirname "$stamp")"
touch "$stamp"


[shfmt] reported by reviewdog 🐶

apt-get update
apt-get -y install puppet
# shellcheck source=/dev/null
. /etc/os-release


[shfmt] reported by reviewdog 🐶

# Note: in posix shell, string comparison is done with a single =
if [ "${ID}" = "debian" ] && [ "${VERSION_ID}" -ge 12 ] || ([ "${ID}" = "ubuntu" ] && dpkg --compare-versions "${VERSION_ID}" ge 24.04); then
apt-get -y install \
cron \
puppet-module-camptocamp-augeas \
puppet-module-puppetlabs-apt \
puppet-module-puppetlabs-concat \
puppet-module-puppetlabs-cron-core \
puppet-module-puppetlabs-stdlib \
puppet-module-puppetlabs-vcsrepo


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

mkdir -p "$(dirname "${stamp}")"
touch "${stamp}"


[shfmt] reported by reviewdog 🐶

apt-get update
# If we don't install emacs before yaml-mode the default emacs package
# will be emacs-gtk which brings x11 with friends which we don't need.
apt-get -y install emacs-nox
apt-get -y install hiera-eyaml yaml-mode


[shfmt] reported by reviewdog 🐶

# hiera-eyaml wants a certificate and public key, not just a public key oddly enough
echo "$0: Generating eyaml key in ${EYAMLDIR} - this might take a while..."
mkdir -p /etc/hiera/eyaml
openssl req -x509 -newkey rsa:4096 -keyout ${EYAMLDIR}/private_key.pkcs7.pem \
-out ${EYAMLDIR}/public_certkey.pkcs7.pem -days 3653 -nodes -sha256 \
-subj "/C=SE/O=SUNET/OU=EYAML/CN=$(hostname)"
rm -f ${EYAMLDIR}/public_key.pkcs7.pem # cleanup


[shfmt] reported by reviewdog 🐶

apt-get update
apt-get -y install ruby-gpgme


[shfmt] reported by reviewdog 🐶

if [ "$1" != "--force" ]; then
echo ""
echo "Automatic Hiera-GPG key generation DISABLED (to not block on missing entropy)"
echo ""
echo " Run \`$0 --force' manually"
echo ""
exit 0
fi


[shfmt] reported by reviewdog 🐶

if [ ! -f /usr/bin/gpg2 ]; then
apt-get update
apt-get -y install gnupg2
fi


[shfmt] reported by reviewdog 🐶

mkdir -p $GNUPGHOME
chmod 700 $GNUPGHOME


[shfmt] reported by reviewdog 🐶

TMPFILE=$(mktemp /tmp/hiera-gpg.XXXXXX)
cat >"$TMPFILE" <<EOF


[shfmt] reported by reviewdog 🐶

gpg2 --batch --gen-key "$TMPFILE"
rm -f "$TMPFILE"


[shfmt] reported by reviewdog 🐶

echo "Usage: $0 fqdn"
exit 1


[shfmt] reported by reviewdog 🐶

echo "$0: No host-directory for '$HOSTNAME' found - execute in top-level cosmos dir"
exit 1


[shfmt] reported by reviewdog 🐶

echo "Copying files to host..."
rsync -av --exclude '*~' global/overlay/etc/puppet/cosmos-rules.yaml root@"$HOSTNAME":/etc/puppet/cosmos-rules.yaml
rsync -av --exclude '*~' global/overlay/etc/puppet/manifests/cosmos-site.pp root@"$HOSTNAME":/etc/puppet/manifests/cosmos-site.pp
rsync -av --exclude '*~' global/overlay/etc/hiera/data/common.yaml root@"$HOSTNAME":/etc/hiera/data/common.yaml


[shfmt] reported by reviewdog 🐶

# Test if the user has symlinked puppet-sunet correctly
# by first checking if the link exits and then whether
# or not the directory contains any files.
if [ -L global/overlay/etc/puppet/cosmos-modules/sunet ] &&
[ -n "$(ls -A global/overlay/etc/puppet/cosmos-modules/sunet/*)" ]; then
rsync -av --delete --exclude '*~' global/overlay/etc/puppet/cosmos-modules/sunet/* root@"$HOSTNAME":/etc/puppet/cosmos-modules/sunet/.
fi


[shfmt] reported by reviewdog 🐶

echo "Running puppet apply..."
ssh root@"$HOSTNAME" /usr/bin/puppet apply "${PUPPET_ARGS[@]}" /etc/puppet/manifests/cosmos-site.pp


[shfmt] reported by reviewdog 🐶

echo "Cosmos or puppet already running. Exiting."
exit 1


[shfmt] reported by reviewdog 🐶

echo "Please specify a cloud image host that the script should do the following on:"
echo " #1 enable root-login"
echo " #2 remove the default user"
echo " #3 run apt-get update and dist-upgrade without interaction"
echo " #4 reboot to start using the new kernel, updated packages etc."
exit 1


[shfmt] reported by reviewdog 🐶

proxyjump+=("-o")
proxyjump+=("ProxyJump=${ssh_proxy}")


[shfmt] reported by reviewdog 🐶

echo "Please specify a cloud image host that the script should do the following on:"
echo " #1 enable root-login"
echo " #2 remove the default user"
echo " #3 run apt-get update and dist-upgrade without interaction"
echo " #4 reboot to start using the new kernel, updated packages etc."
exit 1


[shfmt] reported by reviewdog 🐶

proxyjump+=("-o")
proxyjump+=("ProxyJump=${ssh_proxy}")

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

shfmt

[shfmt] reported by reviewdog 🐶

SCRIPTHERDER_CMD+=('/usr/local/bin/scriptherder')
SCRIPTHERDER_CMD+=('--mode')
SCRIPTHERDER_CMD+=('wrap')
SCRIPTHERDER_CMD+=('--syslog')
SCRIPTHERDER_CMD+=('--name')
SCRIPTHERDER_CMD+=('cosmos')
SCRIPTHERDER_CMD+=('--')


[shfmt] reported by reviewdog 🐶

case "$1" in
-h)
echo "Usage: $0 [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]"
exit 0
;;
-H)
hostname="$2"
shift
;;
-s)
src_image="$2"
shift
;;
-D) dhcp="yes" ;;
-S)
size="$2"
shift
;;
-B)
bridge="$2"
shift
;;
-M)
mem="$2"
shift
;;
-C)
cpus="$2"
shift
;;
-R)
resolver="$2"
shift
;;
-i)
ip="$2"
shift
;;
-g)
gateway="$2"
shift
;;
-n)
netmask="$2"
shift
;;
-I)
ip6="$2"
shift
;;
-G)
gateway6="$2"
shift
;;
-N)
netmask6="$2"
shift
;;
--)
shift
break
;;
-*)
printf "Unknown option %s\nUsage: %s [-h] [-H hostname] [-M <memory>] [-C <#cpus>] [-B <bridge>] [-D (dhcp)] [-i/-I <ip4/6>] [-n/-N <mask4/6>] [-g/-G <gw4/6>] [-R <resolver(s)>] [-s <src image>]" "$1" "$0"
exit 1
;;
*) break ;;
esac
shift


[shfmt] reported by reviewdog 🐶

cat >>"${meta_data}" <<EOF


[shfmt] reported by reviewdog 🐶

if [ -n "${ip}" ]; then
cat >>"${meta_data}" <<EOF


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

if [ -n "${ip6}" ]; then
cat >>"${meta_data}" <<EOF


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

test "$COSMOS_VERBOSE" = "y" &&
echo "$0: /etc/puppet/cosmos-modules.conf is present in the model, exiting"
exit 0


[shfmt] reported by reviewdog 🐶

test "$COSMOS_VERBOSE" = "y" &&
echo "$0: Updating /etc/puppet/cosmos-modules.conf with /etc/puppet/setup_cosmos_modules"
/etc/puppet/setup_cosmos_modules


[shfmt] reported by reviewdog 🐶

test -f /etc/puppet/cosmos-modules.conf && exit 0


[shfmt] reported by reviewdog 🐶

echo "$0: Creating/updating /etc/puppet/cosmos-modules.conf with defaults from this script"


[shfmt] reported by reviewdog 🐶

test "$(stat -t /root/.ssh | cut -d\ -f5)" != 0; then
chown root.root /root/.ssh


[shfmt] reported by reviewdog 🐶

test "$(stat -c %a /root/.ssh)" != 700; then
chmod 700 /root/.ssh


[shfmt] reported by reviewdog 🐶

if test "$(stat -t /root/.ssh/authorized_keys | cut -d\ -f5)" != 0; then
chown root.root /root/.ssh/authorized_keys
fi
if test "$(stat --printf=%a /root/.ssh/authorized_keys)" != 440; then
chmod 440 /root/.ssh/authorized_keys
fi


[shfmt] reported by reviewdog 🐶

test -z "$COSMOS_VERBOSE" || echo "$self: COSMOS_BASE was not found. Aborting change of permissions."
exit 0


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

# gpg on Ubuntu 16 and less is gnupg < 2, which doesn't have --import-options show-only
# but on the other hand defaults to this mode (https://dev.gnupg.org/T2943)
gnupg_show_options=("--dry-run")


[shfmt] reported by reviewdog 🐶

COSMOS_KEYS=/etc/cosmos/keys


[shfmt] reported by reviewdog 🐶

if [[ ! -s $k ]]; then
# Silently ignore empty files
continue
fi
pubkeys_in_file=$(cosmos gpg "${gnupg_show_options[@]}" \
--with-colons --with-fingerprint --quiet <"$k" |
grep "^pub:")


[shfmt] reported by reviewdog 🐶

# We only support files with one key in them
num_pub_keys=$(echo "$pubkeys_in_file" | wc -l)
if [ "$num_pub_keys" -ne 1 ]; then
echo -e "$0: ${red}Ignoring file that does not have exactly one pubkey (found $num_pub_keys): ${k}${reset}"
continue
fi


[shfmt] reported by reviewdog 🐶

expired_pubkey_in_file=$(echo "${pubkeys_in_file}" | awk -F: '$2 == "e" { print $0 }')
if [[ $expired_pubkey_in_file ]]; then
echo -e "$0: ${red}Ignoring file with expired pubkey: ${k}${reset}"
continue
fi


[shfmt] reported by reviewdog 🐶

fp=$(echo "${pubkeys_in_file}" | awk -F: '{print $5}')


[shfmt] reported by reviewdog 🐶

# Remember that we saw fingerprint $fp in file $k
SEEN[$fp]=$k


[shfmt] reported by reviewdog 🐶

# Always import a non-expired file since it may have been updated
gpg_output=$(cosmos gpg --no-tty --import <"$k" 2>&1)
# Only print output if a key is changed
echo "$gpg_output" | grep -q " not changed$" || echo "$gpg_output"


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

echo -e "$0: ${red}NO trusted keys found in directory ${COSMOS_KEYS} - aborting${reset}"
echo "(this is probably a syntax problem with the gpg commands in this script)"
exit 1


[shfmt] reported by reviewdog 🐶

if [[ ! ${SEEN[$fp]} ]]; then
echo -e "$0: ${bold}Deleting key${reset} ${fp} not present (or expired) in ${COSMOS_KEYS}"
cosmos gpg --fingerprint "$fp"
cosmos gpg --yes --batch --delete-key "$fp" || true
fi


[shfmt] reported by reviewdog 🐶

rm -rf "$CACHE_DIR/staging/$1"
git archive --format=tar --prefix="$1"/ "$2" | (cd "$CACHE_DIR/staging/" && tar xf -)


[shfmt] reported by reviewdog 🐶

if [ ! -d "$MODULES_DIR" ]; then
mkdir -p "$MODULES_DIR"
fi
if [ ! -d "$CACHE_DIR" ]; then
mkdir -p "$CACHE_DIR"/{scm,staging}
fi


[shfmt] reported by reviewdog 🐶

files=()
if [ -f "$CONFIG" ]; then
files+=("$CONFIG")
fi


[shfmt] reported by reviewdog 🐶

if [ -f "$LOCALCONFIG" ]; then
files+=("$LOCALCONFIG")
fi


[shfmt] reported by reviewdog 🐶

# First pass to clone any new modules, and update those marked for updating.
grep -h -E -v "^#" "${files[@]}" | sort | (
while read -r module src update pattern; do
# We only support git://, file:/// and https:// urls at the moment
if [ "${src:0:6}" = "git://" ] || [ "${src:0:8}" = "file:///" ] || [ "${src:0:8}" = "https://" ]; then
if [ ! -d "$CACHE_DIR/scm/$module" ]; then
git clone -q "$src" "$CACHE_DIR/scm/$module"
elif [ -d "$CACHE_DIR/scm/$module/.git" ]; then
if [ "$update" = "yes" ]; then
cd "$CACHE_DIR/scm/$module" || exit 1
if [ "$src" != "$(git config remote.origin.url)" ]; then
git config remote.origin.url "$src"
fi
# Support master branch being renamed to main
git branch --all | grep -q '^[[:space:]]*remotes/origin/main$' && git checkout main
# Update repo and clean out any local inconsistencies
git pull -q || (git fetch && git reset --hard)
else
continue
fi
else
echo -e "${red}ERROR: Ignoring non-git repository${reset}"
continue
fi
elif [[ "$src" =~ .*:// ]]; then
echo -e "${red}ERROR: Don't know how to install '${src}'${reset}"
continue
else
echo -e "${bold}WARNING - attempting UNSAFE installation/upgrade of puppet-module ${module} from ${src}${reset}"
if [ ! -d "/etc/puppet/modules/$module" ]; then
puppet module install "$src"
elif [ "$update" = "yes" ]; then
puppet module upgrade "$src"
fi
fi
done
)


[shfmt] reported by reviewdog 🐶

# Second pass to verify the signatures on all modules and stage those that
# have good signatures.
grep -h -E -v "^#" "${files[@]}" | sort | (
while read -r module src update pattern; do
# We only support git://, file:/// and https:// urls at the moment
if [ "${src:0:6}" = "git://" ] || [ "${src:0:8}" = "file:///" ] || [ "${src:0:8}" = "https://" ]; then
# Verify git tag
cd "$CACHE_DIR/scm/$module" || exit 1
TAG=$(git tag -l "${pattern:-*}" | sort | tail -1)
if [ "$COSMOS_VERBOSE" = "y" ]; then
echo -e "Checking signature on puppet-module:tag ${bold}${module}:${TAG}${reset}"
fi
if [ -z "$TAG" ]; then
echo -e "${red}ERROR: No git tag found for pattern '${pattern:-*}' on puppet-module ${module}${reset}"
continue
fi
if git tag -v "$TAG" &>/dev/null; then
#if [ "$COSMOS_VERBOSE" = "y" ]; thengg
# # short output on good signature
# git tag -v $TAG 2>&1 | grep "gpg: Good signature"
#fi
# Put archive in staging since tag verified OK
stage_module "$module" "$TAG"
else
echo -e "${red}FAILED signature check on puppet-module ${module}${reset}"
git tag -v "$TAG"
echo ''
fi
fi
done
)


[shfmt] reported by reviewdog 🐶

# Cleanup removed puppet modules from CACHE_DIR
for MODULE in "$CACHE_DIR"/staging/*; do
if ! grep -h -E -q "^$MODULE\s+" "$CONFIG" "$LOCALCONFIG"; then
rm -rf "$CACHE_DIR"/{scm,staging}/"$MODULE"
fi
done


[shfmt] reported by reviewdog 🐶

# Installing verified puppet modules
rsync --archive --delete "$CACHE_DIR/staging/" "$MODULES_DIR/"


[shfmt] reported by reviewdog 🐶

args+=('--verbose')
args+=('--show_diff')


[shfmt] reported by reviewdog 🐶

args+=('--logdest=syslog')


[shfmt] reported by reviewdog 🐶

find /etc/puppet/manifests -name \*.pp | while read -r m; do
test "$COSMOS_VERBOSE" = "y" && echo "$0: Applying Puppet manifest $m"
puppet apply "${args[@]}" "$m"
done


[shfmt] reported by reviewdog 🐶

PUPPET_REPORTS_DIR='/var/lib/puppet/reports'
if [ -d "${PUPPET_REPORTS_DIR}" ]; then
find "${PUPPET_REPORTS_DIR}" -type f -mtime +10 -print0 | xargs -0 rm -f
fi


[shfmt] reported by reviewdog 🐶

apt-get -qq update
apt-get -qq -y autoremove


[shfmt] reported by reviewdog 🐶

# May contain ALLOW_REBOOT_AT=
# Eg. ALLOW_REBOOT_AT=06
# shellcheck source=/dev/null
. /etc/cosmos-automatic-reboot


[shfmt] reported by reviewdog 🐶

if [ -n "${ALLOW_REBOOT_AT}" ]; then
if [ "${ALLOW_REBOOT_AT}" != "$(date +%H)" ]; then
echo "Scheduled to reboot at ${ALLOW_REBOOT_AT}"
exit
fi
fi


[shfmt] reported by reviewdog 🐶

if [[ $HOSTNAME =~ -tug- ]]; then
# Reboot hosts in site TUG with 15 seconds delay (enough to manually
# cancel the reboot if logged in and seeing the 'emerg' message broadcasted to console)
sleep=15
elif [[ $HOSTNAME =~ -fre- ]]; then
# reboot hosts in site FRE with 15+180 to 15+180+180 seconds delay
sleep=$((180 + (RANDOM % 180)))
elif [[ $HOSTNAME =~ -lla- ]]; then
# reboot hosts in site LLA with 15+180+180 to 15+180+180+180 seconds delay
sleep=$((375 + (RANDOM % 180)))
else
# reboot hosts in any other site with 15 to 315 seconds delay
sleep=$((15 + (RANDOM % 300)))
fi


[shfmt] reported by reviewdog 🐶

logger -p local0.emerg -i -t cosmos-automatic-reboot "Rebooting automatically in $sleep seconds (if /var/run/reboot-required still exists)"
sleep $sleep
if [ -f /var/run/reboot-required ]; then
logger -p local0.crit -i -t cosmos-automatic-reboot "Rebooting automatically"
# Signal to run-cosmos
touch /var/run/cosmos-reboot-in-progress
reboot
fi


[shfmt] reported by reviewdog 🐶

test -z "$COSMOS_VERBOSE" || echo "$self: COSMOS_BASE was not found. Aborting change of permissions."
exit 0


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

test -z "$COSMOS_VERBOSE" || echo "$self: overlay is a no-op"
exit 0


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

chown ${args} root:root "$MODEL_OVERLAY"/root
chmod ${args} 0700 "$MODEL_OVERLAY"/root


[shfmt] reported by reviewdog 🐶

chown ${args} -R root:root "$MODEL_OVERLAY"/root/.ssh
chmod ${args} 0700 "$MODEL_OVERLAY"/root/.ssh


[shfmt] reported by reviewdog 🐶

apt-get -y install vim traceroute tcpdump molly-guard less rsync git-core unattended-upgrades
update-alternatives --set editor /usr/bin/vim.basic


[shfmt] reported by reviewdog 🐶

mkdir -p "$(dirname "$stamp")"
touch "$stamp"


[shfmt] reported by reviewdog 🐶

apt-get update
apt-get -y install puppet
# shellcheck source=/dev/null
. /etc/os-release


[shfmt] reported by reviewdog 🐶

# Note: in posix shell, string comparison is done with a single =
if [ "${ID}" = "debian" ] && [ "${VERSION_ID}" -ge 12 ] || ([ "${ID}" = "ubuntu" ] && dpkg --compare-versions "${VERSION_ID}" ge 24.04); then
apt-get -y install \
cron \
puppet-module-camptocamp-augeas \
puppet-module-puppetlabs-apt \
puppet-module-puppetlabs-concat \
puppet-module-puppetlabs-cron-core \
puppet-module-puppetlabs-stdlib \
puppet-module-puppetlabs-vcsrepo


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

mkdir -p "$(dirname "${stamp}")"
touch "${stamp}"


[shfmt] reported by reviewdog 🐶

apt-get update
# If we don't install emacs before yaml-mode the default emacs package
# will be emacs-gtk which brings x11 with friends which we don't need.
apt-get -y install emacs-nox
apt-get -y install hiera-eyaml yaml-mode


[shfmt] reported by reviewdog 🐶

# hiera-eyaml wants a certificate and public key, not just a public key oddly enough
echo "$0: Generating eyaml key in ${EYAMLDIR} - this might take a while..."
mkdir -p /etc/hiera/eyaml
openssl req -x509 -newkey rsa:4096 -keyout ${EYAMLDIR}/private_key.pkcs7.pem \
-out ${EYAMLDIR}/public_certkey.pkcs7.pem -days 3653 -nodes -sha256 \
-subj "/C=SE/O=SUNET/OU=EYAML/CN=$(hostname)"
rm -f ${EYAMLDIR}/public_key.pkcs7.pem # cleanup


[shfmt] reported by reviewdog 🐶

apt-get update
apt-get -y install ruby-gpgme


[shfmt] reported by reviewdog 🐶

if [ "$1" != "--force" ]; then
echo ""
echo "Automatic Hiera-GPG key generation DISABLED (to not block on missing entropy)"
echo ""
echo " Run \`$0 --force' manually"
echo ""
exit 0
fi


[shfmt] reported by reviewdog 🐶

if [ ! -f /usr/bin/gpg2 ]; then
apt-get update
apt-get -y install gnupg2
fi


[shfmt] reported by reviewdog 🐶

mkdir -p $GNUPGHOME
chmod 700 $GNUPGHOME


[shfmt] reported by reviewdog 🐶

TMPFILE=$(mktemp /tmp/hiera-gpg.XXXXXX)
cat >"$TMPFILE" <<EOF


[shfmt] reported by reviewdog 🐶

gpg2 --batch --gen-key "$TMPFILE"
rm -f "$TMPFILE"


[shfmt] reported by reviewdog 🐶

echo "Usage: $0 fqdn"
exit 1


[shfmt] reported by reviewdog 🐶

echo "$0: No host-directory for '$HOSTNAME' found - execute in top-level cosmos dir"
exit 1


[shfmt] reported by reviewdog 🐶

echo "Copying files to host..."
rsync -av --exclude '*~' global/overlay/etc/puppet/cosmos-rules.yaml root@"$HOSTNAME":/etc/puppet/cosmos-rules.yaml
rsync -av --exclude '*~' global/overlay/etc/puppet/manifests/cosmos-site.pp root@"$HOSTNAME":/etc/puppet/manifests/cosmos-site.pp
rsync -av --exclude '*~' global/overlay/etc/hiera/data/common.yaml root@"$HOSTNAME":/etc/hiera/data/common.yaml


[shfmt] reported by reviewdog 🐶

# Test if the user has symlinked puppet-sunet correctly
# by first checking if the link exits and then whether
# or not the directory contains any files.
if [ -L global/overlay/etc/puppet/cosmos-modules/sunet ] &&
[ -n "$(ls -A global/overlay/etc/puppet/cosmos-modules/sunet/*)" ]; then
rsync -av --delete --exclude '*~' global/overlay/etc/puppet/cosmos-modules/sunet/* root@"$HOSTNAME":/etc/puppet/cosmos-modules/sunet/.
fi


[shfmt] reported by reviewdog 🐶

echo "Running puppet apply..."
ssh root@"$HOSTNAME" /usr/bin/puppet apply "${PUPPET_ARGS[@]}" /etc/puppet/manifests/cosmos-site.pp


[shfmt] reported by reviewdog 🐶

echo "Cosmos or puppet already running. Exiting."
exit 1


[shfmt] reported by reviewdog 🐶

echo "Please specify a cloud image host that the script should do the following on:"
echo " #1 enable root-login"
echo " #2 remove the default user"
echo " #3 run apt-get update and dist-upgrade without interaction"
echo " #4 reboot to start using the new kernel, updated packages etc."
exit 1


[shfmt] reported by reviewdog 🐶

proxyjump+=("-o")
proxyjump+=("ProxyJump=${ssh_proxy}")


[shfmt] reported by reviewdog 🐶

echo "Please specify a cloud image host that the script should do the following on:"
echo " #1 enable root-login"
echo " #2 remove the default user"
echo " #3 run apt-get update and dist-upgrade without interaction"
echo " #4 reboot to start using the new kernel, updated packages etc."
exit 1


[shfmt] reported by reviewdog 🐶

proxyjump+=("-o")
proxyjump+=("ProxyJump=${ssh_proxy}")

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

shfmt

[shfmt] reported by reviewdog 🐶

files=()
if [ -f "$CONFIG" ]; then
files+=("$CONFIG")
fi


[shfmt] reported by reviewdog 🐶

if [ -f "$LOCALCONFIG" ]; then
files+=("$LOCALCONFIG")
fi


[shfmt] reported by reviewdog 🐶

# First pass to clone any new modules, and update those marked for updating.
grep -h -E -v "^#" "${files[@]}" | sort | (
while read -r module src update pattern; do
# We only support git://, file:/// and https:// urls at the moment
if [ "${src:0:6}" = "git://" ] || [ "${src:0:8}" = "file:///" ] || [ "${src:0:8}" = "https://" ]; then
if [ ! -d "$CACHE_DIR/scm/$module" ]; then
git clone -q "$src" "$CACHE_DIR/scm/$module"
elif [ -d "$CACHE_DIR/scm/$module/.git" ]; then
if [ "$update" = "yes" ]; then
cd "$CACHE_DIR/scm/$module" || exit 1
if [ "$src" != "$(git config remote.origin.url)" ]; then
git config remote.origin.url "$src"
fi
# Support master branch being renamed to main
git branch --all | grep -q '^[[:space:]]*remotes/origin/main$' && git checkout main
# Update repo and clean out any local inconsistencies
git pull -q || (git fetch && git reset --hard)
else
continue
fi
else
echo -e "${red}ERROR: Ignoring non-git repository${reset}"
continue
fi
elif [[ "$src" =~ .*:// ]]; then
echo -e "${red}ERROR: Don't know how to install '${src}'${reset}"
continue
else
echo -e "${bold}WARNING - attempting UNSAFE installation/upgrade of puppet-module ${module} from ${src}${reset}"
if [ ! -d "/etc/puppet/modules/$module" ]; then
puppet module install "$src"
elif [ "$update" = "yes" ]; then
puppet module upgrade "$src"
fi
fi
done
)


[shfmt] reported by reviewdog 🐶

# Second pass to verify the signatures on all modules and stage those that
# have good signatures.
grep -h -E -v "^#" "${files[@]}" | sort | (
while read -r module src update pattern; do
# We only support git://, file:/// and https:// urls at the moment
if [ "${src:0:6}" = "git://" ] || [ "${src:0:8}" = "file:///" ] || [ "${src:0:8}" = "https://" ]; then
# Verify git tag
cd "$CACHE_DIR/scm/$module" || exit 1
TAG=$(git tag -l "${pattern:-*}" | sort | tail -1)
if [ "$COSMOS_VERBOSE" = "y" ]; then
echo -e "Checking signature on puppet-module:tag ${bold}${module}:${TAG}${reset}"
fi
if [ -z "$TAG" ]; then
echo -e "${red}ERROR: No git tag found for pattern '${pattern:-*}' on puppet-module ${module}${reset}"
continue
fi
if git tag -v "$TAG" &>/dev/null; then
#if [ "$COSMOS_VERBOSE" = "y" ]; thengg
# # short output on good signature
# git tag -v $TAG 2>&1 | grep "gpg: Good signature"
#fi
# Put archive in staging since tag verified OK
stage_module "$module" "$TAG"
else
echo -e "${red}FAILED signature check on puppet-module ${module}${reset}"
git tag -v "$TAG"
echo ''
fi
fi
done
)


[shfmt] reported by reviewdog 🐶

# Cleanup removed puppet modules from CACHE_DIR
for MODULE in "$CACHE_DIR"/staging/*; do
if ! grep -h -E -q "^$MODULE\s+" "$CONFIG" "$LOCALCONFIG"; then
rm -rf "$CACHE_DIR"/{scm,staging}/"$MODULE"
fi
done


[shfmt] reported by reviewdog 🐶

# Installing verified puppet modules
rsync --archive --delete "$CACHE_DIR/staging/" "$MODULES_DIR/"


[shfmt] reported by reviewdog 🐶

args+=('--verbose')
args+=('--show_diff')


[shfmt] reported by reviewdog 🐶

args+=('--logdest=syslog')


[shfmt] reported by reviewdog 🐶

find /etc/puppet/manifests -name \*.pp | while read -r m; do
test "$COSMOS_VERBOSE" = "y" && echo "$0: Applying Puppet manifest $m"
puppet apply "${args[@]}" "$m"
done


[shfmt] reported by reviewdog 🐶

PUPPET_REPORTS_DIR='/var/lib/puppet/reports'
if [ -d "${PUPPET_REPORTS_DIR}" ]; then
find "${PUPPET_REPORTS_DIR}" -type f -mtime +10 -print0 | xargs -0 rm -f
fi


[shfmt] reported by reviewdog 🐶

apt-get -qq update
apt-get -qq -y autoremove


[shfmt] reported by reviewdog 🐶

# May contain ALLOW_REBOOT_AT=
# Eg. ALLOW_REBOOT_AT=06
# shellcheck source=/dev/null
. /etc/cosmos-automatic-reboot


[shfmt] reported by reviewdog 🐶

if [ -n "${ALLOW_REBOOT_AT}" ]; then
if [ "${ALLOW_REBOOT_AT}" != "$(date +%H)" ]; then
echo "Scheduled to reboot at ${ALLOW_REBOOT_AT}"
exit
fi
fi


[shfmt] reported by reviewdog 🐶

if [[ $HOSTNAME =~ -tug- ]]; then
# Reboot hosts in site TUG with 15 seconds delay (enough to manually
# cancel the reboot if logged in and seeing the 'emerg' message broadcasted to console)
sleep=15
elif [[ $HOSTNAME =~ -fre- ]]; then
# reboot hosts in site FRE with 15+180 to 15+180+180 seconds delay
sleep=$((180 + (RANDOM % 180)))
elif [[ $HOSTNAME =~ -lla- ]]; then
# reboot hosts in site LLA with 15+180+180 to 15+180+180+180 seconds delay
sleep=$((375 + (RANDOM % 180)))
else
# reboot hosts in any other site with 15 to 315 seconds delay
sleep=$((15 + (RANDOM % 300)))
fi


[shfmt] reported by reviewdog 🐶

logger -p local0.emerg -i -t cosmos-automatic-reboot "Rebooting automatically in $sleep seconds (if /var/run/reboot-required still exists)"
sleep $sleep
if [ -f /var/run/reboot-required ]; then
logger -p local0.crit -i -t cosmos-automatic-reboot "Rebooting automatically"
# Signal to run-cosmos
touch /var/run/cosmos-reboot-in-progress
reboot
fi


[shfmt] reported by reviewdog 🐶

test -z "$COSMOS_VERBOSE" || echo "$self: COSMOS_BASE was not found. Aborting change of permissions."
exit 0


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

test -z "$COSMOS_VERBOSE" || echo "$self: overlay is a no-op"
exit 0


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

chown ${args} root:root "$MODEL_OVERLAY"/root
chmod ${args} 0700 "$MODEL_OVERLAY"/root


[shfmt] reported by reviewdog 🐶

chown ${args} -R root:root "$MODEL_OVERLAY"/root/.ssh
chmod ${args} 0700 "$MODEL_OVERLAY"/root/.ssh


[shfmt] reported by reviewdog 🐶

apt-get -y install vim traceroute tcpdump molly-guard less rsync git-core unattended-upgrades
update-alternatives --set editor /usr/bin/vim.basic


[shfmt] reported by reviewdog 🐶

mkdir -p "$(dirname "$stamp")"
touch "$stamp"


[shfmt] reported by reviewdog 🐶

apt-get update
apt-get -y install puppet
# shellcheck source=/dev/null
. /etc/os-release


[shfmt] reported by reviewdog 🐶

# Note: in posix shell, string comparison is done with a single =
if [ "${ID}" = "debian" ] && [ "${VERSION_ID}" -ge 12 ] || ([ "${ID}" = "ubuntu" ] && dpkg --compare-versions "${VERSION_ID}" ge 24.04); then
apt-get -y install \
cron \
puppet-module-camptocamp-augeas \
puppet-module-puppetlabs-apt \
puppet-module-puppetlabs-concat \
puppet-module-puppetlabs-cron-core \
puppet-module-puppetlabs-stdlib \
puppet-module-puppetlabs-vcsrepo


[shfmt] reported by reviewdog 🐶


[shfmt] reported by reviewdog 🐶

mkdir -p "$(dirname "${stamp}")"
touch "${stamp}"


[shfmt] reported by reviewdog 🐶

apt-get update
# If we don't install emacs before yaml-mode the default emacs package
# will be emacs-gtk which brings x11 with friends which we don't need.
apt-get -y install emacs-nox
apt-get -y install hiera-eyaml yaml-mode


[shfmt] reported by reviewdog 🐶

# hiera-eyaml wants a certificate and public key, not just a public key oddly enough
echo "$0: Generating eyaml key in ${EYAMLDIR} - this might take a while..."
mkdir -p /etc/hiera/eyaml
openssl req -x509 -newkey rsa:4096 -keyout ${EYAMLDIR}/private_key.pkcs7.pem \
-out ${EYAMLDIR}/public_certkey.pkcs7.pem -days 3653 -nodes -sha256 \
-subj "/C=SE/O=SUNET/OU=EYAML/CN=$(hostname)"
rm -f ${EYAMLDIR}/public_key.pkcs7.pem # cleanup


[shfmt] reported by reviewdog 🐶

apt-get update
apt-get -y install ruby-gpgme


[shfmt] reported by reviewdog 🐶

if [ "$1" != "--force" ]; then
echo ""
echo "Automatic Hiera-GPG key generation DISABLED (to not block on missing entropy)"
echo ""
echo " Run \`$0 --force' manually"
echo ""
exit 0
fi


[shfmt] reported by reviewdog 🐶

if [ ! -f /usr/bin/gpg2 ]; then
apt-get update
apt-get -y install gnupg2
fi


[shfmt] reported by reviewdog 🐶

mkdir -p $GNUPGHOME
chmod 700 $GNUPGHOME


[shfmt] reported by reviewdog 🐶

TMPFILE=$(mktemp /tmp/hiera-gpg.XXXXXX)
cat >"$TMPFILE" <<EOF


[shfmt] reported by reviewdog 🐶

gpg2 --batch --gen-key "$TMPFILE"
rm -f "$TMPFILE"


[shfmt] reported by reviewdog 🐶

echo "Usage: $0 fqdn"
exit 1


[shfmt] reported by reviewdog 🐶

echo "$0: No host-directory for '$HOSTNAME' found - execute in top-level cosmos dir"
exit 1


[shfmt] reported by reviewdog 🐶

echo "Copying files to host..."
rsync -av --exclude '*~' global/overlay/etc/puppet/cosmos-rules.yaml root@"$HOSTNAME":/etc/puppet/cosmos-rules.yaml
rsync -av --exclude '*~' global/overlay/etc/puppet/manifests/cosmos-site.pp root@"$HOSTNAME":/etc/puppet/manifests/cosmos-site.pp
rsync -av --exclude '*~' global/overlay/etc/hiera/data/common.yaml root@"$HOSTNAME":/etc/hiera/data/common.yaml


[shfmt] reported by reviewdog 🐶

# Test if the user has symlinked puppet-sunet correctly
# by first checking if the link exits and then whether
# or not the directory contains any files.
if [ -L global/overlay/etc/puppet/cosmos-modules/sunet ] &&
[ -n "$(ls -A global/overlay/etc/puppet/cosmos-modules/sunet/*)" ]; then
rsync -av --delete --exclude '*~' global/overlay/etc/puppet/cosmos-modules/sunet/* root@"$HOSTNAME":/etc/puppet/cosmos-modules/sunet/.
fi


[shfmt] reported by reviewdog 🐶

echo "Running puppet apply..."
ssh root@"$HOSTNAME" /usr/bin/puppet apply "${PUPPET_ARGS[@]}" /etc/puppet/manifests/cosmos-site.pp


[shfmt] reported by reviewdog 🐶

echo "Cosmos or puppet already running. Exiting."
exit 1


[shfmt] reported by reviewdog 🐶

echo "Please specify a cloud image host that the script should do the following on:"
echo " #1 enable root-login"
echo " #2 remove the default user"
echo " #3 run apt-get update and dist-upgrade without interaction"
echo " #4 reboot to start using the new kernel, updated packages etc."
exit 1


[shfmt] reported by reviewdog 🐶

proxyjump+=("-o")
proxyjump+=("ProxyJump=${ssh_proxy}")


[shfmt] reported by reviewdog 🐶

echo "Please specify a cloud image host that the script should do the following on:"
echo " #1 enable root-login"
echo " #2 remove the default user"
echo " #3 run apt-get update and dist-upgrade without interaction"
echo " #4 reboot to start using the new kernel, updated packages etc."
exit 1


[shfmt] reported by reviewdog 🐶

proxyjump+=("-o")
proxyjump+=("ProxyJump=${ssh_proxy}")

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

shfmt

[shfmt] reported by reviewdog 🐶

if [ "$1" != "--force" ]; then
echo ""
echo "Automatic Hiera-GPG key generation DISABLED (to not block on missing entropy)"
echo ""
echo " Run \`$0 --force' manually"
echo ""
exit 0
fi


[shfmt] reported by reviewdog 🐶

if [ ! -f /usr/bin/gpg2 ]; then
apt-get update
apt-get -y install gnupg2
fi


[shfmt] reported by reviewdog 🐶

mkdir -p $GNUPGHOME
chmod 700 $GNUPGHOME


[shfmt] reported by reviewdog 🐶

TMPFILE=$(mktemp /tmp/hiera-gpg.XXXXXX)
cat >"$TMPFILE" <<EOF


[shfmt] reported by reviewdog 🐶

gpg2 --batch --gen-key "$TMPFILE"
rm -f "$TMPFILE"


[shfmt] reported by reviewdog 🐶

echo "Usage: $0 fqdn"
exit 1


[shfmt] reported by reviewdog 🐶

echo "$0: No host-directory for '$HOSTNAME' found - execute in top-level cosmos dir"
exit 1


[shfmt] reported by reviewdog 🐶

echo "Copying files to host..."
rsync -av --exclude '*~' global/overlay/etc/puppet/cosmos-rules.yaml root@"$HOSTNAME":/etc/puppet/cosmos-rules.yaml
rsync -av --exclude '*~' global/overlay/etc/puppet/manifests/cosmos-site.pp root@"$HOSTNAME":/etc/puppet/manifests/cosmos-site.pp
rsync -av --exclude '*~' global/overlay/etc/hiera/data/common.yaml root@"$HOSTNAME":/etc/hiera/data/common.yaml


[shfmt] reported by reviewdog 🐶

# Test if the user has symlinked puppet-sunet correctly
# by first checking if the link exits and then whether
# or not the directory contains any files.
if [ -L global/overlay/etc/puppet/cosmos-modules/sunet ] &&
[ -n "$(ls -A global/overlay/etc/puppet/cosmos-modules/sunet/*)" ]; then
rsync -av --delete --exclude '*~' global/overlay/etc/puppet/cosmos-modules/sunet/* root@"$HOSTNAME":/etc/puppet/cosmos-modules/sunet/.
fi


[shfmt] reported by reviewdog 🐶

echo "Running puppet apply..."
ssh root@"$HOSTNAME" /usr/bin/puppet apply "${PUPPET_ARGS[@]}" /etc/puppet/manifests/cosmos-site.pp


[shfmt] reported by reviewdog 🐶

echo "Cosmos or puppet already running. Exiting."
exit 1


[shfmt] reported by reviewdog 🐶

echo "Please specify a cloud image host that the script should do the following on:"
echo " #1 enable root-login"
echo " #2 remove the default user"
echo " #3 run apt-get update and dist-upgrade without interaction"
echo " #4 reboot to start using the new kernel, updated packages etc."
exit 1


[shfmt] reported by reviewdog 🐶

proxyjump+=("-o")
proxyjump+=("ProxyJump=${ssh_proxy}")


[shfmt] reported by reviewdog 🐶

echo "Please specify a cloud image host that the script should do the following on:"
echo " #1 enable root-login"
echo " #2 remove the default user"
echo " #3 run apt-get update and dist-upgrade without interaction"
echo " #4 reboot to start using the new kernel, updated packages etc."
exit 1


[shfmt] reported by reviewdog 🐶

proxyjump+=("-o")
proxyjump+=("ProxyJump=${ssh_proxy}")

@sonarqubecloud
Copy link

@theseal
Copy link
Author

theseal commented Nov 27, 2025

Closing to get rid of some errors.

@theseal theseal closed this Nov 27, 2025
@theseal
Copy link
Author

theseal commented Nov 27, 2025

Continues in #70.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants