#!/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" SERVER_IP=$CloudSync_DRT_SERVER_IP SERVER_PORT="3000" SERVER_SWITCH="-S" SERVER_CONFIG_LINE=".*\"url_mft\".*" SERVER_CONFIG_BGN=" \"url_mft\": \"http:\/\/" SERVER_CONFIG_END=":$SERVER_PORT\"," SERVER_CONFIG_FILE="./cloudsync/cloudsync/config/config.json" GLOBALS_CONFIG_MDFY="CloudSync_DRT_SERVER_IP=" GLOBALS_CONFIG_LINE="CloudSync_DRT_SERVER_IP=.*" GLOBALS_FILE="./globals.sh" # command line parameters detection function setupParameters() { for arg in "$@"; do case $arg in "$DEMO_SWITCH") DEMO_SETUP=1 echo "Setup started for demo mode" ;; "$SERVER_SWITCH"*) SERVER_IP="${arg:2}" getServerIP if [[ $SERVER_IP == $CloudSync_DRT_SERVER_IP ]]; then # same as default and no need to update. return fi #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_FILE #and modify the globals.sh sed -i "s/$GLOBALS_CONFIG_LINE/$GLOBALS_CONFIG_MDFY$SERVER_IP/" $GLOBALS_FILE echo "DRT Server IP: $SERVER_IP" ;; esac done } 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]]" echo "device IP : M ust always be the first parameter" echo "-D : Indicating the system is going to be setup in Demo mode." echo " The Demo application folder need to be copied to the update folder." echo "-S : Shall include the server IP in complete 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" 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 defaultIP() { if [ -n "$1" ]; then if [ ${#1} -gt $IP_SEG_MAX_LEN ]; then DST_IP=$1 else DST_IP=$IP_EMT"$1" fi 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() { # I set to always enabled for now to always go to the manufacturing mode # 1 - Even for normal setup start is moving files to /home/root, so the lockdown needs to run to move files. # 2 - The UI still needs to be executed to decrypt the /var/configurations, otherwise the configurations can not be updated, # and I don't have the ability to just decrypt and exit right now. # Note: after the Cybersecurity release I will improve the user experience and will make it easier for manufacturing. # CONTINUE="y" 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 "$IP_EMT" -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 "$CloudSync_DRT_SERVER_IP" -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 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() { displayHelp "$1" defaultIP "$1" getDeviceIP setupLogs sshKeyGen sshKeyCopy killPrompt manufacturingModePrompt wipe_device setupBootupScripts setupSettingsScripts setupConfigurations setupCloudSync setupFonts setupApplication setupDemoMode } # running the main function setupParameters "$@" main "$1" connect exit 0