DEBUG="n"
QUIET='n'
-VERSION="0.2"
+VERSION="1.0"
# console colors:
RED=""
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=()
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() {
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}'."
+
}
################################################################################
get_options "$@"
get_config 'live'
get_config 'stage'
+ cleanup_tmp_file
}