Skip to content

Commit

Permalink
Work in Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ojullien committed Aug 1, 2019
1 parent cee2809 commit 38f6dca
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 127 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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

Expand Down
46 changes: 46 additions & 0 deletions scripts/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## -----------------------------------------------------------------------------
## Linux Scripts.
## Configuration file.
##
## @package ojullien\Shell\scripts
## @license MIT <https://github.com/ojullien/Shell/blob/master/LICENSE>
## -----------------------------------------------------------------------------

## -----------------------------------------------------------------------------
## 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
70 changes: 70 additions & 0 deletions scripts/includes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
## -----------------------------------------------------------------------------
## Linux Scripts.
## Usefull functions.
##
## @package ojullien\bash-pki\scripts
## @license MIT <https://github.com/ojullien/bash-pki/blob/master/LICENSE>
## -----------------------------------------------------------------------------

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}
}
165 changes: 40 additions & 125 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/ojullien/Shell/blob/master/LICENSE>
## @package ojullien\bash-pki\scripts
## @license MIT <https://github.com/ojullien/bash-pki/blob/master/LICENSE>
## -----------------------------------------------------------------------------
#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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 38f6dca

Please sign in to comment.