-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Denis Arnaud
authored and
Denis Arnaud
committed
Jul 20, 2021
1 parent
2660fa1
commit 9987d02
Showing
4 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "#############################################################################" | ||
} | ||
|