Skip to content

Commit

Permalink
Fixes #5791: Implementation of a command line for rudder agent
Browse files Browse the repository at this point in the history
  • Loading branch information
peckpeck committed Nov 21, 2014
1 parent 75f1a6e commit c30431e
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 0 deletions.
110 changes: 110 additions & 0 deletions bin/rudder
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/sh
#
# Wrapper to call rudder commands.
#
# All commands are taken from $BASEDIR (/opt/rudder/share/commands).
#
# To add a new command, just drop it in this directory with the name <role>-<command>
# where <role> can be 'agent' or 'server' and <command> is your command name.
# The command file is executed with remaining arguments and can be written in any executable form.
# The command file should contain a line with a description of the form "# @description <your description>"
#


set -e
BASEDIR="/opt/rudder/share/commands"

role_list() {
ls "${BASEDIR}" | sed 's/\([A-Za-z_]*\)-.*/\1/' | uniq
}

usage() {
echo ""
echo "Usage: rudder help"
for role in `role_list`
do
echo " rudder ${role} <command> [parameters ...]"
done
echo ""
echo " This is rudder generic command line interface."
echo ""
echo " help: this help"
for role in `role_list`
do
echo " ${role}: rudder ${role} related commands"
done
echo ""
}

role_usage() {
role="$1"
echo ""
echo "Usage: rudder ${role} help"
echo " rudder ${role} <command> [parameters ...]"
echo ""
echo " Run commands on ${role}."
echo ""
echo " Available commands:"
for file in `cd "${BASEDIR}"; find . -type f -name "${role}-*" | sort`
do
doc=`grep "# @description" "${BASEDIR}/${file}" | sed -e "s/#[ \t]*@description[ \t]*//"`
name=`echo "${file}" | sed -e "s/\.\/${role}-//"`
printf " %-10s\t%s\n" "${name}" "${doc}"
done
echo ""
}

# first parameter is role parameter
role=`echo "$1" | tr -C -d 'A-Za-z_'`
if [ "${role}" = "help" ] || [ -z "${role}" ]
then
if [ -z "$2" ]
then
usage
else
role=`echo "$2" | tr -C -d 'A-Za-z_'`
role_usage "${role}"
fi
exit 0
fi
shift

# test validity of role parameter
for installed_role in `role_list`
do
if [ "${role}" = "${installed_role}" ]
then
role_ok="y"
fi
done

if [ "${role_ok}" != "y" ]
then
echo "Unknown role '${role}'"
echo ""
usage
exit 1
fi

# second parameter is command parameter
command=`echo "$1" | tr -C -d 'A-Za-z_-'`
if [ "${command}" = "help" ] || [ "${command}" = "" ]
then
role_usage "${role}"
exit 3
fi
shift

# test validity of command parameter
if [ ! -f "${BASEDIR}/${role}-${command}" ]
then
echo "Command ${command} on ${role} not found"
echo ""
role_usage "${role}"
exit 3
fi


# OK GO !
exec "${BASEDIR}/${role}-${command}" "$@"

3 changes: 3 additions & 0 deletions share/commands/agent-disable
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# @description forbid rudder-agent to be run by cron or service
touch /opt/rudder/etc/disable-agent
13 changes: 13 additions & 0 deletions share/commands/agent-enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
# @description re-enable a disabled rudder-agent

while getopts "s" opt; do
case $opt in
s)
START=y
;;
esac
done

rm -f /opt/rudder/etc/disable-agent
[ "${START}" = "y" ] && service rudder-agent start
3 changes: 3 additions & 0 deletions share/commands/agent-inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# @description force the agent to create and send a new inventory
/var/rudder/cfengine-community/bin/cf-agent -KC -D force_inventory
12 changes: 12 additions & 0 deletions share/commands/agent-reinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
# @description re-initialise the agent to make it be seen as a new node on the server

# - replace ppkeys
/bin/rm -f /var/rudder/cfengine-community/ppkeys/localhost*
/var/rudder/cfengine-community/bin/cf-key

# - generate a new uuid
uuidgen > /opt/rudder/etc/uuid.hive

# - resend inventory
rudder agent inventory
14 changes: 14 additions & 0 deletions share/commands/agent-reset
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
# @description reset agent status and cache

# Try to remove everything that can block
# - restore initial promises
rsync --delete -r /opt/rudder/share/initial-promises/ /var/rudder/cfengine-community/inputs

# - remove state
/bin/rm -rf /var/rudder/cfengine-community/state/*
/bin/rm -f /var/rudder/cfengine-community/*.lmdb

# - remove locks
/bin/rm -f /var/rudder/cfengine-community/*lock

3 changes: 3 additions & 0 deletions share/commands/agent-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# @description force run agent promises
/var/rudder/cfengine-community/bin/cf-agent -KC
3 changes: 3 additions & 0 deletions share/commands/agent-update
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# @description update promises on agent
/var/rudder/cfengine-community/bin/cf-agent -KC -f failsafe.cf

0 comments on commit c30431e

Please sign in to comment.