Created
November 13, 2018 00:40
-
-
Save jmrenouard/77c74ab04de40e13a28a31346f49cebf to your computer and use it in GitHub Desktop.
devops bash
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
#!/bin/sh | |
function parse_yaml() { | |
local yaml_file=$1 | |
local prefix=$2 | |
local s | |
local w | |
local fs | |
s='[[:space:]]*' | |
w='[a-zA-Z0-9_.-]*' | |
fs="$(echo @|tr @ '\034')" | |
sed -e "/- [^\"][^\'].*:/s|\([ ]*\)- \($s\)|\1-\n \1\2|g" "$yaml_file" | | |
sed -ne '/^--/s|--||g; s|\"|\\\"|g; s/\s*$//g;' \ | |
-e "/#.*[\"\']/!s| #.*||g; /^#/s|#.*||g;" \ | |
-e "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \ | |
-e "s|^\($s\)\($w\)$s[:-]$s\(.*\)$s\$|\1$fs\2$fs\3|p" | | |
awk -F"$fs" '{ | |
indent = length($1)/2; | |
if (length($2) == 0) { conj[indent]="+";} else {conj[indent]="";} | |
vname[indent] = $2; | |
for (i in vname) {if (i > indent) {delete vname[i]}} | |
if (length($3) > 0) { | |
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")} | |
printf("%s%s%s%s=(\"%s\")\n", "'"$prefix"'",vn, $2, conj[indent-1],$3); | |
} | |
}' | | |
sed -e 's/_=/+=/g' | | |
awk 'BEGIN { | |
FS="="; | |
OFS="=" | |
} | |
/(-|\.).*=/ { | |
gsub("-|\\.", "_", $1) | |
} | |
{ print }' | |
} | |
function create_variables() { | |
local yaml_file="$1" | |
eval "$(parse_yaml "$yaml_file")" | |
} | |
function sep1() | |
{ | |
echo '-------------------------------------------------------------------------' | |
} | |
function sep2() | |
{ | |
echo '#########################################################################' | |
} | |
function info() | |
{ | |
sep1 | |
echo "INFO - $(date +"%m-%d-%Y %H:%M") $@" | |
sep1 | |
} | |
function error() | |
{ | |
sep1 | |
echo "ERROR - $(date +"%m-%d-%Y %H:%M") $@" | |
sep1 | |
} | |
cmd() | |
{ | |
local label="$1" | |
local cmd="$2" | |
local is_test="$3" | |
local lRC | |
info "BEGIN - $label" | |
echo "Commande: $cmd" | |
[ -d "$md5_dir" ] || mkdir -p $md5_dir | |
if [ -f "$md5_dir/$(echo "$label$cmd" |md5sum|awk '{print $1}')" -a -z "$is_test" ];then | |
info "END - $label Skipped ..." | |
return 0 | |
fi | |
sep1 | |
eval $cmd | |
lRC=$? | |
sep1 | |
echo "Code retour: $lRC" | |
if [ $lRC -ne 0 ]; then | |
error "END - $label" | |
return $lRC | |
fi | |
[ -z "$is_test" ] && touch $md5_dir/$(echo "$label$cmd"|md5sum|awk '{print $1}') | |
info "END - $label" | |
} | |
parse_conf() | |
{ | |
local conf=$1 | |
create_variables $conf | |
} | |
execute_tasks() | |
{ | |
local label | |
local cmd | |
env|grep task | |
for i in $(seq 1 10000); do | |
task_label="$(eval "echo \${task${i}_label}")" | |
task_cmd="$(eval "echo \${task${i}_command}")" | |
task_is_test="$(eval "echo \${task${i}_is_test}")" | |
if [ -z "$task_label" ];then | |
break | |
fi | |
task_sudo="$(eval "echo \${task${i}_sudo_mode}")" | |
if [ "$task_sudo" = "1" -o "$task_sudo" = "yes" -o "$task_sudo" = "YES" -o "$task_sudo" = "on" ];then | |
task_cmd="sudo $task_cmd" | |
fi | |
cmd "$i) $task_label" "$task_cmd" $task_is_test | |
sep2 | |
done | |
} | |
parse_conf $1 | |
execute_tasks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment