Index: scripts/start.sh =================================================================== diff -u -r54c4136d95375116e6daf23b7d4179159cf13d0c -r025abd59dd1582ac0a7ad7af79358929042ea4bc --- scripts/start.sh (.../start.sh) (revision 54c4136d95375116e6daf23b7d4179159cf13d0c) +++ scripts/start.sh (.../start.sh) (revision 025abd59dd1582ac0a7ad7af79358929042ea4bc) @@ -18,234 +18,233 @@ # @details # This file is used to setup newly flashed SoM to copy everythin on the device. -FLG_QUIET=0 -SSH_QUIET="" -EMT_IP="192.168." +source ./globals.sh -ERR_CONNECTION=1 -ERR_DENALI_BIN=2 -ERR_FONTS_EMTY=3 -ERR_FONTS_PATH=4 -ERR_KILLPROMPT=5 - -SRC_PATH_SCRIPTS="scripts" -SRC_PATH_CONFIG="settings" -SRC_PATH_FONTS="fonts" - -DST_IP=$EMT_IP -DST_USER=root -DST_PATH_CONFIG="/home/$DST_USER/.config" -DST_PATH_HOME="/home/$DST_USER" -DST_PATH_SCRIPTS="/home/$DST_USER/scripts" -DST_PATH_FONTS="/usr/share/fonts/truetype" - -SRC_PATH_DENALI="." -DENALI_BIN=denali - - -if [ "$1" = "-h" -o "$1" = "--help" ]; then - echo -e "-h \t This help" - echo -e "-q \t Quiet mode" - echo "usage start.sh [] [-q]" - exit 0 -fi - -if [ "$1" = "-q" -o "$2" = "-q" ]; then - FLG_QUIET=1 - SSH_QUIET="-oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no -oLogLevel=ERROR -oConnectTimeout=5 -oHostKeyAlgorithms=+ssh-rsa" - echo -e "Running in quiet mode." -fi - -if [ -n "$1" -a "$1" != "-q" ]; then - DST_IP="$1" -fi - -function runssh() { - ssh $SSH_QUIET $DST_USER@$DST_IP "$1" +function displayHelp() { + if [ "$1" = "-h" -o "$1" = "--help" ]; then + echo -e "-h \t This help" + echo "usage start.sh [ [xxx] or [xxx.xxx.xxx.xxx] or [] ]" + exit 0 + fi } -function folderExists() { - if [ ! -d "$1" ]; then - echo 1 - return +function exitConfirm() { + read -p "Continue? [y,n]" -n 1 -r CONTINUE + if [ "$CONTINUE" != "y" ]; then + echo "" + exit "$1" + else + echo "" fi - echo 0 } -function fileExists() { - if [ ! -f "$1" ]; then - echo 1 - return +function defaultIP() { + if [ -n "$1" ]; then + if [ ${#1} -gt $IP_SEG_MAX_LEN ]; then + DST_IP=$1 + else + DST_IP=$IP_EMT"$1" + fi fi - echo 0 } -function copyFolderTo() { - echo $1 - scp -r $SSH_QUIET $1/. $DST_USER@$DST_IP:$2 +function setupLogs() { + local log_location=$LOG_LOCATION/$DST_IP + mkdir -p $log_location + LOG_OUT_FILE="$log_location/update-$(timestamp).log" + LOG_ERR_FILE="$log_location/update-$(timestamp).err" + touch $LOG_OUT_FILE + touch $LOG_ERR_FILE +} - if [ ! $? -eq 0 ];then - echo "copy of folder $1 unsuccessful" - return 1 - fi - return 0 +function sshKeyExists() { + existsFile $SRC_FILE_SSHKEY_PUB + return $? } -function copyFileTo() { - if [ $(fileExists "$1") -eq 0 ]; then - scp $SSH_QUIET $1 $DST_USER@$DST_IP:$2 - if [ ! $? -eq 0 ];then - echo "copy of file $1 unsuccessful" - return 1 - fi +function sshKeyGen() { + sshKeyExists + if [ ! $? -eq 0 ]; then + echo "No ssh key found,Generating ssh key" + ssh-keygen -N "" -f $SRC_FILE_SSHKEY 1>>$LOG_OUT_FILE 2>>$LOG_ERR_FILE + return $? else - echo " ~~~ File $1 doesn't exist" + echo "Found ssh key " $SRC_FILE_SSHKEY_PUB fi return 0 } -function exitconfirm() { - read -p "Continue? [y,n]" -n 1 -r CONTINUE - if [ "$CONTINUE" != "y" ]; then - echo "" - exit "$1" - else - echo "" +function sshKeyCopy() { + echo "Registering the ssh key on the device $DST_IP" + echo "Please wait ..." + ssh-copy-id $SSH_PARAM -f -i $SRC_FILE_SSHKEY_PUB $DST_USER@$DST_IP 1>>$LOG_OUT_FILE 2>>$LOG_ERR_FILE + if [ ! $? -eq 0 ]; then + echo "Connection to host $DST_IP failed." | tee -a $LOG_OUT_FILE + exitConfirm $? + return $? fi } -function killPrompt() { - if [ ! $FLG_QUIET -eq 1 ]; then - echo "All the settings, configurations and binaries will be overwritten" - exitconfirm $ERR_KILLPROMPT - fi +function sshRun() { + ssh $SSH_PARAM $DST_USER@$DST_IP $1 + return $? } -function removeIPFromHost() { - ssh-keygen -f "/home/denali/.ssh/known_hosts" -R "$DST_IP" - sleep 5 -} - -# getting the ip address of the device -function getIpAddress() { - while true; do - if [ "$DST_IP" = "$EMT_IP" ]; then - read -p "Please enter the device Ip address: " -e -i "$DST_IP" -r DST_IP - else - if [ ! $FLG_QUIET -eq 1 ]; then - read -p "Please enter the device Ip address: " -e -i "$DST_IP" -r DST_IP +function copyFolderTo() { + existsFolder "$1" + if [ $? -eq 0 ]; then + echo -n "Copy folder" >> $LOG_OUT_FILE + scp -r $SSH_PARAM $1/* $DST_USER@$DST_IP:$2 + if [ $? -eq 0 ]; then + echo " Successfull - $1" >> $LOG_OUT_FILE + else + echo " Failed" >> $LOG_OUT_FILE + exitConfirm $? + return $? fi + else + echo "File '$1' doesn't exist" | tee -a $LOG_OUT_FILE + exitConfirm $? fi - if [ ! -z "$DST_IP" ]; then - echo "removing device Ip Address from known hosts" - removeIPFromHost - echo "Testing connection on IP $DST_IP" + return 0 +} - runssh "exit 0" +function copyFileTo() { + existsFile "$1" + if [ $? -eq 0 ]; then + echo -n "Copy file" >> $LOG_OUT_FILE + scp $SSH_PARAM $1 $DST_USER@$DST_IP:$2 if [ $? -eq 0 ]; then - break + echo " Successfull - $1" >> $LOG_OUT_FILE else - echo " ~~~ Cannot connect to the device." - echo " ~~~ Either device not connected or the IP address is not correct." - exitconfirm $ERR_CONNECTION + echo " Failed" >> $LOG_OUT_FILE + exitConfirm $? + return $? fi + else + echo "File '$1' doesn't exist" | tee -a $LOG_OUT_FILE + exitConfirm $? fi - done - echo "Connection successful on device with IP $DST_IP" - echo "" + return 0 } -# getting the denali application path -function getDenaliPath() { - while true; do - if [ ! $FLG_QUIET -eq 1 ]; then - read -p "Please enter the Denali application path: " -e -i $SRC_PATH_DENALI -r SRC_PATH_DENALI - fi - echo "Testing the Denali application path" - if [ $(fileExists "$SRC_PATH_DENALI/$DENALI_BIN") -eq 0 ]; then - break - else - echo " ~~~ Cannot find the Denali application." - echo " ~~~ Either the path is not correct or the Denali application doesn't exist." - exitconfirm $ERR_DENALI_BIN - fi - done - echo "The Denali application found successfully in path '$SRC_PATH_DENALI'" - echo "" +function killPrompt() { + echo_star_comment + echo_star_message "Current running UI Software on the device will be stopped." + echo_star_message "All the settings, configurations and binaries will be overwritten" + echo_star_comment + exitConfirm $ERR_KILLPROMPT } -# getting the fonts path -function getFontsPath() { +function getDeviceIP() { while true; do - if [ ! $FLG_QUIET -eq 1 ]; then - read -p "Please enter the fonts path: " -e -i $SRC_PATH_FONTS -r SRC_PATH_FONTS - fi - echo "Testing fonts path" - if [ "$(folderExists "$SRC_PATH_FONTS")" -eq 0 ]; then - if [ "$(ls)" != "" ]; then + validIP "$DST_IP" + if [ $? -eq 0 ]; then + break + else + read -p "Please enter the device Ip address: " -e -i "$IP_EMT" -r DST_IP + validIP "$DST_IP" + if [ $? -eq 0 ]; then break else - echo " ~~~ The folder $SRC_PATH_FONTS is empty" - exitconfirm $ERR_FONTS_EMTY + echo "The entered IP address is not valid [$DST_IP]" + exitConfirm $? fi - else - echo " ~~~ Cannot find Fonts path." - exitconfirm $ERR_FONTS_PATH fi done - echo "Fonts found successfully in path '$SRC_PATH_FONTS'" - echo "" } -# SSH Connection -function connect() { - echo "******************************************************" - echo "please ssh into device $DST_IP and run ./setup.sh" - echo "******************************************************" - read -p "Hit enter to continue" - runssh # the setup.sh has to run on the device while user has logged into the device. +function setupBootupScripts() { + echo_dash_comment + echo_dash_message "Installing bootup scripts" | tee -a $LOG_OUT_FILE + echo_dash_comment + copyFileTo "globals.sh" $DST_PATH_HOME + copyFileTo "autostart" $DST_PATH_HOME + copyFileTo "run.sh" $DST_PATH_HOME + copyFileTo "setup.sh" $DST_PATH_HOME } -# copy the settings scripts function setupSettingsScripts() { - runssh "mkdir -p $DST_PATH_SCRIPTS ;" - runssh "rm -frd $DST_PATH_SCRIPTS/*;" + echo_dash_comment + echo_dash_message "Installing settings scripts" | tee -a $LOG_OUT_FILE + echo_dash_comment + sshRun "rm -frd $DST_PATH_SCRIPTS;" + sshRun "mkdir -p $DST_PATH_SCRIPTS;" copyFolderTo $SRC_PATH_SCRIPTS $DST_PATH_SCRIPTS - runssh "cd $1; chmod a+x *.sh;" + sshRun "cd $1; chmod a+x *.sh;" } -# copy the instructions -function setupInstructions() { - runssh "mkdir -p $DST_PATH_CONFIG ;" - runssh "rm -frd $DST_PATH_CONFIG/*;" +function setupConfigurations() { + echo_dash_comment + echo_dash_message "Installing configurations" | tee -a $LOG_OUT_FILE + echo_dash_comment + sshRun "rm -frd $DST_PATH_CONFIG;" + sshRun "mkdir -p $DST_PATH_CONFIG;" copyFolderTo $SRC_PATH_CONFIG $DST_PATH_CONFIG } -# copy the fonts +function setupCloudSync() { + echo_dash_comment + echo_dash_message "Installing CloudSync" | tee -a $LOG_OUT_FILE + echo_dash_comment + sshRun "killall python3" + sshRun "rm -frd $DST_PATH_CLOUDSYNC;" + sshRun "mkdir -p $DST_PATH_CLOUDSYNC;" + copyFolderTo $SRC_PATH_CLOUDSYNC $DST_PATH_CLOUDSYNC +} + function setupFonts() { - getFontsPath + echo_dash_comment + echo_dash_message "Installing fonts" | tee -a $LOG_OUT_FILE + echo_dash_comment copyFolderTo $SRC_PATH_FONTS $DST_PATH_FONTS } -function main() { - getIpAddress +function setupApplication() { + echo_dash_comment + echo_dash_message "Installing UI Software" | tee -a $LOG_OUT_FILE + echo_dash_comment + sshRun "killall $DENALI_BIN" + copyFileTo $DENALI_BIN $DST_PATH_HOME +} - killPrompt +function connect() { + echo_dash_comment + echo_dash_message "please ssh into device $DST_IP and run ./setup.sh " + echo_dash_comment + read -p "Hit enter to continue" + sshRun # the setup.sh has to run on the device while user has logged into the device. +} - copyFileTo "autostart" $DST_PATH_HOME - copyFileTo "run.sh" $DST_PATH_HOME - copyFileTo "setup.sh" $DST_PATH_HOME +function wipe_device() { + sshRun "rm -frd *" + sshRun "rm -frd .*" +} +function main() { + displayHelp "$1" + + defaultIP "$1" + getDeviceIP + setupLogs + + sshKeyGen + sshKeyCopy + + killPrompt + wipe_device + + setupBootupScripts setupSettingsScripts - setupInstructions + setupConfigurations + setupCloudSync setupFonts - getDenaliPath - runssh "killall $DENALI_BIN" - copyFileTo "$SRC_PATH_DENALI/$DENALI_BIN" $DST_PATH_HOME + setupApplication } # running the main function -main +main "$1" connect +exit 0