opsi-productadd

This is a shell-script setting a package to be set up, if another package is already installed
I use this script to either set “setup”-Task on all hosts, already having a special package (for updating) or to install a software like adobeflash, if a host has already Mozilla Firefox

#!/bin/bash
# insert proper values for your opsi-server here
# Feel free to modify, as long as you let us participate on your changes... 
# Licensed under GPL
# Copyright Sven Schumacher, 2011
# NO WARRANTY
 
defaultuser="admin"
BASENAME=`basename $0`
OPSIDEPOT=`cat /etc/opsi/opsiconfd.conf|grep opsi-depot|awk -F= '{ print $2; }'|cut -d ' ' -f 2`   # NOT the workbench-dir
#OPSIDEPOT=/opsidepot/pcbin/install
CUT_OF_DOMAIN=`dnsdomainname`
# if backend file is used for softwareaudits, scroll down and uncomment line starting with "#  hostlist=`fgrep "
OPSI_SOFTWARE_INVENTORY_DIR="/var/lib/opsi/config/audit" # where to find *.sw-files generated by swaudit
# if backend mysql is used for softwareaudits nothing needs to be changed
 
# NO MORE CHANGES BELOW THIS LINE NEEDED
 
function usageinfo {
    echo "$BASENAME [OPTIONS] PRODUCT PRODUCTID
 
  $BASENAME sets for each found host which has PRODUCT installed the PRODUCTID to be installed
  Usage (e.g.):
  $BASENAME Firefox adobeflash
 
  Options:
        -q|--quiet      be quiet
        -h              prints this help
        --host          single host to install
        -u USER         the user to be used (default: admin)
        -p PASSWORD     the password for user in OPSI (read by STDIN if missing)
  Where
        PRODUCT         product, which should be listed in software-inventory
        PRODUCTID       id of the package, which should be installed
"
}
user=$defaultuser;
#use getopt to parse options
TEMP=`getopt -o hqp:u: --long help,quiet,host:,user:,password: \
     -n 'opsi-productadd' -- "$@"`
 
if [ $? != 0 ] ; then usageinfo ; exit 1 ; fi
 
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
quiet="1"
while true ; do
  case "$1" in
    -q|--quiet)
        quiet="0";
        shift;
       ;;
    -u|--user)
        user=$2;
        shift 2;
       ;;
    --host)
        host=$2;
        shift 2;
        ;;
    -p|--password)
        password=$2;
        shift 2;
       ;;
    -h|--help)
        usageinfo;
        shift;
        exit 1;
     ;;
    --) shift; break;;
     *) echo "We never should get here. Fatal error. AAAARRGS"; exit 1;;
  esac
done;
if [ $# != 2 ]; then
  echo "Missing essential parameters"
  usageinfo
  exit 1;
fi
#if you don't want to enter the password in commandline, you'll be asked for it
if [ -z "$password" ]; then
  read -s -p "Enter password for user $user (will not be displayed):"
  echo
  password=$REPLY
fi
product=$1;
productid=$2;
 
cd $OPSI_SOFTWARE_INVENTORY_DIR
if [ ! -d "$OPSIDEPOT/$productid" ]; then
  echo "No such package: $productid";
  exit 1;
fi
if [ -z "$host" ]; then
  # to be replaced by mysql query
  # use the following line if software-audit has backend "mysql"
hostlist=`(mysql -N --password=opsi -u opsi opsi <<--ENDE
SELECT clientid FROM SOFTWARE_CONFIG WHERE name LIKE "%$product%";
-ENDE
)|sed " s/.$CUT_OF_DOMAIN//g; "|uniq`
# if your software-audit has backend file use this line instead
#  hostlist=`fgrep -r $product *.sw |awk -F: '{ print $1; }'|egrep -v "global"|sed " s/.sw//g; s/.$CUT_OF_DOMAIN//g; "|uniq`
else
  hostlist=$host
fi
for I in $hostlist; do
  if [[ $quiet = "1" ]] ; then
    echo "Setting install for $I of $productid"
  fi
  opsi-admin -u $user -p $password method setProductActionRequest $productid $I.$CUT_OF_DOMAIN setup;
done