auanasgheps / snapraid-aio-script

The definitive all-in-one SnapRAID script on Linux. Diff, sync, scrub are things of the past. Manage SnapRAID and much, much more!
GNU General Public License v3.0
225 stars 36 forks source link

Add logic to handle multiple arrays. #85

Closed tehniemer closed 4 months ago

tehniemer commented 4 months ago

This is a continuation of the discussion of #80

OMV7 allows for configuring multiple SnapRAID arrays, it would be nice if this script could be configured similarly.

auanasgheps commented 4 months ago

This is doable.

This script is rough, but works. Written with ChatGPT in less than 3 questions, no manual adjustment. Can be improved and implemented in the main AIO Script. We can auto select when one config file is found, otherwise will tell the user to manually configure the config file it wants.

I can work on it, but first I have to push some changes to the dev branch and I don't want them to conflict. Let me know if you had a better idea.

#!/bin/bash

search_conf_files() {
    folder="$1"

    # Check if the directory exists
    if [ ! -d "$folder" ]; then
        echo "Directory $folder does not exist."
        return 1
    fi

    conf_files=("$folder"/omv-snapraid-*.conf)

    echo "Searching in folder: $folder"
    echo "Found files matching pattern: ${conf_files[@]}"

    if [ ${#conf_files[@]} -eq 0 ]; then
        echo "No omv-snapraid-.conf files found in $folder"
        return 1
    elif [ ${#conf_files[@]} -eq 1 ]; then
        echo "Found 1 omv-snapraid-.conf file: ${conf_files[0]}"
        single_conf_file="${conf_files[0]}"
        return 0
    else
        echo "Found multiple omv-snapraid-.conf files:"
        for file in "${conf_files[@]}"; do
            echo "$file"
        done
        return 2
    fi
}

# Usage example:
search_conf_files "/etc/snapraid/"
result=$?
if [ $result -eq 0 ]; then
    # Only one omv-snapraid-.conf file found, you can proceed with it
    echo "Proceeding with the single omv-snapraid-.conf file: $single_conf_file"
    # Use $single_conf_file variable wherever you need the file path
elif [ $result -eq 2 ]; then
    # Multiple omv-snapraid-.conf files found, stopping the script
    echo "Stopping the script due to multiple omv-snapraid-.conf files."
else
    # No omv-snapraid-.conf files found, handle this case accordingly
    echo "Handle the case when no omv-snapraid-.conf files are found or the directory does not exist."
fi
auanasgheps commented 4 months ago

I have pushed the changes (Snapraid config file check and notifiy) so we can start working on this.

Here's an updated (rough) version, which has updated messages and notify logic:

#!/bin/bash

function pick_snapraid_conf_file() {
search_conf_files "/etc/snapraid"
result=$?
if [ $result -eq 0 ]; then
    # Only one SnapRAID config file found, proceeding
    echo "Proceeding with the single omv-snapraid-.conf file: $SNAPRAID_CONF"

elif [ $result -eq 2 ]; then
    # Multiple SnapRAID config files found, stopping the script
    echo "Stopping the script due to multiple SnapRAID configuration files. Please choose one config file and update your settings in ""$CONFIG_FILE"". SnapRAID config files to be chosen:"
        for file in "${conf_files[@]}"; do
            echo "$file"
        done
    mklog "WARN: Stopping the script due to multiple SnapRAID configuration files. Please pick up one config file and update your settings in ""$CONFIG_FILE""."
    SUBJECT="[WARNING] - Multiple SnapRAID configuration files!"
    FORMATTED_CONF="\`$SNAPRAID_CONF\`"
    NOTIFY_OUTPUT="$SUBJECT Stopping the script due to multiple SnapRAID configuration files. Please pick up one config file and update your settings in "$CONFIG_FILE"."
    notify_warning
    if [ "$EMAIL_ADDRESS" ]; then
      trim_log < "$TMP_OUTPUT" | send_mail
    fi
    exit 1; 

else
    # No SnapRAID conf file found, stopping the script
    echo "SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file ""$SNAPRAID_CONF"" does not exist."
    mklog "WARN: SnapRAID configuration file not found. The script cannot be run! Please check your settings, because the specified file ""$SNAPRAID_CONF"" does not exist."
    SUBJECT="[WARNING] - SnapRAID configuration file not found!"
    FORMATTED_CONF="\`$SNAPRAID_CONF\`"
    NOTIFY_OUTPUT="$SUBJECT The script cannot be run! Please check your settings, because the specified file $FORMATTED_CONF does not exist."
    notify_warning
    if [ "$EMAIL_ADDRESS" ]; then
      trim_log < "$TMP_OUTPUT" | send_mail
    fi
    exit 1;
fi
}

search_conf_files() {
    folder="$1"

    # Check if the directory exists
    if [ ! -d "$folder" ]; then
        echo "Directory $folder does not exist."
        return 1
    fi

    conf_files=("$folder"/omv-snapraid-*.conf)

    #echo "Searching in folder: $folder"
    #echo "Found files matching pattern: ${conf_files[@]}"

    # if no files are found 
    if [ ${#conf_files[@]} -eq 0 ]; then
        return 1
    # if one file is found  
    elif [ ${#conf_files[@]} -eq 1 ]; then
        $SNAPRAID_CONF="${conf_files[0]}"
        return 0
    # if multiple files are found 
    else
        return 2
    fi
}

pick_snapraid_conf_file
auanasgheps commented 4 months ago

@tehniemer I have added a lot of logic to pick up the OMV7 config file automatically. Can you please help me testing on your side?

The logic is the following:

If the SnapRAID config file referenced in script-conf.sh is not found:

Output as usual is fully featured: screen, log file, email and notification. Since it's quite complex I'm asking for your support, which would make me more confident about the quality of this implementation.

I've tested on OMV7 several scenarios:

Later today I'll test on my OMV6 real server.

I want this to be the last change before release. Since OMV7 is out I want to act fast!

tehniemer commented 4 months ago

I just pulled the dev branch over to my fork and will test over the next few days. There are a lot of changes in this update, will this be a major version bump?

One thing I will comment on right away that if the script is going to exit if multiple snapraid.conf files are found and one is not explicitly defined in the script configuration it should be written to work with multiple script configuration files, maybe as a user defined input argument for the array desired to be run, otherwise if there is only one array no extra input is necessary.

auanasgheps commented 4 months ago

Next release will be 3.3 which is a major release compared to 3.2. It includes all the work we've done in the last year and so. The change log is ready too but not published yet.

So yeah there might me many changes since your last pull/PR.

One thing I will comment on right away that if the script is going to exit if multiple snapraid.conf files are found and one is not explicitly defined in the script configuration it should be written to work with multiple script configuration files, maybe as a user defined input argument for the array desired to be run, otherwise if there is only one array no extra input is necessary.

Not sure if I understand what you mean. Since the user has to make a manual configuration due to multiple arrays, he/she should enter to the desired SnapRAID config file in the script-config file.

To use multiple arrays the user can rely on the multi scipt-config functionality that I built some months ago, you might have missed it.

From the dev README:

  • Multiple Configuration files
  • By default the script will use the predefined config file script-config.sh that must be placed in the same folder
  • You can specify another file when running the script like snapraid-aio-script.sh /home/alternate_config.sh

You can create multiple schedules if you want to use a different config file for a specific need. Just append the config file path after the script, like snapraid-aio-script.sh /home/alternate_config.sh

tehniemer commented 4 months ago

Again, I failed to read the right documentation, that's exactly what I was envisioning. My apologies.

tehniemer commented 4 months ago

I ran a test and it completed successfully with my single array on OMV7, but I came across a couple of oddities. I received my normal Telegram notification that the script started, but not that it finished. I also did not receive the email summary I normally do even though the script said it was sent. I'll dig into this a bit more later.

edit: I just received the email about 5 minutes after the script completed, still haven't received the Telegram notification though...

auanasgheps commented 4 months ago

I have ran multiple tests today on a OMV7 VM, always received the final notification. Also, I haven't touched that part since a while.

Since you got your email 5 mins later, maybe you got some network errors? At the moment any error from curl would be suppressed, so we would never know what happened.

tehniemer commented 4 months ago

I'm thinking as much, I'll keep monitoring it.

tehniemer commented 4 months ago

Everything has been working as expected for the last week or so.