opsi.admintools/bin/opsi-administration.sh
2013-03-04 17:52:36 +01:00

269 lines
7.6 KiB
Bash
Executable File

#! /bin/sh
# $Id$
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Copyright 2013 Mario Fetka. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
#
# Author: Mario Fetka <mario.fetka@gmail.com>
#
# treat unset variables as an error
set -u
die()
{
[ $# -gt 0 ] && echo "error: $@" | boxes -d shell >&2
exit 1
}
BASE_DIR="`dirname $0`/.."
LIB_DIR="${BASE_DIR}/lib"
OPSI_WEB_ROOT=""
# load libraries
. ${LIB_DIR}/shflags || die 'unable to load shflags library'
. ${LIB_DIR}/shlib || die 'unable to load shlib library'
. ${LIB_DIR}/versions || die 'unable to load versions library'
# Source local build configuration (must be done AFTER sourcing the builder-product.cfg.cfg)
test -f $HOME/.opsi-administration.cfg && . $HOME/.opsi-administration.cfg
# define flags
DEFINE_string 'name' '' 'the name of the package' 'n'
DEFINE_string 'version' '' 'the version of the package' 'v'
DEFINE_string 'type' '' 'type of package' 'y'
DEFINE_string 'from' '' 'source level of package' 'f'
DEFINE_string 'to' '' 'output file to write to' 't'
DEFINE_string 'root' '' 'output file to write to' 'r'
DEFINE_string 'show' '' 'show a list of the' 's'
DEFINE_string 'delete' '' 'delete specific versions of the package' 'd'
DEFINE_boolean 'purge' false 'delete old versions of the package' 'p'
FLAGS_HELP="USAGE: ${ARGV0} [flags]"
#if [ `which 7z` ] || exit
go_show()
{
local opsi_dir=$1
local show_name=$2
local show_type=$3
local show_from=$4
local show_to=$5
local show_version=""
local show_dir="/${opsi_dir}/${show_type}/${show_from}/"
# Find all revision files and sort them
local file_list=`mktemp /tmp/opsi-administration.show.XXXXXXXXXXX`
local file_list_version=`mktemp /tmp/opsi-administration.show.version.XXXXXXXXXXX`
local file_list_release=`mktemp /tmp/opsi-administration.show.release.XXXXXXXXXXX`
local file_list_final=`mktemp /tmp/opsi-administration.show.final.XXXXXXXXXXX`
local show_list=`mktemp /tmp/opsi-administration.show.list.XXXXXXXXXXX`
echo ""
echo "Web Root: ${show_dir} Package Name: ${show_name}" >> $show_list
echo "" >> $show_list
for cfg_file in `find ${show_dir} -name "${show_name}-*.cfg" -print ` ; do
. ${cfg_file}
echo $REV_VERSION >> ${file_list}
done
sort -V ${file_list} | uniq > ${file_list_version}
for pkg_version in `cat ${file_list_version}` ; do
for cfg_file_ver in ${show_dir}/${show_name}-${pkg_version}-*.cfg ; do
. ${cfg_file_ver}
echo ${pkg_version}-$REV_CREATOR_TAG$REV_RELEASE >> ${file_list_release}
done
done
sort -V ${file_list_release} | uniq > ${file_list_final}
for release_file_list in `cat ${file_list_final}` ; do
. ${show_dir}/${show_name}-${release_file_list}.cfg
echo $REV_PN-$REV_VERSION-$REV_CREATOR_TAG$REV_RELEASE >> $show_list
show_version=$REV_VERSION-$REV_CREATOR_TAG$REV_RELEASE
done
if [ -n "${show_version}" ];then
echo "./${ARGV0} --root ${OPSI_ROOT} --name ${show_name} --type ${show_type} --from ${show_from} --to ${show_to} --version ${show_version} (--purge)" >> $show_list
echo "./${ARGV0} --root ${OPSI_ROOT} --name ${show_name} --type ${show_type} --delete ${show_from} --version ${show_version} " >> $show_list
else
echo "no package named ${show_name}-* in this dir ${show_dir}" >> $show_list
fi
boxes -d shell $show_list
echo ""
rm -f ${show_list}
rm -f ${file_list}
rm -f ${file_list_final}
rm -f ${file_list_version}
rm -f ${file_list_release}
}
go_copy()
{
local opsi_dir=$1
local copy_name=$2
local copy_type=$3
local copy_from=$4
local copy_to=$5
local copy_version=$6
local copy_from_dir="/${opsi_dir}/${copy_type}/${copy_from}/"
local copy_to_dir="/${opsi_dir}/${copy_type}/${copy_to}/"
local copy_list=`mktemp /tmp/opsi-administration.copy.list.XXXXXXXXXXX`
echo ""
echo "Web Root Source: ${copy_from_dir} Web Root Destination: ${copy_to_dir}" >> $copy_list
echo "Package Name: ${copy_name}" >> $copy_list
echo "" >> $copy_list
if [ ${FLAGS_purge} -eq ${FLAGS_TRUE} ]; then
rm -f ${copy_to_dir}/${copy_name}-* &>> $copy_list
fi
ln -vf ${copy_from_dir}/${copy_name}{-,_}${copy_version}{.,-}* ${copy_to_dir}/ &>> $copy_list
boxes -d shell $copy_list
echo ""
rm -f ${copy_list}
}
go_delete()
{
local opsi_dir=$1
local delete_name=$2
local delete_type=$3
local delete_delete=$4
local delete_version=$5
local delete_dir="/${opsi_dir}/${delete_type}/${delete_delete}/"
local delete_list=`mktemp /tmp/opsi-administration.delete.list.XXXXXXXXXXX`
echo ""
echo "Web Root : ${delete_dir} Package Name: ${delete_name}" >> $delete_list
echo "" >> $delete_list
rm -f ${delete_dir}/${delete_name}{-,_}${delete_version}{.,-}* &>> $delete_list
boxes -d shell $delete_list
echo ""
rm -f ${delete_list}
}
main()
{
# checks
# check if a config file is present or the --root flag has been added
if [ -n "${OPSI_WEB_ROOT}" ];then
OPSI_ROOT="${OPSI_WEB_ROOT}"
else
if [ -n "${FLAGS_root}" ];then
OPSI_ROOT=${FLAGS_root}
else
die 'root flag or config file mising'
fi
fi
# the name falg is always needed
[ -n "${FLAGS_name:-}" ] || die 'name flag missing'
# check if the type is correctly set
if [ -n "${FLAGS_type}" ] ; then
case "${FLAGS_type}" in
public)
;;
restricted)
;;
*) die "type is only public or restricted"
;;
esac
fi
# check if the show is correctly set
if [ -n "${FLAGS_show}" ] ; then
case "${FLAGS_show}" in
integration)
;;
testing)
;;
release)
;;
*) die "show is onyl integration testing or release"
;;
esac
fi
# check if the from is correctly set
if [ -n "${FLAGS_from}" ] ; then
case "${FLAGS_from}" in
integration)
;;
testing)
;;
release)
;;
*) die "from is onyl integration testing or release"
;;
esac
fi
# check if the to is correctly set
if [ -n "${FLAGS_to}" ] ; then
case "${FLAGS_to}" in
integration)
;;
testing)
;;
release)
;;
*) die "to is onyl integration testing or release"
;;
esac
fi
# check if the delete is correctly set
if [ -n "${FLAGS_delete}" ] ; then
case "${FLAGS_delete}" in
integration)
;;
testing)
;;
release)
;;
*) die "to is onyl integration testing or release"
;;
esac
fi
# set some local vars
local show_from=""
local show_to=""
if [ -z "${FLAGS_from}" ] && [ -z "${FLAGS_to}" ] && [ -z "${FLAGS_type}" ] ; then
go_show ${OPSI_ROOT} ${FLAGS_name:-} "public" "integration" "testing"
elif [ -n "${FLAGS_show}" ] && [ -n "${FLAGS_type}" ] ; then
case "${FLAGS_show}" in
integration)
show_from="integration"
show_to="testing"
;;
testing)
show_from="testing"
show_to="release"
;;
esac
go_show ${OPSI_ROOT} ${FLAGS_name:-} ${FLAGS_type:-} ${show_from} ${show_to}
elif [ -z "${FLAGS_show}" ] && [ -n "${FLAGS_type}" ] && [ -n "${FLAGS_from}" ] && [ -n "${FLAGS_to}" ] && [ -n "${FLAGS_version}" ]; then
go_copy ${OPSI_ROOT} ${FLAGS_name:-} ${FLAGS_type:-} ${FLAGS_from:-} ${FLAGS_to:-} ${FLAGS_version:-} ${FLAGS_purge}
elif [ -z "${FLAGS_show}" ] && [ -n "${FLAGS_type}" ] && [ -n "${FLAGS_delete}" ] && [ -n "${FLAGS_version}" ]; then
go_delete ${OPSI_ROOT} ${FLAGS_name:-} ${FLAGS_type:-} ${FLAGS_delete:-} ${FLAGS_version:-}
else
die "read documetation for how to use"
fi
}
FLAGS "$@" || exit $?
[ ${FLAGS_help} -eq ${FALSE} ] || exit
eval set -- "${FLAGS_ARGV}"
main "$@"