#!/usr/bin/env bash
-# Copyright (c) 2021-2023 VMware, Inc.
+# Copyright 2021-2023 VMware, Inc.
# SPDX-License-Identifier: Apache-2
## Salt VMware Tools Integration script
# using bash for now
# run this script as root, as needed to run salt
-readonly SCRIPT_VERSION='1.4'
+## readonly SCRIPT_VERSION='SCRIPT_VERSION_REPLACE'
+readonly SCRIPT_VERSION='1.5'
# definitions
readonly salt_name="salt"
readonly repo_json_file="repo.json"
salt_url_version="${default_salt_url_version}"
-base_url="https://repo.saltproject.io/salt/vmware-tools-onedir"
+pre_3006_base_url="https://repo.saltproject.io/salt/vmware-tools-onedir"
+# Release
+post_3005_base_url="https://repo.saltproject.io/salt/py3/onedir"
+base_url=""
# Salt file and directory locations
readonly base_salt_location="/opt/saltstack"
readonly salt_dir="${base_salt_location}/${salt_name}"
-readonly test_exists_file="${salt_dir}/run/run"
-
readonly salt_conf_dir="/etc/salt"
readonly salt_minion_conf_name="minion"
readonly salt_minion_conf_file="${salt_conf_dir}/${salt_minion_conf_name}"
readonly log_dir="/var/log"
+readonly list_files_systemd_to_remove="/lib/systemd/system/salt-minion.service
+/usr/lib/systemd/system/salt-minion.service
+/usr/local/lib/systemd/system/salt-minion.service
+/etc/systemd/system/salt-minion.service
+"
+
readonly list_file_dirs_to_remove="${base_salt_location}
/etc/salt
/var/run/salt
/var/cache/salt
/var/log/salt
/usr/bin/salt-*
-/lib/systemd/system/salt-minion.service
-/usr/lib/systemd/system/salt-minion.service
-/usr/local/lib/systemd/system/salt-minion.service
-/etc/systemd/system/salt-minion.service
+${list_files_systemd_to_remove}
"
## /var/log/vmware-${SCRIPTNAME}-*
"
readonly salt_minion_service_wrapper=\
-"# Copyright (c) 2021-2022 VMware, Inc. All rights reserved.
+"# Copyright 2021-2023 VMware, Inc.
+# SPDX-License-Identifier: Apache-2
[Unit]
Description=The Salt Minion
LimitNOFILE=8192
MemoryLimit=250M
Nice=19
-ExecStart=/opt/saltstack/salt/run/run minion
+ExecStart=/usr/bin/salt-minion
[Install]
WantedBy=multi-user.target
"
+# Onedir detection locations
+readonly onedir_post_3005_location="${salt_dir}/salt-minion"
+readonly onedir_pre_3006_location="${salt_dir}/run/run"
+
+declare -a list_of_onedir_locations_check
+list_of_onedir_locations_check[0]="${onedir_pre_3006_location}"
+list_of_onedir_locations_check[1]="${onedir_post_3005_location}"
## VMware file and directory locations
readonly vmtools_base_dir_etc="/etc/vmware-tools"
# 100 + 3 => installFailed
# 100 + 4 => removing
# 100 + 5 => removeFailed
+# 100 + 6 => externalInstall
# 126 => scriptFailed
# 130 => scriptTerminated
declare -A STATUS_CODES_ARY
STATUS_CODES_ARY[installFailed]=103
STATUS_CODES_ARY[removing]=104
STATUS_CODES_ARY[removeFailed]=105
+STATUS_CODES_ARY[externalInstall]=106
STATUS_CODES_ARY[scriptFailed]=126
STATUS_CODES_ARY[scriptTerminated]=130
SOURCE_FLAG=0
SOURCE_PARAMS=""
+# Flag for pre_3006 and post_3005, 0 => pre_3006, 1 => post_3005
+POST_3005_FLAG=0
+POST_3005_MAJOR_VER_FLAG=0
+
+
# helper functions
_timestamp() {
# work functions
#
-# _cleanup
+# _cleanup_int
#
# Cleanups any running process and areas on control-C
#
# Exits with hard-coded value 130
#
-_cleanup() {
+_cleanup_int() {
+ rm -rf "$WORK_DIR"
+ _debug_log "$0:${FUNCNAME[0]} Deleted temp working directory $WORK_DIR"
+
exit ${STATUS_CODES_ARY[scriptTerminated]}
}
-trap _cleanup INT
+#
+# _cleanup_exit
+#
+# Cleanups any running process and areas on exit
+#
+_cleanup_exit() {
+ rm -rf "$WORK_DIR"
+ _debug_log "$0:${FUNCNAME[0]} Deleted temp working directory $WORK_DIR"
+ ## exit ${CURRENT_STATUS}
+}
+
+trap _cleanup_int INT
+trap _cleanup_exit EXIT
# cheap trim relying on echo to convert tabs to spaces and
return 0
}
+#
+# _set_post_3005_flags_from_version
+#
+# Sets the POST_3005_FLAG and POST_3005_MAJOR_VER_FLAG
+# from the version currently present in salt_url_version
+#
+# Will also set base_url if not already defined by --source option
+#
+# Results:
+# Returns with exit code
+#
+_set_post_3005_flags_from_version() {
+ _info_log "$0:${FUNCNAME[0]} setting POST_3005_FLAG and "\
+ "POST_3005_MAJOR_VER_FLAG from salt version '${salt_url_version}'"
+
+ if [[ "latest" = "${salt_url_version}" ]]; then
+ POST_3005_FLAG=1
+ POST_3005_MAJOR_VER_FLAG=1
+ base_url="${post_3005_base_url}"
+ # done, already have url for latest & major versions
+ _debug_log "$0:${FUNCNAME[0]} post-3005 install, using latest "\
+ "base_url '${base_url}'"
+ else
+ ver_chk=$(echo "${salt_url_version}" | cut -d '.' -f 1)
+ if [[ ${ver_chk} -ge 3006 ]]; then
+ POST_3005_FLAG=1
+ ver_chk_major=$(echo "${salt_url_version}" | cut -d '.' -f 1)
+ ver_chk_minor=$(echo "${salt_url_version}" | cut -d '.' -f 2)
+ _debug_log "$0:${FUNCNAME[0]} post-3005 install, checking "\
+ "for major version only '${ver_chk_major}', minor "\
+ "'${ver_chk_minor}'"
+ if [[ "${ver_chk_major}" = "${ver_chk_minor}" ]]; then
+ POST_3005_MAJOR_VER_FLAG=1
+ base_url="${post_3005_base_url}"
+ else
+ base_url="${post_3005_base_url}/minor"
+ fi
+ _debug_log "$0:${FUNCNAME[0]} post-3005 install, for "\
+ "'${salt_url_version}' using base_url '${base_url}'"
+ else
+ # install pre-3006, use older url
+ base_url="${pre_3006_base_url}"
+ _debug_log "$0:${FUNCNAME[0]} pre-3006 install, for "\
+ "'${salt_url_version}' using base_url '${base_url}'"
+ fi
+ fi
+}
+
#
# _update_minion_conf_ary
local var1=""
local var2=""
+ local machine_arch_chk="${MACHINE_ARCH}"
declare -A rdict
_info_log "$0:${FUNCNAME[0]} parsing of repo json file '${file_name}'"
+ if [[ ${POST_3005_FLAG} -eq 0 ]]; then
+ machine_arch_chk="amd64" # pre_3006 used amd64
+ fi
+
file_value=$(<"${file_name}")
# limit 80 cols
elif [[ "${line}" = "}" ]]; then
# examine directory just read in
if [[ ${specd_ver_flag} -eq 1 ]]; then
- if [[ "${rdict['os']}" = "linux" ]]; then
+ if [[ "${rdict['os']}" = "linux" \
+ && "${rdict['arch']}" = "${machine_arch_chk}" ]]; then
# have linux values for specd_ver
_debug_log "$0:${FUNCNAME[0]} parsed following linux for"\
"specified version '${salt_url_version}' from repo json"\
if [[ ${blk_count} -eq ${specd_ver_blk_count} ]]; then
specd_ver_flag=0
- break
+ ## break
fi
(( blk_count-- ))
else
- line_key=$(echo "${line}" | cut -d ':' -f 1 | xargs)
- line_value=$(echo "${line}" | cut -d ':' -f 2 | xargs)
- if [[ "${line_key}" = "${salt_url_version}" ]]; then
- # note blk_count encountered 'specd_ver', closing brace check
- specd_ver_flag=1
- specd_ver_blk_count=${blk_count}
- (( specd_ver_blk_count++ ))
+ if [[ ${POST_3005_FLAG} -eq 1 \
+ && ${POST_3005_MAJOR_VER_FLAG} -eq 1 ]]; then
+ # doing major version check
+ line_major_key=$(echo "${line}" | cut -d ':' -f 1 | cut -d '-' -f 2 | cut -d '.' -f 1 |xargs)
+ line_key=$(echo "${line}" | cut -d ':' -f 1 | xargs)
+ line_value=$(echo "${line}" | cut -d ':' -f 2 | xargs)
+ _debug_log "$0:${FUNCNAME[0]} check line_major_key "\
+ "'${line_major_key}' again salt_url_version "\
+ "'${salt_url_version}', line_key '${line_key}', "\
+ "line_value '${line_value}'"
+ if [[ "${line_major_key}" = "${salt_url_version}" ]]; then
+ # blk_count encountered 'specd_ver', closing brace check
+ specd_ver_flag=1
+ specd_ver_blk_count=${blk_count}
+ (( specd_ver_blk_count++ ))
+ _debug_log "$0:${FUNCNAME[0]} found specd version, "\
+ "version '${salt_url_version}' and line_major_key "\
+ "'${line_major_key}', line_key '${line_key}' "\
+ "specd_ver_blk_count '${specd_ver_blk_count}'"
+ else
+ rdict["${line_key}"]="${line_value}"
+ _debug_log "$0:${FUNCNAME[0]} updated dictionary for "\
+ "major version with line_key '${line_key}' and "\
+ "line_value '${line_value}'"
+ fi
else
- rdict["${line_key}"]="${line_value}"
- _debug_log "$0:${FUNCNAME[0]} updated dictionary with"\
- "line_key '${line_key}' and line_value '${line_value}'"
+ line_key=$(echo "${line}" | cut -d ':' -f 1 | xargs)
+ line_value=$(echo "${line}" | cut -d ':' -f 2 | xargs)
+ _debug_log "$0:${FUNCNAME[0]} check line_key '${line_key}' "\
+ "again salt_url_version '${salt_url_version}', "\
+ "line_value '${line_value}'"
+ if [[ "${line_key}" = "${salt_url_version}" ]]; then
+ # blk_count encountered 'specd_ver', closing brace check
+ specd_ver_flag=1
+ specd_ver_blk_count=${blk_count}
+ (( specd_ver_blk_count++ ))
+ _debug_log "$0:${FUNCNAME[0]} found specd version, "\
+ "version '${salt_url_version}' and line_key "\
+ "'${line_key}' and specd_ver_blk_count "\
+ "'${specd_ver_blk_count}'"
+ else
+ rdict["${line_key}"]="${line_value}"
+ _debug_log "$0:${FUNCNAME[0]} updated dictionary with" \
+ "line_key '${line_key}' and line_value '${line_value}'"
+ fi
fi
fi
done <<< "${var2}"
#
# Retrieve the salt-minion from Salt repository
#
+# Note:
+# pre_3006 The repo.json file only existed in one place (salt/onedir)
+# and contained everything in the directory and sub-directories
+# where the repo.json file resides.
+# post_3005 There are two repo.json files:
+# top level (salt/py3/onedir):
+# repo.json contains 'latest' and 'major versions' only.
+# minor level (salt/py3/onedir/minor):
+# repo.json contains 'latest' and 'minor versions' only.
+#
+# With the 3006 release a breaking change in directory structure was introduced
+# to bring comformity with directory structure used for packages.
+#
# Side Effects:
# CURRENT_STATUS updated
#
local local_base_url=""
local local_file_flag=0
+ local local_count_repo_json=0
local salt_tarball=""
local salt_tarball_SHA512=""
local salt_json_sha512=""
local salt_pkg512=""
+ local install_onedir_chk=0
+ local sys_arch=""
+
+ local ver_chk=""
+ local ver_chk_major=""
+ local ver_chk_minor=""
+
_debug_log "$0:${FUNCNAME[0]} retrieve the salt-minion and check"\
"its validity"
CURRENT_STATUS=${STATUS_CODES_ARY[installFailed]}
mkdir -p ${base_salt_location}
- cd ${base_salt_location} || return $?
-
- # curl on Liux doesn't support file:// support
- if echo "${base_url}" | grep -q '^/' ; then
- local_base_url="${base_url}"
- local_file_flag=1
- _debug_log "$0:${FUNCNAME[0]} using source '${local_base_url}'"\
- "from '${base_url}'"
- elif echo "${base_url}" | grep -q '^file://' ; then
- local_base_url="${base_url//file:/}"
- local_file_flag=1
- _debug_log "$0:${FUNCNAME[0]} using source '${local_base_url}'"\
- "from '${base_url}'"
+ ## cd ${base_salt_location} || return $?
+ cd "${WORK_DIR}" || return $?
+
+ # check for pre-3006 or post-3005 and adjust base_url
+ # unless already defined by --source option
+ if [[ -z "${base_url}" ]]; then
+ _debug_log "$0:${FUNCNAME[0]} no source option used, determine "\
+ "version attempting to install, version '${salt_url_version}"
+ _set_post_3005_flags_from_version
else
- _debug_log "$0:${FUNCNAME[0]} using source '${base_url}'"
+ _debug_log "$0:${FUNCNAME[0]} source url provided, need to scan for "\
+ "pre 3006 / post 3005, and local file using base_url '${base_url}'"
+
+ # curl on Linux doesn't support file:// support
+ if echo "${base_url}" | grep -q '^/' ; then
+ local_base_url="${base_url}"
+ local_file_flag=1
+ _debug_log "$0:${FUNCNAME[0]} using source '${local_base_url}'"\
+ "from '${base_url}'"
+ elif echo "${base_url}" | grep -q '^file://' ; then
+ local_base_url="${base_url//file:/}"
+ local_file_flag=1
+ _debug_log "$0:${FUNCNAME[0]} using source '${local_base_url}'"\
+ "from '${base_url}'"
+ else
+ _debug_log "$0:${FUNCNAME[0]} using non-local source '${base_url}'"
+
+ ver_chk=$(echo "${base_url}" | grep 'salt/py3/onedir')
+ if [[ -n "${ver_chk}" ]]; then
+ POST_3005_FLAG=1
+ _set_post_3005_flags_from_version
+ fi
+ fi
fi
- if [[ ${local_file_flag} -ne 0 ]]; then
+ if [[ ${local_file_flag} -eq 1 ]]; then
# local absolute path
# and allow for Linux handling multiple slashes
- salt_url="${local_base_url}/${salt_url_version}"
+
+ # need to determine if pre 3005 or post 3006
+ local_count_repo_json=$(find "${local_base_url}" -name repo.json|wc -l)
+ if [[ ${local_count_repo_json} -eq 2 ]]; then
+ POST_3005_FLAG=1
+ _set_post_3005_flags_from_version
+ else
+ _debug_log "$0:${FUNCNAME[0]} pre-3006 local install, for "\
+ "'${salt_url_version}' using specified source "\
+ "'${local_base_url}'"
+ fi
+
+ if [[ ${POST_3005_FLAG} -eq 1 ]]; then
+ if [[ ${POST_3005_MAJOR_VER_FLAG} -eq 1 ]]; then
+ salt_url="${local_base_url}"
+ else
+ salt_url="${local_base_url}/minor"
+ fi
+ else
+ salt_url="${local_base_url}/${salt_url_version}"
+ fi
if [[ -f "${salt_url}/${repo_json_file}" ]]; then
_debug_log "$0:${FUNCNAME[0]} successfully found file"\
- "'${repo_json_file}' in '${base_url}/${repo_json_file}'"
+ "'${repo_json_file}' in '${salt_url}/${repo_json_file}'"
cp -a "${salt_url}/${repo_json_file}" .
_retn=$?
"'${salt_json_sha512}'"
salt_pkg_name="${salt_json_name}"
- cp -a "${salt_url}/${salt_json_version}/${salt_pkg_name}" .
+ if [[ ${POST_3005_FLAG} -eq 1 \
+ && ${POST_3005_MAJOR_VER_FLAG} -eq 1 ]]; then
+ cp -a "${salt_url}/minor/${salt_json_version}/${salt_pkg_name}" .
+ else
+ cp -a "${salt_url}/${salt_json_version}/${salt_pkg_name}" .
+ fi
_retn=$?
if [[ ${_retn} -ne 0 ]]; then
CURRENT_STATUS=${STATUS_CODES_ARY[installFailed]}
# repo.json file is missing, look for 'latest'
# directory with onedir files and retrieve files from it
salt_url="${local_base_url}/${salt_url_version}"
- salt_tarball="${salt_name}*-linux-amd64.tar.gz"
- salt_tarball_SHA512="${salt_name}*_SHA512"
- ## shellcheck cp -a ${salt_url}/${salt_tarball} .
- cp -a "${salt_url}/${salt_name}"*-linux-amd64.tar.gz .
+ _debug_log "$0:${FUNCNAME[0]} current directory $(pwd)"
+
+ cp -a "${salt_url}/${salt_name}"*-linux-*.tar.?z .
_retn=$?
if [[ ${_retn} -ne 0 ]]; then
CURRENT_STATUS=${STATUS_CODES_ARY[installFailed]}
_error_log "$0:${FUNCNAME[0]} failed to find file" \
- "'${salt_tarball}' in specified location ${salt_url},"\
+ "for Linux in specified location ${salt_url},"\
"error '${_retn}'"
fi
- ## shellcheck cp -a ${salt_url}/${salt_tarball_SHA512} .
+ cp -a "${salt_url}/${salt_name}"*-linux-*.tar.?z.sha512 . 2>/dev/null
cp -a "${salt_url}/${salt_name}"*_SHA512 .
_retn=$?
if [[ ${_retn} -ne 0 ]]; then
CURRENT_STATUS=${STATUS_CODES_ARY[installFailed]}
_error_log "$0:${FUNCNAME[0]} failed to find file" \
- "'${salt_tarball_SHA512}' in specified location ${salt_url},"\
+ "SHA 512 in specified location ${salt_url},"\
"error '${_retn}'"
fi
## shellcheck
- ## salt_pkg_name=$(ls ${salt_tarball})
- ## salt_chksum_file=$(ls ${salt_tarball_SHA512})
- salt_pkg_name=$(ls "${salt_url}/${salt_name}"*-linux-amd64.tar.gz)
- salt_chksum_file=$(ls "${salt_url}/${salt_name}"*_SHA512)
+ salt_chksum_file=$(ls "${salt_name}"*_SHA512)
+ salt_pkg_name=$(ls "${salt_name}"*-linux-amd64.tar.gz 2>/dev/null)
+ if [[ -z "${salt_pkg_name}" ]]; then
+ # failed to find pre-3006 linux tarball,
+ # attempt to find post-3005 with appro. arch
+ sys_arch="${MACHINE_ARCH}"
+ salt_chksum_file=$(ls "${salt_name}"*-linux-"${sys_arch}".tar.xz.sha512)
+ salt_pkg_name=$(ls "${salt_name}"*-linux-"${sys_arch}".tar.xz)
+ fi
_debug_log "$0:${FUNCNAME[0]} successfully copied tarball from"\
- "'${salt_url}' to file '${salt_pkg_name}'"
+ "'${salt_url}' file '${salt_pkg_name}'"
_debug_log "$0:${FUNCNAME[0]} successfully coped checksum from"\
- "'${salt_url}' to file '${salt_chksum_file}'"
+ "'${salt_url}' file '${salt_chksum_file}'"
calc_sha512sum=$(grep "${salt_pkg_name}" \
"${salt_chksum_file}" | sha512sum --check --status)
if [[ ${calc_sha512sum} -ne 0 ]]; then
"'${salt_json_sha512}'"
salt_pkg_name="${salt_json_name}"
- salt_url="${base_url}/${salt_json_version}/${salt_pkg_name}"
+ if [[ ${POST_3005_FLAG} -eq 1 \
+ && ${POST_3005_MAJOR_VER_FLAG} -eq 1 ]]; then
+ salt_url="${base_url}/minor/${salt_json_version}/${salt_pkg_name}"
+ else
+ salt_url="${base_url}/${salt_json_version}/${salt_pkg_name}"
+ fi
_curl_download "${salt_pkg_name}" "${salt_url}"
_debug_log "$0:${FUNCNAME[0]} successfully downloaded from"\
"'${salt_url}' into file '${salt_pkg_name}'"
# repo.json file is missing, look for 'latest'
# directory with onedir files and retrieve files from it
salt_url="${base_url}/${salt_url_version}"
- salt_tarball="${salt_name}*-linux-amd64.tar.gz"
+ salt_tarball="${salt_name}*-linux-*.tar.?z"
salt_tarball_SHA512="${salt_name}*_SHA512"
# assume http://, https:// or similar
fi
## shellcheck
- ## salt_pkg_name=$(ls ${salt_tarball})
- ## salt_chksum_file=$(ls ${salt_tarball_SHA512})
- salt_pkg_name=$(ls "${salt_url}/${salt_name}"*-linux-amd64.tar.gz)
- salt_chksum_file=$(ls "${salt_url}/${salt_name}"*_SHA512)
+ salt_chksum_file=$(ls "${salt_name}"*_SHA512)
+ salt_pkg_name=$(ls "${salt_name}"*-linux-amd64.tar.gz)
+ if [[ -z "${salt_pkg_name}" ]]; then
+ # failed to find pre-3006 linux tarball,
+ # attempt to find post-3005 with appro. arch
+ sys_arch="${MACHINE_ARCH}"
+ salt_chksum_file=$(ls "${salt_name}"*-linux-"${sys_arch}".tar.xz.sha512)
+ salt_pkg_name=$(ls "${salt_name}"*-linux-"${sys_arch}".tar.xz)
+ fi
_debug_log "$0:${FUNCNAME[0]} successfully downloaded tarball"\
"from '${salt_url}' into file '${salt_pkg_name}'"
_debug_log "$0:${FUNCNAME[0]} successfully downloaded checksum"\
fi
_debug_log "$0:${FUNCNAME[0]} sha512sum match was successful"
- tar xzf "${salt_pkg_name}" 1>/dev/null
+ tar xf "${salt_pkg_name}" -C "${base_salt_location}" 1>/dev/null
_retn=$?
if [[ ${_retn} -ne 0 ]]; then
CURRENT_STATUS=${STATUS_CODES_ARY[installFailed]}
- _error_log "$0:${FUNCNAME[0]} tar xzf expansion of downloaded"\
+ _error_log "$0:${FUNCNAME[0]} tar xf expansion of downloaded"\
"file '${salt_pkg_name}' failed, return code '${_retn}'"
fi
- if [[ ! -f ${test_exists_file} ]]; then
+ install_onedir_chk=$(_check_onedir_minion_install)
+ if [[ ${install_onedir_chk} -eq 0 ]]; then
CURRENT_STATUS=${STATUS_CODES_ARY[installFailed]}
_error_log "$0:${FUNCNAME[0]} expansion of downloaded file"\
- "'${salt_url}' failed to provide critical file"\
- "'${test_exists_file}'"
+ "'${salt_url}' failed to provide any onedir installed "\
+ "critical files for salt-minion"
fi
CURRENT_STATUS=${STATUS_CODES_ARY[installed]}
cd "${CURRDIR}" || return $?
# check if more than one version of the script is running
#
# Results:
-# Echos the number of scripts running, allowing for forks etc
-# from bash etc, a single instance of the script returns 3
+# Checks the number of scripts running, allowing for forks etc
+# from bash etc, as root a single instance of the script returns 3
+# as sudo root a single instance of the script returns 4
#
_check_multiple_script_running() {
"bashpid '${BASHPID}', processes found '${procs_found}',"\
"and count '${count}'"
- echo "${count}"
+ if [[ ${count} -gt 4 ]]; then
+ _error_log "$0:${FUNCNAME[0]} failed to check status, " \
+ "multiple versions of the script are running"
+ fi
+
return 0
}
#
-# _check_std_minion_install
+# _check_classic_minion_install
#
-# Check if standard salt-minion is installed for the OS
+# Check if classic salt-minion is installed for the OS
# for example: install salt-minion from rpm or deb package
#
# Results:
# !0 - Standard install found and Salt version found output
#
-_check_std_minion_install() {
+_check_classic_minion_install() {
# checks for /usr/bin, then /usr/local/bin
# this catches 80% to 90% of the reqular cases
for idx in ${list_of_files_check}
do
- if [[ -f "${idx}" ]]; then
+ if [[ -h "${idx}" ]]; then
+ _debug_log "$0:${FUNCNAME[0]} found file '${idx}' "\
+ "symbolic link, post-3005 installation"
+ break
+ elif [[ -f "${idx}" ]]; then
#check size of file, if larger than 200, not script wrapper file
local file_sz=0
file_sz=$(( $(wc -c < "${idx}") ))
}
+#
+# _check_onedir_minion_install
+#
+# Check if onedir pre_3006 or post_3005 salt-minion is installed on the OS
+# for example: install salt-minion from rpm or deb package
+#
+# Results:
+# Echos the following values:
+# 0 - No onedir install found and empty string output
+# 1 - pre_3006 onedir install found
+# 2 - post_3005 onedir install found
+#
+
+_check_onedir_minion_install() {
+
+ # checks for following executables:
+ # post_3005 - /opt/saltstack/salt/salt-minion
+ # pre_3006 - /opt/saltstack/salt/run/run
+
+ local _retn=0
+ local pre_3006=1
+ local post_3005=2
+
+ _info_log "$0:${FUNCNAME[0]} check if standard onedir-minion installed"
+
+ if [[ -f "${list_of_onedir_locations_check[0]}" ]]; then
+ _debug_log "$0:${FUNCNAME[0]} found pre 3006 version of Salt, "\
+ "at location ${list_of_onedir_locations_check[0]}"
+ _retn=${pre_3006}
+ elif [[ -f "${list_of_onedir_locations_check[1]}" ]]; then
+ _debug_log "$0:${FUNCNAME[0]} found post 3005 version of Salt, "\
+ "at location ${list_of_onedir_locations_check[1]}"
+ _retn=${post_3005}
+ else
+ _debug_log "$0:${FUNCNAME[0]} failed to find a ondir installation"
+ fi
+ echo ${_retn}
+}
+
+
#
# _find_salt_pid
#
_find_salt_pid() {
# find the pid for salt-minion if active
local salt_pid=0
- salt_pid=$(pgrep -f "${salt_name}\/run\/run minion" | head -n 1 |
- awk -F " " '{print $1}')
+ if [[ ${POST_3005_FLAG} -eq 1 ]]; then
+ salt_pid=$(pgrep -f "\/usr\/bin\/salt-minion" | head -n 1)
+ else
+ salt_pid=$(pgrep -f "${salt_name}\/run\/run minion" | head -n 1 |
+ awk -F " " '{print $1}')
+ fi
_debug_log "$0:${FUNCNAME[0]} checking for salt-minion process id,"\
"found '${salt_pid}'"
echo "${salt_pid}"
#
-# _create_helper_scripts
+# _create_pre_3006_helper_scripts
#
# Create helper scripts for salt-call and salt-minion
#
-# Example: _create_helper_scripts
+# Example: _create_pre_3006_helper_scripts
#
# Results:
# Exits with 0 or error code
#
-_create_helper_scripts() {
+_create_pre_3006_helper_scripts() {
for idx in ${salt_wrapper_file_list}
do
# 3 => installFailed
# 4 => removing
# 5 => removeFailed
+# 6 => externalInstall
# 126 => scriptFailed
#
# Side Effects:
_status_fn() {
# return status
local _retn_status=${STATUS_CODES_ARY[notInstalled]}
- local script_count=0
+ local install_onedir_chk=0
+ local found_salt_ver=""
_info_log "$0:${FUNCNAME[0]} checking status for script"
- script_count=$(_check_multiple_script_running)
- if [[ ${script_count} -gt 3 ]]; then
- _error_log "$0:${FUNCNAME[0]} failed to check status, " \
- "multiple versions of the script are running"
- fi
- svpid=$(_find_salt_pid)
- if [[ ! -f "${test_exists_file}" && -z ${svpid} ]]; then
- # check not installed and no process id
- CURRENT_STATUS=${STATUS_CODES_ARY[notInstalled]}
- _retn_status=${STATUS_CODES_ARY[notInstalled]}
- elif [[ -f "${test_exists_file}" ]]; then
- # check installed
- CURRENT_STATUS=${STATUS_CODES_ARY[installed]}
- _retn_status=${STATUS_CODES_ARY[installed]}
- # normal case but double-check
- svpid=$(_find_salt_pid)
- if [[ -z ${svpid} ]]; then
- # Note: someone could have stopped the salt-minion,
- # so installed but not running,
- # status codes don't allow for that case
- CURRENT_STATUS=${STATUS_CODES_ARY[installFailed]}
- _retn_status=${STATUS_CODES_ARY[installFailed]}
+ _check_multiple_script_running
+
+ found_salt_ver=$(_check_classic_minion_install)
+ if [[ -n "${found_salt_ver}" ]]; then
+ _debug_log "$0:${FUNCNAME[0]}" \
+ "existing Standard Salt Installation detected,"\
+ "Salt version: '${found_salt_ver}'"
+ CURRENT_STATUS=${STATUS_CODES_ARY[externalInstall]}
+ _retn_status=${STATUS_CODES_ARY[externalInstall]}
+ else
+ _debug_log "$0:${FUNCNAME[0]} no standardized install found"
+
+ install_onedir_chk=$(_check_onedir_minion_install)
+ if [[ ${install_onedir_chk} -eq 2 ]]; then
+ POST_3005_FLAG=1 # ensure note 3006 and above
fi
- elif [[ -z ${svpid} ]]; then
- # check no process id and main directory still left, then removeFailed
- if [[ -f "${test_exists_file}" ]]; then
- CURRENT_STATUS=${STATUS_CODES_ARY[removeFailed]}
- _retn_status=${STATUS_CODES_ARY[removeFailed]}
+
+ svpid=$(_find_salt_pid)
+ if [[ ${install_onedir_chk} -eq 0 && -z ${svpid} ]]; then
+ # check not installed and no process id
+ CURRENT_STATUS=${STATUS_CODES_ARY[notInstalled]}
+ _retn_status=${STATUS_CODES_ARY[notInstalled]}
+ elif [[ ${install_onedir_chk} -ne 0 ]]; then
+ # check installed
+ CURRENT_STATUS=${STATUS_CODES_ARY[installed]}
+ _retn_status=${STATUS_CODES_ARY[installed]}
+ # normal case but double-check
+ svpid=$(_find_salt_pid)
+ if [[ -z ${svpid} ]]; then
+ # Note: someone could have stopped the salt-minion,
+ # so installed but not running,
+ # status codes don't allow for that case
+ CURRENT_STATUS=${STATUS_CODES_ARY[installFailed]}
+ _retn_status=${STATUS_CODES_ARY[installFailed]}
+ fi
+ elif [[ -z ${svpid} ]]; then
+ # check no process id and main directory still left, =>removeFailed
+ if [[ ${install_onedir_chk} -ne 0 ]]; then
+ CURRENT_STATUS=${STATUS_CODES_ARY[removeFailed]}
+ _retn_status=${STATUS_CODES_ARY[removeFailed]}
+ fi
fi
fi
-
return ${_retn_status}
}
_install_fn () {
# execute install of Salt minion
local _retn=0
- local script_count=0
local existing_chk=""
local found_salt_ver=""
+ local install_onedir_chk=0
_info_log "$0:${FUNCNAME[0]} processing script install"
- script_count=$(_check_multiple_script_running)
- if [[ ${script_count} -gt 3 ]]; then
- _error_log "$0:${FUNCNAME[0]} failed to install, " \
- "multiple versions of the script are running"
- fi
+ _check_multiple_script_running
- found_salt_ver=$(_check_std_minion_install)
+ found_salt_ver=$(_check_classic_minion_install)
if [[ -n "${found_salt_ver}" ]]; then
- _error_log "$0:${FUNCNAME[0]} failed to install, " \
+ _warning_log "$0:${FUNCNAME[0]} failed to install, " \
"existing Standard Salt Installation detected,"\
"Salt version: '${found_salt_ver}'"
+ CURRENT_STATUS=${STATUS_CODES_ARY[externalInstall]}
+ exit ${STATUS_CODES_ARY[externalInstall]}
else
_debug_log "$0:${FUNCNAME[0]} no standardized install found"
fi
done
fi
- # fetch salt-minion form repository
+ # fetch salt-minion from repository
_fetch_salt_minion || {
_error_log "$0:${FUNCNAME[0]} failed to fetch salt-minion"\
"from repository , retcode '$?'";
"salt-minion, retcode '$?'";
}
- if [[ ${_retn} -eq 0 && -f "${test_exists_file}" ]]; then
+ if [[ ${_retn} -eq 0 && -f "${onedir_pre_3006_location}" ]]; then
# create helper scripts for /usr/bin to ensure they are present
# before attempting to use them in _ensure_id_or_fqdn
+ # this is for earlier than 3006 versions of Salt onedir
_debug_log "$0:${FUNCNAME[0]} creating helper files salt-call"\
"and salt-minion in directory /usr/bin"
- _create_helper_scripts || {
+ _create_pre_3006_helper_scripts || {
_error_log "$0:${FUNCNAME[0]} failed to create helper files"\
"salt-call or salt-minion in directory /usr/bin, retcode '$?'";
}
+ elif [[ ${_retn} -eq 0 && -f "${onedir_post_3005_location}" ]]; then
+ # create symbolic links for /usr/bin to ensure they are present
+ _debug_log "$0:${FUNCNAME[0]} creating symbolic links for salt-call"\
+ "and salt-minion in directory /usr/bin"
+ ln -s -f "${salt_dir}/salt-minion" "/usr/bin/salt-minion" || {
+ _error_log "$0:${FUNCNAME[0]} failed to create symbolic link "\
+ "for salt-minion in directory /usr/bin, retcode '$?'";
+ }
+ ln -s -f "${salt_dir}/salt-call" "/usr/bin/salt-call" || {
+ _error_log "$0:${FUNCNAME[0]} failed to create symbolic link "\
+ "for salt-call in directory /usr/bin, retcode '$?'";
+ }
+ else
+ _error_log "$0:${FUNCNAME[0]} problems creating helper files"\
+ "or symbolic links for salt-call or salt-minion in "\
+ "directory /usr/bin, should not have reached this code point";
fi
# ensure minion id or fqdn for salt-minion
- _ensure_id_or_fqdn
+ _ensure_id_or_fqdn
+ install_onedir_chk=$(_check_onedir_minion_install)
- if [[ ${_retn} -eq 0 && -f "${test_exists_file}" ]]; then
+ if [[ ${_retn} -eq 0 && ${install_onedir_chk} -ne 0 ]]; then
if [[ -n "${existing_chk}" ]]; then
# be nice and stop any current salt functionalty found
for idx in ${existing_chk}
"file ${name_service} to directory"\
"${systemd_lib_path}, retcode '$?'";
}
- cd /etc/systemd/system || return $?
- rm -f "${name_service}"
- ln -s "${systemd_lib_path}/${name_service}" \
- "${name_service}" || {
- _error_log "$0:${FUNCNAME[0]} failed to symbolic link"\
- "systemd service file ${name_service} in"\
- "directory /etc/systemd/system, retcode '$?'";
- }
- _debug_log "$0:${FUNCNAME[0]} symbolically linked systemd service"\
- "file ${name_service} in directory /etc/systemd/system"
+
cd "${CURRDIR}" || return $?
# start the salt-minion using systemd
local salt_id_flag=0
local minion_id=""
local minion_ip_id=""
- local script_count=0
+ local install_onedir_chk=0
_info_log "$0:${FUNCNAME[0]} processing clearing of salt-minion"\
"identifier and its keys"
- script_count=$(_check_multiple_script_running)
- if [[ ${script_count} -gt 3 ]]; then
- _error_log "$0:${FUNCNAME[0]} failed to clear,"\
- "multiple versions of the script are running"
- fi
+ _check_multiple_script_running
- if [[ ! -f "${test_exists_file}" ]]; then
+ install_onedir_chk=$(_check_onedir_minion_install)
+ if [[ ${install_onedir_chk} -eq 0 ]]; then
_debug_log "$0:${FUNCNAME[0]} salt-minion is not installed,"\
"nothing to do"
return ${_retn}
_uninstall_fn () {
# remove Salt minion
local _retn=0
- local script_count=0
+ local found_salt_ver=""
+ local install_onedir_chk=0
_info_log "$0:${FUNCNAME[0]} processing script remove"
- script_count=$(_check_multiple_script_running)
- if [[ ${script_count} -gt 3 ]]; then
- _error_log "$0:${FUNCNAME[0]} failed to remove,"\
- "multiple versions of the script are running"
- fi
+ _check_multiple_script_running
- found_salt_ver=$(_check_std_minion_install)
+ found_salt_ver=$(_check_classic_minion_install)
if [[ -n "${found_salt_ver}" ]]; then
- _error_log "$0:${FUNCNAME[0]} failed to remove,"\
+ _warning_log "$0:${FUNCNAME[0]} failed to install, " \
"existing Standard Salt Installation detected,"\
"Salt version: '${found_salt_ver}'"
+ CURRENT_STATUS=${STATUS_CODES_ARY[externalInstall]}
+ exit ${STATUS_CODES_ARY[externalInstall]}
else
_debug_log "$0:${FUNCNAME[0]} no standardized install found"
fi
- if [[ ! -f "${test_exists_file}" ]]; then
+ install_onedir_chk=$(_check_onedir_minion_install)
+ if [[ ${install_onedir_chk} -eq 0 ]]; then
CURRENT_STATUS=${STATUS_CODES_ARY[notInstalled]}
# assumme rest is gone
_retn=0
else
CURRENT_STATUS=${STATUS_CODES_ARY[removing]}
- svpid=$(_find_salt_pid)
- if [[ -n ${svpid} ]]; then
- # stop the active salt-minion using systemd
- # and give it a little time to stop
- systemctl stop salt-minion || {
- _error_log "$0:${FUNCNAME[0]} failed to stop salt-minion"\
- "using systemctl, retcode '$?'";
- }
- _debug_log "$0:${FUNCNAME[0]} successfully executed systemctl"\
- "stop salt-minion"
- systemctl disable salt-minion || {
- _error_log "$0:${FUNCNAME[0]} disabling the salt-minion"\
- "using systemctl failed , retcode '$?'";
+ # remove salt-minion from systemd
+ # and give it a little time to stop
+ systemctl stop salt-minion || {
+ _error_log "$0:${FUNCNAME[0]} failed to stop salt-minion"\
+ "using systemctl, retcode '$?'";
+ }
+ _debug_log "$0:${FUNCNAME[0]} successfully executed systemctl"\
+ "stop salt-minion"
+ systemctl disable salt-minion || {
+ _error_log "$0:${FUNCNAME[0]} disabling the salt-minion"\
+ "using systemctl failed , retcode '$?'";
+ }
+ _debug_log "$0:${FUNCNAME[0]} successfully executed systemctl"\
+ "disable salt-minion"
+
+ _debug_log "$0:${FUNCNAME[0]} removing systemd directories and files"\
+ "in '${list_files_systemd_to_remove}'"
+ for idx in ${list_files_systemd_to_remove}
+ do
+ rm -fR "${idx}" || {
+ _error_log "$0:${FUNCNAME[0]} failed to remove file or"\
+ "directory '${idx}' , retcode '$?'";
}
- _debug_log "$0:${FUNCNAME[0]} successfully executed systemctl"\
- "disable salt-minion"
- fi
+ done
+
+ systemctl daemon-reload || {
+ _error_log "$0:${FUNCNAME[0]} reloading the systemd daemon"\
+ "failed , retcode '$?'";
+ }
+ _debug_log "$0:${FUNCNAME[0]} successfully executed systemctl"\
+ "daemon-reload"
+ systemctl reset-failed || {
+ _error_log "$0:${FUNCNAME[0]} reloading the systemd daemon"\
+ "failed , retcode '$?'";
+ }
+ _debug_log "$0:${FUNCNAME[0]} successfully executed systemctl"\
+ "reset-failed"
if [[ ${_retn} -eq 0 ]]; then
svpid=$(_find_salt_pid)
CURRDIR=$(pwd)
+# get machine architecture once
+MACHINE_ARCH=$(uname -m)
+
+# setup work-area
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+# the temp working directory used, within $DIR
+WORK_DIR=$(mktemp -d -p "$DIR")
+
+# check if temp working dir was created
+if [[ ! "${WORK_DIR}" || ! -d "${WORK_DIR}" ]]; then
+ echo "Could not create temp dir"
+ exit 1
+fi
+
# default status is notInstalled
CURRENT_STATUS=${STATUS_CODES_ARY[notInstalled]}
export CURRENT_STATUS
retn=$?
fi
if [[ ${SOURCE_FLAG} -eq 1 ]]; then
- echo "DGM SOURCE source params ${SOURCE_PARAMS}"
CLI_ACTION=1
LOG_ACTION="install"
# ensure this is processed before install