mirror of
https://github.com/m8tin/cis.git
synced 2026-11-02 14:17:24 +01:00
Refactoring using "local -r"
This commit is contained in:
9 files changed
+97
-137
No files matched your search
@@ -27,10 +27,8 @@ function checkPermissions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function cloneOrPull() {
|
function cloneOrPull() {
|
||||||
local _FOLDER _REPOSITORY
|
local -r _FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
local -r _REPOSITORY="${2:?"Missing second parameter REPOSITORY"}"
|
||||||
_REPOSITORY="${2:?"Missing second parameter REPOSITORY"}"
|
|
||||||
readonly _FOLDER _REPOSITORY
|
|
||||||
|
|
||||||
[ -d "${_FOLDER}.git" ] \
|
[ -d "${_FOLDER}.git" ] \
|
||||||
&& git -C "${_FOLDER}" pull &> /dev/null \
|
&& git -C "${_FOLDER}" pull &> /dev/null \
|
||||||
@@ -47,11 +45,9 @@ function cloneOrPull() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function printRepository() {
|
function printRepository() {
|
||||||
local _FOLDER _CONFIGURED_REPOSITORY _SUGGESTED_REPOSITORY
|
local -r _FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
local -r _CONFIGURED_REPOSITORY="$(git -C "${_FOLDER:?"Missing FOLDER"}" config --get remote.origin.url 2> /dev/null)"
|
||||||
_CONFIGURED_REPOSITORY="$(git -C "${_FOLDER:?"Missing FOLDER"}" config --get remote.origin.url 2> /dev/null)"
|
local -r _SUGGESTED_REPOSITORY="${2}"
|
||||||
_SUGGESTED_REPOSITORY="${2}"
|
|
||||||
readonly _FOLDER _CONFIGURED_REPOSITORY _SUGGESTED_REPOSITORY
|
|
||||||
|
|
||||||
! [ -z "${_CONFIGURED_REPOSITORY}" ] \
|
! [ -z "${_CONFIGURED_REPOSITORY}" ] \
|
||||||
&& echo "${_CONFIGURED_REPOSITORY}" \
|
&& echo "${_CONFIGURED_REPOSITORY}" \
|
||||||
@@ -74,11 +70,9 @@ function printRepository() {
|
|||||||
# Note that an unprivileged user can use this script successfully,
|
# Note that an unprivileged user can use this script successfully,
|
||||||
# if no user has to be added to the host because it already exists.
|
# if no user has to be added to the host because it already exists.
|
||||||
function addAndCheckGitRepository() {
|
function addAndCheckGitRepository() {
|
||||||
local _FOLDER _REPOSITORY _RIGHTS
|
local -r _FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
local -r _RIGHTS="${2:?"Missing second parameter RIGHTS: (readonly, writable) "}"
|
||||||
_RIGHTS="${2:?"Missing second parameter RIGHTS: (readonly, writable) "}"
|
local -r _REPOSITORY="$(printRepository "${_FOLDER}" "${3}")"
|
||||||
_REPOSITORY="$(printRepository "${_FOLDER}" "${3}")"
|
|
||||||
readonly _FOLDER _REPOSITORY _RIGHTS
|
|
||||||
|
|
||||||
echo \
|
echo \
|
||||||
&& cloneOrPull "${_FOLDER}" "${_REPOSITORY:?"Missing REPOSITORY: e.g. ssh://git@your.domain.com/cis.git"}" \
|
&& cloneOrPull "${_FOLDER}" "${_REPOSITORY:?"Missing REPOSITORY: e.g. ssh://git@your.domain.com/cis.git"}" \
|
||||||
|
|||||||
@@ -9,9 +9,7 @@ source /cis/core/base.module.sh
|
|||||||
# Note that an unprivileged user can use this script successfully,
|
# Note that an unprivileged user can use this script successfully,
|
||||||
# if no user has to be added to the host because it already exists.
|
# if no user has to be added to the host because it already exists.
|
||||||
function addNormalUser() {
|
function addNormalUser() {
|
||||||
local _USER
|
local -r _USER="${1:?"Missing first parameter USER"}"
|
||||||
_USER="${1:?"Missing first parameter USER"}"
|
|
||||||
readonly _USER
|
|
||||||
|
|
||||||
#The user already exists
|
#The user already exists
|
||||||
id -u "${_USER}" &> /dev/null \
|
id -u "${_USER}" &> /dev/null \
|
||||||
|
|||||||
@@ -9,10 +9,8 @@ source /cis/core/base.module.sh
|
|||||||
# Note that an unprivileged user can use this script successfully,
|
# Note that an unprivileged user can use this script successfully,
|
||||||
# if no user has to be added to the host because it already exists.
|
# if no user has to be added to the host because it already exists.
|
||||||
function addToCrontabEveryHour() {
|
function addToCrontabEveryHour() {
|
||||||
local _MINUTE_VALUE _STRING
|
! [ -z "${2##*[!0-9]*}" ] && local -r _MINUTE_VALUE=$((${2}%60)) # if second parameter is integer then (minute-value % 60) as safe guard
|
||||||
! [ -z "${2##*[!0-9]*}" ] && _MINUTE_VALUE=$((${2}%60)) # if second parameter is integer then (minute-value % 60) as safe guard
|
local -r _STRING="${_MINUTE_VALUE:?"Missing MINUTE_VALUE"} * * * * ${1:?"Missing first parameter COMMAND"} > /dev/null 2>&1"
|
||||||
_STRING="${_MINUTE_VALUE:?"Missing MINUTE_VALUE"} * * * * ${1:?"Missing first parameter COMMAND"} > /dev/null 2>&1"
|
|
||||||
readonly _MINUTE_VALUE _STRING
|
|
||||||
|
|
||||||
[ "$(id -u)" == "0" ] \
|
[ "$(id -u)" == "0" ] \
|
||||||
&& crontab -l | grep -qF "${_STRING:?"Missing CRON_STRING"}" \
|
&& crontab -l | grep -qF "${_STRING:?"Missing CRON_STRING"}" \
|
||||||
|
|||||||
+24
-28
@@ -396,51 +396,47 @@ function base.printWithColor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function base.set() {
|
function base.set() {
|
||||||
local _CLEAN_VARNAME _MODE _REGEX _VALUE _VARNAME
|
local -r _base_set_VARNAME="${1:?"base.set(): Missing first parameter VARNAME"}"
|
||||||
_VARNAME="${1:?"base.set(): Missing first parameter VARNAME"}"
|
local -r _base_set_CLEAN_VARNAME="${_base_set_VARNAME//[^a-zA-Z0-9_]/}"
|
||||||
_CLEAN_VARNAME="${_VARNAME//[^a-zA-Z0-9_]/}"
|
local -r _base_set_VALUE="${2}"
|
||||||
_VALUE="${2}"
|
local -r _base_set_REGEX="${3:?"base.set(): Missing third parameter REGEX"}"
|
||||||
_REGEX="${3:?"base.set(): Missing third parameter REGEX"}"
|
local -r _base_set_MODE="${4}"
|
||||||
_MODE="${4}"
|
|
||||||
readonly _CLEAN_VARNAME _MODE _REGEX _VALUE _VARNAME
|
|
||||||
|
|
||||||
[ "${_VARNAME}" != "${_CLEAN_VARNAME}" ] \
|
[ "${_base_set_VARNAME}" != "${_base_set_CLEAN_VARNAME}" ] \
|
||||||
&& echo "FAILURE - base.set(): Invalid name of variable: ${_VARNAME}" >&2 \
|
&& echo "FAILURE - base.set(): Invalid name of variable: ${_base_set_VARNAME}" >&2 \
|
||||||
&& exit 1
|
&& exit 1
|
||||||
|
|
||||||
[ -z "${_VALUE}" ] \
|
[ -z "${_base_set_VALUE}" ] \
|
||||||
&& [ "${_MODE}" == 'optional' ] \
|
&& [ "${_base_set_MODE}" == 'optional' ] \
|
||||||
&& readonly "${_CLEAN_VARNAME}" \
|
&& readonly "${_base_set_CLEAN_VARNAME}" \
|
||||||
&& return 0
|
&& return 0
|
||||||
|
|
||||||
# Sets the value to a global variable with name $_VARNAME
|
# Sets the value to a global variable with name $_base_set_VARNAME
|
||||||
[[ "${_VALUE}" =~ ^(${_REGEX})$ ]] \
|
[[ "${_base_set_VALUE}" =~ ^(${_base_set_REGEX})$ ]] \
|
||||||
&& printf -v "${_CLEAN_VARNAME}" -- "%s" "${_VALUE}" \
|
&& printf -v "${_base_set_CLEAN_VARNAME}" -- "%s" "${_base_set_VALUE}" \
|
||||||
&& readonly "${_CLEAN_VARNAME}" \
|
&& readonly "${_base_set_CLEAN_VARNAME}" \
|
||||||
&& return 0
|
&& return 0
|
||||||
|
|
||||||
printf -- '%s\n' "FAILURE: - base.set(): Unable to set variable '${_VARNAME}' to value '${_VALUE}', validation failed: '${_REGEX}'" >&2
|
printf -- '%s\n' "FAILURE: - base.set(): Unable to set variable '${_base_set_VARNAME}' to value '${_base_set_VALUE}', validation failed: '${_REGEX}'" >&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function base.setFromFile() {
|
function base.setFromFile() {
|
||||||
local _CLEAN_VARNAME _FILENAME _VARNAME
|
local -r _base_setFromFile_VARNAME="${1:?"base.setFromFile(): Missing first parameter VARNAME"}"
|
||||||
_VARNAME="${1:?"base.setFromFile(): Missing first parameter VARNAME"}"
|
local -r _base_setFromFile_CLEAN_VARNAME="${_base_setFromFile_VARNAME//[^a-zA-Z0-9_]/}"
|
||||||
_CLEAN_VARNAME="${_VARNAME//[^a-zA-Z0-9_]/}"
|
local -r _base_setFromFile_FILENAME="${2:?"base.serFromFile(): Missing second parameter FILENAME"}"
|
||||||
_FILENAME="${2:?"base.serFromFile(): Missing second parameter FILENAME"}"
|
|
||||||
readonly _CLEAN_VARNAME _FILENAME _VARNAME
|
|
||||||
|
|
||||||
[ "${_VARNAME}" != "${_CLEAN_VARNAME}" ] \
|
[ "${_base_setFromFile_VARNAME}" != "${_base_setFromFile_CLEAN_VARNAME}" ] \
|
||||||
&& echo "FAILURE - base.setFromFile(): Invalid name of variable: ${_VARNAME}" >&2 \
|
&& echo "FAILURE - base.setFromFile(): Invalid name of variable: ${_base_setFromFile_VARNAME}" >&2 \
|
||||||
&& exit 1
|
&& exit 1
|
||||||
|
|
||||||
local _LINE=$(grep -m 1 -oE "^[[:blank:]]*${_CLEAN_VARNAME}=[^[:blank:]]+.*" "${_FILENAME}" 2> /dev/null) \
|
local _LINE=$(grep -m 1 -oE "^[[:blank:]]*${_base_setFromFile_CLEAN_VARNAME}=[^[:blank:]]+.*" "${_base_setFromFile_FILENAME}" 2> /dev/null) \
|
||||||
&& [ -n "${_LINE}" ] \
|
&& [ -n "${_LINE}" ] \
|
||||||
&& printf -v "${_CLEAN_VARNAME}" -- "%s" "${_LINE#*=}" \
|
&& printf -v "${_base_setFromFile_CLEAN_VARNAME}" -- "%s" "${_LINE#*=}" \
|
||||||
&& readonly "${_CLEAN_VARNAME}" \
|
&& readonly "${_base_setFromFile_CLEAN_VARNAME}" \
|
||||||
&& return 0
|
&& return 0
|
||||||
|
|
||||||
printf -- '%s\n' "FAILURE: - base.setFromFile(): Unable to set variable '${_VARNAME}' from file '${_FILENAME}', value not found." >&2
|
printf -- '%s\n' "FAILURE: - base.setFromFile(): Unable to set variable '${_base_setFromFile_VARNAME}' from file '${_base_setFromFile_FILENAME}', value not found." >&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,9 @@ source /cis/core/base.module.sh
|
|||||||
|
|
||||||
|
|
||||||
function prepareFolder() {
|
function prepareFolder() {
|
||||||
local _HOME_FOLDER _SSH_FOLDER _USER
|
local -r _SSH_FOLDER="${1:?"prepareFolder(): Missing parameter SSH_PATH"}"
|
||||||
_SSH_FOLDER="${1:?"prepareFolder(): Missing parameter SSH_PATH"}"
|
local -r _HOME_FOLDER="${_SSH_FOLDER%%/.ssh*}" #Removes longest matching pattern '/.ssh*' from the end
|
||||||
_HOME_FOLDER="${_SSH_FOLDER%%/.ssh*}" #Removes longest matching pattern '/.ssh*' from the end
|
local -r _USER="${_HOME_FOLDER##*/}" #Removes longest matching pattern '*/' from the begin
|
||||||
_USER="${_HOME_FOLDER##*/}" #Removes longest matching pattern '*/' from the begin
|
|
||||||
readonly _HOME_FOLDER _SSH_FOLDER _USER
|
|
||||||
|
|
||||||
! id "${_USER}" &> /dev/null \
|
! id "${_USER}" &> /dev/null \
|
||||||
&& echo "FAIL: The given user does not exist: ("$(readlink -f ${0})")" \
|
&& echo "FAIL: The given user does not exist: ("$(readlink -f ${0})")" \
|
||||||
@@ -67,10 +65,8 @@ function prepareFolder() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function ensureGroupMembership() {
|
function ensureGroupMembership() {
|
||||||
local _SSH_GROUP _USER
|
local -r _SSH_GROUP="ssh_login"
|
||||||
_SSH_GROUP="ssh_login"
|
local -r _USER="${1:?"ensureGroupMembership(): Missing first parameter USER"}"
|
||||||
_USER="${1:?"ensureGroupMembership(): Missing first parameter USER"}"
|
|
||||||
readonly _SSH_GROUP _USER
|
|
||||||
|
|
||||||
! id "${_USER}" &> /dev/null \
|
! id "${_USER}" &> /dev/null \
|
||||||
&& echo "FAIL: The given user does not exist: ("$(readlink -f ${0})")" \
|
&& echo "FAIL: The given user does not exist: ("$(readlink -f ${0})")" \
|
||||||
@@ -107,17 +103,16 @@ function ensureGroupMembership() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function defineAuthorizedKeysOfUser() {
|
function defineAuthorizedKeysOfUser() {
|
||||||
local _CIS_ROOT _CORE_SCRIPTS _DOMAIN _DEFINITIONS _USER
|
local _DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")"
|
||||||
_DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")"
|
local -r _CIS_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
||||||
_CIS_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
local _DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
||||||
_DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
readonly _DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
||||||
_DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
|
||||||
#Build from components for safety
|
#Build from components for safety
|
||||||
_DEFINITIONS="${_CIS_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}"
|
readonly _DEFINITIONS="${_CIS_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}"
|
||||||
|
|
||||||
_USER="${2:?"Missing second parameter USER"}"
|
|
||||||
_CORE_SCRIPTS="${_CIS_ROOT:?"Missing ROOT"}core/"
|
local -r _USER="${2:?"Missing second parameter USER"}"
|
||||||
readonly _CIS_ROOT _CORE_SCRIPTS _DOMAIN _DEFINITIONS _USER
|
local -r _CORE_SCRIPTS="${_CIS_ROOT:?"Missing ROOT"}core/"
|
||||||
|
|
||||||
case "${_USER:?"Missing USER"}" in
|
case "${_USER:?"Missing USER"}" in
|
||||||
root)
|
root)
|
||||||
|
|||||||
@@ -34,9 +34,7 @@ function isCoreDefinition() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function filterInvalidAuthorizedKeysFilesOfRoot() {
|
function filterInvalidAuthorizedKeysFilesOfRoot() {
|
||||||
local _FILE_DEFINED
|
local -r _FILE_DEFINED="${1:?"Missing DEFINITION FILE"}"
|
||||||
_FILE_DEFINED="${1:?"Missing DEFINITION FILE"}"
|
|
||||||
readonly _FILE_DEFINED
|
|
||||||
|
|
||||||
#If the full filename contains 'root/.ssh/authorized_keys' then check the content.
|
#If the full filename contains 'root/.ssh/authorized_keys' then check the content.
|
||||||
#Skip lines starting with '#' and if at least one remaining line contains 'ssh' and '@' then print the filename.
|
#Skip lines starting with '#' and if at least one remaining line contains 'ssh' and '@' then print the filename.
|
||||||
@@ -56,15 +54,13 @@ function filterInvalidAuthorizedKeysFilesOfRoot() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function printSelectedDefinition() {
|
function printSelectedDefinition() {
|
||||||
local _DEFINITIONS _CORE_FILE_DEFAULT_ALL_HOSTS _CORE_FILE_DEFINED_ALL_HOSTS _CORE_FILE_DEFINED_THIS_HOST _FILE_DEFAULT_ALL_HOSTS _FILE_DEFINED_ALL_HOSTS _FILE_DEFINED_THIS_HOST
|
local -r _DEFINITIONS="${1:?"Missing CIS_ROOT"}definitions/${2:?"Missing DOMAIN"}/"
|
||||||
_DEFINITIONS="${1:?"Missing CIS_ROOT"}definitions/${2:?"Missing DOMAIN"}/"
|
local -r _CORE_FILE_DEFAULT_ALL_HOSTS="${1:?"Missing CIS_ROOT"}definitions/default/core/all${3:?"Missing CURRENT_FULLFILE"}"
|
||||||
_CORE_FILE_DEFAULT_ALL_HOSTS="${1:?"Missing CIS_ROOT"}definitions/default/core/all${3:?"Missing CURRENT_FULLFILE"}"
|
local -r _CORE_FILE_DEFINED_ALL_HOSTS="${_DEFINITIONS:?"Missing DEFINITIONS"}core/all${3:?"Missing CURRENT_FULLFILE"}"
|
||||||
_CORE_FILE_DEFINED_ALL_HOSTS="${_DEFINITIONS:?"Missing DEFINITIONS"}core/all${3:?"Missing CURRENT_FULLFILE"}"
|
local -r _CORE_FILE_DEFINED_THIS_HOST="${_DEFINITIONS:?"Missing DEFINITIONS"}core/$(hostname -s)${3:?"Missing CURRENT_FULLFILE"}"
|
||||||
_CORE_FILE_DEFINED_THIS_HOST="${_DEFINITIONS:?"Missing DEFINITIONS"}core/$(hostname -s)${3:?"Missing CURRENT_FULLFILE"}"
|
local -r _FILE_DEFAULT_ALL_HOSTS="${1:?"Missing CIS_ROOT"}definitions/default/hosts/all${3:?"Missing CURRENT_FULLFILE"}"
|
||||||
_FILE_DEFAULT_ALL_HOSTS="${1:?"Missing CIS_ROOT"}definitions/default/hosts/all${3:?"Missing CURRENT_FULLFILE"}"
|
local -r _FILE_DEFINED_ALL_HOSTS="${_DEFINITIONS:?"Missing DEFINITIONS"}hosts/all${3:?"Missing CURRENT_FULLFILE"}"
|
||||||
_FILE_DEFINED_ALL_HOSTS="${_DEFINITIONS:?"Missing DEFINITIONS"}hosts/all${3:?"Missing CURRENT_FULLFILE"}"
|
local -r _FILE_DEFINED_THIS_HOST="${_DEFINITIONS:?"Missing DEFINITIONS"}hosts/$(hostname -s)${3:?"Missing CURRENT_FULLFILE"}"
|
||||||
_FILE_DEFINED_THIS_HOST="${_DEFINITIONS:?"Missing DEFINITIONS"}hosts/$(hostname -s)${3:?"Missing CURRENT_FULLFILE"}"
|
|
||||||
readonly _DEFINITIONS _CORE_FILE_DEFAULT_ALL_HOSTS _CORE_FILE_DEFINED_ALL_HOSTS _CORE_FILE_DEFINED_THIS_HOST _FILE_DEFAULT_ALL_HOSTS _FILE_DEFINED_ALL_HOSTS _FILE_DEFINED_THIS_HOST
|
|
||||||
|
|
||||||
#The following are special definitions that affect the core functionality.
|
#The following are special definitions that affect the core functionality.
|
||||||
#Try this host first because it should be priorized.
|
#Try this host first because it should be priorized.
|
||||||
@@ -105,12 +101,10 @@ function printSelectedDefinition() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createSymlinkToDefinition() {
|
function createSymlinkToDefinition() {
|
||||||
local _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINED_FULLFILE _SAVED_FULLFILE
|
local -r _CURRENT_FOLDER="${1:?"Missing CURRENT_FOLDER"}"
|
||||||
_CURRENT_FOLDER="${1:?"Missing CURRENT_FOLDER"}"
|
local -r _CURRENT_FULLFILE="${2:?"Missing CURRENT_FULLFILE"}"
|
||||||
_CURRENT_FULLFILE="${2:?"Missing CURRENT_FULLFILE"}"
|
local -r _DEFINED_FULLFILE="${3:?"Missing DEFINED_FULLFILE"}"
|
||||||
_DEFINED_FULLFILE="${3:?"Missing DEFINED_FULLFILE"}"
|
local -r _SAVED_FULLFILE="${4:?"Missing SAVED_FULLFILE"}"
|
||||||
_SAVED_FULLFILE="${4:?"Missing SAVED_FULLFILE"}"
|
|
||||||
readonly _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINED_FULLFILE _SAVED_FULLFILE
|
|
||||||
|
|
||||||
[ -f "${_CURRENT_FULLFILE}" ] \
|
[ -f "${_CURRENT_FULLFILE}" ] \
|
||||||
&& [ "$(sha256sum "${_DEFINED_FULLFILE}" | cut -d' ' -f1)" == "$(sha256sum "${_CURRENT_FULLFILE}" | cut -d' ' -f1)" ] \
|
&& [ "$(sha256sum "${_DEFINED_FULLFILE}" | cut -d' ' -f1)" == "$(sha256sum "${_CURRENT_FULLFILE}" | cut -d' ' -f1)" ] \
|
||||||
@@ -132,36 +126,35 @@ function createSymlinkToDefinition() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function ensureUsageOfDefinitions() {
|
function ensureUsageOfDefinitions() {
|
||||||
local _CIS_ROOT _CURRENT_FILE _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINITIONS _DOMAIN _DEFINED_FULLFILE _NOW _SAVED_FULLFILE
|
local _DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")/"
|
||||||
_DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")/"
|
local -r _CIS_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
||||||
_CIS_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
local _DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
||||||
_DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
readonly _DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
||||||
_DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
|
||||||
#Build from components for safety
|
#Build from components for safety
|
||||||
_DEFINITIONS="$(printIfEqual "${_DEFINITIONS}" "${_CIS_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}/")"
|
readonly _DEFINITIONS="$(printIfEqual "${_DEFINITIONS}" "${_CIS_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}/")"
|
||||||
|
|
||||||
|
|
||||||
_CURRENT_FULLFILE="${2:?"Missing second parameter CURRENT_FULLFILE"}"
|
local _CURRENT_FULLFILE="${2:?"Missing second parameter CURRENT_FULLFILE"}"
|
||||||
_CURRENT_FOLDER="${_CURRENT_FULLFILE%/*}/" #Removes shortest matching pattern '/*' from the end
|
local _CURRENT_FOLDER="${_CURRENT_FULLFILE%/*}/" #Removes shortest matching pattern '/*' from the end
|
||||||
! [ -d "${_CURRENT_FOLDER}" ] \
|
if [ -d "${_CURRENT_FOLDER:?"Missing CURRENT_FOLDER"}" ]; then
|
||||||
|
_CURRENT_FOLDER="$(realpath -s "${_CURRENT_FOLDER}")"
|
||||||
|
readonly _CURRENT_FOLDER="${_CURRENT_FOLDER%/}/" #Removes shortest matching pattern '/' from the end
|
||||||
|
else
|
||||||
|
true \
|
||||||
&& echo "FAIL: The folder cannot be read: ("$(readlink -f ${0})")" \
|
&& echo "FAIL: The folder cannot be read: ("$(readlink -f ${0})")" \
|
||||||
&& echo " - '${_CURRENT_FOLDER}'" \
|
&& echo " - '${_CURRENT_FOLDER}'" \
|
||||||
&& echo " - user '"$(whoami)"' has insufficient rights on this host '$(hostname -s)'" \
|
&& echo " - user '"$(whoami)"' has insufficient rights on this host '$(hostname -s)'" \
|
||||||
&& echo " - or the folder does not exist." \
|
&& echo " - or the folder does not exist." \
|
||||||
&& return 1
|
&& return 1
|
||||||
|
fi
|
||||||
_CURRENT_FOLDER="$(realpath -s "${_CURRENT_FOLDER:?"Missing CURRENT_FOLDER"}")"
|
local -r _CURRENT_FILE="$(basename "${2:?"Missing second parameter CURRENT_FULLFILE"}")"
|
||||||
_CURRENT_FOLDER="${_CURRENT_FOLDER%/}/" #Removes shortest matching pattern '/' from the end
|
|
||||||
|
|
||||||
_CURRENT_FILE="$(basename "${2:?"Missing second parameter CURRENT_FULLFILE"}")"
|
|
||||||
#Build from components for safety
|
#Build from components for safety
|
||||||
_CURRENT_FULLFILE="${_CURRENT_FOLDER:?"Missing CURRENT_FOLDER"}${_CURRENT_FILE:?"Missing CURRENT_FILE"}"
|
readonly _CURRENT_FULLFILE="${_CURRENT_FOLDER:?"Missing CURRENT_FOLDER"}${_CURRENT_FILE:?"Missing CURRENT_FILE"}"
|
||||||
|
|
||||||
|
|
||||||
_DEFINED_FULLFILE="$(printSelectedDefinition "${_CIS_ROOT}" "${_DOMAIN}" "${_CURRENT_FULLFILE}")"
|
local -r _DEFINED_FULLFILE="$(printSelectedDefinition "${_CIS_ROOT}" "${_DOMAIN}" "${_CURRENT_FULLFILE}")"
|
||||||
_NOW="$(date +%Y%m%d_%H%M)"
|
local -r _NOW="$(date +%Y%m%d_%H%M)"
|
||||||
_SAVED_FULLFILE="${_CURRENT_FULLFILE}.backup@${_NOW:?"Missing NOW"}"
|
local -r _SAVED_FULLFILE="${_CURRENT_FULLFILE}.backup@${_NOW:?"Missing NOW"}"
|
||||||
readonly _CIS_ROOT _CURRENT_FILE _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINITIONS _DOMAIN _DEFINED_FULLFILE _NOW _SAVED_FULLFILE
|
|
||||||
|
|
||||||
[ -z "${_DEFINED_FULLFILE}" ] \
|
[ -z "${_DEFINED_FULLFILE}" ] \
|
||||||
&& isCoreDefinition "${_CURRENT_FULLFILE}" \
|
&& isCoreDefinition "${_CURRENT_FULLFILE}" \
|
||||||
|
|||||||
@@ -6,11 +6,12 @@
|
|||||||
&& exit 1
|
&& exit 1
|
||||||
|
|
||||||
function goOn() {
|
function goOn() {
|
||||||
local _QUESTION="${1:?"goOn(): Mising first parameter QUESTION"}"
|
local -r _QUESTION="${1:?"goOn(): Mising first parameter QUESTION"}"
|
||||||
local _TIPP="${2}"
|
local -r _TIPP="${2}"
|
||||||
local _ANSWER
|
local _ANSWER
|
||||||
|
|
||||||
read -p "${_QUESTION}: [y]es or [n]o : " _ANSWER
|
read -p "${_QUESTION}: [y]es or [n]o : " _ANSWER
|
||||||
|
readonly _ANSWER
|
||||||
[ "${_ANSWER}" == "y" ] && return 0
|
[ "${_ANSWER}" == "y" ] && return 0
|
||||||
[ "${_ANSWER}" == "Y" ] && return 0
|
[ "${_ANSWER}" == "Y" ] && return 0
|
||||||
[ "${_ANSWER}" == "yes" ] && return 0
|
[ "${_ANSWER}" == "yes" ] && return 0
|
||||||
@@ -18,13 +19,13 @@ function goOn() {
|
|||||||
[ "${_ANSWER}" == "YES" ] && return 0
|
[ "${_ANSWER}" == "YES" ] && return 0
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "${_TIPP}"
|
[ -n "${_TIPP}" ] && echo "${_TIPP}"
|
||||||
echo
|
echo
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function setNeededHostnameOrExit() {
|
function setNeededHostnameOrExit() {
|
||||||
_FQDN="${1}"
|
local -r _FQDN="${1}"
|
||||||
|
|
||||||
[ -z "${_FQDN}" ] \
|
[ -z "${_FQDN}" ] \
|
||||||
&& ! echo "$(hostname -b)" | grep -q -F '.' \
|
&& ! echo "$(hostname -b)" | grep -q -F '.' \
|
||||||
@@ -55,8 +56,8 @@ function setNeededHostnameOrExit() {
|
|||||||
function printOrGenerateSSHKeys() {
|
function printOrGenerateSSHKeys() {
|
||||||
git --version > /dev/null || (apt update; apt upgrade -y; apt install git)
|
git --version > /dev/null || (apt update; apt upgrade -y; apt install git)
|
||||||
|
|
||||||
local _FULL_USERNAME="$(whoami)@$(hostname -b)"
|
local -r _FULL_USERNAME="$(whoami)@$(hostname -b)"
|
||||||
local _PUBKEY_FILE=~/.ssh/id_ed25519.pub
|
local -r _PUBKEY_FILE=~/.ssh/id_ed25519.pub
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "Printing public SSH-Key of ${_FULL_USERNAME}:"
|
echo "Printing public SSH-Key of ${_FULL_USERNAME}:"
|
||||||
|
|||||||
+11
-25
@@ -32,9 +32,7 @@ function checkGitIsAvailable() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function checkPreconditions() {
|
function checkPreconditions() {
|
||||||
local _DOMAIN
|
local -r _DOMAIN="${1}" # Optional parameter DOMAIN
|
||||||
_DOMAIN="${1}" # Optional parameter DOMAIN
|
|
||||||
readonly _DOMAIN
|
|
||||||
|
|
||||||
! [ -z "${_DOMAIN}" ] \
|
! [ -z "${_DOMAIN}" ] \
|
||||||
&& [ "$(hostname -d)" != "${_DOMAIN}" ] \
|
&& [ "$(hostname -d)" != "${_DOMAIN}" ] \
|
||||||
@@ -69,11 +67,9 @@ function checkPreconditions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getOrSetDomain() {
|
function getOrSetDomain() {
|
||||||
local _CURRENT_DOMAIN _GIVEN_DOMAIN _OVERRIDE_DOMAIN_FILE
|
local -r _CURRENT_DOMAIN="${CIS[DOMAIN]:?"Missing CIS_DOMAIN"}"
|
||||||
_CURRENT_DOMAIN="${CIS[DOMAIN]:?"Missing CIS_DOMAIN"}"
|
local -r _GIVEN_DOMAIN="${1}" # Optional parameter DOMAIN
|
||||||
_GIVEN_DOMAIN="${1}" # Optional parameter DOMAIN
|
local -r _OVERRIDE_DOMAIN_FILE="${CIS[ROOT]:?"Missing CIS_ROOT"}overrideOwnDomain"
|
||||||
_OVERRIDE_DOMAIN_FILE="${CIS[ROOT]:?"Missing CIS_ROOT"}overrideOwnDomain"
|
|
||||||
readonly _CURRENT_DOMAIN _GIVEN_DOMAIN _OVERRIDE_DOMAIN_FILE
|
|
||||||
|
|
||||||
! [ -z "${_CURRENT_DOMAIN}" ] \
|
! [ -z "${_CURRENT_DOMAIN}" ] \
|
||||||
&& [ -z "${_GIVEN_DOMAIN}" ] \
|
&& [ -z "${_GIVEN_DOMAIN}" ] \
|
||||||
@@ -104,9 +100,8 @@ function getOrSetDomain() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getRemoteRepositoryPath() {
|
function getRemoteRepositoryPath() {
|
||||||
local _REPOSITORY="$(git -C "${CIS[ROOT]:?"Missing CIS_ROOT"}" config --get remote.origin.url 2> /dev/null | grep -i 'git@')"
|
local -r _REPOSITORY="$(git -C "${CIS[ROOT]:?"Missing CIS_ROOT"}" config --get remote.origin.url 2> /dev/null | grep -i 'git@')"
|
||||||
local _PATH="${_REPOSITORY%/*}" #Removes shortest matching pattern '/*' from the end
|
local -r _PATH="${_REPOSITORY%/*}" #Removes shortest matching pattern '/*' from the end
|
||||||
readonly _REPOSITORY _PATH
|
|
||||||
|
|
||||||
! [ -z "${_PATH}" ] \
|
! [ -z "${_PATH}" ] \
|
||||||
&& echo "${_PATH}/" \
|
&& echo "${_PATH}/" \
|
||||||
@@ -116,9 +111,7 @@ function getRemoteRepositoryPath() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setSSLPermissions(){
|
function setSSLPermissions(){
|
||||||
local _SSLPATHS
|
local -r _SSLPATHS="${CIS[DOMAINSTATES]}hosts/*/etc/ssl/domains/"
|
||||||
_SSLPATHS="${CIS[DOMAINSTATES]}hosts/*/etc/ssl/domains/"
|
|
||||||
readonly _SSLPATHS
|
|
||||||
|
|
||||||
addgroup --gid 775 ssl 2> /dev/null
|
addgroup --gid 775 ssl 2> /dev/null
|
||||||
for _path in ${_SSLPATHS}; do
|
for _path in ${_SSLPATHS}; do
|
||||||
@@ -132,9 +125,7 @@ function setSSLPermissions(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addDefinition(){
|
function addDefinition(){
|
||||||
local _REPOSITORY
|
local -r _REPOSITORY="$(getRemoteRepositoryPath)cis-definition-${CIS[DOMAIN]}.git"
|
||||||
_REPOSITORY="$(getRemoteRepositoryPath)cis-definition-${CIS[DOMAIN]}.git"
|
|
||||||
readonly _REPOSITORY
|
|
||||||
|
|
||||||
[ "$(id -u)" == "0" ] \
|
[ "$(id -u)" == "0" ] \
|
||||||
&& echo \
|
&& echo \
|
||||||
@@ -154,9 +145,7 @@ function addDefinition(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function addState() {
|
function addState() {
|
||||||
local _REPOSITORY
|
local -r _REPOSITORY="$(getRemoteRepositoryPath)cis-state-${CIS[DOMAIN]}.git"
|
||||||
_REPOSITORY="$(getRemoteRepositoryPath)cis-state-${CIS[DOMAIN]}.git"
|
|
||||||
readonly _REPOSITORY
|
|
||||||
|
|
||||||
[ "$(id -u)" == "0" ] \
|
[ "$(id -u)" == "0" ] \
|
||||||
&& echo \
|
&& echo \
|
||||||
@@ -178,9 +167,7 @@ function addState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setupCoreFunctionality() {
|
function setupCoreFunctionality() {
|
||||||
local _MINUTE_FROM_OWN_IP
|
local -r _MINUTE_FROM_OWN_IP="$(hostname -I | xargs -n 1 | grep -F '.' | head -n 1 | cut -d. -f4 || echo 0)" #uses last value from first own ipv4 or 0 as minute value
|
||||||
_MINUTE_FROM_OWN_IP="$(hostname -I | xargs -n 1 | grep -F '.' | head -n 1 | cut -d. -f4 || echo 0)" #uses last value from first own ipv4 or 0 as minute value
|
|
||||||
readonly _MINUTE_FROM_OWN_IP
|
|
||||||
|
|
||||||
[ "$(id -u)" != "0" ] \
|
[ "$(id -u)" != "0" ] \
|
||||||
&& echo \
|
&& echo \
|
||||||
@@ -208,8 +195,7 @@ function setupCoreFunctionality() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setup() {
|
function setup() {
|
||||||
local _DOMAIN="$(getOrSetDomain "${1}")"
|
local -r _DOMAIN="$(getOrSetDomain "${1}")"
|
||||||
readonly _DOMAIN
|
|
||||||
|
|
||||||
! checkPreconditions "${_DOMAIN}" \
|
! checkPreconditions "${_DOMAIN}" \
|
||||||
&& return 1
|
&& return 1
|
||||||
|
|||||||
@@ -23,8 +23,7 @@ source /cis/core/base.module.sh
|
|||||||
|
|
||||||
|
|
||||||
function update_repositories() {
|
function update_repositories() {
|
||||||
local _MODE="${1:-"--core"}"
|
local -r _MODE="${1:-"--core"}"
|
||||||
readonly _MODE
|
|
||||||
|
|
||||||
[ "${_MODE}" == "--repair" ] \
|
[ "${_MODE}" == "--repair" ] \
|
||||||
&& (git -C "${CIS[ROOT]:?"Missing CISROOT"}" reset --hard origin/main; \
|
&& (git -C "${CIS[ROOT]:?"Missing CISROOT"}" reset --hard origin/main; \
|
||||||
|
|||||||
Reference in new issue
Block a user