From 87e49a541f5930b0048bb10654576baf954bae05 Mon Sep 17 00:00:00 2001 From: Leif Johansson Date: Wed, 20 Apr 2016 15:07:37 +0200 Subject: [PATCH 1/9] safe update & upgrade --- global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index 613b74e..fe41456 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -22,7 +22,7 @@ fi set -x -apt-get -y install rsync git-core wget +apt-get -y update && apt-get -y upgrade && apt-get -y install rsync git git-core wget dpkg -i cosmos_1.5-1_all.deb if ! test -d /var/cache/cosmos/repo; then From 7f0c457a3368faec9401a10714f061d36cdb5f4e Mon Sep 17 00:00:00 2001 From: Leif Johansson Date: Wed, 20 Apr 2016 15:37:25 +0200 Subject: [PATCH 2/9] try very hard to find git --- global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index fe41456..d3f219a 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -22,7 +22,10 @@ fi set -x -apt-get -y update && apt-get -y upgrade && apt-get -y install rsync git git-core wget +apt-get -y update && apt-get -y upgrade +for pkg in rsync git git-core wget; do + apt-get -y install $pkg +done dpkg -i cosmos_1.5-1_all.deb if ! test -d /var/cache/cosmos/repo; then From 378dfe04fa4f24743665bd840cea85250e6249b8 Mon Sep 17 00:00:00 2001 From: Leif Johansson Date: Wed, 20 Apr 2016 16:44:14 +0200 Subject: [PATCH 3/9] try very hard to find git --- global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index d3f219a..88bd549 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -1,6 +1,7 @@ #!/bin/sh -set -e +#set -e +# not all breakage is un-recoverable... cmd_hostname="$1" if test -z "$cmd_hostname"; then @@ -20,9 +21,8 @@ if test -z "$cmd_tags"; then exit 3 fi -set -x - -apt-get -y update && apt-get -y upgrade +apt-get -y update +apt-get -y upgrade for pkg in rsync git git-core wget; do apt-get -y install $pkg done From 19304f2d79b05edc114f8863831d9c7ec0d289df Mon Sep 17 00:00:00 2001 From: Leif Johansson Date: Wed, 20 Apr 2016 17:29:04 +0200 Subject: [PATCH 4/9] short hostname i /etc/hosts --- global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index 88bd549..1534dc5 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -33,6 +33,8 @@ if ! test -d /var/cache/cosmos/repo; then fi hostname $cmd_hostname +short=`echo ${cmd_hostname} | awk -F. '{print $1}'` +echo "127.0.1.1 ${cmd_hostname} ${short}" >> /etc/hosts perl -pi -e "s,#COSMOS_REPO_MODELS=.*,COSMOS_REPO_MODELS=\"\\\$COSMOS_REPO/global/:\\\$COSMOS_REPO/$cmd_hostname/\"," /etc/cosmos/cosmos.conf perl -pi -e "s,#COSMOS_UPDATE_VERIFY_GIT_TAG_PATTERN=.*,COSMOS_UPDATE_VERIFY_GIT_TAG_PATTERN=\"${cmd_tags}*\"," /etc/cosmos/cosmos.conf From b2272d409fd6a15e71adfb7af30ccc061f6c53e1 Mon Sep 17 00:00:00 2001 From: Fredrik Thulin Date: Fri, 16 Feb 2018 14:40:35 +0100 Subject: [PATCH 5/9] free-hand updates from eduid-ops --- .../etc/cosmos/apt/bootstrap-cosmos.sh | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index 1534dc5..a93de05 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -21,6 +21,14 @@ if test -z "$cmd_tags"; then exit 3 fi +set -x + + +# cloud-init runs with LANG='US-ASCII' which is likely to fail because of non-US-ASCII chars in the manifest +export LANG='en_US.UTF-8' + +export DEBIAN_FRONTEND='noninteractive' + apt-get -y update apt-get -y upgrade for pkg in rsync git git-core wget; do @@ -32,16 +40,40 @@ if ! test -d /var/cache/cosmos/repo; then cosmos clone "$cmd_repo" fi +# re-run cosmos at reboot until it succeeds - use bash -l to get working proxy settings +grep -v "^exit 0" /etc/rc.local > /etc/rc.local.new +(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" +) >> /etc/rc.local.new +mv -f /etc/rc.local.new /etc/rc.local + +touch /etc/run-cosmos-at-boot + hostname $cmd_hostname short=`echo ${cmd_hostname} | awk -F. '{print $1}'` echo "127.0.1.1 ${cmd_hostname} ${short}" >> /etc/hosts -perl -pi -e "s,#COSMOS_REPO_MODELS=.*,COSMOS_REPO_MODELS=\"\\\$COSMOS_REPO/global/:\\\$COSMOS_REPO/$cmd_hostname/\"," /etc/cosmos/cosmos.conf +# Set up cosmos models. They are in the order of most significant first, so we want +# +_host_type=`echo $cmd_hostname | cut -d - -f 1` +models=$( + echo -n '\\$COSMOS_REPO/'"$cmd_hostname/:" + test -d /var/cache/cosmos/repo/${_host_type}-common && echo -n '\\$COSMOS_REPO/'"${_host_type}-common/:" + echo -n '\\$COSMOS_REPO/global/' +) +echo "Configuring cosmos with the following models:" +echo "${models}" + +perl -pi -e "s,#COSMOS_REPO_MODELS=.*,COSMOS_REPO_MODELS=\"${models}\"," /etc/cosmos/cosmos.conf perl -pi -e "s,#COSMOS_UPDATE_VERIFY_GIT_TAG_PATTERN=.*,COSMOS_UPDATE_VERIFY_GIT_TAG_PATTERN=\"${cmd_tags}*\"," /etc/cosmos/cosmos.conf env COSMOS_BASE=/var/cache/cosmos COSMOS_KEYS=/var/cache/cosmos/repo/global/overlay/etc/cosmos/keys /var/cache/cosmos/repo/global/post-tasks.d/015cosmos-trust -(date; nohup cosmos -v update && nohup cosmos -v apply; date) 2>&1 | tee /var/log/cosmos.log +mkdir -p /var/cache/scriptherder + +(date; nohup cosmos -v update && nohup cosmos -v apply && rm /etc/run-cosmos-at-boot; date) 2>&1 | tee /var/log/cosmos.log exit 0 From 3b80ba32c71d071140dc702f9a3a835114f3cd6f Mon Sep 17 00:00:00 2001 From: John Van de Meulebrouck Brendgard Date: Wed, 13 Feb 2019 19:07:04 +0100 Subject: [PATCH 6/9] Set manage_etc_hosts to false for cloudimage based hosts this is needed so that our changes in /etc/hosts are not overwritten. --- global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index a93de05..c1ccf8c 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -51,6 +51,12 @@ mv -f /etc/rc.local.new /etc/rc.local touch /etc/run-cosmos-at-boot +# If this cloud-config is set, it will interfere with our changes to /etc/hosts +grep -q 'manage_etc_hosts: true' /etc/cloud/cloud.cfg +if [ ${?} -eq 0 ]; then + sed -i 's/manage_etc_hosts: true/manage_etc_hosts: false/g' /etc/cloud/cloud.cfg +fi + hostname $cmd_hostname short=`echo ${cmd_hostname} | awk -F. '{print $1}'` echo "127.0.1.1 ${cmd_hostname} ${short}" >> /etc/hosts From 0692cabba3dc3a8306d1afcb3ee9b46034cd7cb9 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Wed, 20 Feb 2019 11:39:38 +0100 Subject: [PATCH 7/9] Remove that '.novalocal' line in /etc/hosts, added by cloud-init It messes up `hostname -f` on Debian, even if there's a correct line further down in /etc/hosts. --- global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index c1ccf8c..24369bb 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -57,6 +57,9 @@ if [ ${?} -eq 0 ]; then sed -i 's/manage_etc_hosts: true/manage_etc_hosts: false/g' /etc/cloud/cloud.cfg fi +# Remove potential $hostname.novalocal line from /etc/hosts, added by cloud-init +sed -i.bak -e "s/^127\.0\.1\.1 $(hostname)\..*novalocal.*//1" /etc/hosts + hostname $cmd_hostname short=`echo ${cmd_hostname} | awk -F. '{print $1}'` echo "127.0.1.1 ${cmd_hostname} ${short}" >> /etc/hosts From c55e5535a2428ffab5ac7b959ebae15556fb96f0 Mon Sep 17 00:00:00 2001 From: Patrik Lundin Date: Mon, 10 Oct 2022 16:06:16 +0200 Subject: [PATCH 8/9] Add gpg to cosmos bootstrap script Without this Debian 11 fails to bootstrap: ``` /etc/cosmos/gpg.d/50gpg: 36: gpg: not found ``` --- global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index 24369bb..d3c4b80 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -31,7 +31,7 @@ export DEBIAN_FRONTEND='noninteractive' apt-get -y update apt-get -y upgrade -for pkg in rsync git git-core wget; do +for pkg in rsync git git-core wget gpg; do apt-get -y install $pkg done dpkg -i cosmos_1.5-1_all.deb From 020b8fe34c159d1d5f427bcd44d82c07c811ba3f Mon Sep 17 00:00:00 2001 From: Patrik Lundin Date: Wed, 12 Oct 2022 16:26:20 +0200 Subject: [PATCH 9/9] Enable "set -e" again Good idea to fail when unexpected things go wrong. Additional fixes added to the script to not stop where we can expect a non-zero return code. Requested by @fredrikt who also reviewed the patch before going in, thanks! --- .../overlay/etc/cosmos/apt/bootstrap-cosmos.sh | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh index d3c4b80..1426904 100755 --- a/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh +++ b/global/overlay/etc/cosmos/apt/bootstrap-cosmos.sh @@ -1,7 +1,6 @@ #!/bin/sh -#set -e -# not all breakage is un-recoverable... +set -e cmd_hostname="$1" if test -z "$cmd_hostname"; then @@ -32,7 +31,9 @@ export DEBIAN_FRONTEND='noninteractive' apt-get -y update apt-get -y upgrade for pkg in rsync git git-core wget gpg; do - apt-get -y install $pkg + # script is running with "set -e", use "|| true" to allow packages to not + # exist without stopping the script + apt-get -y install $pkg || true done dpkg -i cosmos_1.5-1_all.deb @@ -40,8 +41,10 @@ if ! test -d /var/cache/cosmos/repo; then cosmos clone "$cmd_repo" fi -# re-run cosmos at reboot until it succeeds - use bash -l to get working proxy settings -grep -v "^exit 0" /etc/rc.local > /etc/rc.local.new +# Re-run cosmos at reboot until it succeeds - use bash -l to get working proxy settings. +# It is possible the file does not exist or contains no matching lines, +# both cases are OK +grep -v "^exit 0" /etc/rc.local > /etc/rc.local.new || true (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 "" @@ -52,8 +55,7 @@ mv -f /etc/rc.local.new /etc/rc.local touch /etc/run-cosmos-at-boot # If this cloud-config is set, it will interfere with our changes to /etc/hosts -grep -q 'manage_etc_hosts: true' /etc/cloud/cloud.cfg -if [ ${?} -eq 0 ]; then +if [ -f /etc/cloud/cloud.cfg ]; then sed -i 's/manage_etc_hosts: true/manage_etc_hosts: false/g' /etc/cloud/cloud.cfg fi