]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Obviously finished bin/get-k8s-master-configs
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 28 May 2019 14:25:36 +0000 (16:25 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 28 May 2019 14:25:36 +0000 (16:25 +0200)
bin/backup-pgsql.sh
bin/get-k8s-master-configs

index 195ae0ff6ac6ae458b140551bee4e2935e394cc2..20f00ee802ee48c114ec8a40f3b2861e7ee573ff 100755 (executable)
@@ -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
 }
 
index 279d79350aff55bb25db327ab9d4394554a14312..9a915edd7943117cbc806904c9769ae481f8a8c3 100755 (executable)
@@ -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
 
 }