Skip to content
Snippets Groups Projects
Commit db6791cc authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
bin/getfile.sh.cfg
profiles/ci-webgui/config.sh
profiles/ci-webgui/replace.txt
profiles/ci-webgui/tasks_config.sh
#!/usr/bin/env bash
# ======================================================================
#
# IML DEPLOYMENT ... REPLACE SCRIPT FOR TEMPLATES
#
# ----------------------------------------------------------------------
#
# ----------------------------------------------------------------------
# 2020-12-15 v0.0 <axel.hahn@iml.unibe.ch>
# ======================================================================
# ----------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------
tplfile=$1
outfile=$2
replacefile=$3
test -z "$replacefile" && replacefile="replace*.txt"
tmpfile=/tmp/myreplacement.tmp
usage="`basename $0` TEMPLATE-FILE OUTFILE [optional: REPLACE-DATA-FILE]"
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
echo
echo "===== IML DEPLOYMENT :: replace variables in erb syntax ====="
echo
echo "template : $tplfile"
echo "output : $outfile"
echo "replacements: $replacefile"
echo
# --- parameter checks
if [ -z "$tplfile" -o -z "$outfile" ]; then
echo "ERROR: missing params."
echo $usage
exit 1
fi
if [ ! -f "$tplfile" ]; then
echo "ERROR: template [$tplfile] does not exist."
echo $usage
exit 2
fi
ls $replacefile >/dev/null
if [ $? -ne 0 ]; then
echo "ERROR: replacement data file [$replacefile] does not exist."
echo $usage
exit 2
fi
# --- create temporary file to make all replacements in it
cat $tplfile > $tmpfile
# --- apply replace rules
grep "^[a-zA-Z]" $replacefile | grep "=" | while read line
do
key=$( echo $line | cut -f 1 -d "=" )
val=$( echo $line | cut -f 2- -d "=" )
regex="<\%\=\ *\@replace\[\"$key\"\]\ *\%>"
grep "$regex" $tmpfile >/dev/null || echo "WARNING: template [$tplfile] has no placeholder key [$key]"
# Ruby erb template syntax: <% @replace["mykey"] %>
# sed -i "s#<\%\ *\@replace\[\"$key\"\]\ *\%>#$val#g" $tmpfile
sed -i "s#$regex#$val#g" $tmpfile
done
# --- verify changes
echo
echo --- changes:
diff "$tplfile" "$tmpfile"
if [ $? -eq 0 ]; then
echo "ERROR: template was unchanged. Check placeholders in $tplfile and replacements:"
cat $replacefile
exit 3
fi
# --- create target file
mv -f "${tmpfile}" "${outfile}"
echo
echo --- output file:
ls -l "${outfile}" && exit 0
echo "ERROR: unable to write target file $outfile."
exit 4
# ----------------------------------------------------------------------
\ No newline at end of file
#!/usr/bin/env bash
# ======================================================================
#
# API CLIENT :: GET A CI FILE FROM PACKAGE SERVER
#
# ----------------------------------------------------------------------
# 2021-03-31 v1.0 <axel.hahn@iml.unibe.ch> init
# 2021-04-13 v1.1 <axel.hahn@iml.unibe.ch> add support for custom config
# 2021-04-15 v1.2 <axel.hahn@iml.unibe.ch> added debugging of curl request
# ======================================================================
# ----------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------
line="----------------------------------------------------------------------"
bDebug=0
customconfig=
. $0.cfg
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
function showhelp(){
self=$( basename $0 )
echo "
CIPGK GETTER
Get packages from a software sattelite of IML ci server.
SYNTAX:
$self [OPTIONS]
OPTIONS:
-c CFGFILE load custom config file after defaults in $self.cfg
-d enable debug infos
-e PHASE phase; overrides env variable IMLCI_PHASE
-f FILE filename to get (without path); overrides env variable IMLCI_FILE
-l ITEM list
-o OUTFILE optional output file
-p PROJECT ci project id; overrides env variable IMLCI_PROJECT
-s SECRET override secret in IMLCI_PKG_SECRET
-u URL URL of iml ci server without trailing /; overrides env variable IMLCI_URL
VALUES:
CFGFILE custom config file. It is useful to handle files of different
projects on a server.
PHASE is a phase of the ci server; one of preview|stage|live
FILE is a filename without path that was created by ci server.
OUTFILE Output file. It can countain a path. If none is given the filename
will be taken from FILE and stored in current directory
PROJECT project id of the ci server
SECRET secret to access project data on package server. Your given secret
must match the secret on package server to get access to any url.
ITEM type what to list; one of phases|projects|files
To list projects a phase must be set.
To list files a phase and a project must be set.
DEFAULTS:
You don't need to set all values by command line. Use a config to set defaults
$0.cfg
EXAMPLES:
If url, secret, project and phase are set in the config you can operate by
setting the filename to request.
$self -f FILE
downloads FILE to the current dir.
$self -f FILE -o my-own-filename.tgz
downloads FILE as my-own-filename.tgz
$self -f ALL
there is a special file ALL; it fetches all filenames by executing a directory
listing and then downloads all remote files with their original name
$self -e preview -l projects
list existing projects in phase preview
$self -l files
list existing files of current project
Remark: The directory listing can be turned off on the package server and
results in a 403 status.
"
}
function makeRequest(){
local apiMethod=$1
local apiRequest=$2
local outfile=$3
local secret=$4
# local outfile=$( mktemp )
if [ $bDebug = 1 ]; then
echo $line
echo $apiMethod ${apiHost}${apiRequest}
echo $line
fi
if [ ! -z "$secret" ]; then
# --- date in http format
LANG=en_EN
# export TZ=GMT
apiTS=`date "+%a, %d %b %Y %H:%M:%S %Z"`
# --- generate data to hash: method + uri + timestamp; delimited with line break
data="${apiMethod}
${apiRequest}
${apiTS}
"
# generate hash - split in 2 commands (piping "cut" sends additional line break)
myHash=`echo -n "$data" | openssl sha1 -hmac "${secret}" | cut -f 2 -d" "`
myHash=`echo -n "$myHash" | base64`
moreheaders="--fail"
test $bDebug = 1 && moreheaders="-i"
tmpdownloadfile="${outfile}.downloading"
curl \
-H "Accept: application/json" -H "Content-Type: application/json" \
-H "Date: ${apiTS}" \
-H "Authorization: demo-bash-client:${myHash}" \
-X $apiMethod \
-o "${tmpdownloadfile}" \
$moreheaders \
-s \
${IMLCI_URL}${apiRequest}
rc=$?
if [ "$bDebug" = "1" ]; then
cat "${tmpdownloadfile}"
rm -f "${tmpdownloadfile}"
exit 0
fi
if [ $rc -eq 0 ]; then
# echo OK.
# no outfile (= request to a directory)
if [ -z "$outfile" ]; then
# echo
# echo ----- RESPONSE BODY:
cat "${tmpdownloadfile}"
rm -f "${tmpdownloadfile}"
else
mv "${tmpdownloadfile}" "${outfile}"
ls -l "${outfile}"
fi
else
echo ERROR: Download failed.
fi
else
curl\
-H "Accept: application/json" -H "Content-Type: application/json" \
-X $apiMethod \
-o "${tmpdownloadfile}" \
${IMLCI_URL}${apiRequest}
fi
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
if [ $# -lt 1 ]; then
showhelp
exit 1
fi
while getopts "c:de:f:l:o:p:s:u:" option; do
case ${option} in
c) customconfig="$OPTARG" ;;
d) bDebug=1 ;;
e) export IMLCI_PHASE=$OPTARG ;;
f) export IMLCI_FILE=$OPTARG ;;
l) case $OPTARG in
phases)
IMLCI_PHASE=''
IMLCI_PROJECT=''
IMLCI_FILE=''
;;
projects)
IMLCI_PROJECT=''
IMLCI_FILE=''
;;
files)
IMLCI_FILE=''
;;
*)
echo ERROR: invalid value for option [-l]
echo
showhelp
exit 2
esac
;;
o) export IMLCI_OUTFILE=$OPTARG ;;
p) export IMLCI_PROJECT=$OPTARG ;;
s) export IMLCI_PKG_SECRET=$OPTARG ;;
u) export IMLCI_URL=$OPTARG ;;
*)
echo ERROR: invalid option [${option}]
echo
showhelp
exit 2
esac
done
if [ ! -z "$customconfig" ]; then
if [ -r "$customconfig" ]; then
. "$customconfig" || exit 2
else
echo "ERROR: unable to read custom config [$customconfig]."
exit 2
fi
fi
test -z ${IMLCI_OUTFILE} && IMLCI_OUTFILE=$IMLCI_FILE
if [ $bDebug = 1 ]; then
pre=">>>>>> "
echo $line
echo
echo DEBUG INFOS
echo
echo "${pre} defaults in $0.cfg"
cat $0.cfg 2>/dev/null
echo
if [ ! -z "$customconfig" ]; then
echo "${pre} custom config $customconfig"
cat "$customconfig"
echo
fi
echo "${pre} Params (override default values)"
echo $*
echo
echo "${pre} effective values"
echo "IMLCI_URL = $IMLCI_URL"
echo "IMLCI_PKG_SECRET = $IMLCI_PKG_SECRET"
echo "IMLCI_PROJECT = $IMLCI_PROJECT"
echo "IMLCI_PHASE = $IMLCI_PHASE"
echo "IMLCI_FILE = $IMLCI_FILE"
echo "IMLCI_OUTFILE = $IMLCI_OUTFILE"
echo
fi
if [ "$IMLCI_FILE" = "ALL" ]; then
# echo ALL files were requested ...
printf "%-30s" "get list of all files... "
tmpfilelist=$( mktemp )
$0 -u "${IMLCI_URL}" \
-p "${IMLCI_PROJECT}" \
-e "${IMLCI_PHASE}" \
-s "${IMLCI_PKG_SECRET}" \
-l files \
-o "${tmpfilelist}"
# cat "${tmpfilelist}"
cat "${tmpfilelist}" | grep "^file:" | while read fileline
do
# echo $line
myfile=$( echo $fileline | cut -f 2- -d ':' )
printf "%-30s" "GET $myfile... "
$0 -u "${IMLCI_URL}" \
-p "${IMLCI_PROJECT}" \
-e "${IMLCI_PHASE}" \
-s "${IMLCI_PKG_SECRET}" \
-f "${myfile}"
done
else
makeRequest GET "/packages/$IMLCI_PHASE/$IMLCI_PROJECT/$IMLCI_FILE" "$IMLCI_OUTFILE" "$IMLCI_PKG_SECRET"
fi
\ No newline at end of file
# ----------------------------------------------------------------------
# defaults
# ----------------------------------------------------------------------
IMLCI_PKG_SECRET=our-package-server-secret
IMLCI_URL=https://cipkg.example.com
IMLCI_PHASE=preview
# must/ should be set in a [profile]/config.sh
IMLCI_PROJECT=
#!/usr/bin/env bash
# ======================================================================
#
# DEPLOYMENT POC CLIENT
#
# ----------------------------------------------------------------------
# 2021-04-19 v0.1 <axel.hahn@iml.unibe.ch> initial version
# ======================================================================
# ----------------------------------------------------------------------
# CONFIG
# ----------------------------------------------------------------------
cd $( dirname $0 )
selfdir=$( /bin/pwd )
tmpdir=/tmp
wait=0
# wait=1
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
# get a list profiles by searching a config.sh
# no param
function getprofiles(){
find ${selfdir}/profiles/ -name "config.sh" | rev | cut -f 2 -d "/" | rev
}
# set a profile, load it, verify required parameters
# param string name of a subdir in ./profiles/
function setprofile(){
profile=$1
# source config for software download - as default.
. ${selfdir}/bin/getfile.sh.cfg
# my install dir
installdir=
# fileowner
appowner=
profiledir=${selfdir}/profiles/${profile}
. ${profiledir}/config.sh || exit 11
echo "[${profiledir}/config.sh] was loaded."
if [ -z "$installdir" -o -z "${IMLCI_PHASE}" -o -z "${IMLCI_PROJECT}" ]; then
echo "to be defined in ${profiledir}/config.sh:"
echo "installdir = $installdir"
echo "These variables must be set in bin/getfile.sh.cfg or in [profile]/config.sh:"
echo "IMLCI_PHASE = $IMLCI_PHASE"
echo "IMLCI_PROJECT = $IMLCI_PROJECT"
exit 12
fi
echo "OK, profile [${profile}] was set."
}
# output a colored infoline with date and given message
# param string message text
function header(){
local COLOR="\033[34m"
local NO_COLOR="\033[0m"
echo -en "${COLOR}"
echo ______________________________________________________________________
echo
echo -n ">>>>>>>>>> $(date) "
test ! -z "$profile" && echo -n "${profile} :: "
echo -n " - $* "
echo -en "${NO_COLOR}"
if [ "$wait" = "1" ]; then
echo -n "RETURN"; read dummy;
fi
echo
}
# execute a task/ hook - if the given task script exists and has executable
# persmissions; if not it is not an error
# param string filename
function run_task(){
local taskscript=$1
if [ -x "${taskscript}" ]; then
. "${taskscript}" || exit 10
else
echo "SKIP: task script ${taskscript} does not exist."
fi
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
cd $( dirname $0 )
echo
echo
echo "<<<<<<<<<<##########| POC :: DEPLOYMENT SCRIPT |##########>>>>>>>>>>"
echo
if [ $# -eq 0 ]; then
header "looping over all profiles"
getprofiles
echo
for myprofile in $( getprofiles )
do
$0 $myprofile
done
exit $?
fi
# ----------------------------------------------------------------------
header "Set profile [$1]"
setprofile $1
# ----------------------------------------------------------------------
header "Download ${IMLCI_PROJECT}.tgz"
cd $tmpdir
${selfdir}/bin/getfile.sh -f ${IMLCI_PROJECT}.tgz || exit 1
# ----------------------------------------------------------------------
header "Go into install dir ${installdir}"
test -d "${installdir}" || mkdir -p "${installdir}"
cd ${installdir} || exit 2
pwd
# ----------------------------------------------------------------------
header "PRE tasks"
# what you could do here:
# - enable maintenance flag
# - stop service
# - clenaup directory ... up to remove all current files
run_task "${profiledir}/tasks_preinstall.sh"
# ----------------------------------------------------------------------
header "Extract ${IMLCI_PROJECT}.tgz"
tar -xzf ${tmpdir}/${IMLCI_PROJECT}.tgz . || exit 3
ls -l
# ----------------------------------------------------------------------
header "Remove download archive ${IMLCI_PROJECT}.tgz"
echo rm -f ${IMLCI_PROJECT}.tgz
# ----------------------------------------------------------------------
header "Update config files"
echo "Showing replacements:" ; grep '@replace\[' hooks/templates/*
run_task "${profiledir}/tasks_config.sh"
# ----------------------------------------------------------------------
header "Set file owner [${appowner}]"
if [ ! -z "${appowner}" ]; then
sudo chown -R $appowner * || exit 5
ls -l
else
echo "SKIP: variable appowner was not set"
fi
# ----------------------------------------------------------------------
header "POST tasks"
# what you could do here:
# - start current deploy scripts
# - apply database updates
# - set permissions
# - start service
# - remove maintenance flag
# - send success message as email/ slack/ [another fancy tool]
run_task "${profiledir}/tasks_postinstall.sh"
# ----------------------------------------------------------------------
header "DONE :-)"
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# TASKS :: CONFIGS
# ----------------------------------------------------------------------
# my install dir
installdir=/var/www/FQDN
# fileowner
# appowner="ladmin:axel"
# ----- settings for CI server software package
# override global value
# export IMLCI_PHASE=preview
# see output of
export IMLCI_PROJECT=example
# ----------------------------------------------------------------------
# REPLACEMENTS
# ----------------------------------------------------------------------
#
# SYNTAX:
# key=value
#
# - no spaces around first "="
# - value can contain any char, no quoting
# ----------------------------------------------------------------------
dbhost=localhost
dbuser=fred
dbpassword=vom-jupiter
dbport=3306
api-key=12345678
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# TASKS :: GENERATE CONFIGS
# ----------------------------------------------------------------------
# create_config.sh template_file target_file replacements (can be multiple files)
# | | | |
# v v v v
"${selfdir}/bin/create_config.sh" hooks/templates/mytemplate.erb config/target.php ${projectdir}/replace.txt
# ----------------------------------------------------------------------
# TASKS :: POST INSTALL ACTIONS
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# TASKS :: PRE INSTALL ACTIONS
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment