Skip to content

Commit 7f393b2

Browse files
authored
Refactor create-secrets.ssh to append missing secrets instead of failing if kubesecrets exists (#1431)
1 parent 14d7fa0 commit 7f393b2

File tree

1 file changed

+72
-13
lines changed

1 file changed

+72
-13
lines changed

bin/create-secrets.sh

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ generate_password() {
3636
< /dev/urandom tr -dc _A-Za-z0-9 | head -c${1:-32}
3737
}
3838

39+
backup_suffix="$(date +%Y%m%d%H%M%S)"
40+
for ssh_key_file in nova_ssh_key nova_ssh_key.pub manila_ssh_key manila_ssh_key.pub; do
41+
if [[ -f "${ssh_key_file}" ]]; then
42+
mv "${ssh_key_file}" "${ssh_key_file}.bak.${backup_suffix}"
43+
echo "Moved existing ${ssh_key_file} to ${ssh_key_file}.bak.${backup_suffix}"
44+
fi
45+
done
46+
3947
mariadb_root_password=$(generate_password 32)
4048
mariadb_password=$(generate_password 32)
4149
keystone_rabbitmq_password=$(generate_password 64)
@@ -129,16 +137,23 @@ zaqar_admin_password=$(generate_password 32)
129137
zaqar_keystone_test_password=$(generate_password 32)
130138

131139
OUTPUT_FILE="/etc/genestack/kubesecrets.yaml"
140+
GENERATED_FILE=$(mktemp)
141+
EXISTING_NAMES_FILE=$(mktemp)
142+
MISSING_SECRETS_FILE=$(mktemp)
132143

133-
if [[ -f ${OUTPUT_FILE} ]]; then
134-
echo "Error: ${OUTPUT_FILE} already exists. Please remove it before running this script."
135-
echo " This will replace an existing file and will lead to mass rotation, which is"
136-
echo " likely not what you want to do. If you really want to break your system, please"
137-
echo " make sure you know what you're doing."
138-
exit 99
144+
if [[ -f "${OUTPUT_FILE}" ]]; then
145+
cp "${OUTPUT_FILE}" "${OUTPUT_FILE}.bak.${backup_suffix}"
146+
echo "Backed up existing ${OUTPUT_FILE} to ${OUTPUT_FILE}.bak.${backup_suffix}"
139147
fi
140148

141-
cat <<EOF > $OUTPUT_FILE
149+
cleanup() {
150+
rm -f nova_ssh_key nova_ssh_key.pub
151+
rm -f manila_ssh_key manila_ssh_key.pub
152+
rm -f "${GENERATED_FILE}" "${EXISTING_NAMES_FILE}" "${MISSING_SECRETS_FILE}"
153+
}
154+
trap cleanup EXIT
155+
156+
cat <<EOF > "${GENERATED_FILE}"
142157
---
143158
apiVersion: v1
144159
kind: Secret
@@ -997,7 +1012,7 @@ EOF
9971012
SKYLINE_SECRETS_FILE="/etc/genestack/skylinesecrets.yaml"
9981013
if [[ -f ${SKYLINE_SECRETS_FILE} ]]; then
9991014
echo "Found existing ${SKYLINE_SECRETS_FILE}, appending skyline secrets..."
1000-
cat ${SKYLINE_SECRETS_FILE} >> ${OUTPUT_FILE}
1015+
cat ${SKYLINE_SECRETS_FILE} >> "${GENERATED_FILE}"
10011016
echo "Skyline secrets appended from ${SKYLINE_SECRETS_FILE}"
10021017
else
10031018
echo "Note: ${SKYLINE_SECRETS_FILE} not found. Run create-skyline-secrets.sh to add skyline secrets."
@@ -1006,7 +1021,7 @@ fi
10061021
# Check if kube-ovn-tls secret exists, and copy to openstack namespace if it does
10071022
if kubectl -n kube-system get secret kube-ovn-tls >/dev/null 2>&1
10081023
then
1009-
cat <<EOF >> $OUTPUT_FILE
1024+
cat <<EOF >> "${GENERATED_FILE}"
10101025
---
10111026
apiVersion: v1
10121027
kind: Secret
@@ -1021,8 +1036,52 @@ data:
10211036
EOF
10221037
fi
10231038

1024-
rm nova_ssh_key nova_ssh_key.pub
1025-
rm manila_ssh_key manila_ssh_key.pub
1026-
chmod 0640 ${OUTPUT_FILE}
1039+
if [[ -f "${OUTPUT_FILE}" ]]; then
1040+
awk '/ name:/ {print $2}' "${OUTPUT_FILE}" | sort -u > "${EXISTING_NAMES_FILE}"
1041+
1042+
awk '
1043+
BEGIN {
1044+
while ((getline < ARGV[1]) > 0) {
1045+
existing[$1] = 1
1046+
}
1047+
ARGV[1] = ""
1048+
}
1049+
/^---$/ {
1050+
if (doc != "") {
1051+
if (name == "" || !(name in existing)) {
1052+
printf "%s", doc
1053+
}
1054+
}
1055+
doc = $0 ORS
1056+
name = ""
1057+
next
1058+
}
1059+
{
1060+
doc = doc $0 ORS
1061+
}
1062+
$1 == "name:" {
1063+
name = $2
1064+
}
1065+
END {
1066+
if (doc != "") {
1067+
if (name == "" || !(name in existing)) {
1068+
printf "%s", doc
1069+
}
1070+
}
1071+
}
1072+
' "${EXISTING_NAMES_FILE}" "${GENERATED_FILE}" > "${MISSING_SECRETS_FILE}"
1073+
1074+
if [[ -s "${MISSING_SECRETS_FILE}" ]]; then
1075+
cat "${MISSING_SECRETS_FILE}" >> "${OUTPUT_FILE}"
1076+
echo "Appended missing secrets to existing ${OUTPUT_FILE}"
1077+
else
1078+
echo "No missing secrets found. ${OUTPUT_FILE} unchanged."
1079+
fi
1080+
else
1081+
mv "${GENERATED_FILE}" "${OUTPUT_FILE}"
1082+
echo "Created new secrets YAML file as ${OUTPUT_FILE}"
1083+
fi
1084+
1085+
chmod 0640 "${OUTPUT_FILE}"
10271086
echo ""
1028-
echo "Secrets YAML file created as ${OUTPUT_FILE}"
1087+
echo "Secrets YAML file is ready at ${OUTPUT_FILE}"

0 commit comments

Comments
 (0)