wrs_custom_boot_script.sh 4.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#!/bin/sh

set -o pipefail

dotconfig=/wr/etc/dot-config
tmpdir=/tmp
tmpscript="$tmpdir"/custom_boot_script.sh
custom_boot_status_file="$tmpdir"/custom_boot_script_status
custom_boot_source_file="$tmpdir"/custom_boot_script_source
custom_boot_source_url_file="$tmpdir"/custom_boot_script_url

print_error() {
    echo "$1"
    eval echo "$0: $1" $LOGPIPE
}
start() {
    echo -n "Executing custom boot script: "

    if [ -f $dotconfig ]; then
	. $dotconfig
    else
	echo "$0 unable to source dot-config ($dotconfig)!"
    fi

    # set log destination
    WRS_LOG=$CONFIG_WRS_LOG_OTHER

    # if empty turn it to /dev/null
    if [ -z $WRS_LOG ]; then
	WRS_LOG="/dev/null";
    fi

    # if a pathname, use it
    if echo "$WRS_LOG" | grep / > /dev/null; then
	eval LOGPIPE=\" \> $WRS_LOG 2\>\&1 \";
    else
	# not a pathname: use verbatim
38
	eval LOGPIPE=\" 2\>\&1 \| logger -t custom_boot_script -p $WRS_LOG\"
39 40
    fi

41 42 43 44 45 46
    # set msg level
    if [ ! -z $CONFIG_WRS_LOG_LEVEL_OTHER ]; then
	WRS_MSG_LEVEL=$CONFIG_WRS_LOG_LEVEL_OTHER
	export WRS_MSG_LEVEL
    fi

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    # If custom boot script is not enabled, exit
    if [ ! "$CONFIG_CUSTOM_BOOT_SCRIPT_ENABLED" = "y" ]; then
	echo "disabled"
	echo "disabled" > "$custom_boot_status_file"
	exit;
    fi

    if [ "$CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_LOCAL" = "y" ]; then
	echo "local" > "$custom_boot_source_file"
	execute_script=/wr/bin/custom_boot_script.sh
    fi
    if [ "$CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE" = "y" ]; then
	echo "remote" > "$custom_boot_source_file"

	# replace IPADDR and MACADDR, to have a device-specific name
	macaddr=$(cat /sys/class/net/eth0/address)
	ipaddr=$(ifconfig eth0 | grep inet | cut -d: -f 2 | cut '-d '  -f 1)
	if [ -z "$ipaddr" ]; then
	    eval echo "$0: Warning no IP set!" $LOGPIPE
	fi
	host_name=`hostname`
	URL=$(echo $CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE_URL | \
69
	    sed -e s/MACADDR/$macaddr/g -e s/IPADDR/$ipaddr/g -e s/HOSTNAME/$host_name/g)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	# split the parts, as we need to handle tftp by hand
	proto=$(echo $URL | cut -d: -f 1)
	host=$(echo $URL | cut -d/ -f 3)
	filename=$(echo $URL | cut -d/ -f 4-)

	 # save URL of custom boot script, to be used by SNMPd
	echo "$URL" > "$custom_boot_source_url_file"
	rm -f "$tmpscript"
	case $proto in
	    http|ftp)
		wget $URL -O "$tmpscript"
		;;
	    tftp)
		tftp -g -r "$filename" -l "$tmpscript" "$host"
		;;
	    *)
		echo "Invalid URL for custom boot script: \"$URL\"" >& 2
		;;
	esac
	if [ ! -f "$tmpscript" ]; then
	    print_error "Download error!"
	    echo "download_error" > "$custom_boot_status_file"
	    exit
	fi
	execute_script="$tmpscript"
	chmod +x "$execute_script"
    fi

    if [ -z "$execute_script" ]; then
	print_error "Please specify local or remote in dot-config!"
	echo "wrong_source" > "$custom_boot_source_file"
	echo "wrong_source" > "$custom_boot_status_file"
	exit
    fi

    if [ ! -f "$execute_script" ]; then
	print_error "$execute_script not found!"
	exit
    fi
    # redirect output of script to OTHER_LOG
    eval "$execute_script" start $LOGPIPE
    ret=$?
    if [ $ret -eq 0 ]; then
	echo "OK"
	echo "ok" > "$custom_boot_status_file"
    else
	echo "Failed"
	echo "failed" > "$custom_boot_status_file"
    fi
}

stop() {
    if [ -f $dotconfig ]; then
	. $dotconfig
    else
	echo "$0 unable to source dot-config ($dotconfig)!"
    fi

    # set log destination
    WRS_LOG=$CONFIG_WRS_LOG_OTHER

    # if empty turn it to /dev/null
    if [ -z $WRS_LOG ]; then
	WRS_LOG="/dev/null";
    fi

    # if a pathname, use it
    if echo "$WRS_LOG" | grep / > /dev/null; then
	eval LOGPIPE=\" \> $WRS_LOG 2\>\&1 \";
    else
	# not a pathname: use verbatim
141
	eval LOGPIPE=\" 2\>\&1 \| logger -t custom_boot_script -p $WRS_LOG\"
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    fi

    if [ ! "$CONFIG_CUSTOM_BOOT_SCRIPT_ENABLED" = "y" ]; then
	exit;
    fi
    echo -n "Stopping custom boot script: "
    if [ "$CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_LOCAL" = "y" ]; then
	execute_script=/wr/bin/custom_boot_script.sh
    fi
    if [ "$CONFIG_CUSTOM_BOOT_SCRIPT_SOURCE_REMOTE" = "y" ]; then
	execute_script="$tmpscript"
    fi

    if [ -z "$execute_script" ]; then
	print_error "Please specify local or remote in dot-config!"
	exit
    fi

    if [ ! -f "$execute_script" ]; then
	print_error "$execute_script not found!"
	exit
    fi

    # redirect output of script to OTHER_LOG
    eval "$execute_script" stop $LOGPIPE
    ret=$?
    if [ $ret -eq 0 ]; then
	echo "OK"
    else
	echo "Failed"
    fi

}

restart() {
    stop
    start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
	;;
esac