From 9987d02d28730465dff8281a71bdb604fbbed340 Mon Sep 17 00:00:00 2001 From: Denis Arnaud Date: Tue, 20 Jul 2021 20:34:17 +0200 Subject: [PATCH] Creation --- README.md | 69 +++++++++++++++++++++++++++++++++ setDirs.sh | 64 +++++++++++++++++++++++++++++++ setGnuTools.sh | 35 +++++++++++++++++ setLogFunc.sh | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 270 insertions(+) create mode 100644 README.md create mode 100755 setDirs.sh create mode 100755 setGnuTools.sh create mode 100755 setLogFunc.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..f4d53ef --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ + + +# Overview +[This project](https://github.com/cloud-helpers/k8s-job-wrappers) +contains a few Shell scripts supporting the execution of tasks both from +within Kubernetes (k8s) pods (_i.e._, from within a container) and from +a laptop or a virtual machine (VM). +The idea is to use the same support scripts to test an application both +from the usual daily environment (_e.g._, laptop, virtual machine (VM) on +a cloud provider) and from a container in a Kubernertes deployment. + +# Usage +* Download and extract the archive of Shell scripts: +```bash +$ wget https://github.com/cloud-helpers/k8s-job-wrappers/archive/refs/tags/v0.0.1.tar.gz -O k8s-job-wrappers.tar.gz +$ tar zxf k8s-job-wrappers.tar.gz && rm -f k8s-job-wrappers.tar.gz +``` + +* All the following steps may be performed from your own Shell script + +* Specify a few environment variables + + URL of the caller script: +```bash +export SCRIPT_GIT_URL="https://github.com/cloud-helpers/k8s-job-wrappers/tree/master/k8s-job-wrapper-main.sh" +``` + + File-path to the log file: +```bash +export LOG_FILE="$HOME/tmp/application/my-application.log" +``` + + Name of the caller function: +```bash +export FUNC="main" +``` + +* Make sure that the log file is writeable: +```bash +$ mkdir -p $(dirname $LOG_FILE) +$ touch $LOG_FILE +``` + +* Source the Shell support script: +```bash +source setLogFunc.sh +``` + +* Call the `log` functions + + Beginning of the script: +```bash +logStart "My own application - We can achieve great things with collaboration" +``` + + Single-line log: +```bash +log "A single line log" +``` + + Multi-line log: +```bash +logMulti "The first line of a multi-line log" \ + ". Another line" \ + ". Last line" +``` + + End of the script: +```bash +logEnd "My own application - We can achieve great things with collaboration" +``` + +* And that is it. In order to check the resulting log file: +```bash +$ cat $LOG_FILE +``` diff --git a/setDirs.sh b/setDirs.sh new file mode 100755 index 0000000..7c4c7a6 --- /dev/null +++ b/setDirs.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +# +# File: https://github.com/cloud-helpers/k8s-job-wrappers/tree/master/setDirs.sh +# +# Utility supporting executing tasks from within Kubernetes (k8s) pods +# + +## +# Caller script +CALLER_SCRIPT="setDirs.sh" +if [ "$1" != "" ] +then + CALLER_SCRIPT="$1" +fi + +## +# Variables specified in the remaining of the script +export TMP_DIR +export EXEC_PATH +export EXEC_FULL_PATH +export EXEC_DIR_NAME + +## +# Temporary path +TMP_DIR="/tmp/por" + +## +# Path of the executable: set it to empty when this is the current directory. +EXEC_PATH="$(dirname ${CALLER_SCRIPT})" +# Trick to get the actual full-path +EXEC_FULL_PATH="$(pushd ${EXEC_PATH})" +EXEC_FULL_PATH="$(echo ${EXEC_FULL_PATH} | cut -d' ' -f1)" +EXEC_FULL_PATH="$(echo ${EXEC_FULL_PATH} | sed -e 's|~|'${HOME}'|')" +# +CURRENT_DIR="$(pwd)" +if [ ${CURRENT_DIR} -ef ${EXEC_PATH} ] +then + EXEC_PATH="." + TMP_DIR="." +fi +# If the Geonames dump file is in the current directory, then the current +# directory is certainly intended to be the temporary directory. +if [ -f ${GEO_RAW_FILENAME} ] +then + TMP_DIR="." +fi +EXEC_PATH="${EXEC_PATH}/" +TMP_DIR="${TMP_DIR}/" + +if [ ! -d ${TMP_DIR} -o ! -w ${TMP_DIR} ] +then + \mkdir -p ${TMP_DIR} +fi + +## +# Reporting +echo +echo "Caller script: ${CALLER_SCRIPT}" +echo "Environment variables set:" +echo " - TMP_DIR=\"${TMP_DIR}\"" +echo " - EXEC_PATH=\"${EXEC_PATH}\"" +echo " - EXEC_FULL_PATH=\"${EXEC_FULL_PATH}\"" +echo " - EXEC_DIR_NAME=\"${EXEC_DIR_NAME}\"" +echo diff --git a/setGnuTools.sh b/setGnuTools.sh new file mode 100755 index 0000000..20bcbd3 --- /dev/null +++ b/setGnuTools.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# +# File: https://github.com/cloud-helpers/k8s-job-wrappers/tree/master/setGnuTools.sh +# +# Utility supporting executing tasks from within Kubernetes (k8s) pods +# +# GNU tools +# +# On MacOS: +# - Reference: https://ryanparman.com/posts/2019/using-gnu-command-line-tools-in-macos-instead-of-freebsd-tools/ +# - coreutils provides, in a non exhaustive list, date, wc, head +# - sed is provided by gnu-sed +# +export DATE_TOOL="date" +export WC_TOOL="wc" +export HEAD_TOOL="head" +export SED_TOOL="sed" +if [ -f /usr/bin/sw_vers ] +then + DATE_TOOL="gdate" + WC_TOOL="gwc" + HEAD_TOOL="ghead" + SED_TOOL="gsed" + if [ ! $(command -v ${DATE_TOOL}) ] + then + echo "Error - Cannot find GNU coreutils tools (e.g., ${DATE_TOOL}, " \ + "${WC_TOOL}, ${HEAD_TOOL}. Install those with \`brew install coreutils\`" + return 1 + fi + if [ ! $(command -v ${SED_TOOL}) ] + then + echo "Error - Cannot find ${SED_TOOL}. Install it with \`brew install gnu-sed\`" + return 1 + fi +fi diff --git a/setLogFunc.sh b/setLogFunc.sh new file mode 100755 index 0000000..e28a769 --- /dev/null +++ b/setLogFunc.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash +# +# File: https://github.com/cloud-helpers/k8s-job-wrappers/tree/master/shlib/setLogFunc.sh +# +# Utility supporting executing tasks from within Kubernetes (k8s) pods +# + +# +THIS_SCRIPT_GIT_URL="https://github.com/cloud-helpers/k8s-job-wrappers/tree/master/shlib/setLogFunc.sh" +FUNC="default" +HNAME="$(cat /etc/hostname 2> /dev/null || hostname 2> /dev/null || echo "Unknown-hostname")" +UNAME="$(id -u -n)" + +export DATE_TOOL="date" +if [ -f /usr/bin/sw_vers ] +then + DATE_TOOL="gdate" + if [ ! $(command -v ${DATE_TOOL}) ] + then + echo "Error - Cannot find GNU coreutils tools (e.g., ${DATE_TOOL}). " \ + "Install those with \`brew install coreutils\`" + return 1 + fi +fi + +# Logging module +log() { + if [ -z "${LOG_FILE}" ] + then + echo "Error - The LOG_FILE environment variable should be set and points to the file in which the logs may be written." > /dev/stderr + echo " See ${THIS_SCRIPT_GIT_URL} for the details of the log() function." > /dev/stderr + return 1 + fi + local canWriteToFile="$(touch ${LOG_FILE} 2> /dev/null && echo "Y" || echo "N")" + if [ "${canWriteToFile}" == "N" ] + then + echo "Error - The ${LOG_FILE} is expected to be a wrtiteable file in which the logs may be written; apparently, no logs may be written to ${LOG_FILE}." > /dev/stderr + echo " See ${THIS_SCRIPT_GIT_URL} for the details of the log() function." > /dev/stderr + return 1 + fi + if [ -z "${SCRIPT_GIT_URL}" ] + then + echo "Error - The SCRIPT_GIT_URL environment variable should be set, but is not." + echo "See ${THIS_SCRIPT_GIT_URL} for the details of the log() function." + return 1 + fi + local caller_script="$(basename ${THIS_SCRIPT_GIT_URL})" + logTime="$(${DATE_TOOL} "+%Y-%m-%d %H:%M:%S" --utc)" + echo "############### [${logTime} (UTC)][${UNAME}@${HNAME}][${caller_script}][${FUNC}]: $1 ######################" | tee -a ${LOG_FILE} +} + +logMulti() { + if [ -z "${LOG_FILE}" ] + then + echo "Error - The LOG_FILE environment variable should be set and points to the file in which the logs may be written." > /dev/stderr + echo " See ${THIS_SCRIPT_GIT_URL} for the details of the log() function." > /dev/stderr + return 1 + fi + local canWriteToFile="$(touch ${LOG_FILE} 2> /dev/null && echo "Y" || echo "N")" + if [ "${canWriteToFile}" == "N" ] + then + echo "Error - The ${LOG_FILE} is expected to be a wrtiteable file in which the logs may be written; apparently, no logs may be written to ${LOG_FILE}." > /dev/stderr + echo " See ${THIS_SCRIPT_GIT_URL} for the details of the log() function." > /dev/stderr + return 1 + fi + if [ -z "${SCRIPT_GIT_URL}" ] + then + echo "Error - The SCRIPT_GIT_URL environment variable should be set, but is not." + echo "See ${THIS_SCRIPT_GIT_URL} for the details of the log() function." + return 1 + fi + local caller_script="$(basename ${THIS_SCRIPT_GIT_URL})" + logTime="$(${DATE_TOOL} "+%Y-%m-%d %H:%M:%S" --utc)" + echo "############### [${logTime} (UTC)][${UNAME}@${HNAME}][${caller_script}][${FUNC}] - begin ######################" | tee -a ${LOG_FILE} + #declare -a line_list=($@) + for myline in "$@" + do + echo "${myline}" | tee -a ${LOG_FILE} + done + echo "############### [${logTime} (UTC)][${UNAME}@${HNAME}][${FUNC}] - end ######################" | tee -a ${LOG_FILE} +} + +logStart() { + local descr="Generic Description - Place your own description here by calling logStart() with an argument" + if [ ! -z "$1" ] + then + descr="$1" + fi + log "#############################################################################" + log "#### ${descr} - Start ####" +} + +logEnd() { + local descr="Generic Description - Place your own description here by calling logStart() with an argument" + if [ ! -z "$1" ] + then + descr="$1" + fi + log "#### ${descr} - End ####" + log "#############################################################################" +} +