Your IP : 3.128.204.196


Current Path : /usr/local/mgr5/lib/pkgsh/
Upload File :
Current File : //usr/local/mgr5/lib/pkgsh/core_pkg_funcs.sh

#!/bin/bash
# vim: ts=4 noexpandtab

MGRDIR="/usr/local/mgr5"
MGRCTL="${MGRDIR}/sbin/mgrctl"

OSType() {
	if [ -f /etc/redhat-release  ]; then
		echo REDHAT
		return
	elif [ -f /etc/debian_version ]; then
		echo DEBIAN
		return
	elif [ "$(uname)" = "FreeBSD" ]; then
		echo FREEBSD
		return
	fi
}

OSTYPE=$(OSType)

if [ "$(OSType)" = "DEBIAN" ]; then
	if [ -f /lib/systemd/system/mariadb.service ]; then
		MYSQL_SERVICE=mariadb
	else
		MYSQL_SERVICE=mysql
	fi
elif [ "$(OSType)" = "REDHAT" ]; then
	OSVERSION=$(rpm -q --qf "%{version}" -f /etc/redhat-release | cut -d. -f1)
	export OSVERSION
	if [ -f /usr/lib/systemd/system/mariadb.service ]; then
		MYSQL_SERVICE=mariadb
	else
		MYSQL_SERVICE=mysqld
	fi
fi

Errorn() {
	printf "\033[1;31m$@\033[0m"
}

Error() {
	Errorn "$@\n"
}

Infon() {
	printf "\033[1;32m$@\033[0m"
}

Info() {
	Infon "$@\n"
}

version_ge() {
	test "$(echo "$@" | tr " " "\n" | sort -V | tail -n 1)" = "$1";
}

version_gt() {
	test "$1" = "$2" && return 1
	version_ge "$1" "$2"
}

ReloadService() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	if [ -n "$(which systemctl 2>/dev/null)" ] || [ -x /usr/bin/systemctl ]; then
		systemctl restart "${1}"
	else
		service "${1}" restart >/dev/null 2>&1 || :
	fi
}

RcconfSet() {
	if [ -z "${2}" ] || [ -z "${1}" ]; then
		# Empty argument
		return 1
	fi
	if [ "#${1}" = "#_enable" ]; then
		return 1
	fi
	test -f /etc/rc.conf || exit 1
	if grep -q "${1}=" /etc/rc.conf ; then
		sed -i ".bak" -r "s|^${1}=.*|${1}=\"${2}\"|" /etc/rc.conf
	else
		echo "${1}=\"${2}\"" >> /etc/rc.conf
	fi
}


EnableService() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	if [ -n "${2}" ]; then
		RCNAME=${2}
	else
		RCNAME=${1}
	fi

	service ${1} stop >/dev/null 2>&1 || :
	# If delay is set sleeping
	if [ -n "${RESTART_DELAY}" ]; then
		sleep ${RESTART_DELAY} || :
	fi
	if [ -n "$(which systemctl 2>/dev/null)" ] || [ -x /usr/bin/systemctl ]; then
		systemctl enable "${1}"
	elif [ "$(OSType)" = "REDHAT" ]; then
		chkconfig ${1} on >/dev/null 2>&1 || :
	elif [ "$(OSType)" = "DEBIAN" ]; then
		update-rc.d ${1} enable >/dev/null 2>&1 || :
	elif [ "${OSTYPE}" = "FREEBSD" ]; then
		RcconfSet ${RCNAME}_enable YES
	fi	
	service ${1} start >/dev/null 2>&1
}

DisableService() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	if [ -n "${2}" ]; then
		RCNAME=${2}
	else
		RCNAME=${1}
	fi

	service ${1} stop >/dev/null 2>&1 || echo -n ""
	if [ "$(OSType)" = "REDHAT" ]; then 
		chkconfig ${1} off >/dev/null 2>&1 || echo -n ""
	elif [ "$(OSType)" = "DEBIAN" ]; then
		update-rc.d ${1} disable >/dev/null 2>&1 || echo -n ""
	elif [ "${OSTYPE}" = "FREEBSD" ]; then
		RcconfSet ${RCNAME}_enable NO
	fi	
}

Service() {
# $1 - name
# $2 - command
	if [ -n "$(which systemctl 2>/dev/null)" ] || [ -x /usr/bin/systemctl ] ; then
		systemctl ${2} ${1}.service
	else
		if [ "${2}" = "enable" ] || [ "${2}" = "disable" ] ; then
			if [ "${OSTYPE}" = "DEBIAN" ]; then
				update-rc.d ${1} ${2}
			else
				if [ "${2}" = "enable" ]; then
					chkconfig ${1} on
				else
					chkconfig ${1} off
				fi
			fi
		else
			service ${1} ${2}
		fi
	fi
}

ReloadMgr() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi

	if [ "${1}" = "all" ]; then
		${MGRCTL} -l >/dev/null 2>&1
		${MGRCTL} -u >/dev/null 2>&1
	elif [ -f ${MGRDIR}/var/${1}.pid ]; then
		# Pid file exists
		local pid
		pid=$(cat ${MGRDIR}/var/${1}.pid 2>/dev/null)
		if [ -n "${pid}" ] && [ -f /proc/${pid}/status ]; then
			# Proc by PID exists. Call this to exit
			${MGRCTL} -m ${1} exit >/dev/null 2>&1
		fi
	fi
}

AddMgr() {
	[ "#${1}" = "#" ] && return 0
	grep -qE "^manager\s+${1}\$" ${MGRDIR}/etc/mgrlist.conf || echo "manager ${1}" >> ${MGRDIR}/etc/mgrlist.conf
}

RemoveMgr() {
	[ "#${1}" = "#" ] && return 0
	if [ "#${OSTYPE}" = "#FREEBSD" ]; then
		sed -i "" -r "/^manager\s+${1}$/d" ${MGRDIR}/etc/mgrlist.conf
	else
		sed -i -r "/^manager\s+${1}$/d" ${MGRDIR}/etc/mgrlist.conf
	fi
}

LockMgr() {
	if [ "#${1}" = "#" ]; then
		${MGRCTL} -l
	else
		${MGRCTL} -m ${1} -l
	fi
}

UnlockMgr() {
	if [ "#${1}" = "#" ]; then
		${MGRCTL} -u
	else
		${MGRCTL} -m ${1} -u
	fi
}

AfterinstallMgr() {
	if [ "#${1}" = "#" ]; then
		MGR=core
	else
		MGR=${1}
	fi
	${MGRCTL} -m ${MGR} afterinstall xset.up=on >/dev/null 2>&1 &
}

AfterinstallModule() {
	if [ -z ${1} ]; then
		echo "Empty argument MGR"
		return 1
	fi
	if [ -z ${2} ]; then
		echo "Empty argument MODULE"
		return 1
	fi
	MGR=${1}
	MODULE=${2}
	${MGRCTL} -R -m ${MGR} afterinstall elid=${MODULE} xset.up=on >/dev/null 2>&1 &
}

AfterupdateMgr() {
	if [ "#${1}" = "#" ]; then
		MGR=core
	else
		MGR=${1}
		shift
	fi
	if [ -n "${@}" ]; then
		echo "${@}" > ${MGRDIR}/var/${MGR}.startup
	else
		touch ${MGRDIR}/var/${MGR}.startup
	fi
}


TMPCNF=/usr/local/mgr5/tmp/.$$.cnf

check_my_cnf() {
	test -f /root/.my.cnf 2>/dev/null
	return $?
}

mysql_version() {
	# Вернуть полную версию mysql(mariadb)
	# mysqladmin version не слишком удобен для парсинга
	# Вызывать только если есть файл my.cnf у коры
	local ver
	if [ -n "${MGRDIR}" ] && [ -f ${MGRDIR}/etc/my.cnf ]; then
		ver=$(echo "SELECT VERSION()" | mysql --defaults-file=${MGRDIR}/etc/my.cnf -N)
	fi
	if [ -z "${ver}" ]; then
		ver=$(echo "SELECT VERSION()" | mysql -N)
	fi
	if [ -z "${ver}" ] && [ "${OSTYPE}" = "REDHAT" ]; then
		ver=$(rpm -qf --qf '%{version}' $(which /usr/sbin/mysqld))
	fi
	echo "${ver}"
}

mysql_short_version() {
	# Вернуть короткую(2 знака) версию mysql(mariadb)
	# Вызывать только если есть файл my.cnf у коры
	mysql_version | awk -F. '{print $1 "." $2}'
}

mysql_update_password() {
	# Сбросить пароль пользователю(но не root)
	# $1 - username
	# $2 - password
	# $@ - mysql arguments
	# Если не нужно звать FLUSH PRIVILEGES, то выставить ENV NOFLUSH
	local USERNAME=${1}
	local PW=${2}
	shift 2
	local MYSQLOPTS="$@"
	if echo ${PW} | grep -q '^\*'; then
		local restores_pw=yes
	fi
	local mysql_ver=$(mysql_short_version)
	if [ "${mysql_ver}" = "5.7" ] || [ "${mysql_ver}" = "8.0" ] || version_ge "${mysql_ver}" "10.3"; then
		local SQLQUERY="ALTER USER '${USERNAME}'@'localhost'"
		if [ -n "${restores_pw}" ]; then
			SQLQUERY="${SQLQUERY} IDENTIFIED WITH mysql_native_password AS '${PW}'"
		else
			SQLQUERY="${SQLQUERY} IDENTIFIED BY '${PW}'"
		fi
		if [ "${USERNAME}" = "root" -a -z "${MYSQLOPTS}" ] ; then
			SQLQUERY="FLUSH PRIVILEGES; ${SQLQUERY}"
		fi
		mysql ${MYSQLOPTS} mysql -e "${SQLQUERY}" >/dev/null 2>&1 || return 1
	else
		if [ -n "${restores_pw}" ]; then
			local PW_S="'${PW}'"
		else
			local PW_S="PASSWORD('${PW}')"
		fi
		echo "UPDATE user SET Password=${PW_S} WHERE user='${USERNAME}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1 || return 1
		if [ -z "${NOFLUSH}" ]; then
			echo "FLUSH PRIVILEGES" | mysql ${MYSQLOPTS} >/dev/null 2>&1 || return 1
		fi
	fi
}

check_core_cnf() {
	if [ -f /usr/local/mgr5/etc/my.cnf ]; then
		DLIST=$(check_connect /usr/local/mgr5/etc/my.cnf)
		ret=$?
		echo ${DLIST}
		return $ret
	else
		return 1
	fi
}

check_connect() {
	# Return dblist
	# Если есть аргумент, то это --defaults-file
	if [ -n "$1" ]; then
		MYSQLCOMMAND="mysql --defaults-file=$1 -N"
	else
		MYSQLCOMMAND="mysql -N"
	fi
	DLIST=$(echo "show databases" | ${MYSQLCOMMAND} 2>/dev/null)
	RET=$?
	if [ ${RET} -ne 0 ]; then
		DLIST=""
	fi
	echo ${DLIST}
	return ${RET}
}

delete_empty_passwords() {
	if [ "$(mysql_short_version | awk -F. '{print $1}')" != "10" ]; then
		echo "delete from user where password = ''" | mysql -N mysql > /dev/null 2>&1
	fi
}

create_root_passwd() {
	PW=$(pwgen -s 10 1)
	export PW
	local mysql_short_version_var
	mysql_short_version_var="$(mysql_short_version)"
	if [ "${mysql_short_version_var}" = "8.0" -o "${mysql_short_version_var}" = "5.7" ]; then
		echo "alter user 'root'@'localhost' identified with mysql_native_password by '${PW}';" | mysql -N
	elif version_ge "${mysql_short_version_var}" "10.4"; then
		echo "alter user 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('${PW}')" | mysql -N
	elif [ "${mysql_short_version_var}" = "10.3" ]; then
		echo "alter user 'root'@'localhost' IDENTIFIED BY '${PW}'" | mysql -N
	else
		sh -c 'mysqladmin -u root password "${PW}"'
	fi
	echo "[client]" > /root/.my.cnf 
	chmod 400 /root/.my.cnf
	echo "user = root" >> /root/.my.cnf
	echo "password = ${PW}" >> /root/.my.cnf
	delete_empty_passwords
}

create_coremgr_passwd() {
	if [ -f ${TMPCNF} ]; then
		MYSQLOPTS="--defaults-file=${TMPCNF}"
	else
		MYSQLOPTS=""
	fi
	PW=$(pwgen -s 10 1)
	export PW
	USERNAME=coremgr
	echo "DROP USER '${USERNAME}'@'localhost'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	echo "CREATE USER '${USERNAME}'@'localhost' IDENTIFIED BY '${PW}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		mysql_update_password ${USERNAME} ${PW} ${MYSQLOPTS}
	fi
	echo "GRANT ALL PRIVILEGES ON *.* TO '${USERNAME}'@'localhost' WITH GRANT OPTION" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	echo "[client]" > /usr/local/mgr5/etc/my.cnf
	chmod 400 /usr/local/mgr5/etc/my.cnf
	echo "user = ${USERNAME}" >> /usr/local/mgr5/etc/my.cnf
	echo "password = ${PW}" >> /usr/local/mgr5/etc/my.cnf
}

skip_grant_to_mycnf() {
	if [ -f /etc/mysql/mysql.conf.d/mysqld.cnf ] && grep -q '\[mysqld\]' /etc/mysql/mysql.conf.d/mysqld.cnf; then
		MYCNF=/etc/mysql/mysql.conf.d/mysqld.cnf
	elif [ -f /etc/mysql/my.cnf ]; then
		MYCNF=/etc/mysql/my.cnf
	elif [ -f /etc/my.cnf.d/mysql-server.cnf ]; then
		MYCNF=/etc/my.cnf.d/mysql-server.cnf
	else
		MYCNF=/etc/my.cnf
	fi
	if grep -q '\[mysqld\]' ${MYCNF} ; then
		if ! cat ${MYCNF} | sed -n -r '/\[mysqld\]/,/\[/p' | grep -q skip-grant-tables ; then
			sed -i -r '/^\[mysqld\]/a skip-grant-tables' ${MYCNF}
		fi
		if ! cat ${MYCNF} | sed -n -r '/\[mysqld\]/,/\[/p' | grep -q skip-networking ; then
			sed -i -r '/^\[mysqld\]/a skip-networking' ${MYCNF}
		fi
	else
		printf "[mysqld]\nskip-grant-tables\nskip-networking\n" >> ${MYCNF}
	fi
}

skip_grant_from_mycnf() {
	if [ -f /etc/mysql/mysql.conf.d/mysqld.cnf ] && grep -q '\[mysqld\]' /etc/mysql/mysql.conf.d/mysqld.cnf; then
		MYCNF=/etc/mysql/mysql.conf.d/mysqld.cnf
	elif [ -f /etc/mysql/my.cnf ]; then
		MYCNF=/etc/mysql/my.cnf
	elif [ -f /etc/my.cnf.d/mysql-server.cnf ]; then
		MYCNF=/etc/my.cnf.d/mysql-server.cnf
	else
		MYCNF=/etc/my.cnf
	fi
	sed -i -r '/\[mysqld\]/,/\[/ {/^(skip-networking|skip-grant-tables)/d}' ${MYCNF}
}

reset_root_passwd() {
	# Save passwd to ${TMPCNF}
	service ${MYSQL_SERVICE} stop >/dev/null 2>&1
	sleep 2
	if [ -f /bin/systemctl ]; then 
		# Systemd. Can`t stop service by pid
		skip_grant_to_mycnf
		service ${MYSQL_SERVICE} start
		skip_grant_from_mycnf
	else
		mysqld_safe --skip-grant-tables --skip-networking >/dev/null 2>&1 &
	fi
	sleep 2
	MYSQLSOCK=$(mysql --help | grep ^socket | awk '{print $2}')
	if ! echo "${MYSQLSOCK}" | grep -q sock ; then
		MYSQLSOCK=$(/usr/bin/my_print_defaults mysqld socket |sed -n "s/^--socket=//p" | tail -n 1)
	fi
	SLEEPCOUNT=1
	while ! test -S ${MYSQLSOCK}; do
		sleep 2
		SLEEPCOUNT=$(expr ${SLEEPCOUNT} + 1)
		if [ ${SLEEPCOUNT} -gt 5 ]; then # Если мы уже 5 раз ждали мускуля, то больше не ждём и выходим с ошибкой
			break
		fi
	done
	local mysql_short_version_var pwfield
	mysql_short_version_var="$(mysql_short_version)"
	if [ "${mysql_short_version_var}" = "5.7" -o "${mysql_short_version_var}" = "10.3" -o "${mysql_short_version_var}" = "8.0" ]; then
		pwfield=authentication_string
	else
		pwfield=password
	fi
    if [ "${mysql_short_version_var}" = "8.0" ]; then
        OLDPW=$(echo "SELECT ${pwfield} FROM user WHERE user='root' AND ${pwfield}!=''" | mysql -N mysql | head -1)
    else
        OLDPW=$(echo "SELECT ${pwfield} FROM user WHERE user='root' AND password!=''" | mysql -N mysql | head -1)
    fi
	PW=$(pwgen -s 10 1)
	export PW
	mysql_update_password root ${PW}
	echo "[client]" > ${TMPCNF}
	chmod 400 ${TMPCNF}
	echo "password = ${PW}" >> ${TMPCNF}
	service ${MYSQL_SERVICE} restart >/dev/null 2>&1
	delete_empty_passwords
	echo ${OLDPW}
}

restore_root_passwd() {
	if [ ! -f ${TMPCNF} ]; then
		return 0
	fi
	OLDROOTPW=${1}
	MYSQLOPTS="--defaults-file=${TMPCNF}"
	NOFLUSH=yes
	mysql_update_password root ${OLDROOTPW} ${MYSQLOPTS} || return 1
	service ${MYSQL_SERVICE} restart >/dev/null 2>&1
}

create_coremgr_user() {
	create_coremgr_passwd #>/dev/null 2>&1
}

create_db() {
	CORECNF=/usr/local/mgr5/etc/my.cnf
	if [ -z "${1}" ]; then
		return 1
	fi
	local MYSQLOPTS="--defaults-file=${CORECNF}"
	mysqladmin ${MYSQLOPTS} create ${1} >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "mysqladmin create database ${1} failed" >&2
	fi
	local USERNAME=${1}
	local PW
	if [ -n "${CREATEDB_PW}" ]; then
		PW=${CREATEDB_PW}
	else
		PW=$(pwgen -s 10 1)
	fi
	echo "CREATE USER '${USERNAME}'@'localhost' IDENTIFIED BY '${PW}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		mysql_update_password ${USERNAME} ${PW} ${MYSQLOPTS}
		if [ $? -ne 0 ]; then
			echo "create or update mysql user ${USERNAME} failed" >&2
		fi
	fi
	if [ "#${2}" = "#1" ]; then
		echo "GRANT ALL PRIVILEGES ON *.* TO '${USERNAME}'@'localhost'"	| mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	else
		echo "GRANT ALL PRIVILEGES ON ${1}.* TO '${USERNAME}'@'localhost'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	fi
	if [ $? -ne 0 ]; then
		echo "grant privileges to ${USERNAME} failed" >&2
		return 1
	fi
	echo ${PW}
}

create_admin_user() {
	# $1 - username
	local USERNAME=${1}
	CORECNF=/usr/local/mgr5/etc/my.cnf
	local MYSQLOPTS="--defaults-file=${CORECNF}"
	if [ -z "${1}" ]; then
		return 1
	fi
	local PW
	if [ -n "${CREATEDB_PW}" ]; then
		PW=${CREATEDB_PW}
	else
		PW=$(pwgen -s 10 1)
	fi
	echo "CREATE USER '${USERNAME}'@'localhost' IDENTIFIED BY '${PW}'" | mysql ${MYSQLOPTS} mysql >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		mysql_update_password ${USERNAME} ${PW} ${MYSQLOPTS}
		if [ $? -ne 0 ]; then
			echo "create or update mysql user ${USERNAME} failed" >&2
		fi
	fi
	echo "GRANT ALL PRIVILEGES ON *.* TO '${USERNAME}'@'localhost' WITH GRANT OPTION" | mysql ${MYSQLOPTS}  mysql >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "grant privileges to ${USERNAME} failed" >&2
		return 1
	fi
	echo ${PW}

}

MgrInstalled() {
	if [ -z ${1} ]; then echo "Empty arg 1" ; return 1; fi
	if [ -z ${2} ]; then echo "Empty arg 2" ; return 1; fi

	IPADDR=$(echo $SSH_CONNECTION | awk '{print $3}')
	if [ -z ${IPADDR} ]; then
		IPADDR=$(ip addr show | awk '$1 ~ /inet/ && $2 !~ /127.0.0|::1|fe80:/ {print $2}' |cut -d/ -f1 | head -1)
	fi

	Info "================================================="
	Info "$2 is installed"
	Info "Go to the \"https://${IPADDR}:1500/${1}\" to login"
	Info "Login: root"
	Info "Password: <root password>"
	Info "================================================="
}

IsMgrExist() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	grep -qE "^manager\s+${1}\$" ${MGRDIR}/etc/mgrlist.conf 2>/dev/null
	return $?
}

LicError() {
	Error "Trial license for this IP has expired"
	Error "You have no complete license on this manager or it can no be activated automatically"
	Error "If You have that license set environment variable ACTIVATION_KEY"
	exit 1
}

LicCheck() {
	if [ "#${1}" = "#" ]; then
		# Epty argument
		return 1
	fi
	MGR=${1}
	FIRSTSTART=yes ${MGRDIR}/sbin/licctl fetch ${MGR} >/dev/null 2>&1 || LicError
}

PathConvert() {
	# $1 - new conf
	test  "#${ISPCONVERT}" = "#yes" || return 1
	test -n "${PATHLIST}" || return 1
	for VAL in ${PATHLIST} ; do
		VALPATH=""
		VALPATH=$(grep -E "^path\s+${VAL}\s+" ${MGRDIR}/etc/*.conf | head -1 | awk '{for(i=3;i<=NF;i++){printf "%s ", $i}; printf "\n"}')
		if [ -n "${VALPATH}" ]; then
			sed --follow-symlinks -i -r "/^path\s+${VAL}\s+/d" ${MGRDIR}/etc/*.conf
			sed --follow-symlinks -i -r "/^path\s+${VAL}\s+/d" $1
			echo "path ${VAL} ${VALPATH}" >> $1
		fi
	done
}

ParamConvert() {
	# $1 - new conf
	test  "#${ISPCONVERT}" = "#yes" || return 1
	test -n "${PARAMLIST}" || return 1
	for VAL in ${PARAMLIST} ; do
		VALPARAM=""
		VALPARAM=$(grep -E "^${VAL}\s+" ${MGRDIR}/etc/*.conf | head -1 | awk '{for(i=2;i<=NF;i++){printf "%s ", $i}; printf "\n"}')
		if [ -n "${VALPARAM}" ]; then
			sed --follow-symlinks -i -r "/^${VAL}\s+/d" ${MGRDIR}/etc/*.conf
			sed --follow-symlinks -i -r "/^${VAL}\s+/d" $1
			echo "${VAL} ${VALPARAM}" >> $1
		fi
	done
}

MgrParamConvert() {
	# $1 - mgr
	# $2 - new conf
	test  "#${ISPCONVERT}" = "#yes" || return 1
	test -n "${PARAMLIST}" || return 1
	for VAL in ${PARAMLIST} ; do
		VALPARAM=""
		VALPARAM=$(grep -E "^${VAL}\s+" "${MGRDIR}/etc/${1}.conf" | head -1 | awk '{for(i=2;i<=NF;i++){printf "%s ", $i}; printf "\n"}')
		if [ -n "${VALPARAM}" ]; then
			sed --follow-symlinks -i -r "/^${VAL}\s+/d" "${MGRDIR}/etc/${1}.conf"
			if [ -f "${2}" ]; then
				sed --follow-symlinks -i -r "/^${VAL}\s+/d" "${2}"
			fi
			echo "${VAL} ${VALPARAM}" >> "${2}"
			chmod 600 "${2}" 2>/dev/null || :
		fi
	done
}


InsertOrReplace() {
	# If param exist, replace it. If it commented put after comment. Else append to end of file
	# $1 - param
	# $2 - value
	# $3 - file
	#
	if grep -qE "^(#\s*){0,1}${1}\s+" "${3}" ; then
		if grep -qE "^\s*${1}\s+" "${3}" ; then
			sed -i -r "s;(^\s*${1})(\s+).*(#.*)*;\1\2${2}\3;" "${3}"
		else
			sed -i -r "0,/^(#\s*${1}\s+.*)/s;;\1\n${1} ${2}\n;" "${3}"
		fi
	else
		echo "${1} ${2}" >> "${3}"
	fi
}

CronLink() {
	# $1 - mgr
	test -z "${1}" && return 1
	if [ ! -e "${MGRDIR}/sbin/cron-${1}" ]; then
		ln -s cron "${MGRDIR}/sbin/cron-${1}"
	fi
}

CheckDF() {
	# Check free disk space for centos
	# $1 - partition
	# $2 - min size
	test -n "${1}" || return 1
	test -n "${2}" || return 1

	# skip if silent install
	# shellcheck disable=SC2039
	local cursize
	cursize=$(df -P -m "${1}" 2>/dev/null | tail -1 | awk '{print $4}')
	test -z "${cursize}" && return 0
	if [ "${cursize}" -lt "${2}" ]; then
		return 1
	fi
}

PkgUpdateRepos() {
	# Update repos
	case ${OSTYPE} in
		REDHAT)
			yum -y update
		;;
		DEBIAN)
			apt-get -o DPkg::Lock::Timeout=600 -y update
		;;
		*)
			return 1
		;;
	esac
}

PkgAvailable() {
	case ${OSTYPE} in
		REDHAT)
			# shellcheck disable=SC2086
			yum -q -C info "${1}" >/dev/null 2>/dev/null
		;;
		DEBIAN)
			# shellcheck disable=SC2086
			apt-cache -q show "${1}" | grep -q "${1}" >/dev/null 2>/dev/null
		;;
		*)
			return 1
		;;
	esac
}

PkgInstall() {
	# Install packages
	case ${OSTYPE} in
		REDHAT)
			# shellcheck disable=SC2068
			yum -y install ${@}
		;;
		DEBIAN)
			# shellcheck disable=SC2068
			/usr/bin/apt-get -o DPkg::Lock::Timeout=600 -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"  -qy --allow-unauthenticated install ${@}
		;;
		*)
			return 1
		;;
	esac
}