View sourcecode

The following files exists in this folder. Click to view.

dbwebb

1 lines ASCII Unix (LF)
1
#!/bin/sh

#SCRIPT=$(readlink -f "$0")
#SCRIPTPATH=$(dirname "$SCRIPT")
#. "$SCRIPTPATH/.dbwebb.version"
. "bin/.dbwebb.version"

ECHO="printf"


#
# Print usage of script
#
printUsage()
{
    $ECHO "\nUtility dbwebb $VERSION by Mikael Roos, to upload, verify and publish course results when done."
    $ECHO "\n"
    $ECHO "\nUsage: dbwebb [options] <command> [arguments]"
    $ECHO "\n"
    $ECHO "\nOptions:"
    $ECHO "\n"
    $ECHO "\n  -h         Print this message and exit."
    #$ECHO "\n  -i         Ignore validation faults and proceed anyway."
    $ECHO "\n  -v         Print the version of this program and exit."
    $ECHO "\n  -w         Very verbose, print out whats happening."
    $ECHO "\n  -y         Do not wait for my input, just proceed."
    $ECHO "\n"
    $ECHO "\nCommand:"
    $ECHO "\n"
    $ECHO "\n  config    Create, or re-create the config file .dbwebb.config."
    $ECHO "\n  version   Display version information."
    $ECHO "\n  test      Test the connection by loggin in."
    $ECHO "\n  sshkey    Create and install ssh-keys to avoid using password.."
    $ECHO "\n  login     Login to the server using ssh."
    $ECHO "\n  init      Init the remote server and create a destination directory."
    $ECHO "\n  update    Update the courselibb (repo) with latest updates from master repo."
    $ECHO "\n  upload    Upload all files to the server."
    $ECHO "\n  download  Download all personal files in the me-directory from the server."
    $ECHO "\n  create    Create a laboration, use additional argument for naming what lab to create."
    $ECHO "\n  validate  Upload your files and validate that your results passes automatic tests."
    $ECHO "\n            Send in kmom01, kmom02, etc to validate only one kmom."
    $ECHO "\n            Default is to validate all."
    $ECHO "\n  publish   Upload, Validate and Publish your course results to the web."
    $ECHO "\n            Send in kmom01, kmom02, etc to publish only one kmom."
    $ECHO "\n            Default is to publish all."
    $ECHO "\n  inspect   Inspect a kmom for yourself or a specific user, needs teacher privilegies"
    $ECHO "\n            to check another students kmom."
    $ECHO "\n"
    $ECHO "\nExample:"
    $ECHO "\n"
    $ECHO "\n  $ dbwebb config"
    $ECHO "\n  $ dbwebb test"
    $ECHO "\n  $ dbwebb sshkey"
    $ECHO "\n  $ dbwebb login"
    $ECHO "\n  $ dbwebb init"
    $ECHO "\n  $ dbwebb -y upload"
    $ECHO "\n  $ dbwebb download"
    $ECHO "\n  $ dbwebb create lab1"
    $ECHO "\n  $ dbwebb -y validate kmom01"
    $ECHO "\n  $ dbwebb -y publish lab1"
    $ECHO "\n  $ dbwebb inspect kmom01"
    $ECHO "\n  $ dbwebb inspect kmom01 mosstud"
    $ECHO "\n"
    $ECHO "\n"
    exit
}



#
# Login to the server
#
executeCommand()
{
    INTRO=$1

    if [ $SKIP_READLINE = "no" ]
    then
        $ECHO "\n$INTRO"
        $ECHO "\nPress a key to continue..."
        read void
    fi

    COMMAND=$2

    if [ $VERY_VERBOSE = "yes" ]
    then
        $ECHO "\nExecuting command:"
        $ECHO "\n$COMMAND"
        $ECHO "\n-----------------------------------------"
        $ECHO "\n"
    fi

    sh -c "$COMMAND"
    STATUS=$?

    if [ $VERY_VERBOSE = "yes" ]
    then
        $ECHO "\n-----------------------------------------"
    fi

    MESSAGE=$3
    if [ $STATUS = 0 ]
    then
        $ECHO "\n$MSG_OK $MESSAGE"
    else
        $ECHO "\n$MSG_FAILED $MESSAGE"
    fi
    $ECHO "\n"
    $ECHO "\n"

    return $STATUS
}



#
# Display version information
#
version()
{
    $ECHO "Version of bin/dbwebb is $VERSION"
    $ECHO "\n\nLatest update to course repo was:\n"
    git log -1
}



#
# Display version information
#
updateConfigIfNeeded()
{
    WHAT=$1

    if [ -z "$VERSION_CONFIG" -o ! "$VERSION" = "$VERSION_CONFIG" ]
    then
        createConfig "$WHAT" "no-user-input"
        exit
    fi
}




#
# Test the connection to the server
#
testConnection()
{
    INTRO="I will now try to establish a connection with the server by connecting to it."
    COMMAND="$SSH cat /etc/motd"
    MESSAGE="to establish the connection."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"
}



#
# Login to the server
#
loginToServer()
{
    INTRO="I will now try to establish a connection with the server by loggin in to it."
    COMMAND="$SSH"
    MESSAGE="to establish the connection."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"
}



#
# Create and use ssh-keys to login.
#
installSshKeys()
{
    SSH_KEY="$HOME/.ssh/dbwebb"

    if [ ! -d $HOME/.ssh ]
    then
        mkdir $HOME/.ssh
    fi

    INTRO="First we need to create a ssh key and store it locally."
    COMMAND="ssh-keygen -t dsa -f $SSH_KEY -N ''"
    MESSAGE="to create the ssh key."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"

    # Bug (?) om Cygwin & win 8
    #echo $IS_CYGWIN
    if [ $IS_CYGWIN = "yes" ]
    then
        chgrp -vR $CYGWIN_DEFAULT_GROUP $HOME/.ssh
    fi

    chmod 700 $HOME/.ssh
    chmod 600 $SSH_KEY $SSH_KEY.pub

    INTRO="I will now install the ssh-key at the remote server."
    COMMAND="cat $SSH_KEY.pub | ssh $USER@$HOST 'sh -c \"if [ ! -d .ssh ]; then mkdir .ssh; fi; chmod 700 .ssh; touch .ssh/authorized_keys; cat >> .ssh/authorized_keys\"'"
    MESSAGE="to install the ssh-keys."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"
}



#
# Create the config file .dwbebb.config.
#
createConfig()
{
    FIRST=$1
    NO_INPUT=$2

    if [ -z "$COURSE" ]
    then
        GUESS_COURSE=${PWD##*/}
    else
        GUESS_COURSE=$COURSE
    fi

    if [ -z $FIRST ]
    then

        $ECHO "Welcome to dbwebb utility! It seems like its your first time here?"
        $ECHO "\nI'm missing the configuration file \".dbwebb.config\"."
        $ECHO "\nI will now create it."

    elif [ $FIRST = "update" ]
    then

        $ECHO "Your config file will be automatically updated. Then re-run your command.\n"

    elif [ $FIRST = "upgrade" ]
    then

        $ECHO "Your config file will be automatically updated."

    elif [ $FIRST = "create" ]
    then

        $ECHO "I will now re-create the configuration file \".dbwebb.config\"."

    fi


    if [ -z "$NO_INPUT" ]
    then

        $ECHO "\nWhat is your student acronym? [$USER] "    
        read ACRONYM

    fi

    ACRONYM=${ACRONYM:-$USER}

    cat bin/dbwebb-config-sample | sed s/DEFAULT_USER/$ACRONYM/g | sed s/DEFAULT_COURSE/$GUESS_COURSE/g > .dbwebb.config

    $ECHO "\nThe config file \".dbwebb.config\" is now up-to-date.\n"
}



#
# Create default files.
#
createDefaultFiles()
{
    DEFAULT="$TARGET/me/default/"

    INTRO="Initiating me-directory by copying files from me/default to me/ (will not overwrite existing)."
    COMMAND="rsync -a --ignore-existing \"$DEFAULT\" \"$TARGET/me/\""
    MESSAGE="to create course specific files in me-directory."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"
}



#
# Init directory structure at the server.
#
initServer()
{
    INTRO="I will now try to init the remote server and create a directory where all uploaded files will reside."
    COMMAND="$SSH 'sh -c \"if [ ! -d dbwebb-kurser ]; then mkdir dbwebb-kurser; fi; chmod 700 dbwebb-kurser; ls -l dbwebb-kurser\"'"
    MESSAGE="to init the server."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"
}



#
# Upload results to the server
#
updateFromMaster()
{
    INTRO="Update course repo with latest changes from course master repo."
    COMMAND="git pull"
    MESSAGE="to update course repo."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"

    #. "$SCRIPTPATH/.dbwebb.version"
    . "bin/.dbwebb.version"
    updateConfigIfNeeded "upgrade"
}



#
# Set proper rights for files and directories
#
setChmod()
{
    $ECHO "\nEnsuring that all files and directories are readable for all."

    for filename in $(find "$SOURCE" -type d); do
        chmod 755 "$filename"
    done

    chmod -R u+rw "$SOURCE" 
    chmod -R go+r "$SOURCE"
}



#
# Upload results to the server
#
uploadToServer()
{
    INTRO="Your current working directory will now be synced to remote server."
    COMMAND=$UPLOAD
    MESSAGE="to upload data."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"
}



#
# Download from the server
#
downloadFromServer()
{
    INTRO="You will now download the content of youre me-directory FROM the server. Existing local files that are newer will not be overwritten."
    COMMAND="rsync -avu --exclude default -e \"ssh $SSH_KEY_OPTION\" \"$DESTINATION/me/\" \"$SOURCE/me/\""
    MESSAGE="to download the me-directory from the server."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"
}



#
# Create a lab
#
createLab()
{
    LAB="$1"
    INTRO="Create laboration $LAB."
    COMMAND="bin/dbwebb-create \"$LAB\""
    MESSAGE="to create the lab."
    executeCommand "$INTRO" "$COMMAND" "$MESSAGE"
}



#
# Validate the uploaded files
#
validateUploadedResults()
{
    WHAT=$1
    LOG="$HOME/$BASEDIR/.dbwebb-validate.log"
    INTRO="I will now upload files to the remote server and validate them."
    COMMAND="$SSH 'cd $BASEDIR/$PROJECT; bin/dbwebb-validate -n $IGNORE_FAULTS $WHAT' | tee $LOG;"
    MESSAGE="to validate course results. Saved a log of the output, review it as:\nless -R $LOG"
    executeCommand "$INTRO" "$UPLOAD; $COMMAND" "$MESSAGE"
}



#
# Validate and Publish the uploaded files
#
publishResults()
{
    WHAT=$1
    LOG="$HOME/$BASEDIR/.dbwebb-publish.log"
    INTRO="I will now try to init the remote server and create a directory where all uploaded files will reside."
    COMMAND="$SSH 'cd $BASEDIR/$PROJECT; bin/dbwebb-validate $IGNORE_FAULTS $WHAT' | tee $LOG;"
    MESSAGE="to validate and publish all course results. Saved a log of the output, review it as:\nless -R $LOG"
    executeCommand "$INTRO" "$UPLOAD; $COMMAND" "$MESSAGE"

    if [ $? -eq 0 ]
    then
        $ECHO "Your files are now"
    else
        $ECHO "Some of your files might be"
    fi
    $ECHO " published on $BASEURL"
    $ECHO "\n"
}



#
# Inspect uploaded files
#
inspectResults()
{
    WHAT=$1
    WHO=$2
    LOG="$HOME/$BASEDIR/.dbwebb-inspect.log"
    INTRO="I will now inspect the choosen kmom for the choosen user, if you have privilegies to do that."
    COMMAND="$SSH 'cd $BASEDIR/$PROJECT; bin/dbwebb-inspect $WHAT $WHO' | tee $LOG; "
    MESSAGE="to inspect the course results. Saved a log of the output, review it as:\nless -R $LOG"

    if [ "$USER" = "$WHO" ]
    then
        upload="$UPLOAD"
    else
        upload="$UPLOAD_MINIMAL"
    fi

    executeCommand "$INTRO" "$upload; $COMMAND" "$MESSAGE"
}




#
# Main
#
if [ $# -lt 1 ]
then
    printUsage
fi

# Get settings from config-file
if [ ! -f ".dbwebb.config" ]
then
    createConfig
    $ECHO "\nYou can now re-run your command.\n"
    exit
fi

. "./.dbwebb.config"

updateConfigIfNeeded "update"


UPLOAD="rsync -av --delete --exclude literature --exclude .git -e \"ssh $SSH_KEY_OPTION\" \"$SOURCE\" \"$DESTINATION\""
UPLOAD_MINIMAL="rsync -av --delete --exclude me --exclude tutorial --exclude example --exclude literature --exclude .git -e \"ssh $SSH_KEY_OPTION\" \"$SOURCE\" \"$DESTINATION\""

SSH="ssh ${USER}@${HOST} $SSH_KEY_OPTION"



# Get options
SKIP_READLINE="no"
IGNORE_FAULTS="-i"
VERY_VERBOSE="no"

while getopts ":hivwy" opt
do
    case $opt in
        (h)
            printUsage
            ;;

        (i)
            IGNORE_FAULTS="-i"
            ;;

        (v)
            $ECHO "$VERSION\n"
            exit 0
            ;;

        (w)
            VERY_VERBOSE="yes"
            ;;

        (y)
            SKIP_READLINE="yes"
            ;;

        \?)
            $ECHO "\nInvalid option: -$OPTARG" >&2
            $ECHO "\n"
            $ECHO "\nUse the following to get help."
            $ECHO "\ndbwebb -h"
            $ECHO "\n"
            exit 1
            ;;
    esac
done
shift $((OPTIND - 1))


# Execute command
CMD=$1
case $CMD in

    config)
        createConfig "create"
        ;;

    test)
        testConnection
        ;;

    version)
        version
        ;;

    login)
        loginToServer
        ;;

    sshkey)
        installSshKeys
        ;;

    init)
        initServer
        createDefaultFiles
        #uploadToServer
        ;;

    update)
        updateFromMaster
        ;;

    upload)
        setChmod
        uploadToServer
        ;;

    download)
        downloadFromServer
        ;;

    create)
        LAB="$2"
        if [ -z "$LAB" ]
        then
            echo "Missing name for lab, perhaps use lab1, lab2 or lab3?"
            exit 1
        fi
        createLab "$LAB"
        ;;

    validate)
        WHICH=$2
        validateUploadedResults ${WHICH:=all}
        ;;

    publish)
        WHICH=$2
        publishResults ${WHICH:=all}
        ;;

    inspect)
        WHICH=$2
        WHO=$3
        inspectResults ${WHICH:=all} ${WHO:=$USER}
        ;;

    *)
        printUsage
        ;;
esac