Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to handle multiple arrays. #85

Closed
tehniemer opened this issue Mar 8, 2024 · 10 comments
Closed

Add logic to handle multiple arrays. #85

tehniemer opened this issue Mar 8, 2024 · 10 comments

Comments

@tehniemer
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

@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:

  • check if running on OMV
    • If running on OMV7, run the fuction that will search for a conf file, if there's one go ahead, if there are multiple files, stop and output the result
  • If running on OMV6 or any other distro, stop and output that a config file cannot be 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:

  • Manually configured the correct file => this logic does not kick in
  • No changes with one array => this logic picks up the single config file
  • No changes with two arrays => this logic stops and outputs the available files

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
Copy link
Contributor Author

tehniemer commented Mar 14, 2024

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
Copy link
Owner

auanasgheps commented Mar 14, 2024

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
Copy link
Contributor Author

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

@tehniemer
Copy link
Contributor Author

tehniemer commented Mar 14, 2024

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
Copy link
Owner

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
Copy link
Contributor Author

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

@tehniemer
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants