From 67b5cf70d7a48bf9333e448b0d1ca53aacc252ea Mon Sep 17 00:00:00 2001 From: Elijah Saxon Date: Fri, 8 Jul 2005 19:21:46 +0000 Subject: [PATCH] made it so that helpers are dynamically defined. --- README | 24 ++++++++++++++ backupninja | 4 +-- handlers/easydialog.sh | 73 ++++++++++++++++++++++++++++++++++++------ handlers/ldap.helper | 1 + handlers/mysql.helper | 1 + handlers/rdiff.helper | 15 +++++---- handlers/sys.helper | 1 + ninjahelper | 67 +++++++++++++++++++++----------------- 8 files changed, 139 insertions(+), 47 deletions(-) diff --git a/README b/README index 13c89bf..80e9f07 100644 --- a/README +++ b/README @@ -222,3 +222,27 @@ VSERVERINFO (default: /usr/sbin/vserver-info) VSERVER (default: /usr/sbin/vserver) VROOTDIR (default: `$VSERVERINFO info SYSINFO |grep vserver-Rootdir | awk '{print $2}'; fi`) +NINJAHELPER +=========== + +Ninjahelper is an additional script which will walk you through the process of +configuring backupninja. Ninjahelper has a menu driven curses based interface +(using dialog). + +To add an additional 'wizard' to ninjahelper, follow these steps: + +(1) to add a helper for the handler "blue", create the file + blue.helper in the directory where the handlers live. + (ie /usr/share/backupninja). + +(2) next, you need to add your helper to the global HELPERS variable + and define the main function for your helper (the function name + is always _wizard). for example, blue.helper: + HELPERS="$HELPERS blue:description_of_this_helper + blue_wizard() { + ... do work here ... + } + +(3) check the examples of the included helpers to see how they are + written. The dialog functions are defined in easydialog.sh. + diff --git a/backupninja b/backupninja index 804c1c2..a1bfabc 100755 --- a/backupninja +++ b/backupninja @@ -163,8 +163,8 @@ function check_perms() { local perms=`ls -ld $file` perms=${perms:4:6} if [ "$perms" != "------" ]; then - echo "Configuration files must not be group or world readable! Dying on file $file" - fatal "Configuration files must not be group or world readable! Dying on file $file" + echo "Configuration files must not be group or world writable/readable! Dying on file $file" + fatal "Configuration files must not be group or world writable/readable! Dying on file $file" fi if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then echo "Configuration files must be owned by root! Dying on file $file" diff --git a/handlers/easydialog.sh b/handlers/easydialog.sh index b96a5c1..5d70653 100644 --- a/handlers/easydialog.sh +++ b/handlers/easydialog.sh @@ -1,6 +1,7 @@ #!/bin/bash # copyright 2002 lmoore@tump.com under the terms of the GNU LGPL. +# additions 2005 collective@riseup.net # whiptail has trouble being called in the foo=$(whiptail ...) fashion for # some reason. this is very annoying. this means that we need to use @@ -22,7 +23,7 @@ setApplicationTitle() { } setHelp() { - HELP="$@" + HELP="$@" } setDimension() { @@ -99,6 +100,8 @@ menuBox() { _genericListBox --menu "$@" } +## a menu box with additional help info displayed +## at the bottom of the window when an item is selected menuBoxHelp() { HELP="--item-help" _genericListBox --menu "$@" @@ -107,6 +110,7 @@ menuBoxHelp() { return $status } +## a menu box with an addition button 'help' menuBoxHelpFile() { HELP="--help-button" _genericListBox --menu "$@" @@ -115,7 +119,6 @@ menuBoxHelpFile() { return $status } - checkBox() { _genericListBox --checklist "$@" } @@ -140,8 +143,64 @@ passwordBox() { return $status } + +######################################################### +## begin-item-display style lists +## +## these lists are built by calling fuctions multiple times. +## this can make it easier to build your list in a loop +## + +listBegin() { + _menu_title=$1 + _menu_msg=$2 + _menu_items=0 + _menu_text= + _menu_labels= +} + +listItem() { + _menu_labels[$_menu_items]=$1 + _menu_text[$_menu_items]=$2 + let "_menu_items += 1" +} + + +## +## takes one of: +## menu, checklist, radiolist +## +listDisplay() { + boxtype=$1 + local temp=$(mktemp -t) || exit 1 + trap "rm -f $temp" 0 + + ( + echo -ne " $HELP $_DEFAULT " + echo -ne " --backtitle '$BACKTITLE' " + echo -ne " --title '$_menu_title' " + echo -ne " --$boxtype '$_menu_msg' " + echo -ne " $HEIGHT $WIDTH 10 " + for ((i=0; i < $_menu_items ; i++)); do + label=${_menu_labels[$i]} + text=${_menu_text[$i]} + echo -ne " $label '$text' " + done + ) | xargs $DIALOG 2> $temp + + local status=$? + REPLY="" + [ $status = 0 ] && REPLY=`cat $temp` + rm -f $temp + _DEFAULT= + return $status +} + +#################################################### +## FORM + _form_gap=2 -startForm() { +formBegin() { _form_title=$1 _form_items=0 _form_labels= @@ -154,7 +213,7 @@ formItem() { let "_form_items += 1" } -displayForm() { +formDisplay() { local temp=$(mktemp -t) || exit 1 max_length=0 @@ -167,17 +226,13 @@ displayForm() { done let "max_length += 2" - local form= local xpos=1 ( echo -n -e "--form '$_form_title' 0 0 20" for ((i=0; i < $_form_items ; i++)); do label=${_form_labels[$i]} text=${_form_text[$i]} -# if [ "$text" == "" ]; then -# text='_empty_' -# fi - echo -n -e "$form $label $xpos 1 '$text' $xpos $max_length 30 30" + echo -n -e " $label $xpos 1 '$text' $xpos $max_length 30 30" let "xpos += _form_gap" done ) | xargs $DIALOG 2> $temp diff --git a/handlers/ldap.helper b/handlers/ldap.helper index 44b08cb..7670817 100644 --- a/handlers/ldap.helper +++ b/handlers/ldap.helper @@ -1,3 +1,4 @@ +HELPERS="$HELPERS ldap:ldap_database_backup" ldap_create_file() { while true; do diff --git a/handlers/mysql.helper b/handlers/mysql.helper index 765f228..759ee26 100644 --- a/handlers/mysql.helper +++ b/handlers/mysql.helper @@ -1,3 +1,4 @@ +HELPERS="$HELPERS mysql:mysql_database_backup" do_mysql_user() { inputBox "mysql action wizard" "specify a system user:" diff --git a/handlers/rdiff.helper b/handlers/rdiff.helper index 2efbe11..2ad2a49 100644 --- a/handlers/rdiff.helper +++ b/handlers/rdiff.helper @@ -1,10 +1,13 @@ + +HELPERS="$HELPERS rdiff:incremental_remote_filesystem_backup" + do_rdiff_dest() { - startForm "rdiff action wizard" + formBegin "rdiff action wizard" formItem "keep" "$rdiff_keep" formItem "dest_directory" "$rdiff_directory" formItem "dest_host" "$rdiff_host" formItem "dest_user" "$rdiff_user" - displayForm + formDisplay [ $? = 1 ] && return; set -- $REPLY @@ -18,7 +21,7 @@ do_rdiff_dest() { } do_rdiff_src() { - startForm "rdiff action wizard: includes" + formBegin "rdiff action wizard: includes" formItem include /var/spool/cron/crontabs formItem include /var/backups formItem include /etc @@ -29,7 +32,7 @@ do_rdiff_src() { formItem include formItem include formItem include - displayForm + formDisplay [ $? = 1 ] && return; rdiff_includes= @@ -39,11 +42,11 @@ do_rdiff_src() { done set +o noglob - startForm "rdiff action wizard: excludes" + formBegin "rdiff action wizard: excludes" formItem exclude '/home/*/.gnupg' formItem exclude formItem exclude - displayForm + formDisplay [ $? = 1 ] && return; rdiff_excludes= diff --git a/handlers/sys.helper b/handlers/sys.helper index 22eb01d..c97f53f 100644 --- a/handlers/sys.helper +++ b/handlers/sys.helper @@ -1,3 +1,4 @@ +HELPERS="$HELPERS sys:general_hardware_and_system_info" sys_wizard() { require_packages hwinfo diff --git a/ninjahelper b/ninjahelper index 9568b53..f487183 100755 --- a/ninjahelper +++ b/ninjahelper @@ -3,6 +3,22 @@ #################################################### ## Functions +function check_perms() { + local file=$1 + local perms=`ls -ld $file` + perms=${perms:4:6} + if [[ "$perms" != "------" && "$perms" != "r--r--" ]]; then + echo $perms + echo "helper scripts must not be group or world writable! Dying on file $file" + exit + fi + if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then + echo "helper scripts must be owned by root! Dying on file $file" + exit + fi +} + + ## ## returns the next available file name given a file ## in the form /etc/backup.d/10.sys @@ -37,37 +53,25 @@ require_packages() { done } -doradiobox() { - defaultchoice="red is.pretty on" - choices="green is_nice_too off blue i_love_blue off yellow is.bright off orange make.me.hungry off" - radioBox "radio title" "choose one color" $defaultchoice $choices - case $? in - 0) ;; - 1) echo "color choice cancelled..."; sleep 1;; - 255) echo "something went wrong, exiting..." - exit 1 ;; - esac - result="$REPLY" - msgBox "message title" "you said $result." -} - +## +## menu for the wizards +## donew() { - menuBox "new action menu" "select an action to create" \ - return "return to main menu" \ - sys "general hardware and system info" \ - mysql "mysql database backup" \ - ldap "ldap database backup" \ - rdiff "incremental filesystem backup" - - [ $? = 1 ] && return; - result="$REPLY" - case "$result" in - "sys") sys_wizard;; - "mysql") mysql_wizard;; - "ldap") ldap_wizard;; - "rdiff") rdiff_wizard;; - "return") return;; - esac + listBegin "new action menu" "select an action to create" + listItem return "return to main menu" + for data in $HELPERS; do + data=${data//_/ } + helper_function=${data%%:*} + helper_info=${data##*:} + listItem $helper_function "$helper_info" + done + listDisplay menu + + [ $? = 1 ] && return + result="$REPLY" + [ "$result" = "return" ] && return + result=${result}_wizard + $result } do_rm_action() { @@ -172,7 +176,10 @@ if [ "$UID" != "0" ]; then exit 1 fi +# load all the helpers +HELPERS="" for file in `find $scriptdir -follow -name '*.helper'`; do + check_perms $file . $file done -- 2.30.2