## DebMirror Manager Functions Library function funcAddLocalRepo { ## This function performs several checks in a sequence... ## 1. Validates the given local repo name, checks if it is in ${localrepos} and that there is no existing repo file before adding it to ${localrepos} (must be lowercase, digits and dashes only, no leading or trailing dashes) ## 2. The given pretty name is added to ${prettynames} ## 3. Validates the given remote FQDN and checks if it can be resolved to an IP address (must contain lowercase, digits, dashes and periods only, no leading or trailing dashes, no leading or trailing periods, no dashes or periods adjacent to periods) ## 4. Validates the given remote repo name (must be lowercase, digits and dashes only, no leading or trailing dashes) ## 5. Validates the given section (must be lowercase, digits and dashes only, no leading or trailing dashes) ## 6. Validates the given distributions list (comma delimited list, must be lowercase and digits only) ## 7. Validates the given architectures list (comma delimited list, must be lowercase and digits only) ## 8. Validates the given method to be either http or https (if omitted it will default to https) ## 9. Validates the given keyring URL (must be a valid URL to the remote repo GPG key, if omitted no keyring will be used) ## 10. Validates the given boolean (true or false) for if sources data must be included in the local repo (normally only required if you intend to maintain the Deb packages on the mirror, if omitted then it will be set to false) ## 11. Validates the given boolean (true or false) for if the Debian Install data must be included in the local repo (normally only required when mirroring the official Debian repo, if omitted then it will be set to false) ## When all check pass, a new repo file {localreponame}.repo is created in /etc/debmirrorman, settings.conf is updated in /etc/debmirrorman, and if required the GPG key is downloaded and saved to /etc/debmirrorman as {localreponame}.gpg ## Example Usage: funcAddLocalRepo {localreponame} desc="{prettyname}" fqdn="{remotefqdn}" repo={remotereponame} sect={sectionname} dist="{distributions}" arch="{architectures}" meth={http|https} kurl="{keyringurl}" srcs={true|false} debi={true|false} ## If successful, this function will only returns exit code 0 ## If any step fails, this function will output an error message and exit with the corresponding exit code ## 0 - Adding local repo was successful ## 31 - No local repo name given (hard exit) ## 32 - Given local repo name is invalid (hard exit) ## 33 - Given local repo name already exists in ${localrepos} (hard exit) ## 34 - There is already a file {localreponame}.repo in /etc/debmirrorman (hard exit) ## 35 - No pretty name given (hard exit) ## 36 - No remote FQDN given (hard exit) ## 37 - Given remote FQDN is invalid (hard exit) ## 38 - Given remote FQDN does not resolve to an IP address (hard exit) ## 39 - No remote repo name given (hard exit) ## 40 - Given remote repo name is invalid (hard exit) ## 41 - No section name given (hard exit) ## 42 - Given section name is invalid (hard exit) ## 43 - No distributions list given (hard exit) ## 44 - Given distributions list is invalid (hard exit) ## 45 - No architectures list given (hard exit) ## 46 - Given architectures list is invalid (hard exit) ## 127 - Unknown error funcDebugMessage "Function called..." if [[ -z "${1}" ]]; then funcDebugMessage "No repo name given..." return 125 else local localrepo="${1}" fi if [[ -z "${2}" ]]; then funcDebugMessage "No pretty name given..." return 126 else local prettyname="${2}" fi if funcValidateRepoName "${repo}"; then funcDebugMessage "Val" echo ${localrepos} fi } function funcRemoveLocalRepo { # doRemoveRepo donothing=0 } function funcWriteSettingsFile { # doAddRepo donothing=0 } function funcWriteRepoFile { # doAddRepo donothing=0 } function funcCheckLocalRepo { ## Validates the given repo name, checks it is in ${localrepos} and that the repo file exists ## Example Usage: funcCheckLocalRepo {reponame} ## Given repo name must contain lowercase, digits and dashes only (no leading or trailing dashes) ## This function only returns exit codes ## 0 - All checks passed ## 1 - Invalid repo name ## 2 - Repo name is not in ${localrepos} ## 3 - Repo file does not exist ## 126 - No repo name given ## 127 - Unknown error funcDebugMessage "Function called..." if [[ -z "${1}" ]]; then funcDebugMessage "No repo name given..." return 126 else local repo="${1}" fi if funcValidateRepoName ${repo}; then if funcIsLocalRepo ${repo}; then if funcDoesRepoFileExist ${repo}; then funcDebugMessage "All checks passed..." return 0 else funcDebugMessage "File '${etcdirectory}/${repo}.repo' does not exist..." return 3 fi else funcDebugMessage "Repo name is not in \${localrepos}..." return 2 fi else funcDebugMessage "Invalid repo name..." return 1 fi funcDebugMessage "Unknown Error..." return 127 } function funcDebugMessage { ## If ${debugmode} == true then output the given text to STDERR ## Example Usage: funcDebugMessage "This is a debug message" ## 0 - Message sent to STDERR or ${debugmode} == false ## 126 - No message was given but ${debugmode} == true ## 127 - Unknown error [[ -n ${debugmode} ]] || debugmode=false [[ ${debugmode} == true ]] || return 0 [[ -n "${1}" ]] || return 126 local message="${1}" echo -e "DEBUG [function ${FUNCNAME[1]}]: ${message}" >&2 && return 0 return 127 } function funcDoesRepoFileExist { ## Checks that the given repo name exists as a file in /etc/debmirrorman ## Example Usage: funcDoesRepoFileExist {reponame} ## Given repo name must contain lowercase, digits and dashes only (no leading or trailing dashes) ## This function only returns exit codes ## 0 - Repo file exists ## 1 - Repo file does not exist ## 126 - No repo name given ## 127 - Unknown error funcDebugMessage "Function called..." if [[ -z "${1}" ]]; then funcDebugMessage "No repo name given..." return 126 else local repo="${1}" fi if [[ -f "${etcdirectory}/${repo}.repo" ]]; then funcDebugMessage "File '${etcdirectory}/${repo}.repo' exists..." return 0 else funcDebugMessage "File '${etcdirectory}/${repo}.repo' does not exist..." return 1 fi funcDebugMessage "Unknown Error..." return 127 } function funcError { ## Outputs the given error message to STDERR and exits with the given exit code ## Example Usage: funcError "{errormessage}" {exitcode} ## Exit code must be an integer between 1 and 127. If omitted then 127 will be used. funcDebugMessage "Function called..." if [[ -z ${1} ]]; then funcDebugMessage "No error message given..." echo "ERROR: An unknown error occurred..." >&2 else funcDebugMessage "Error message given..." echo -e "ERROR: ${1}" >&2 fi if [[ -n ${2} ]] && [[ ${2} -ge 1 ]] && [[ ${2} -le 127 ]]; then funcDebugMessage "Valid exit code given..." exit ${2} fi funcDebugMessage "No valid exit code given..." exit 127 } function funcIsLocalRepo { ## Checks that the given repo name exists in the array ${localrepos} ## Example Usage: funcIsLocalRepo {reponame} ## Given repo name must contain lowercase, digits and dashes only (no leading or trailing dashes) ## This function only returns exit codes ## 0 - Repo name is in ${localrepos} ## 1 - Repo name is not in ${localrepos} ## 126 - No repo name given ## 127 - Unknown error funcDebugMessage "Function called..." if [[ -z "${1}" ]]; then funcDebugMessage "No repo name given..." return 126 else local repo="${1}" fi if echo ${localrepos[@]} | tr ' ' '\n' | grep -q ^${repo}$; then funcDebugMessage "Repo name is in \${localrepos}..." return 0 else funcDebugMessage "Repo name is not in \${localrepos}..." return 1 fi funcDebugMessage "Unknown Error..." return 127 } function funcValidateFQDN { ## Validates the given FQDN ## Example Usage: funcValidateFQDN "{fqdn}" ## Given FQDN must contain lowercase, digits, dashes and periods only (no leading or trailing dashes, no leading or trailing periods, no dashes or periods adjacent to periods) ## This function only returns exit codes ## 0 - FQDN is valid ## 1 - FQDN is not valid ## 126 - No FQDN given ## 127 - Unknown error funcDebugMessage "Function called..." if [[ -z "${1}" ]]; then funcDebugMessage "No FQDN name given..." return 126 else local fqdn="${1}" fi if [[ "${fqdn}" =~ ^([a-z0-9](-?[a-z0-9])*\.)+[a-z]{2,}$ ]]; then funcDebugMessage "FQDN is valid..." return 0 else funcDebugMessage "FQDN is not valid..." return 1 fi funcDebugMessage "Unknown Error..." return 127 } function funcValidateRepoName { ## Checks that a given repo name is valid ## Example Usage: funcValidateLocalName {reponame} ## Given repo name must contain lowercase, digits and dashes only (no leading or trailing dashes) ## This function only returns exit codes ## 0 - Valid repo name ## 1 - Invalid repo name ## 126 - No repo name given ## 127 - Unknown error funcDebugMessage "Function called..." if [[ -z "${1}" ]]; then funcDebugMessage "No repo name given..." return 126 else local repo="${1}" fi if [[ "${repo}" =~ ^[a-z0-9]+([a-z0-9-]*[a-z0-9])?$ ]]; then funcDebugMessage "Valid repo name..." return 0 else funcDebugMessage "Invalid repo name..." return 1 fi funcDebugMessage "Unknown Error..." return 127 } function funcValidateSettings { ## Checks if /etc/debmirrorman/settings.conf exists, confirms the existance of ${localfqdn} abd that it is a valid FQDN ## Example Usage: funcValidateSettings ## This function returns exit code 0 if all checks pass and exits with exit code 2 and an error message if any checks fail. ## 0 - /etc/debmirrorman/settings.conf passes all checks ## 15 - /etc/debmirrorman/settings.conf does not exist (hard exit) ## 16 - No ${localfqdn} defined in /etc/debmirrorman/settings.conf (hard exit) ## 17 - Invalid ${localfqdn} defined in /etc/debmirrorman/settings.conf (hard exit) ## 127 - Unknown error funcDebugMessage "Function called..." if [[ -f "${etcdirectory}/settings.conf" ]]; then funcDebugMessage "'${etcdirectory}/settings.conf' exists..." source "${etcdirectory}/settings.conf" else funcError "'settings.conf' not found at '${etcdirectory}'..." 21 fi if [[ -z ${localfqdn} ]]; then funcError "No \${localfqdn} defined in '${etcdirectory}/settings.conf'..." 22 else if funcValidateFQDN "${localfqdn}"; then funcDebugMessage "'${etcdirectory}/settings.conf' passed all checks..." return 0 else funcError "Invalid \${localfqdn} defined in '${etcdirectory}/settings.conf'..." 23 fi fi funcDebugMessage "Unknown Error..." return 127 }