#!/bin/bash ########################################################################### # # Copyright (c) 2021-2023 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file start.sh # # @author (last) Behrouz NematiPour # @date (last) 15-Dec-2022 # @author (original) Behrouz NematiPour # @date (original) 11-Aug-2021 # ############################################################################ # @details # This file is used to setup newly flashed SoM to copy everythin on the device. source ./globals.sh DEMO_SETUP=0 DEMO_SWITCH="-D" MANUFACTURING_SETUP=0 MANUFACTURING_SWITCH="-E" #Update only setup SERVER_IP="" # $CloudSync_DRT_SERVER_IP SERVER_WS="" SERVER_PORT="3000" SERVER_SWITCH_DRT_WS="-W" #DRT server workspace/environment SERVER_SWITCH_DRT_IP="-S" #DRT server IP address SERVER_CONFIG_LINE=".*\"url_mft\".*" SERVER_CONFIG_BGN=" \"url_mft\": \"http:\/\/" SERVER_CONFIG_END=":$SERVER_PORT\"," SERVER_CONFIG_FILE="./cloudsync/cloudsync/config/config" SERVER_CONFIG_EXT="json" SERVER_CONFIG_JSON="$SERVER_CONFIG_FILE.$SERVER_CONFIG_EXT" SERVER_CONFIG_JSON_WS="$SERVER_CONFIG_FILE" SERVER_DRT_WS_PRODUCT="production" SERVER_DRT_WS_STAGING="staging" SERVER_DRT_WS_QUALITY="quality" GLOBALS_CONFIG_MDFY="CloudSync_DRT_SERVER_IP=" GLOBALS_CONFIG_LINE="CloudSync_DRT_SERVER_IP=.*" GLOBALS_FILE="./globals.sh" # command line parameters detection function setupParameters() { DST_IP="$1" for arg in "$@"; do case $arg in "$DEMO_SWITCH") DEMO_SETUP=1 ;; "$MANUFACTURING_SWITCH") MANUFACTURING_SETUP=1 ;; "$SERVER_SWITCH_DRT_IP"*) SERVER_IP="${arg:2}" ;; "$SERVER_SWITCH_DRT_WS"*) SERVER_WS="${arg:2}" ;; esac done } function setupPreparation() { getDeviceIP if (( $DEMO_SETUP )); then echo "Device operation Mode : Demo Mode" else echo "Device operation Mode : Normal Operation" fi echo "Device IP Address : $DST_IP" # if it is the update only we don't need the configuration if (( $MANUFACTURING_SETUP )); then echo "Device setup Mode : Manufacturing Setup" else echo "Device setup Mode : Update Only" return fi getServerIP echo "DRT Server IP Address : $SERVER_IP" getServerWS echo "DRT Server Environment: $SERVER_WS" # create the conf file local config_env_json="$SERVER_CONFIG_FILE" local config_json="$SERVER_CONFIG_FILE" # creat the config.json file cp "$SERVER_CONFIG_JSON_WS""_""$SERVER_WS"".""$SERVER_CONFIG_EXT" $SERVER_CONFIG_JSON if [[ ! -e $SERVER_CONFIG_JSON ]]; then echo "The file $SERVER_CONFIG_JSON cannot be created" exit 0 fi # setting up the DRT Server IP address #If the server IP is not given or is invalid will be asked to enter and the defualt Diality prefered will be suggested. #If the server IP is valid and accepted then change the CloudSync conf file sed -i "s/$SERVER_CONFIG_LINE/$SERVER_CONFIG_BGN$SERVER_IP$SERVER_CONFIG_END/" $SERVER_CONFIG_JSON #and modify the globals.sh sed -i "s/$GLOBALS_CONFIG_LINE/$GLOBALS_CONFIG_MDFY$SERVER_IP/" $GLOBALS_FILE } function displayHelp() { if [ "$1" = "-h" -o "$1" = "--help" ]; then echo -e "-h \t This help" echo "usage start.sh [ [xxx.xxx.xxx.xxx] ] -D [-S[xxx.xxx.xxx.xxx] -W]" echo "device IP : The Device IP Address Under Setup" echo " Must always be the first parameter" echo "-D : Demo mode" echo " Demo mode mode can be used to use the device as a demo unit." echo " In this mode Application will not work with the actual hardware and will work in simulation mode." echo " The Demo application folder need to be copied to the update folder manually." echo "-U : Update mode" echo " Update mode can only be used when the device is already registered," echo " Therefore the -S and -W will be ignored." echo "-S : The DRT Server IP address" echo " Need to be entered fully in a valid IP format [xxx.xxx.xxx.xxx]" echo " There should be no space between -S and the server IP address" echo " For example : -S192.168.1.5" echo "-W : The DRT Server Workspace/Environment:" echo " Shall only include the server environment from the following list:" echo " - production" echo " - staging" echo " - quality" echo " For example : -Wproduction" exit 0 fi } function exitConfirm() { read -p "Continue? [y,n]" -n 1 -r CONTINUE if [ "$CONTINUE" != "y" ]; then echo "" exit "$1" else echo "" fi } 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 } function sshKeyExists() { existsFile $SRC_FILE_SSHKEY_PUB return $? } 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 "Found ssh key " $SRC_FILE_SSHKEY_PUB fi return 0 } 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 sshRun() { ssh $SSH_PARAM $DST_USER@$DST_IP $1 return $? } 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 return 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 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 return 0 } 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 } function manufacturingModePrompt() { if (( $MANUFACTURING_SETUP )); then continue; else return; fi echo_star_comment echo_star_message "Do you want to run in the Manufacturing Mode?" echo_star_comment read -p "Continue? [y,n]" -n 1 -r CONTINUE echo "" # to echo prompts on new line if [ "$CONTINUE" == "y" ]; then sshRun "echo $SETUP_ENABLE_MANUFACTURING_MODE > $SETUP_CONF_FILE" echo_star_message "Set the setup in manufacturing mode" else sshRun "echo '' > $SETUP_CONF_FILE" echo_star_message "Continuing the setup in normal mode" fi } function getDeviceIP() { while true; do validIP "$DST_IP" if [ $? -eq 0 ]; then break else read -p "Please enter the device Ip address: " -e -i "" -r DST_IP validIP "$DST_IP" if [ $? -eq 0 ]; then break else echo "The entered IP address is not valid [$DST_IP]" exitConfirm $? fi fi done } function getServerIP() { while true; do validIP "$SERVER_IP" if [ $? -eq 0 ]; then break else read -p "Please enter the server Ip address: " -e -i "" -r SERVER_IP validIP "$SERVER_IP" if [ $? -eq 0 ]; then break else echo "The entered IP address is not valid [$SERVER_IP]" exitConfirm $? fi fi done } function validWS() { if [[ -z $SERVER_WS ]]; then return 1; fi case $SERVER_WS in "$SERVER_DRT_WS_PRODUCT");; "$SERVER_DRT_WS_STAGING");; "$SERVER_DRT_WS_QUALITY");; *) echo "The entered server environment is not valid [$SERVER_WS]" return 1 esac return 0 } function getServerWS() { while true; do validWS "$SERVER_WS" if [ $? -eq 0 ]; then break else read -p "Please enter the server Ip address: " -e -i "" -r SERVER_WS validWS "$SERVER_WS" if [ $? -eq 0 ]; then break else echo "The entered server environment is not valid [$SERVER_WS]" exitConfirm $? fi fi done } 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 } function setupSettingsScripts() { 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 sshRun "cd $1; chmod a+x *.sh;" } 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 } 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() { echo_dash_comment echo_dash_message "Installing fonts" | tee -a $LOG_OUT_FILE echo_dash_comment copyFolderTo $SRC_PATH_FONTS $DST_PATH_FONTS } 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 } function setupDemoMode() { if [ "$DEMO_SETUP" != "1" ]; then return; fi echo_dash_comment echo_dash_message "Installing Dry-Demo" | tee -a $LOG_OUT_FILE echo_dash_comment sshRun "killall python3" sshRun "rm -frd $DST_PATH_DRYDEMO;" sshRun "mkdir -p $DST_PATH_DRYDEMO;" copyFolderTo $SRC_PATH_DRYDEMO $DST_PATH_DRYDEMO } 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. #TODO needs to handle the SD-Card format question # sshRun "./setup.sh" # the setup.sh has to run on the device while user has logged into the device. } function wipe_device() { WIPEOUT="wiped.out" sshRun "find -maxdepth 1 ! \( -name '.ssh' -o -name $SETUP_CONF_FILE -o -name . -o -name .. \) > $WIPEOUT" sshRun "xargs -a $WIPEOUT rm -frd" } function main() { setupLogs sshKeyGen sshKeyCopy killPrompt manufacturingModePrompt wipe_device setupBootupScripts setupSettingsScripts setupConfigurations setupCloudSync setupFonts setupApplication setupDemoMode } # running the main function displayHelp "$1" setupParameters "$@" setupPreparation main "$1" connect exit 0