From 5788bc4f7b612c24c6e58dd59f8e2331dbd5fc15 Mon Sep 17 00:00:00 2001 From: cimryan Date: Fri, 19 Oct 2018 14:26:10 -0700 Subject: [PATCH 1/5] Verify that the archive is mountable before making changes to the file systems. --- windows_archive/setup-teslausb | 66 ++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 5910eab..ef965ce 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -56,6 +56,38 @@ function check_archive_server_reachable () { echo "The archive server is reachable." } +function write_archive_credentials_to () { + local file_path="$1" + + echo "username=$shareuser" > "$file_path" + echo "password=$sharepassword" >> "$file_path" + +} + +function check_archive_mountable () { + local archive_server_ip_address="$1" + + local test_mount_location="/tmp/archivetestmount" + + if [ ! -e "$test_mount_location" ] + then + mkdir "$test_mount_location" + fi + + local tmp_credentials_file_path="/tmp/teslaCamArchiveCredentials" + + write_archive_credentials_to "$tmp_credentials_file_path" + + local mount_failed=false + mount -t cifs "//$archive_server_ip_address/$sharename" "$test_mount_location" -o "vers=${cifs_version},credentials=${tmp_credentials_file_path},iocharset=utf8,file_mode=0777,dir_mode=0777" || mount_failed=true + + if [ "$mount_failed" = true ] + then + echo "STOP: The archive couldn't be mounted with CIFS version ${cifs_version}. Try specifying a lower number for the CIFS version like this: export cifs_version=2" + exit 1 + fi +} + function check_available_space () { echo "Verifying that there is sufficient space available on the MicroSD card..." @@ -70,6 +102,7 @@ function check_available_space () { echo "There is sufficient space available." } + function get_ancillary_setup_scripts () { pushd /tmp wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/create-backingfiles-partition.sh @@ -104,13 +137,23 @@ function create_usb_drive_backing_files () { } function configure_archive () { - echo "Configuring the archive..." - mkdir /mnt/archive - local archive_server_ip_address="$(ping -c 1 -w 1 $archiveserver 2>/dev/null | head -n 1 | grep -o -e "(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\})" | tr -d '()')" - echo "//$archive_server_ip_address/$sharename /mnt/archive cifs vers=3,credentials=/root/.teslaCamArchiveCredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab + local archive_server_ip_address="$1" - echo "username=$shareuser" > /root/.teslaCamArchiveCredentials - echo "password=$sharepassword" >> /root/.teslaCamArchiveCredentials + echo "Configuring the archive..." + + local archive_path="/mnt/archive" + + if [ ! -e "$archive_path" ] + then + mkdir "$archive_path" + fi + + local credentials_file_path="/root/.teslaCamArchiveCredentials" + + write_archive_credentials_to "$credentials_file_path" + + echo "//$archive_server_ip_address/$sharename $archive_path cifs vers=${cifs_version},credentials=${credentials_file_path},iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab + echo "Configured the archive." } @@ -190,6 +233,11 @@ function make_root_fs_readonly () { echo "Verifying environment variables..." +if [ ! -n "${cifs_version+x}" ] +then + cifs_version=3 +fi + check_variable "archiveserver" check_variable "sharename" check_variable "shareuser" @@ -200,6 +248,10 @@ check_pushover_enabled check_archive_server_reachable +ARCHIVE_SERVER_IP_ADDRESS="$(ping -c 1 -w 1 $archiveserver 2>/dev/null | head -n 1 | grep -o -e "(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\})" | tr -d '()')" + +check_archive_mountable "$ARCHIVE_SERVER_IP_ADDRESS" + check_available_space get_ancillary_setup_scripts @@ -216,7 +268,7 @@ echo "" >> /etc/fstab create_usb_drive_backing_files -configure_archive +configure_archive "$ARCHIVE_SERVER_IP_ADDRESS" configure_rc_local From a8e3671a948b7a49e220b11089d7720baab6c1b5 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 06:38:32 -0700 Subject: [PATCH 2/5] Set the branch and repo only if not already set. --- windows_archive/setup-teslausb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index ef965ce..9716c28 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -1,9 +1,17 @@ #!/bin/bash -eu -REPO=cimryan -BRANCH=master user_enabled_pushover=false +if [[ -z $REPO ]] +then + REPO=cimryan +fi + +f [[ -z $BRANCH ]] +then + BRANCH=master +fi + if ! [ $(id -u) = 0 ] then echo "STOP: Run sudo -i." From 9898fe95eec125ca335cc0c7801d635363a37237 Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 06:52:22 -0700 Subject: [PATCH 3/5] Simplify tests for empty variables. Use caps for globals. --- windows_archive/setup-teslausb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 9716c28..8be1acc 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -1,6 +1,6 @@ #!/bin/bash -eu -user_enabled_pushover=false +USER_ENABLED_PUSHOVER=false if [[ -z $REPO ]] then @@ -28,9 +28,9 @@ function check_variable () { } function check_pushover_enabled () { - if [ ! -z "${pushover_enabled+x}" ] + if [ "$pushover_enabled" = "true" ] then - if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] + if [[ -z $pushover_user_key ]] || [[ -z $pushover_app_key ]] then echo "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." echo "Define the variables like this:" @@ -42,7 +42,7 @@ function check_pushover_enabled () { echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values." exit 1 else - user_enabled_pushover=true + USER_ENABLED_PUSHOVER=true echo "export pushover_enabled=true" > /root/.teslaCamPushoverCredentials echo "export pushover_user_key=$pushover_user_key" >> /root/.teslaCamPushoverCredentials echo "export pushover_app_key=$pushover_app_key" >> /root/.teslaCamPushoverCredentials @@ -110,7 +110,6 @@ function check_available_space () { echo "There is sufficient space available." } - function get_ancillary_setup_scripts () { pushd /tmp wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/create-backingfiles-partition.sh @@ -189,9 +188,8 @@ function configure_archive_scripts () { echo "Downloaded script to remount filesystems read/write if needed (/root/remountfs_rw)." } - function configure_pushover_scripts() { -if [ ${user_enabled_pushover} = "true" ] +if [ ${USER_ENABLED_PUSHOVER} = "true" ] then pushd /root/bin wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/send-pushover From b6abdfb7262d68c3e0e3d60c3fc072289d699b2c Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 07:27:58 -0700 Subject: [PATCH 4/5] Simplified tests aren't compatible with bash -u. --- windows_archive/setup-teslausb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 8be1acc..52dd5cc 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -2,15 +2,9 @@ USER_ENABLED_PUSHOVER=false -if [[ -z $REPO ]] -then - REPO=cimryan -fi +REPO=${REPO:-cimryan} -f [[ -z $BRANCH ]] -then - BRANCH=master -fi +BRANCH=${BRANCH:-master} if ! [ $(id -u) = 0 ] then @@ -28,9 +22,9 @@ function check_variable () { } function check_pushover_enabled () { - if [ "$pushover_enabled" = "true" ] + if [ ! -z "${pushover_enabled+x}" ] then - if [[ -z $pushover_user_key ]] || [[ -z $pushover_app_key ]] + if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] then echo "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." echo "Define the variables like this:" From 85372bcb063bd378b93d912cd3dfd001881d6e6c Mon Sep 17 00:00:00 2001 From: cimryan Date: Tue, 23 Oct 2018 07:48:01 -0700 Subject: [PATCH 5/5] Umount the temp mount. --- windows_archive/setup-teslausb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb index 52dd5cc..c50b31b 100644 --- a/windows_archive/setup-teslausb +++ b/windows_archive/setup-teslausb @@ -88,6 +88,8 @@ function check_archive_mountable () { echo "STOP: The archive couldn't be mounted with CIFS version ${cifs_version}. Try specifying a lower number for the CIFS version like this: export cifs_version=2" exit 1 fi + + umount "$test_mount_location" } function check_available_space () {