From b93550e4dea1a01061b0faff63c6e54e0568fb9e Mon Sep 17 00:00:00 2001 From: Ray Tanaka Date: Wed, 31 Oct 2018 01:42:25 -0700 Subject: [PATCH] splitting up usb configuration from script and archive configuration / installation. this will allow a user to run configuration multiple times. --- setup/pi/configure.sh | 216 ++++++++++++++++++++++++++++++++++++++++ setup/pi/setup-teslausb | 135 +------------------------ 2 files changed, 217 insertions(+), 134 deletions(-) create mode 100644 setup/pi/configure.sh diff --git a/setup/pi/configure.sh b/setup/pi/configure.sh new file mode 100644 index 0000000..e9336aa --- /dev/null +++ b/setup/pi/configure.sh @@ -0,0 +1,216 @@ +#!/bin/bash -eu + +REPO=${REPO:-cimryan} +BRANCH=${BRANCH:-master} +INSTALL_DIR=${INSTALL_DIR:-/root/bin} + +upgrade=false #TODO: create an option to just refresh the scripts +debug_on=false + +function debug() { + $debug_on && echo "$1" + return 0 +} + +function check_variable () { + local var_name="$1" + if [ -z "${!var_name+x}" ] + then + echo "STOP: Define the variable $var_name like this: export $var_name=value" + exit 1 + fi +} + +function get_script () { + local local_path="$1" + local name="$2" + local remote_path="${3:-}" + + echo "Starting download for $local_path/$name" + curl -o "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name" + chmod +x "$local_path/$name" + echo "Done" +} + +function parse_command_line() { + echo -n "Parsing command line: " + + local OPTIND=1 # Reset in case getopts has been used previously in the shell. + while getopts "du" opt; do + case "$opt" in + d) debug_on=true + ;; + u) upgrade=true + #TODO: just upgrade all the scripts instead building new configs + echo "STOP: this option is not yet implemented" + exit 1 + ;; + esac + done + shift $((OPTIND-1)) + [ "${1:-}" = "--" ] && shift + + echo "done" +} + +function install_rc_local () { + local install_home="$1" + + if grep -q archiveloop /etc/rc.local + then + echo "Skipping rc.local installation" + return + fi + + echo "Configuring /etc/rc.local to run the archive scripts at startup..." + echo "#!/bin/bash -eu" > ~/rc.local + echo "archiveserver=\"${archiveserver}\"" >> ~/rc.local + echo "install_home=\"${install_home}\"" >> ~/rc.local + cat << 'EOF' >> ~/rc.local +LOGFILE=/tmp/rc.local.log + +function log () { + echo "$( date )" >> "$LOGFILE" + echo "$1" >> "$LOGFILE" +} + +log "Launching archival script..." +"$install_home"/archiveloop "$archiveserver" & +log "All done" +exit 0 +EOF + + cat ~/rc.local > /etc/rc.local + rm ~/rc.local + echo "Installed rc.local." +} + +function check_archive_configs () { + echo -n "Checking archive configs: " + + RSYNC_ENABLE="${RSYNC_ENABLE:-false}" + RCLONE_ENABLE="${RCLONE_ENABLE:-false}" + if [ "$RSYNC_ENABLE" = true ] && [ "$RCLONE_ENABLE" = true ] + then + echo "STOP: Can't enable rsync and rclone at the same time" + exit 1 + fi + + if [ "$RSYNC_ENABLE" = true ] + then + check_variable "RSYNC_USER" + check_variable "RSYNC_SERVER" + check_variable "RSYNC_PATH" + export archiveserver="$RSYNC_SERVER" + + elif [ "$RCLONE_ENABLE" = true ] + then + check_variable "RCLONE_DRIVE" + check_variable "RCLONE_PATH" + export archiveserver="8.8.8.8" # since it's a cloud hosted drive we'll just set this to google dns + else + # default to cifs + check_variable "sharename" + check_variable "shareuser" + check_variable "sharepassword" + check_variable "archiveserver" + fi + + echo "done" +} + +function get_archive_module () { + + if [ "$RSYNC_ENABLE" = true ] + then + archive_module="run/rsync_archive" + elif [ "$RCLONE_ENABLE" = true ] + then + archive_module="run/rclone_archive" + else + archive_module="run/cifs_archive" + fi + + echo $archive_module +} + +function install_archive_scripts () { + local install_path="$1" + local archive_module="$2" + + echo "Installing base archive scripts into $install_path" + get_script $install_path archiveloop run + get_script $install_path remountfs_rw run + get_script $install_path lookup-ip-address.sh run + + echo "Installing archive module scripts ($archive_module)" + + get_script $install_path verify-archive-configuration.sh $archive_module + get_script $install_path configure-archive.sh $archive_module + get_script $install_path archive-clips.sh $archive_module + get_script $install_path connect-archive.sh $archive_module + get_script $install_path disconnect-archive.sh $archive_module +} + +function check_and_configure_pushover () { + if [ ! -z "${pushover_enabled+x}" ] + then + 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:" + echo "export pushover_user_key=put_your_userkey_here" + echo "export pushover_app_key=put_your_appkey_here" + exit 1 + elif [ "${pushover_user_key}" = "put_your_userkey_here" ] || [ "${pushover_app_key}" = "put_your_appkey_here" ] + then + echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values." + exit 1 + else + echo "Enabling pushover" + 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 + fi + else + echo "Pushover not configured." + fi +} + +function install_pushover_scripts() { + local install_path="$1" + get_script $install_path send-pushover run +} + +if ! [ $(id -u) = 0 ] +then + echo "STOP: Run sudo -i." + exit 1 +fi + +if [ ! -e "$INSTALL_DIR" ] +then + mkdir "$INSTALL_DIR" +fi + +parse_command_line "$@" +debug "Getting files from $REPO:$BRANCH" +debug "- debug mode = $debug_on" +debug "- upgrade only = $upgrade" + +check_and_configure_pushover +install_pushover_scripts "$INSTALL_DIR" + +check_archive_configs + +archive_module="$( get_archive_module )" +debug "Archive module: $archive_module" + +install_archive_scripts $INSTALL_DIR $archive_module +"$INSTALL_DIR"/verify-archive-configuration.sh +"$INSTALL_DIR"/configure-archive.sh + +install_rc_local "$INSTALL_DIR" + + + diff --git a/setup/pi/setup-teslausb b/setup/pi/setup-teslausb index 0b52fe1..00bad1d 100644 --- a/setup/pi/setup-teslausb +++ b/setup/pi/setup-teslausb @@ -1,6 +1,5 @@ #!/bin/bash -eu -USER_ENABLED_PUSHOVER=${USER_ENABLED_PUSHOVER:-false} SETUP_LOGFILE=/boot/teslausb-headless-setup.log REPO=${REPO:-cimryan} BRANCH=${BRANCH:-master} @@ -8,7 +7,6 @@ HEADLESS_SETUP=${HEADLESS_SETUP:-false} USE_LED_FOR_SETUP_PROGRESS=true - if ! [ $(id -u) = 0 ] then setup_progress "STOP: Run sudo -i." @@ -90,66 +88,6 @@ function check_variable () { fi } -function check_pushover_enabled () { - if [ ! -z "${pushover_enabled+x}" ] - then - if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] - then - setup_progress "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." - setup_progress "Define the variables like this:" - setup_progress "export pushover_user_key=put_your_userkey_here" - setup_progress "export pushover_app_key=put_your_appkey_here" - exit 1 - elif [ "${pushover_user_key}" = "put_your_userkey_here" ] || [ "${pushover_app_key}" = "put_your_appkey_here" ] - then - setup_progress "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 - 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 - fi - fi -} - -function check_archive_configs () { - - RSYNC_ENABLE="${RSYNC_ENABLE:-false}" - RCLONE_ENABLE="${RCLONE_ENABLE:-false}" - - if [ "$RSYNC_ENABLE" = true ] && [ "$RCLONE_ENABLE" = true ] - then - setup_progress "STOP: Cannot enable rsync and rclone at the same time" - exit 1 - fi - - if [ "$RSYNC_ENABLE" = true ] - then - check_variable "RSYNC_USER" - check_variable "RSYNC_SERVER" - export archiveserver="$RSYNC_SERVER" - check_variable "RSYNC_PATH" - export archive_module="run/rsync_archive" - elif [ "$RCLONE_ENABLE" = true ] - then - check_variable "RCLONE_DRIVE" - check_variable "RCLONE_PATH" - # since it's a cloud hosted drive we'll just set this to google dns - export archiveserver="8.8.8.8" - export archive_module="run/rclone_archive" - else # Else for now, TODO allow both for more redundancy? - check_variable "sharename" - check_variable "shareuser" - check_variable "sharepassword" - - export cifs_version="${cifs_version:-3.0}" - export archive_module="run/cifs_archive" - fi - - check_variable "archiveserver" -} - function check_available_space () { setup_progress "Verifying that there is sufficient space available on the MicroSD card..." @@ -179,6 +117,7 @@ function get_ancillary_setup_scripts () { get_script /tmp create-backingfiles-partition.sh setup/pi get_script /tmp create-backingfiles.sh setup/pi get_script /tmp make-root-fs-readonly.sh setup/pi + get_script /root configure.sh setup/pi } function fix_cmdline_txt_modules_load () @@ -223,59 +162,6 @@ function create_usb_drive_backing_files () { fi } -function configure_archive_scripts () { - setup_progress "Configuring the archive scripts..." - - get_script /root/bin archiveloop run - get_script /root/bin remountfs_rw run - - setup_progress "Configured the archive scripts." -} - -function install_archive_scripts () { - check_variable "archive_module" - setup_progress "Installing scripts for archive module: $archive_module" - - local install_path="/root/bin" - get_script $install_path verify-archive-configuration.sh $archive_module - get_script $install_path configure-archive.sh $archive_module - get_script $install_path archive-clips.sh $archive_module - get_script $install_path connect-archive.sh $archive_module - get_script $install_path disconnect-archive.sh $archive_module -} - -function configure_pushover_scripts() { - get_script /root/bin send-pushover run -} - -function configure_rc_local () { - if grep -q archiveloop /etc/rc.local - then - return - fi - - setup_progress "Configuring /etc/rc.local to run the archive scripts at startup..." - echo "#!/bin/bash -eu" > ~/rc.local - echo "archiveserver=\"${archiveserver}\"" >> ~/rc.local -cat << 'EOF' >> ~/rc.local -LOGFILE=/tmp/rc.local.log - -function log () { - echo "$( date )" >> "$LOGFILE" - echo "$1" >> "$LOGFILE" -} - -log "Launching archival script..." -/root/bin/archiveloop "$archiveserver" & -log "All done" -exit 0 -EOF - - cat ~/rc.local > /etc/rc.local - rm ~/rc.local - setup_progress "Configured rc.local." -} - function configure_hostname () { # Headless image already has hostname set if [ ! $HEADLESS_SETUP = "true" ] @@ -307,35 +193,18 @@ headless_setup_progress_flash 1 setup_progress "Verifying environment variables..." check_variable "campercent" -check_archive_configs -check_pushover_enabled # Flash for Stage 3 headless (grab scripts) headless_setup_progress_flash 2 setup_progress "Downloading additional setup scripts." -if [ ! -e /root/bin ] -then - mkdir /root/bin -fi - -get_script /root/bin get-archiveserver-ip-address.sh run - -install_archive_scripts -/root/bin/configure-archive.sh -/root/bin/verify-archive-configuration.sh - check_available_space get_ancillary_setup_scripts pushd ~ -configure_archive_scripts - -configure_pushover_scripts - fix_cmdline_txt_modules_load echo "" >> /etc/fstab @@ -345,8 +214,6 @@ headless_setup_progress_flash 3 create_usb_drive_backing_files -configure_rc_local - configure_hostname # Flash for stage 5 headless (Mark success, FS readonly)