autoNAS / scripts / autonas-network-handler.sh
Newer Older
5b5a565 3 months ago History
101 lines | 3.351kb
Bogdan Timofte authored 3 months ago
1
#!/bin/bash
2

            
3
# AutoNAS Network Handler - System integration for network interface events
4
# This script is called by udev when network interfaces change state
5
# Uses autonas-core.sh for all business logic
6

            
7
# Load the AutoNAS core library
8
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9
source "${SCRIPT_DIR}/autonas-core.sh" || {
10
    echo "Error: Cannot load AutoNAS core library" >&2
11
    exit 1
12
}
13

            
14
# Set LOG_TAG for this component
15
LOG_TAG="autonas-network-handler"
16

            
17
# Function to handle interface coming up
18
handle_interface_up() {
19
    local interface="$1"
20
    local event_type="$2"
21

            
22
    log_message "Interface up event: $interface ($event_type)" "info"
23

            
24
    # Check if this interface is used by AutoNAS
25
    if interface_has_autonas_config "$interface"; then
26
        log_message "Interface $interface is configured for AutoNAS, restoring IP configurations" "info"
27

            
28
        # Small delay to let interface stabilize completely
29
        sleep 3
30

            
31
        # Restore IPs for this interface
32
        if restore_interface_ips "$interface"; then
33
            log_message "Successfully processed interface $interface restoration" "info"
34
        else
35
            log_message "Failed to fully process interface $interface restoration" "warning"
36
        fi
37
    else
38
        log_message "Interface $interface is not configured for AutoNAS, ignoring" "debug"
39
    fi
40
}
41

            
42
# Function to handle interface going down
43
handle_interface_down() {
44
    local interface="$1"
45
    local event_type="$2"
46

            
47
    log_message "Interface down event: $interface ($event_type)" "info"
48

            
49
    if interface_has_autonas_config "$interface"; then
50
        log_message "AutoNAS-configured interface $interface went down" "warning"
51
    fi
52
}
53

            
54
# Main script logic
55
ACTION="$1"
56
INTERFACE="$2"
57
EXTRA="$3"
58

            
59
case "$ACTION" in
60
    "interface_up"|"usb_interface_up")
61
        if [ -n "$INTERFACE" ]; then
62
            # Run in background to avoid udev timeout, but with shorter delay for faster response
63
            (
64
                sleep 2  # Reduced from 3 seconds for faster response
65
                handle_interface_up "$INTERFACE" "$ACTION"
66
            ) &
67
            log_message "Started background handler for interface up: $INTERFACE (PID: $!)" "debug"
68
        fi
69
        ;;
70
    "interface_down"|"usb_interface_down")
71
        if [ -n "$INTERFACE" ]; then
72
            handle_interface_down "$INTERFACE" "$ACTION"
73
        fi
74
        ;;
75
    "interface_change")
76
        if [ -n "$INTERFACE" ] && [ -n "$EXTRA" ]; then
77
            case "$EXTRA" in
78
                "up"|"carrier_up")
79
                    (
80
                        sleep 2
81
                        handle_interface_up "$INTERFACE" "change_to_$EXTRA"
82
                    ) &
83
                    log_message "Started background handler for interface $EXTRA: $INTERFACE (PID: $!)" "debug"
84
                    ;;
85
                "down"|"carrier_down")
86
                    handle_interface_down "$INTERFACE" "change_to_$EXTRA"
87
                    ;;
88
                *)
89
                    log_message "Unknown interface change state: $EXTRA for interface $INTERFACE" "warning"
90
                    ;;
91
            esac
92
        fi
93
        ;;
94
    *)
95
        log_message "Unknown action: $ACTION with interface: $INTERFACE" "warning"
96
        exit 1
97
        ;;
98
esac
99

            
100
# Exit immediately so udev doesn't wait
101
exit 0