From 5fd99d6173bf7bef30482d2a15bc48d4ab26a413 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 28 May 2019 16:25:36 +0200 Subject: [PATCH] Obviously finished bin/get-k8s-master-configs --- bin/backup-pgsql.sh | 8 ++- bin/get-k8s-master-configs | 114 ++++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 6 deletions(-) diff --git a/bin/backup-pgsql.sh b/bin/backup-pgsql.sh index 195ae0f..20f00ee 100755 --- a/bin/backup-pgsql.sh +++ b/bin/backup-pgsql.sh @@ -42,7 +42,7 @@ VERBOSE="n" DEBUG="n" QUIET='n' -VERSION="3.1" +VERSION="3.2" # console colors: RED="" @@ -395,8 +395,10 @@ get_databases() { #------------------------------------------------------------------------------ cleanup_tmp_dir() { if [[ -n "${TMP_DIR}" ]] ; then - debug "Removing temporary directory is '${TMP_DIR}' ..." - RM --force --recursive "${TMP_DIR}" + if [[ -e "${TMP_DIR}" ]] ; then + debug "Removing temporary directory '${TMP_DIR}' ..." + RM --force --recursive "${TMP_DIR}" + fi fi } diff --git a/bin/get-k8s-master-configs b/bin/get-k8s-master-configs index 279d793..9a915ed 100755 --- a/bin/get-k8s-master-configs +++ b/bin/get-k8s-master-configs @@ -7,7 +7,7 @@ VERBOSE="n" DEBUG="n" QUIET='n' -VERSION="0.2" +VERSION="1.0" # console colors: RED="" @@ -22,7 +22,11 @@ BASENAME="$(basename ${0})" BASE_DIR="$(dirname ${0})" REL_K8S_CFGDIR='.kube' -REL_K8S_CFGFILE='config.yaml' +REL_K8S_CFGFILE='config' + +TIMEOUT=30 + +TEMPFILE= declare -A ENV_HOST=() declare -A ENV_PORT=() @@ -277,6 +281,52 @@ CHGRP() { eval ${cmd} "$@" } +#------------------------------------------------------------------------------ +RM() { + local cmd="rm" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd+=" --verbose" + fi + eval ${cmd} "$@" +} + +#------------------------------------------------------------------------------ +CP() { + local cmd="cp" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd+=" --verbose" + fi + eval ${cmd} "$@" +} + +#------------------------------------------------------------------------------ +cleanup_tmp_file() { + if [[ -n "${TEMPFILE}" ]] ; then + if [[ -e "${TEMPFILE}" ]] ; then + debug "Removing temporary file '${TEMPFILE}' ..." + RM --force "${TEMPFILE}" + fi + fi +} + +#------------------------------------------------------------------------------ +do_backup() { + + local my_user_home="$1" + local k8s_dir="${my_user_home}/${REL_K8S_CFGDIR}" + local k8s_cfg_file="${k8s_dir}/${REL_K8S_CFGFILE}" + + if [[ ! -f "${k8s_cfg_file}" ]] ; then + debug "File '${k8s_cfg_file}' not found for backup" + return 0 + fi + + local backup_file="${k8s_cfg_file}.$( date -r "${k8s_cfg_file}" +'%Y-%m-%d_%H:%M:%S' )" + info "Copying '${k8s_cfg_file}' => '${backup_file}' ..." + CP -p "${k8s_cfg_file}" "${backup_file}" + +} + #------------------------------------------------------------------------------ check_home_dirs() { @@ -360,9 +410,66 @@ get_config() { if check_home_dirs "${user_home}" "${user}" "${group}" ; then : else - continue + return 0 + fi + + TEMPFILE=$( mktemp ) + debug "Temporary file is '${TEMPFILE}'." + trap cleanup_tmp_file INT TERM EXIT ABRT + + debug "Get '${url}' ..." + cmd="curl -o \"${TEMPFILE}\" --silent --max-time \"${TIMEOUT}\" \"${url}\"" + debug "Executing: ${cmd}" + eval ${cmd} + if [[ "${VERBOSE}" == "y" ]] ; then + ls -l "${TEMPFILE}" fi + if [[ ! -s "${TEMPFILE}" ]] ; then + error "Got an empty configuration from '${url}' ..." + trap - INT TERM EXIT ABRT + cleanup_tmp_file + return 0 + fi + + local k8s_dir="${user_home}/${REL_K8S_CFGDIR}" + local k8s_cfg_file="${k8s_dir}/${REL_K8S_CFGFILE}" + + if diff --ignore-tab-expansion --ignore-trailing-space --ignore-blank-lines \ + --text "${k8s_cfg_file}" "${TEMPFILE}" >/dev/null ; then + info "Kubernetes configuration '${GREEN}${k8s_cfg_file}${NORMAL}' will be left unchanged." + else + warn "Installing new Kubernetes configuration '${YELLOW}${k8s_cfg_file}${NORMAL}' ..." + do_backup "${user_home}" + CP -p "${TEMPFILE}" "${k8s_cfg_file}" + + fi + + trap - INT TERM EXIT ABRT + cleanup_tmp_file + + local file_owner=$( stat --printf="%U" "${k8s_cfg_file}" ) + local file_group=$( stat --printf="%G" "${k8s_cfg_file}" ) + local file_mode=$( stat --printf="%a" "${k8s_cfg_file}" ) + debug "File '${k8s_cfg_file}' current: owner='${file_owner}', group='${file_group}', mode='${file_mode}'" + + if [[ "${file_owner}" != "${user}" ]] ; then + info "Setting owner of '${k8s_cfg_file}' to '${user}'." + CHOWN "${user}" "${k8s_cfg_file}" + fi + + if [[ "${file_group}" != "${group}" ]] ; then + info "Setting group of '${k8s_cfg_file}' to '${group}'." + CHGRP "${group}" "${k8s_cfg_file}" + fi + + if [[ "${file_mode}" != "600" ]] ; then + info "Setting mode of '${k8s_cfg_file}' to 0600." + CHMOD "0600" "${k8s_cfg_file}" + fi + + debug "Finished environment '${env}'." + } ################################################################################ @@ -377,6 +484,7 @@ main() { get_options "$@" get_config 'live' get_config 'stage' + cleanup_tmp_file } -- 2.39.5