Skip to content

Commit

Permalink
v3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ojullien committed Jul 10, 2019
1 parent 887b1ab commit 0160ce1
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Requires: a Debian/Ubuntu version of linux and a Bash version ~4.4. [bash-sys](h

## Features

This tool is a wrapper to the 'system and service manager' (systemd or SysV) functions. It allows you to disable, start or stop a list of services with just one command. The services are defined in the [config.sh](src/app/savesystemconf/config.sh) file.
This tool is a wrapper to the 'system and service manager' (systemd or SysV) functions. It allows you to disable, start or stop a list of services with just one command. The services are defined in the [config.sh](src/app/manageservices/config.sh) file.

```bash
Usage: manageservices.sh [options] command
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\bash\scripts
## @license MIT <https://github.com/ojullien/bash-manageservices/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="manageservices"
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
68 changes: 68 additions & 0 deletions scripts/includes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
## -----------------------------------------------------------------------------
## Linux Scripts.
## Usefull functions.
##
## @package ojullien\bash\scripts
## @license MIT <https://github.com/ojullien/bash-manageservices/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() {

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}
}
125 changes: 29 additions & 96 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,10 @@ set -o pipefail
## 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
## -----------------------------------------------------------------------------

# 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="manageservices"
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)"

## -----------------------------------------------------------------------------
## Functions
## -----------------------------------------------------------------------------
Constant::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
}
# shellcheck source=/dev/null
. "${m_DIR_REALPATH}/config.sh"
# shellcheck source=/dev/null
. "${m_DIR_REALPATH}/includes.sh"

## -----------------------------------------------------------------------------
## Includes sources & configuration
Expand Down Expand Up @@ -90,12 +46,6 @@ if [[ ! -d "${m_DIR_SYS}" ]]; then
exit 1
fi

## -----------------------------------------------------------------------------
## Trace
## -----------------------------------------------------------------------------
Constant::trace
Console::waitUser

## -----------------------------------------------------------------------------
## Start
## -----------------------------------------------------------------------------
Expand All @@ -104,48 +54,31 @@ String::notice "Today is: $(date -R)"
String::notice "The PID for $(basename "$0") process is: $$"
Console::waitUser

FileSystem::removeDirectory "${m_DIR_APP}/${m_INSTALL_APP_NAME}"
iReturn=$?
((0!=iReturn)) && exit ${iReturn}

FileSystem::removeDirectory "${m_DIR_BIN}/${m_INSTALL_APP_NAME}.sh"
iReturn=$?
((0!=iReturn)) && exit ${iReturn}

FileSystem::copyFile "${m_INSTALL_SOURCE_APP_DIR}" "${m_DIR_APP}"
iReturn=$?
((0!=iReturn)) && exit ${iReturn}
Console::waitUser

FileSystem::copyFile "${m_INSTALL_SOURCE_BIN_FILE}" "${m_DIR_BIN}"
iReturn=$?
((0!=iReturn)) && exit ${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)) && exit ${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)) && exit ${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)) && exit ${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}
## -----------------------------------------------------------------------------
## Parse the app options and arguments
## -----------------------------------------------------------------------------
declare -i iReturn=1

if (( "$#" )); then
case "$1" in
-t|--trace)
shift
String::separateLine
Constant::trace
;;
-r|--remove)
shift
String::separateLine
Install::run
;;
*) # unknown option
shift
String::separateLine
Option::showHelp
exit 0
;;
esac
fi

## -----------------------------------------------------------------------------
## END
Expand Down
4 changes: 3 additions & 1 deletion src/bin/manageservices.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Linux Scripts.
## Start, stop or disable a list of services.
##
## @package ojullien\bash\bin\manageservices
## @package ojullien\bash\bin
## @license MIT <https://github.com/ojullien/bash-manageservices/blob/master/LICENSE>
## -----------------------------------------------------------------------------
#set -o errexit
Expand Down Expand Up @@ -88,8 +88,10 @@ if (( "$#" )); then
exit 0
;;
*) # unknown option
shift
String::separateLine
ManageServices::showHelp
exit 0
;;
esac
else
Expand Down

0 comments on commit 0160ce1

Please sign in to comment.