Just to follow up, what I ended up doing was this:
Code:
add_action() {
actions[${#actions[*]}]=$1;
safe_rollback[${#safe_rollback[*]}]=$2;
}
run_actions() {
I=0;
while [ $I -le ${#actions[@]} ]; do
${actions[$I]};
if [ $? -ne 0 ];
then
echo "Rollback" $I;
J=$I;
while [ $J -ge 0 ]; do
${safe_rollback[$J]};
$J=$(($J+1));
done;
exit 1;
fi
I=$(($I+1));
done
}
actions=( );
safe_rollback=( );
Then to use it, you create an action function and a rollback function for that action and add them:
Code:
my_action() {
echo "a";
}
rollback_my_action() {
echo "rollback";
}
You then add it with the add_action function:
Code:
add_action my_action rollback_my_action;
Then at the end you run all of them:
If any of them return a code that isn't 0, it will rollback that action and all previous actions as well, going in reverse order.
Bookmarks