From 38f6dcaa066e26b512eead4f9fab8d7051810729 Mon Sep 17 00:00:00 2001 From: ojullien Date: Thu, 1 Aug 2019 13:41:47 +0200 Subject: [PATCH] Work in Progress --- README.md | 4 +- scripts/config.sh | 46 ++++++++++++ scripts/includes.sh | 70 +++++++++++++++++++ scripts/install.sh | 165 +++++++++++--------------------------------- 4 files changed, 158 insertions(+), 127 deletions(-) create mode 100644 scripts/config.sh create mode 100644 scripts/includes.sh diff --git a/README.md b/README.md index 0f14671..ef14185 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Personal Shell scripting projects in bash. This package is a metapackage aggrega Require a Debian/Ubuntu version of linux and a Bash version ~4.4. -1. [Download a release](https://github.com/ojullien/Shell/releases) or clone this repository using thsi command: `git clone --recurse-submodules https://github.com/ojullien/Shell`. +1. [Download a release](https://github.com/ojullien/Shell/releases) or clone this repository using this command: `git clone --recurse-submodules https://github.com/ojullien/Shell`. 2. Use [scripts/install.sh](https://github.com/ojullien/Shell/tree/master/scripts) to automatically install the project in the /opt/oju/bash folder. 3. If needed, add `PATH="$PATH:/opt/oju/bash/bin"` to the .profile files. 4. For each apps in `/opt/oju/bash/app` check out and edit the configuration file named `config.sh`. @@ -41,7 +41,7 @@ I wrote and I use these scripts for my own projects. And, unfortunately, I do no ## Contributing -**Thanks you for taking the time to contribute**. If you wish to contribute, please read the [CONTRIBUTING.md](CONTRIBUTING.md) and [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) files. +**Thanks you for taking the time to contribute**. If you wish to contribute, please read the [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) files. ## License diff --git a/scripts/config.sh b/scripts/config.sh new file mode 100644 index 0000000..985150b --- /dev/null +++ b/scripts/config.sh @@ -0,0 +1,46 @@ +## ----------------------------------------------------------------------------- +## Linux Scripts. +## Configuration file. +## +## @package ojullien\Shell\scripts +## @license MIT +## ----------------------------------------------------------------------------- + +## ----------------------------------------------------------------------------- +## Defines current date +## ----------------------------------------------------------------------------- +readonly m_DATE="$(date +"%Y%m%d")_$(date +"%H%M")" + +## ----------------------------------------------------------------------------- +## Defines main directories +## ----------------------------------------------------------------------------- + +# DESTINATION +readonly m_INSTALL_DESTINATION_DIR="/opt/oju/bash" +readonly m_DIR_APP="${m_INSTALL_DESTINATION_DIR}/app" # Directory holds apps +readonly m_DIR_BIN="${m_INSTALL_DESTINATION_DIR}/bin" # Directory holds app entry point +readonly m_DIR_SYS="${m_INSTALL_DESTINATION_DIR}/sys" # Directory holds system files + +# SOURCE +readonly m_INSTALL_APP_NAME="pki" +readonly m_INSTALL_SOURCE_APP_DIR="$(realpath "${m_DIR_REALPATH}/../src/app/${m_INSTALL_APP_NAME}")" +readonly m_INSTALL_SOURCE_BIN_FILE="$(realpath "${m_DIR_REALPATH}/../src/bin/${m_INSTALL_APP_NAME}.sh")" + +## ----------------------------------------------------------------------------- +## Defines main files +## Log file cannot be in /var/log 'cause few apps clean this directory +## ----------------------------------------------------------------------------- +readonly m_LOGDIR="$(realpath "${m_DIR_REALPATH}/../src/log")" +readonly m_LOGFILE="${m_LOGDIR}/${m_DATE}_$(basename "$0").log" + +## ----------------------------------------------------------------------------- +## Defines colors +## ----------------------------------------------------------------------------- +readonly COLORRED="$(tput -Txterm setaf 1)" +readonly COLORGREEN="$(tput -Txterm setaf 2)" +readonly COLORRESET="$(tput -Txterm sgr0)" + +## ----------------------------------------------------------------------------- +## Defines options +## ----------------------------------------------------------------------------- +declare -i m_INSTALL_OPTION_REMOVE=0 diff --git a/scripts/includes.sh b/scripts/includes.sh new file mode 100644 index 0000000..5fe49c7 --- /dev/null +++ b/scripts/includes.sh @@ -0,0 +1,70 @@ +## ----------------------------------------------------------------------------- +## Linux Scripts. +## Usefull functions. +## +## @package ojullien\bash-pki\scripts +## @license MIT +## ----------------------------------------------------------------------------- + +Install::trace() { + String::separateLine + String::notice "Main configuration" + FileSystem::checkDir "\tSource directory:\t\t${m_DIR_REALPATH}" "${m_DIR_REALPATH}" + FileSystem::checkDir "\tSystem directory:\t\t${m_DIR_SYS}" "${m_DIR_SYS}" + FileSystem::checkDir "\tDestination app directory:\t${m_DIR_APP}" "${m_DIR_APP}" + FileSystem::checkDir "\tDestination bin directory:\t${m_DIR_BIN}" "${m_DIR_BIN}" + FileSystem::checkFile "\tLog file is:\t\t\t${m_LOGFILE}" "${m_LOGFILE}" + return 0 +} + +Install::run() { + + String::separateLine + + if ((m_INSTALL_OPTION_REMOVE)); then + FileSystem::removeDirectory "${m_DIR_APP}/${m_INSTALL_APP_NAME}" + iReturn=$? + ((0!=iReturn)) && return ${iReturn} + fi + + FileSystem::removeDirectory "${m_DIR_BIN}/${m_INSTALL_APP_NAME}.sh" + iReturn=$? + ((0!=iReturn)) && return ${iReturn} + + FileSystem::copyFile "${m_INSTALL_SOURCE_APP_DIR}" "${m_DIR_APP}" + iReturn=$? + ((0!=iReturn)) && return ${iReturn} + Console::waitUser + + FileSystem::copyFile "${m_INSTALL_SOURCE_BIN_FILE}" "${m_DIR_BIN}" + iReturn=$? + ((0!=iReturn)) && return ${iReturn} + Console::waitUser + + String::notice -n "Change owner:" + chown -R root:root "${m_DIR_APP}/${m_INSTALL_APP_NAME}" "${m_DIR_BIN}/${m_INSTALL_APP_NAME}.sh" + iReturn=$? + String::checkReturnValueForTruthiness ${iReturn} + ((0!=iReturn)) && return ${iReturn} + Console::waitUser + + String::notice -n "Change directory access rights:" + find "${m_DIR_APP}" -type d -name "${m_INSTALL_APP_NAME}" -exec chmod u=rwx,g=rx,o=rx {} \; + iReturn=$? + String::checkReturnValueForTruthiness ${iReturn} + ((0!=iReturn)) && return ${iReturn} + + String::notice -n "Change files access rights:" + find "${m_DIR_APP}/${m_INSTALL_APP_NAME}" -type f -exec chmod u=rw,g=r,o=r {} \; + iReturn=$? + String::checkReturnValueForTruthiness ${iReturn} + ((0!=iReturn)) && return ${iReturn} + + String::notice -n "Change sh files access rights:" + chmod +x "${m_DIR_BIN}/${m_INSTALL_APP_NAME}.sh" + iReturn=$? + String::checkReturnValueForTruthiness ${iReturn} + ((0!=iReturn)) && return ${iReturn} + + return ${iReturn} +} diff --git a/scripts/install.sh b/scripts/install.sh index cfa0f1f..c88d887 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -1,72 +1,23 @@ #!/bin/bash ## ----------------------------------------------------------------------------- ## Linux Scripts. -## Install the Shell project into the /opt/Shell directory. +## Install the bash-pki project into the /opt/oju/bash directory. ## -## @package ojullien\Shell -## @license MIT +## @package ojullien\bash-pki\scripts +## @license MIT ## ----------------------------------------------------------------------------- #set -o errexit set -o nounset set -o pipefail ## ----------------------------------------------------------------------------- -## Shell scripts directory, eg: /opt/Shell/scripts/ +## Current project directory, eg: /opt/Shell/scripts/ ## ----------------------------------------------------------------------------- readonly m_DIR_REALPATH="$(realpath "$(dirname "$0")")" - -## ----------------------------------------------------------------------------- -## Defines current date -## ----------------------------------------------------------------------------- -readonly m_DATE="$(date +"%Y%m%d")_$(date +"%H%M")" - -## ----------------------------------------------------------------------------- -## Defines main directories -## ----------------------------------------------------------------------------- - -# Directory holds system files -readonly m_DIR_SYS="$(realpath "${m_DIR_REALPATH}/../src/sys")" -# Directory holds apps -readonly m_DIR_APP="$(realpath "${m_DIR_REALPATH}/../src/app")" -# Directory destination -readonly m_INSTALLSHELL_DIR_SOURCE="$(realpath "${m_DIR_REALPATH}/../src")" -# Directory destination -readonly m_INSTALLSHELL_DIR_DESTINATION="/opt/oju" -# Directory to install -readonly m_INSTALLSHELL_PROJECT_NAME="Shell" -# Cron.daily file destination -readonly m_INSTALLCRON_DESTINATION="/etc/cron.daily/priv-autosave" -# Cron.daily file source -readonly m_INSTALLCRON_SOURCE="${m_DIR_REALPATH}/cron/priv-autosave" - -## ----------------------------------------------------------------------------- -## Defines main files -## Log file cannot be in /var/log 'cause few apps clean this directory -## ----------------------------------------------------------------------------- -readonly m_LOGDIR="$(realpath "${m_DIR_REALPATH}/../src/log")" -readonly m_LOGFILE="${m_LOGDIR}/${m_DATE}_$(basename "$0").log" - -## ----------------------------------------------------------------------------- -## Defines colors -## ----------------------------------------------------------------------------- -readonly COLORRED="$(tput -Txterm setaf 1)" -readonly COLORGREEN="$(tput -Txterm setaf 2)" -readonly COLORRESET="$(tput -Txterm sgr0)" - -## ----------------------------------------------------------------------------- -## Functions -## ----------------------------------------------------------------------------- -Constant::trace() { - String::separateLine - String::notice "Main configuration" - FileSystem::checkDir "\tScript directory:\t\t${m_DIR_REALPATH}" "${m_DIR_REALPATH}" - FileSystem::checkDir "\tSystem directory:\t\t${m_DIR_SYS}" "${m_DIR_SYS}" - FileSystem::checkDir "\tApp directory:\t\t\t${m_DIR_APP}" "${m_DIR_APP}" - FileSystem::checkFile "\tLog file is:\t\t\t${m_LOGFILE}" "${m_LOGFILE}" - String::notice "Distribution" - (( m_OPTION_DISPLAY )) && lsb_release --all - return 0 -} +# shellcheck source=/dev/null +. "${m_DIR_REALPATH}/config.sh" +# shellcheck source=/dev/null +. "${m_DIR_REALPATH}/includes.sh" ## ----------------------------------------------------------------------------- ## Includes sources & configuration @@ -88,21 +39,12 @@ Constant::trace() { ((m_OPTION_SHOWHELP)) && Option::showHelp && exit 0 ## ----------------------------------------------------------------------------- -## Install packages system +## bash-sys must exists ## ----------------------------------------------------------------------------- -String::separateLine -apt-get install "lsb-release" --yes --quiet -Console::waitUser - -## ----------------------------------------------------------------------------- -## Trace -## ----------------------------------------------------------------------------- -Constant::trace -String::separateLine -String::notice "App configuration: installShell" -FileSystem::checkDir "\tSource directory:\t${m_INSTALLSHELL_DIR_SOURCE}" "${m_INSTALLSHELL_DIR_SOURCE}" -FileSystem::checkDir "\tDestination directory:\t${m_INSTALLSHELL_DIR_DESTINATION}" "${m_INSTALLSHELL_DIR_DESTINATION}" -String::notice "\tProject name:\t\t${m_INSTALLSHELL_PROJECT_NAME}" +if [[ ! -d "${m_DIR_SYS}" ]]; then + String::error "bash-sys is not installed on ${m_DIR_SYS}" + exit 1 +fi ## ----------------------------------------------------------------------------- ## Start @@ -112,60 +54,33 @@ String::notice "Today is: $(date -R)" String::notice "The PID for $(basename "$0") process is: $$" Console::waitUser -FileSystem::removeDirectory "${m_INSTALLSHELL_DIR_DESTINATION}/${m_INSTALLSHELL_PROJECT_NAME}" -iReturn=$? -((0!=iReturn)) && return ${iReturn} - -FileSystem::createDirectory "${m_INSTALLSHELL_DIR_DESTINATION}" -iReturn=$? -((0!=iReturn)) && return ${iReturn} - -FileSystem::moveFile "${m_INSTALLSHELL_DIR_SOURCE}" "${m_INSTALLSHELL_DIR_DESTINATION}/${m_INSTALLSHELL_PROJECT_NAME}" -iReturn=$? -((0!=iReturn)) && return ${iReturn} - -String::notice -n "Change owner:" -chown -R root:root "${m_INSTALLSHELL_DIR_DESTINATION}" -iReturn=$? -String::checkReturnValueForTruthiness ${iReturn} -((0!=iReturn)) && return ${iReturn} - -String::notice -n "Change common directories access rights:" -find "${m_INSTALLSHELL_DIR_DESTINATION}" -type d -exec chmod u=rwx,g=rx,o=rx {} \; -iReturn=$? -String::checkReturnValueForTruthiness ${iReturn} -((0!=iReturn)) && return ${iReturn} - -String::notice -n "Change log directory access rights:" -find "${m_INSTALLSHELL_DIR_DESTINATION}/${m_INSTALLSHELL_PROJECT_NAME}/log" -type d -exec chmod u=rwx,g=rwx,o=rwx {} \; -iReturn=$? -String::checkReturnValueForTruthiness ${iReturn} -((0!=iReturn)) && return ${iReturn} - -String::notice -n "Change files access rights:" -find "${m_INSTALLSHELL_DIR_DESTINATION}/${m_INSTALLSHELL_PROJECT_NAME}" -type f -exec chmod u=rw,g=r,o=r {} \; -iReturn=$? -String::checkReturnValueForTruthiness ${iReturn} -((0!=iReturn)) && return ${iReturn} - -String::notice -n "Change sh files access rights:" -chmod +x ${m_INSTALLSHELL_DIR_DESTINATION}/${m_INSTALLSHELL_PROJECT_NAME}/bin/*.sh -iReturn=$? -String::checkReturnValueForTruthiness ${iReturn} -((0!=iReturn)) && return ${iReturn} - -String::notice "Configuring cron.daily ..." -FileSystem::copyFile "${m_INSTALLCRON_SOURCE}" "${m_INSTALLCRON_DESTINATION}" -iReturn=$? -String::notice -n "Configure cron.daily:" -String::checkReturnValueForTruthiness ${iReturn} -((0!=iReturn)) && return ${iReturn} - -String::notice -n "Change cron.daily access rights:" -chmod u=rwx,g=rx,o=rx "${m_INSTALLCRON_DESTINATION}" -iReturn=$? -String::checkReturnValueForTruthiness ${iReturn} -((0!=iReturn)) && return ${iReturn} +## ----------------------------------------------------------------------------- +## Parse the app options and arguments +## ----------------------------------------------------------------------------- +declare -i iReturn=1 + +while (( "$#" )); do + case "$1" in + -t|--trace) + shift + String::separateLine + Install::trace + ;; + -r|--remove) + shift + m_INSTALL_OPTION_REMOVE=1 + ;; + *) # unknown option + shift + String::separateLine + Option::showHelp + exit 0 + ;; + esac +done + +Install::run ${m_INSTALL_OPTION_REMOVE} +Console::waitUser ## ----------------------------------------------------------------------------- ## END