1
0
mirror of https://github.com/limosek/zaf.git synced 2025-11-01 10:07:38 +01:00

Reworked interface

This commit is contained in:
Lukas Macura
2016-04-01 15:51:45 +02:00
parent d0a3244f79
commit 99bd255e3f
6 changed files with 147 additions and 65 deletions

View File

@@ -1,8 +1,6 @@
# Control file related functions
# Get block from stdin
# $1 option
# $2 name
# Get item list from control on stdin
zaf_ctrl_get_items() {
grep '^Item ' | cut -d ' ' -f 2 | cut -d ':' -f 1 | tr '\r\n' ' '
}
@@ -57,14 +55,16 @@ zaf_block_get_option() {
# $1 - control file
# $2 - option name
zaf_ctrl_get_global_option() {
zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" || zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2"
zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \
|| zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2"
}
# Get item specific option (single or multiline)
# $1 - control file
# $2 - item name
# $3 - option name
zaf_ctrl_get_item_option() {
zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" || zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3"
zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \
|| zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3"
}
# Check dependencies based on control file
@@ -82,24 +82,24 @@ zaf_ctrl_check_deps() {
}
# Install binaries from control
# $1 control
# $2 plugindir
# $1 pluginurl
# $2 control
# $3 plugindir
zaf_ctrl_install() {
local binaries
local pdir
local script
local cmd
pdir="$2"
export ZAF_PLUGIN_DIR="$pdir"
binaries=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-bin")
pdir="$3"
binaries=$(zaf_ctrl_get_global_block <$2 | zaf_block_get_option "Install-bin")
for b in $binaries; do
zaf_fetch_url "$url/$b" >"${ZAF_TMP_DIR}/$b"
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir"
done
script=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "Install-script")
script=$(zaf_ctrl_get_global_block <$2 | zaf_block_get_moption "Install-script")
[ -n "$script" ] && eval "$script"
cmd=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-cmd")
cmd=$(zaf_ctrl_get_global_block <$2 | zaf_block_get_option "Install-cmd")
[ -n "$cmd" ] && $cmd
}
@@ -109,33 +109,41 @@ zaf_ctrl_install() {
zaf_ctrl_generate_cfg() {
local items
local cmd
local iscript
local ikey
local lock
items=$(zaf_ctrl_get_items <"$1")
for i in $items; do
ikey=$(echo $i | tr -d '[]*&;:')
iscript=$(echo $i | tr -d '[]*&;:')
params=$(zaf_ctrl_get_item_option $1 $i "Parameters")
if [ -n "$params" ]; then
ikey="$2.$i[*]"
else
ikey="$2.$i"
fi
lock=$(zaf_ctrl_get_item_option $1 $i "Lock")
if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock "
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
echo "UserParameter=$2.${i},$lock$cmd";
echo "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock$cmd";
continue
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Function")
if [ -n "$cmd" ]; then
echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $lock$cmd";
echo "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock$cmd";
continue;
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then
zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${ikey}.sh;
zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${iscript}.sh;
zaf_install_bin ${ZAF_TMP_DIR}/${ikey}.sh ${ZAF_PLUGINS_DIR}/$2/
echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $lock${ZAF_PLUGINS_DIR}/$2/${ikey}.sh";
echo "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $lock${ZAF_PLUGINS_DIR}/$2/${iscript}.sh";
continue;
fi
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
done
}

View File

@@ -68,24 +68,34 @@ zaf_is_root(){
# $2 - directory
zaf_install(){
zaf_dbg "Install file $1 to $INSTALL_PREFIX/$2/$(basename $1)"
cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
}
# $1 - src file
# $2 - directory
zaf_install_bin(){
zaf_dbg "Install binary $1 to $INSTALL_PREFIX/$2/$(basename $1)"
cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
chmod +x "$INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO chmod +x "$INSTALL_PREFIX/$2/$(basename $1)"
}
# $1 - directory
zaf_install_dir(){
zaf_dbg "Install directory $1 to $INSTALL_PREFIX/$1"
mkdir -p "$INSTALL_PREFIX/$1"
$ZAF_DO mkdir -p "$INSTALL_PREFIX/$1"
}
# $1 - file
zaf_touch(){
zaf_dbg "Touch $INSTALL_PREFIX/$1"
touch "$INSTALL_PREFIX/$1"
$ZAF_DO touch "$INSTALL_PREFIX/$1"
}
# $1 - directory
zaf_uninstall(){
if [ -n "$INSTALL_PREFIX" ]; then
zaf_dbg "Removing $INSTALL_PREFIX/$1"
$ZAF_DO rm -rf "$INSTALL_PREFIX/$1"
else
zaf_dbg "Removing $1"
$ZAF_DO rm -rf "$1"
fi
}
# Automaticaly install agent on debian

View File

@@ -1,7 +1,20 @@
#!/bin/sh
. /etc/zaf.conf
[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}"
trap "rm -rif ${ZAF_TMP_DIR}" EXIT
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
. ${ZAF_LIB_DIR}/zaf.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh
. ${ZAF_LIB_DIR}/os.lib.sh
export ZAF_LIB_DIR
export ZAF_TMP_DIR
export ZAF_PLUGINS_DIR
[ -n "$*" ] && $@

View File

@@ -1,4 +1,8 @@
# Hardcoded variables
ZAF_VERSION="trunk"
ZAF_URL="https://raw.githubusercontent.com/limosek/zaf/master/"
############################################ Common routines
zaf_msg() {
@@ -16,6 +20,10 @@ zaf_err() {
exit 1
}
zaf_version(){
echo $ZAF_VERSION
}
# Fetch url to stdout
# $1 url
# It supports real file, file:// and other schemes known by curl
@@ -73,7 +81,7 @@ zaf_discovery_add_row(){
shift;shift
echo " {"
while [ -n "$1" ]; do
echo -n ' "{#'$1'}":"'$2'" '
echo -n ' "'$1'":"'$2'" '
shift;shift
if [ -n "$1" ]; then
echo ","
@@ -135,8 +143,12 @@ zaf_check_agent_config() {
# Update repo
zaf_update_repo() {
[ "$ZAF_GIT" != 1 ] && { zaf_err "Git is not installed. Exiting."; }
! [ -d ${ZAF_REPO_DIR} ] && git clone "${ZAF_PLUGINS_REPO}" "${ZAF_REPO_DIR}"
[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull
if [ -z "${ZAF_PLUGINS_GITURL}" ] || [ -z "${ZAF_REPO_DIR}" ]; then
zaf_err "This system is not configured for git repository."
else
[ ! -d "${ZAF_REPO_DIR}" ] && git clone "${ZAF_PLUGINS_GITURL}" "${ZAF_REPO_DIR}"
(cd ${ZAF_REPO_DIR} && git pull)
fi
}
# Construct url from plugin name
@@ -154,7 +166,7 @@ zaf_get_plugin_url() {
if [ -d "${ZAF_REPO_DIR}/$1" ]; then
url="${ZAF_REPO_DIR}/$1"
else
url="${ZAF_PLUGINS_REPO}/$1";
url="${ZAF_PLUGINS_URL}/$1";
fi
fi
fi
@@ -206,8 +218,9 @@ zaf_install_plugin() {
if zaf_prepare_plugin "$1" $plugindir; then
[ "$ZAF_DEBUG" -gt 0 ] && zaf_plugin_info "${control}"
zaf_ctrl_check_deps "${control}"
zaf_ctrl_install "${control}" "${plugin}"
zaf_ctrl_generate_cfg "${control}" "${plugin}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_ctrl_install "$1" "${control}" "${plugindir}"
zaf_ctrl_generate_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
else
zaf_err "Cannot install plugin $plugin to $plugindir!"
@@ -228,7 +241,6 @@ zaf_list_plugins() {
}
zaf_is_plugin() {
[ -d "$1" ] && [ -f "$1/control.zaf" ] && return
[ -d "$ZAF_PLUGINS_DIR/$1" ] && [ -f "$ZAF_PLUGINS_DIR/$1/control.zaf" ] && return
false
}
@@ -252,25 +264,39 @@ zaf_plugin_version() {
}
zaf_list_plugin_items() {
if [ -z "$1" ]; then
echo "Missing plugin name";
exit 1
local items
local i
local p
local key
if ! zaf_is_plugin "$1"; then
zaf_err "Missing plugin name or plugin $1 unknown. ";
fi
plugindir="${ZAF_PLUGINS_DIR}/$1"
cfile="$plugindir/control"
if [ -d "$plugindir" ] ; then
zaf_ctrl_get_option "$cfile" Item
else
echo "Plugin $1 not installed"
fi
cfile="$plugindir/control.zaf"
items=$(zaf_ctrl_get_items <$cfile)
for i in $items; do
p=$(zaf_ctrl_get_item_option $cfile $i "Parameters")
if [ -n "$p" ]; then
key="$1.$i[]"
else
key="$1.$i"
fi
echo -n "$key "
done
echo
}
zaf_list_items() {
for p in $(zaf_list_plugins); do
zaf_list_plugin_items $p
echo $p: $(zaf_list_plugin_items $p)
done
}
zaf_test_item() {
$ZAF_AGENT_BIN -t "$1"
}
zaf_remove_plugin() {
! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { zaf_err "Plugin $1 not installed!"; }
zaf_wrn "Removing plugin $1"