mig1984 / bashible Goto Github PK
View Code? Open in Web Editor NEWSimple bash DSL framework for writing shell scripts safe and agile.
License: MIT License
Simple bash DSL framework for writing shell scripts safe and agile.
License: MIT License
In order to have reproductible scripts, we have to rely on released version.
Can you document it (and add a changelog & tell that you follow semver)?
running bashible i get:
./bashible: line 574: shopt: inherit_errexit: invalid shell option name
bash manual says this shopt is only available in posix mode
Originally posted by @ootada in #9 (comment)
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
CentOS Linux release 7.9.2009 (Core)
I need run blkid and get partrition id and find line in fstab and replace mount option. How to do that?
Hey,
Great initiative,
I'll share with you some functions I used. If you can include them into bashible, it'll be great.
It requires some refactoring as you handle timeout with a separate function.
Note: I do not share printlnError
functions as it's mainly the same as yours.
# Usage:
# wait_file_present [-t TIMEOUT] [-i INTERVAL] FILE
# Parameters:
# -t: timeout in second (default: 60)
# -i: interval in second (default: 1)
# Example:
# wait_file_present -t 10 -i 2 /tmp/file.txt
function wait_file_present() {
local FILE TIMEOUT TIMEOUT_ORIGIN INTERVAL
TIMEOUT=60
INTERVAL=1
local OPTIND arg
while getopts ":t:i:" arg; do
case $arg in
t) TIMEOUT="${OPTARG}";;
i) INTERVAL="${OPTARG}";;
:) printlnError "Invalid option: $OPTARG requires an argument"; return 1;;
?) printlnError "Unkownn option: $OPTARG" ; return 1;;
esac
done
shift $((OPTIND -1))
FILE="${1?File not defined}"
TIMEOUT_ORIGIN="${TIMEOUT}"
until [ -f "${FILE}" ] > /dev/null 2>&1 && SUCCESS=1 || [ "$TIMEOUT" -le 0 ]; do
sleep "${INTERVAL}"
TIMEOUT=$((TIMEOUT-INTERVAL))
done
if [ "${SUCCESS}" != "1" ]; then
echo "Error: File \"${FILE}\" does not exists after ${TIMEOUT_ORIGIN}s";
return 1;
fi
}
# Usage:
# wait_tcp_port_open [-t TIMEOUT] [-i INTERVAL] HOST PORT
# Parameters:
# -t: timeout in second (default: 60)
# -i: interval in second (default: 1)
# Example:
# wait_tcp_port_open -t 10 -i 2 localhost 8080
function wait_tcp_port_open() {
local HOST PORT TIMEOUT TIMEOUT_ORIGIN INTERVAL
TIMEOUT=60
INTERVAL=1
local OPTIND arg
while getopts ":t:i:" arg; do
case $arg in
t) TIMEOUT="${OPTARG}";;
i) INTERVAL="${OPTARG}";;
:) printlnError "Invalid option: $OPTARG requires an argument"; return 1;;
?) printlnError "Unkownn option: $OPTARG" ; return 1;;
esac
done
shift $((OPTIND -1))
HOST="${1?Host not defined}"
PORT="${2?Port not defined}"
TIMEOUT_ORIGIN="${TIMEOUT}"
SUCCESS=0
until nc -zvw3 "${HOST}" "${PORT}" > /dev/null 2>&1 && SUCCESS=1 || [ "$TIMEOUT" -le 0 ]; do
sleep "${INTERVAL}"
TIMEOUT=$((TIMEOUT-INTERVAL))
done
if [ "${SUCCESS}" != "1" ]; then
echo "Error: Port \"${HOST}\" \"${PORT}\" is not open after ${TIMEOUT_ORIGIN}s";
echo "Debug: Command: nc -zvw3 \"${HOST}\" \"${PORT}\")"
return 1;
fi
}
# Usage:
# curl_status [-c EXPECTED_HTTP_CODE] [-- [CURL_ARGS]...] URL
# Parameters:
# -c: comma separated list of expected HTTP CODE (default: 200)
# -v: verbose (warning: this could print password)
# --: special option to delimit end of wait_curl_status parameters. All options behind are curl options (see example below)
# Example:
# curl_status https://postman-echo.com/status/200
# curl_status -c 204 https://postman-echo.com/status/204
# curl_status -c 204 -- -X GET -H "host: local.com" https://postman-echo.com/status/204
# curl_status -c 500,000 -- -X GET -H "host: local.com" https://postman-echo.com/status/500
function curl_status() {
local EXPECTED_HTTP_CODE CURL_STATUS CURL_TIMEOUT VERBOSE=false
EXPECTED_HTTP_CODE=200
CURL_TIMEOUT=5
local OPTIND arg
while getopts ":c:v" arg; do
case $arg in
c) EXPECTED_HTTP_CODE="${OPTARG}";;
v) VERBOSE=true;;
:) printlnError "Invalid option: $OPTARG requires an argument"; return 1;;
?) printlnError "Unkownn option: $OPTARG" ; return 1;;
esac
done
shift $((OPTIND -1))
EXPECTED_HTTP_CODE_PATTERN=",${EXPECTED_HTTP_CODE},"
CURL_STATUS=$(curl -sS -o /dev/null -w "%{http_code}" --connect-timeout "${CURL_TIMEOUT}" --max-time "${CURL_TIMEOUT}" "$@" 2>/dev/null)
if [[ "${EXPECTED_HTTP_CODE_PATTERN}" != *",${CURL_STATUS},"* ]]; then
echo "Error: Curl returned non expected status code ${CURL_STATUS} (expected status: ${EXPECTED_HTTP_CODE})";
$VERBOSE && echo "Debug: Curl command: curl -s -o /dev/null -w %{http_code} --connect-timeout ${CURL_TIMEOUT} --max-time ${CURL_TIMEOUT} $(_echo_args "$@")"
return 1;
fi
}
# Usage:
# wait_curl_status [-c EXPECTED_HTTP_CODE] [-t TIMEOUT] [-i INTERVAL] [-- [CURL_ARGS]...] URL
# Parameters:
# -c: comma separated list of expected HTTP CODE (default: 200)
# -t: timeout in second (default: 60)
# -i: interval in second (default: 1)
# -v: verbose (warning: this could print password)
# --: special option to delimit end of wait_curl_status parameters. All options behind are curl options (see example below)
# Example:
# wait_curl_status https://postman-echo.com/status/200
# wait_curl_status -c 204 -t 10 -i 2 https://postman-echo.com/status/204
# wait_curl_status -c 204 -t 10 -i 2 -- -X GET -H "host: local.com" https://postman-echo.com/status/204
# wait_curl_status -c 500,000 -t 10 -i 2 -- -X GET -H "host: local.com" https://postman-echo.com/status/500
function wait_curl_status() {
local EXPECTED_HTTP_CODE TIMEOUT TIMEOUT_ORIGIN INTERVAL CURL_STATUS CURL_TIMEOUT VERBOSE=false
EXPECTED_HTTP_CODE="200"
CURL_TIMEOUT=5
TIMEOUT=60
INTERVAL=1
local OPTIND arg
while getopts ":c:t:i:v" arg; do
case $arg in
c) EXPECTED_HTTP_CODE="${OPTARG}";;
t) TIMEOUT="${OPTARG}";;
i) INTERVAL="${OPTARG}";;
v) VERBOSE=true;;
:) printlnError "Invalid option: $OPTARG requires an argument"; return 1;;
?) printlnError "Unkownn option: $OPTARG" ; return 1;;
esac
done
shift $((OPTIND -1))
EXPECTED_HTTP_CODE_PATTERN=",${EXPECTED_HTTP_CODE},"
TIMEOUT_ORIGIN="${TIMEOUT}"
until CURL_STATUS=$(curl -sS -o /dev/null -w "%{http_code}" --connect-timeout "${CURL_TIMEOUT}" --max-time "${CURL_TIMEOUT}" "$@" 2>/dev/null || true) && [[ "${EXPECTED_HTTP_CODE_PATTERN}" == *",${CURL_STATUS},"* ]] || [ "$TIMEOUT" -le 0 ]; do
sleep "${INTERVAL}"
TIMEOUT=$((TIMEOUT-INTERVAL))
done
if [[ "${EXPECTED_HTTP_CODE_PATTERN}" != *",${CURL_STATUS},"* ]]; then
echo "Error: Curl returned non expected status code ${CURL_STATUS} after ${TIMEOUT_ORIGIN}s (expected status: ${EXPECTED_HTTP_CODE})";
$VERBOSE && echo "Debug: Curl command: curl -s -o /dev/null -w %{http_code} --connect-timeout ${CURL_TIMEOUT} --max-time ${CURL_TIMEOUT} $(_echo_args "$@")"
return 1;
fi
}
# Helper function to print args "$@" preserving the quote
# Usage:
# _echo_args "first args" "second args"
# echo "ls $(_echo_args "first args" "second args")"
# # in a function (which is the main purpose)
# echo "ls $(_echo_args "$@")"
function _echo_args () {
local ARG ARGS=""
for ARG in "${@}" ; do
# add quote only if $ARG contains a space
if [[ "$ARG" != "${ARG%[[:space:]]*}" ]]; then
ARG="\"$ARG\""
fi
if [ -z "$ARGS" ]; then
ARGS="$ARG"
else
ARGS="$ARGS $ARG"
fi
done
echo "$ARGS"
}
In this line you mention set -e
in subprocesses created by the script:
Line 230 in 846919d
I'm not sure if I completely understand what you mean by that, but maybe adding this can work?:
shopt -s inherit_errexit 2>/dev/null || true
This makes subprocesses inherit errexit. But maybe I'm confused and you were not referring to that.
I just discovered this project and I find it amazing. Thanks for the work!
Dear @mig1984,
I found bashible on Hacker News, just want to share some thoughts about bashible based on my own Ansible experience.
I checked documents under docs/
directory and some examples, i think the biggest issue of bashible might be unclear command names and syntax, they could/should be greatly improved.
About command names, for example:
may_fail
: better be ignore_errors
(used by Ansible)add_line
, append_line
, prepend_line
: maybe merge to one command named lineinfile
(line in file. used by Ansible), and just use some argument to indicate it should be appended or prepended if not present.fill_var
: why it's called fill
and not something like set
?var_empty
: maybe rename to 'is_empty_var'? easier to understand.About syntax, i'd like to compare bashible with cdist. Although cdist itself is wrote in Python-3, but cdist user just writes bash scripting in cdist syntax.
Compare bashible's add_line
, append_line
and prepend_line
for example, compare them with cdist's __line
command (it's called cdist type
instead of command (bashible) or task (ansible), but let's call it command below and save me some typing). Please allow me to copy cdist official examples below:
# Manage a hosts entry for www.example.com.
__line /etc/hosts \
--line '127.0.0.2 www.example.com'
# Manage another hosts entry for test.example.com.
__line hosts:test.example.com \
--file /etc/hosts \
--line '127.0.0.3 test.example.com'
# Remove the line starting with TIMEZONE from the /etc/rc.conf file.
__line legacy_timezone \
--file /etc/rc.conf \
--regex 'TIMEZONE=.*' \
--state absent
# Insert a line before another one.
__line password-auth-local:classify \
--file /etc/pam.d/password-auth-local \
--line '-session required pam_exec.so debug log=/tmp/classify.log /usr/local/libexec/classify' \
--before '^session[[:space:]]+include[[:space:]]+password-auth-ac$'
# Insert a line after another one.
__line password-auth-local:classify \
--file /etc/pam.d/password-auth-local \
--line '-session required pam_exec.so debug log=/tmp/classify.log /usr/local/libexec/classify' \
--after '^session[[:space:]]+include[[:space:]]+password-auth-ac$'
One command may support multiple arguments to generate different results. This syntax (IMO) is so much better than bashible.
Just my 2 cents. hope it helps a little. :)
I'm interested in starting to roll out some bashible for some general use cases.
It would be nice to get an idea of when you think you'll have a fairly stable version baselined, just so I can understand if there will be breaking changes if I update.
I know you were still adapting it based on a couple of feedback points received.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.