mirror of
https://github.com/m8tin/cis.git
synced 2025-12-06 07:48:26 +01:00
publish ISS
This commit is contained in:
101
core/addAndCheckGitRepository.sh
Executable file
101
core/addAndCheckGitRepository.sh
Executable file
@@ -0,0 +1,101 @@
|
||||
#!/bin/bash
|
||||
|
||||
#WARNING: Used for core functionality in setup.sh
|
||||
# DO NOT rename the script and test changes well!
|
||||
|
||||
|
||||
|
||||
function checkPermissions(){
|
||||
local _FOLDER _REPOSITORY
|
||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||
_RIGHTS="${2:?"Missing second parameter RIGHTS"}"
|
||||
readonly _FOLDER _REPOSITORY
|
||||
|
||||
[ "${_RIGHTS}" == "readonly" ] \
|
||||
&& [ -d "${_FOLDER}/.git" ] \
|
||||
&& ! git -C "${_FOLDER}" push --dry-run &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
[ "${_RIGHTS}" == "writable" ] \
|
||||
&& [ -d "${_FOLDER}/.git" ] \
|
||||
&& git -C "${_FOLDER}" push --dry-run &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The rights of the repository are incorrect: ("$(readlink -f ${0})")"
|
||||
echo " - '${_FOLDER}' is not '${_RIGHTS}'"
|
||||
echo " - check the settings of gitea."
|
||||
return 1
|
||||
}
|
||||
|
||||
function checkRemoteRepository() {
|
||||
local _FOLDER _REPOSITORY
|
||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||
_REPOSITORY="${2:?"Missing second parameter REPOSITORY"}"
|
||||
readonly _FOLDER _REPOSITORY
|
||||
|
||||
#Should exist after successful clone only, therefore the remote repository exists and was accessible.
|
||||
[ -d "${_FOLDER}/.git" ] \
|
||||
&& return 0
|
||||
|
||||
#Checks if repository exists and is accessible.
|
||||
! [ -d "${_FOLDER}/.git" ] \
|
||||
&& git ls-remote "${_REPOSITORY}" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The remote repository is not accessible: ("$(readlink -f ${0})")"
|
||||
echo " - '${_REPOSITORY}'"
|
||||
echo " - check the settings of gitea."
|
||||
return 1
|
||||
}
|
||||
|
||||
function cloneOrPull {
|
||||
local _FOLDER _REPOSITORY
|
||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||
_REPOSITORY="${2:?"Missing second parameter REPOSITORY"}"
|
||||
readonly _FOLDER _REPOSITORY
|
||||
|
||||
! [ -d "${_FOLDER}/.git" ] \
|
||||
&& git clone "${_REPOSITORY}" "${_FOLDER}" &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
[ -d "${_FOLDER}/.git" ] \
|
||||
&& git -C "${_FOLDER}" pull &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The local repository is not updatable: ("$(readlink -f ${0})")"
|
||||
echo " - '${_FOLDER}'"
|
||||
echo " - check your network and the permissions in gitea."
|
||||
return 1
|
||||
}
|
||||
|
||||
# Note that an unprivileged user can use this script successfully,
|
||||
# if no user has to be added to the host because it already exists.
|
||||
function addAndCheckGitRepository() {
|
||||
local _FOLDER _REPOSITORY
|
||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||
_REPOSITORY="${2:?"Missing second parameter REPOSITORY: e.g. ssh://git@your.domain.com/iss.git "}"
|
||||
_RIGHTS="${3:?"Missing third parameter RIGHTS: (readonly, writable) "}"
|
||||
readonly _FOLDER _REPOSITORY
|
||||
|
||||
checkRemoteRepository "${_FOLDER}" "${_REPOSITORY}" \
|
||||
&& cloneOrPull "${_FOLDER}" "${_REPOSITORY}" \
|
||||
&& checkPermissions "${_FOLDER}" "${_RIGHTS}" \
|
||||
&& echo "SUCCESS: The git repository is usable. ("$(readlink -f ${0})")" \
|
||||
&& echo " - remote repository: '${_REPOSITORY}'" \
|
||||
&& echo " - local repository: '${_FOLDER}' (${_RIGHTS})" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The repository is not functional: ("$(readlink -f ${0})")"
|
||||
echo " - remote repository: '${_REPOSITORY}'"
|
||||
echo " - local repository: '${_FOLDER}'"
|
||||
echo " - due to an error or insufficient rights or"
|
||||
echo " - one check failed."
|
||||
return 1
|
||||
}
|
||||
|
||||
# sanitizes all parameters
|
||||
addAndCheckGitRepository \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${2} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${3} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
39
core/addNormalUser.sh
Executable file
39
core/addNormalUser.sh
Executable file
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
|
||||
#WARNING: Used for core functionality in setup.sh
|
||||
# DO NOT rename the script and test changes well!
|
||||
|
||||
|
||||
|
||||
# Note that an unprivileged user can use this script successfully,
|
||||
# if no user has to be added to the host because it already exists.
|
||||
function addNormalUser() {
|
||||
local _USER
|
||||
_USER="${1:?"Missing first parameter USER"}"
|
||||
readonly _USER
|
||||
|
||||
#The user already exists
|
||||
id -u "${_USER}" &> /dev/null \
|
||||
&& echo "SUCCESS: The user already exists: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_USER}'" \
|
||||
&& return 0
|
||||
|
||||
[ "$(id -u)" == "0" ] \
|
||||
&& adduser --gecos 'Normal user' --disabled-password "${_USER}" \
|
||||
&& chown -R "${_USER}:${_USER}" "/home/${_USER}" \
|
||||
&& echo "SUCCESS: The user was created: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_USER}'" \
|
||||
&& echo " - no password was set, use passwd if needed" \
|
||||
&& echo " - existing home directories were taken over" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The user could not be created: ("$(readlink -f ${0})")"
|
||||
echo " - '${_USER}'"
|
||||
echo " - due to an error or insufficient rights."
|
||||
return 1
|
||||
}
|
||||
|
||||
# sanitizes all parameters
|
||||
addNormalUser \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
50
core/addToCrontabEveryHour.sh
Executable file
50
core/addToCrontabEveryHour.sh
Executable file
@@ -0,0 +1,50 @@
|
||||
#!/bin/bash
|
||||
|
||||
#WARNING: Used for core functionality in setup.sh
|
||||
# DO NOT rename the script and test changes well!
|
||||
|
||||
|
||||
|
||||
# Note that an unprivileged user can use this script successfully,
|
||||
# if no user has to be added to the host because it already exists.
|
||||
function addToCrontabEveryHour() {
|
||||
local _ROOT _MINUTE_VALUE _STRING
|
||||
_ROOT="${0%%/core/*}/" #Removes longest matching pattern '/core/*' from the end
|
||||
! [ -z "${2##*[!0-9]*}" ] && _MINUTE_VALUE=$((${2}%60)) # if second parameter is integer then (minute-value % 60) as safe guard
|
||||
_STRING="${_MINUTE_VALUE:?"Missing MINUTE_VALUE"} * * * * ${1:?"Missing first parameter COMMAND"} > /dev/null 2>&1"
|
||||
readonly _ROOT _MINUTE_VALUE _STRING
|
||||
|
||||
[ "$(id -u)" == "0" ] \
|
||||
&& crontab -l | grep -qF "${_STRING:?"Missing CRON_STRING"}" \
|
||||
&& echo "SUCCESS: Entry already is registered to crontab: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_STRING}'" \
|
||||
&& return 0
|
||||
|
||||
[ "$(id -u)" == "0" ] \
|
||||
&& echo "${_ROOT:?"Missing ROOT"}" | grep "home" &> /dev/null \
|
||||
&& echo "SUCCESS: Although the entry will be skipped: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_STRING}'" \
|
||||
&& echo " that is because the current environment is:" \
|
||||
&& echo " - ${_ROOT}" \
|
||||
&& return 0
|
||||
|
||||
[ "$(id -u)" == "0" ] \
|
||||
&& (crontab -l; \
|
||||
echo "# Every hour at ?:${_MINUTE_VALUE:?"Missing MINUTE_VALUE"}:"; \
|
||||
echo "${_STRING:?"Missing CRON_STRING"}") | crontab - \
|
||||
&& crontab -l | grep -qF "${_STRING:?"Missing CRON_STRING"}" \
|
||||
&& echo "SUCCESS: Entry is registered to crontab now: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_STRING}'" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: Entry could not be registered to crontab: ("$(readlink -f ${0})")"
|
||||
echo " - '${_STRING:?"Missing CRON_STRING"}'"
|
||||
echo " - due to an error or insufficient rights."
|
||||
return 1
|
||||
}
|
||||
|
||||
# sanitizes all parameters
|
||||
addToCrontabEveryHour \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${2} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
86
core/defineAuthorizedKeysOfUser.sh
Executable file
86
core/defineAuthorizedKeysOfUser.sh
Executable file
@@ -0,0 +1,86 @@
|
||||
#!/bin/bash
|
||||
|
||||
#WARNING: Used for core functionality in setup.sh
|
||||
# DO NOT rename the script and test changes well!
|
||||
|
||||
function prepareFolder() {
|
||||
local _HOME_FOLDER _SSH_FOLDER _USER
|
||||
_SSH_FOLDER="${1:?"prepareFolder(): Missing parameter SSH_PATH"}"
|
||||
_HOME_FOLDER="${_SSH_FOLDER%%/.ssh*}" #Removes longest matching pattern '/.ssh*' from the end
|
||||
_USER="${_HOME_FOLDER##*/}" #Removes longest matching pattern '*/' from the begin
|
||||
readonly _HOME_FOLDER _SSH_FOLDER _USER
|
||||
|
||||
! [ -d "${_HOME_FOLDER}" ] \
|
||||
&& echo "FAIL: The home folder is unavailable: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_HOME_FOLDER}'" \
|
||||
&& return 1
|
||||
|
||||
#The ssh folder already exists
|
||||
[ -d "${_SSH_FOLDER}" ] \
|
||||
&& [ "$(stat -c '%U:%G' "${_SSH_FOLDER}")" == "${_USER}:${_USER}" ] \
|
||||
&& [ "$(stat -c '%a' "${_SSH_FOLDER}")" == "700" ] \
|
||||
&& echo "SUCCESS: The ssh folder already exists: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_SSH_FOLDER}'" \
|
||||
&& return 0
|
||||
|
||||
#The calling user can create its own folder
|
||||
! [ -d "${_SSH_FOLDER}" ] \
|
||||
&& [ "${_USER:?"Missing USER"}" == "$(whoami)" ] \
|
||||
&& mkdir -p "${_SSH_FOLDER}" \
|
||||
&& chown "${_USER}:${_USER}" "${_SSH_FOLDER}" \
|
||||
&& chmod go-rwx "${_SSH_FOLDER}" \
|
||||
&& echo "SUCCESS: The ssh folder was created: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_SSH_FOLDER}'" \
|
||||
&& return 0
|
||||
|
||||
#The root user can create every folder
|
||||
! [ -d "${_SSH_FOLDER}" ] \
|
||||
&& [ "${_USER:?"Missing USER"}" != "$(whoami)" ] \
|
||||
&& [ "$(id -u)" == "0" ] \
|
||||
&& mkdir -p "${_SSH_FOLDER}" \
|
||||
&& chown "${_USER}:${_USER}" "${_SSH_FOLDER}" \
|
||||
&& chmod go-rwx "${_SSH_FOLDER}" \
|
||||
&& echo "SUCCESS: The ssh folder was created: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_SSH_FOLDER}'" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The ssh folder could not be prepared: ("$(readlink -f ${0})")"
|
||||
echo " - '${_SSH_FOLDER}'"
|
||||
echo " - due to an error or insufficient rights."
|
||||
return 1
|
||||
}
|
||||
|
||||
function defineAuthorizedKeysOfUser() {
|
||||
local _ROOT _CORE_SCRIPTS _DOMAIN _DEFINITIONS _USER
|
||||
_DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")"
|
||||
_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
||||
_DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
||||
_DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
||||
#Build from components for safety
|
||||
_DEFINITIONS="${_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}"
|
||||
|
||||
_USER="${2:?"Missing second parameter USER"}"
|
||||
_CORE_SCRIPTS="${_ROOT:?"Missing ROOT"}core/"
|
||||
readonly _ROOT _CORE_SCRIPTS _DOMAIN _DEFINITIONS _USER
|
||||
|
||||
case "${_USER:?"Missing USER"}" in
|
||||
root)
|
||||
prepareFolder "/root/.ssh" \
|
||||
&& echo \
|
||||
&& source "${_CORE_SCRIPTS:?"Missing CORE_SCRIPTS"}ensureUsageOfDefinitions.sh" "${_DEFINITIONS}" "/root/.ssh/authorized_keys" \
|
||||
&& return 0 || return 1
|
||||
;;
|
||||
*)
|
||||
prepareFolder "/home/${_USER}/.ssh" \
|
||||
&& echo \
|
||||
&& source "${_CORE_SCRIPTS:?"Missing CORE_SCRIPTS"}ensureUsageOfDefinitions.sh" "${_DEFINITIONS}" "/home/${_USER}/.ssh/authorized_keys" \
|
||||
&& return 0 || return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# sanitizes all parameters
|
||||
defineAuthorizedKeysOfUser \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${2} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
177
core/ensureUsageOfDefinitions.sh
Executable file
177
core/ensureUsageOfDefinitions.sh
Executable file
@@ -0,0 +1,177 @@
|
||||
#!/bin/bash
|
||||
|
||||
#WARNING: Used for core functionality in setup.sh
|
||||
# DO NOT rename the script and test changes well!
|
||||
|
||||
function printIfEqual() {
|
||||
[ "${1:?"Missing first parameter"}" == "${2}" ] \
|
||||
&& echo "${1}" \
|
||||
&& return 0
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
function isCoreDefinition() {
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep "/root/.ssh/authorized_keys" &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep "/home/jenkins/.ssh/authorized_keys" &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep "/etc/sudoers.d/allow-jenkins-updateRepositories" &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
function printSelectedDefinition() {
|
||||
local _CORE_FILE_DEFINED_ALL_HOSTS _CORE_FILE_DEFINED_THIS_HOST _FILE_DEFINED_ALL_HOSTS _FILE_DEFINED_THIS_HOST
|
||||
_CORE_FILE_DEFINED_ALL_HOSTS="${1:?"Missing DEFINITIONS"}/core/all${2:?"Missing CURRENT_FULLFILE"}"
|
||||
_CORE_FILE_DEFINED_THIS_HOST="${1:?"Missing DEFINITIONS"}/core/$(hostname -s)${2:?"Missing CURRENT_FULLFILE"}"
|
||||
_FILE_DEFINED_ALL_HOSTS="${1:?"Missing DEFINITIONS"}/hosts/all${2:?"Missing CURRENT_FULLFILE"}"
|
||||
_FILE_DEFINED_THIS_HOST="${1:?"Missing DEFINITIONS"}/hosts/$(hostname -s)${2:?"Missing CURRENT_FULLFILE"}"
|
||||
readonly _CORE_FILE_DEFINED_ALL_HOSTS _CORE_FILE_DEFINED_THIS_HOST _FILE_DEFINED_ALL_HOSTS _FILE_DEFINED_THIS_HOST
|
||||
|
||||
#The following are special definitions that affect the core functionality.
|
||||
#Try this host first because it should be priorized.
|
||||
isCoreDefinition "${2:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_CORE_FILE_DEFINED_THIS_HOST}" ] \
|
||||
&& echo "${_CORE_FILE_DEFINED_THIS_HOST}" \
|
||||
&& return 0
|
||||
|
||||
#The following are special definitions that affect the core functionality.
|
||||
isCoreDefinition "${2:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_CORE_FILE_DEFINED_ALL_HOSTS}" ] \
|
||||
&& echo "${_CORE_FILE_DEFINED_ALL_HOSTS}" \
|
||||
&& return 0
|
||||
|
||||
#Try this host first because it should be priorized.
|
||||
! isCoreDefinition "${2:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_FILE_DEFINED_THIS_HOST}" ] \
|
||||
&& echo "${_FILE_DEFINED_THIS_HOST}" \
|
||||
&& return 0
|
||||
|
||||
! isCoreDefinition "${2:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_FILE_DEFINED_ALL_HOSTS}" ] \
|
||||
&& echo "${_FILE_DEFINED_ALL_HOSTS}" \
|
||||
&& return 0
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
function createSymlinkToDefinition() {
|
||||
local _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINED_FULLFILE _SAVED_FULLFILE
|
||||
_CURRENT_FOLDER="${1:?"Missing CURRENT_FOLDER"}"
|
||||
_CURRENT_FULLFILE="${2:?"Missing CURRENT_FULLFILE"}"
|
||||
_DEFINED_FULLFILE="${3:?"Missing DEFINED_FULLFILE"}"
|
||||
_SAVED_FULLFILE="${4:?"Missing SAVED_FULLFILE"}"
|
||||
readonly _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINED_FULLFILE _SAVED_FULLFILE
|
||||
|
||||
[ -f "${_CURRENT_FULLFILE}" ] \
|
||||
&& [ "$(sha256sum "${_DEFINED_FULLFILE}" | cut -d' ' -f1)" == "$(sha256sum "${_CURRENT_FULLFILE}" | cut -d' ' -f1)" ] \
|
||||
&& echo "The content of the current file already matches the definition, but it will be replaced by a symlink..."
|
||||
|
||||
[ -f "${_CURRENT_FULLFILE}" ] \
|
||||
&& [ "$(sha256sum "${_DEFINED_FULLFILE}" | cut -d' ' -f1)" == "$(sha256sum "${_CURRENT_FULLFILE}" | cut -d' ' -f1)" ] \
|
||||
&& echo "The content of the current file already matches the definition, but it will be replaced by a symlink..."
|
||||
|
||||
|
||||
[ -f "${_CURRENT_FULLFILE}" ] \
|
||||
&& mv "${_CURRENT_FULLFILE:?"Missing CURRENT_FULLFILE"}" "${_SAVED_FULLFILE:?"Missing SAVED_FULLFILE"}" \
|
||||
&& echo "Current file has been backed up to: '${_SAVED_FULLFILE}'"
|
||||
|
||||
[ -d "${_CURRENT_FOLDER}" ] \
|
||||
&& ln -s -f "${_DEFINED_FULLFILE}" "${_CURRENT_FULLFILE}" \
|
||||
&& return 0
|
||||
|
||||
[ -f "${_SAVED_FULLFILE}" ] \
|
||||
&& cp --remove-destination "${_SAVED_FULLFILE}" "${_CURRENT_FULLFILE}" \
|
||||
&& echo "File restored due to a failure."
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
function ensureUsageOfDefinitions() {
|
||||
local _ROOT _CURRENT_FILE _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINITIONS _DOMAIN _DEFINED_FULLFILE _NOW _SAVED_FULLFILE
|
||||
_DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")"
|
||||
_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
||||
_DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
||||
_DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
||||
#Build from components for safety
|
||||
_DEFINITIONS="$(printIfEqual "${_DEFINITIONS}" "${_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}")"
|
||||
|
||||
|
||||
_CURRENT_FOLDER="$(dirname "${2:?"Missing second parameter CURRENT_FULLFILE"}")"
|
||||
_CURRENT_FOLDER="${_CURRENT_FOLDER%/}/" #Removes shortest matching pattern '/' from the end
|
||||
! [ -d "${_CURRENT_FOLDER}" ] \
|
||||
&& echo "FAIL: The folder cannot be read: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_CURRENT_FOLDER}'" \
|
||||
&& echo " - user '"$(whoami)"' has insufficient rights on this host '$(hostname -s)'" \
|
||||
&& echo " - or the folder does not exist." \
|
||||
&& return 1
|
||||
|
||||
_CURRENT_FOLDER="$(realpath -s "${_CURRENT_FOLDER:?"Missing CURRENT_FOLDER"}")"
|
||||
_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
|
||||
_CURRENT_FULLFILE="${_CURRENT_FOLDER:?"Missing CURRENT_FOLDER"}${_CURRENT_FILE:?"Missing CURRENT_FILE"}"
|
||||
|
||||
|
||||
_DEFINED_FULLFILE="$(printSelectedDefinition "${_DEFINITIONS}" "${_CURRENT_FULLFILE}")"
|
||||
_NOW="$(date +%Y%m%d_%H%M)"
|
||||
_SAVED_FULLFILE="${_CURRENT_FULLFILE}-backup@${_NOW:?"Missing NOW"}"
|
||||
readonly _ROOT _CURRENT_FILE _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINITIONS _DOMAIN _DEFINED_FULLFILE _NOW _SAVED_FULLFILE
|
||||
|
||||
! [ -f "${_DEFINED_FULLFILE}" ] \
|
||||
&& echo "FAIL: No definition available for this file: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_CURRENT_FULLFILE}'" \
|
||||
&& return 1
|
||||
|
||||
! [ -s "${_DEFINED_FULLFILE}" ] \
|
||||
&& echo "FAIL: No content available for this file: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_CURRENT_FULLFILE}'" \
|
||||
&& return 1
|
||||
|
||||
[ "${_DEFINED_FULLFILE}" == "$(readlink -f "${_CURRENT_FULLFILE}")" ] \
|
||||
&& echo "SUCCESS: The definition already is in place: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_DEFINED_FULLFILE}'" \
|
||||
&& return 0
|
||||
|
||||
echo "${_ROOT:?"Missing ROOT"}" | grep "home" &> /dev/null \
|
||||
&& echo "SUCCESS: Although this definition will be skipped: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_DEFINED_FULLFILE}'" \
|
||||
&& echo " that is because the current environment is:" \
|
||||
&& echo " - ${_ROOT}" \
|
||||
&& echo " following file is in use:" \
|
||||
&& echo " - $(readlink -f "${_CURRENT_FULLFILE}")" \
|
||||
&& return 0
|
||||
|
||||
! [ -w "${_CURRENT_FOLDER}" ] \
|
||||
&& echo "FAIL: The current file cannot be added: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_CURRENT_FULLFILE}'" \
|
||||
&& echo " - user '$(whoami)' has insufficient rights on this host '$(hostname -s)'" \
|
||||
&& return 1
|
||||
|
||||
[ -f "${_CURRENT_FULLFILE}" ] \
|
||||
&& ! [ -w "${_CURRENT_FULLFILE}" ] \
|
||||
&& echo "FAIL: The current file cannot be modified: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_CURRENT_FULLFILE}'" \
|
||||
&& echo " - user '$(whoami)' has insufficient rights on this host '$(hostname -s)'" \
|
||||
&& return 1
|
||||
|
||||
createSymlinkToDefinition "${_CURRENT_FOLDER}" "${_CURRENT_FULLFILE}" "${_DEFINED_FULLFILE}" "${_SAVED_FULLFILE}" \
|
||||
&& echo "SUCCESS: The definition was ensured: ("$(readlink -f ${0})")" \
|
||||
&& echo "- '${_DEFINED_FULLFILE}'" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The definition could not be ensured: ("$(readlink -f ${0})")"
|
||||
echo " - due to an error or insufficient rights."
|
||||
return 1
|
||||
}
|
||||
|
||||
# sanitizes all parameters
|
||||
ensureUsageOfDefinitions \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${2} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
Reference in New Issue
Block a user