#!/bin/bash
DefaultPort=1314
# verify the arguments
if [[ "$1" == "" ]] || [[ "$2" == "" ]]; then
echo "Usage: install_module
[port] ..."
exit 1
fi
# set the agent's address
address=$1
shift
# set the agent's port number
re='^[0-9]+$'
if [[ $1 =~ $re ]]; then
port=$1
shift
else
port=$DefaultPort
fi
# set a template command for sending the modules
cmd="curl -s -i -X PUT $address:$port/modules"
# setup directory for temporary files
dir="/tmp/"$(date +%s%N)"$RANDOM"
mkdir -p $dir
# for every module in the arguments:
# - archive the module's files to a temporary file
# - update the sending command by adding the module
# if the module is not exist then the program will
# set missing_module flag and then break from the loop
missing_module=0
for module in "$@" ; do
if [[ -d "$module" ]]; then
tar cvzhf $dir/$module.tgz $module 1>/dev/null
cmd="$cmd -F $module=@$dir/$module.tgz"
else
echo "Module $module is not exist!"
missing_module=`expr $missing_module + 1`
fi
done
if [[ $missing_module == 0 ]]; then
# execute the sending command there is no missing module
result=`$cmd`
re='.*HTTP/1\.1 200 OK.*'
if ! [[ $result =~ $re ]]; then
missing_module=`expr $missing_module + 1`
fi
fi
# delete directory that holds temporary files
rm -rf $dir
if [[ $missing_module == 0 ]]; then
echo "status: ok"
else
echo "status: failed"
fi
exit $missing_module