Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #5791: Implementation of a command line for rudder agent #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=`sed -ne "/#[ \t]*@description[ \t]*/s/#[ \t]*@description[ \t]*//p" "${BASEDIR}/${file}"`
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
19 changes: 19 additions & 0 deletions share/commands/agent-enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the enable command also check that cf-execd is running and start it if not? Otherwise, it's not really enabled, or it will be but only after some random delay...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be run after 5mn by cron if the user forgets to run it.
I think enable just mean enable and not run, but we can add a parameter for that.

if [ "${START}" = "y" ]
then
service rudder-agent start
else
echo "rudder-agent has been enabled but not started, use 'service rudder-agent start' or wait for next cron run"
echo "Next time, use -s option to automatically start it"
fi
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