summaryrefslogtreecommitdiff
path: root/scripts/quagga-manager
blob: 77cb5f5aae792d0720c1510aa664a9ffbe1f4d1f (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
141
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
#! /bin/bash
#
# This is special script for start,stop,restart of quagga daemons
#

# debug
# echo $* | logger -p local7.debug -t quagga-manager

usage() {
    echo "Usage: $0 {check|start|stop|restart} {bgpd|ospfd|ripd|ospf6d|ripngd}"
    exit 1
}

if [ $# -lt 1 ]; then
    usage
fi

if [ $EUID -ne 0 ]; then
    echo "must be root!"
    exit 1
fi

pid_dir=/var/run/vyatta/quagga
log_dir=/var/log/vyatta/quagga

. /lib/lsb/init-functions

status() {
    local daemon=$1
    local pidfile=${pid_dir}/${daemon}.pid
    local binpath=/usr/sbin/vyatta-$daemon
    status_of_proc -p $pidfile $binpath $daemon && exit 0 || exit $?
}

start() {
    local daemon=$1
    local pidfile=${pid_dir}/${daemon}.pid
    local binpath=/usr/sbin/vyatta-$daemon

    if [ ! -x $binpath ]; then
	echo "Unknown daemon $daemon"
	return 1
    fi

    # already running?
    pidofproc $pidfile $daemon >/dev/null && return 0
    
    local -a args=( -d -P 0 -i ${pid_dir}/${daemon}.pid )
    case $daemon in
    zebra) args+=( -l -S -s 1048576 ) ;;
    watchquagga)
	    args=( -dz -p ${pid_dir}/${daemon}.pid );
	    # Note: at present ospf/rip only monitored if actually active
	    # probably should get smarter in future here
	    local -a protocols=(`/opt/vyatta/bin/vyatta-show-protocols exists`)

	    if [ ${#protocols[*]} -eq 0 ]
	    then 
		# Only zebra: mode 1 simple restart
		args+=( -R "$0 restart zebra" zebra )
	    else
		# With routing protocols: phased restart
		args+=( -A -r "$0 restart %s" )
		args+=( -s "$0 reload %s" )
		args+=( -k "$0 stop %s" )
		args+=( zebra ${protocols[*]} )
	    fi
	    ;;
    esac

    start-stop-daemon --start --quiet \
	--chdir $log_dir --exec $binpath \
	-- "${args[@]}"
}

stop() {
    local daemon=$1
    local binpath=/usr/sbin/vyatta-$daemon

    start-stop-daemon --stop --quiet --oknodo --retry 5 \
	--exec $binpath --pidfile=$pid_dir/${daemon}.pid
    rm -f $pid_dir/${daemon}.pid
}

get_protocol() {
    local daemon=$1
    case $daemon in
    bgpd|ospfd|ripd|ripngd|isisd)   echo ${daemon/%d/};;
    ospf6d)			    echo "ospfv3";;
    *)  echo "Unknown daemon $daemon" 1>&2; exit 1;;
    esac
}

reload_config() {
    local daemon=$1
    local proto=$(get_protocol $daemon)

    local vyatta_cfg=/opt/vyatta/config/active
    local path=$vyatta_cfg/protocols/$proto

    # If daemon does not have config nothing to do.
    [ -d $path ] || return;

    # Begin reloading transaction
    /opt/vyatta/sbin/vyatta-cfg-cmd-wrapper begin || exit 1

    # In case of error undo
    trap "/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper end" EXIT HUP INT QUIT TERM
    
    # Save current configuration
    tmp=/tmp/${daemon}-restart.$$
    /opt/vyatta/sbin/vyatta-save-config.pl $tmp || exit 1

    # Erase portion of active configuration for that protocol
    rm -fr $path

    # special case for interface, remove parameters force reload
    case $proto in
    rip|ospf)
	    find $vyatta_cfg/interfaces -type d -path "*/ip/$proto" \
		-exec rm -fr '{}' \; ;;
    ripng|ospfv3)
	    find $vyatta_cfg/interfaces -type d -path "*/ipv6/$proto" \
		-exec rm -fr '{}' \; ;;
    esac

    # Reload causing configuration to activate - implies commit
    /opt/vyatta/sbin/vyatta-load-config.pl $tmp || exit 1

    # remove tmp file if successful
    rm $tmp
    trap "" EXIT HUP INT QUIT TERM
}

# Check if interface configuration exists for this protocol
okay_to_stop() {
    local daemon=$1
    local proto=$(get_protocol $daemon)
    local cfg=/opt/vyatta/config/active/interfaces

    case $proto in
    rip|ospf)
        return $(find $cfg -type d -path "*/ip/$proto" | wc -l);;
    ripng|ospfv3)
	return $(find $cfg -type d -path "*/ipv6/$proto" | wc -l);;
    *)
        return 0;;
    esac
}

update() {
    # get list of daemons deleted and added in current transaction
    local -a deleted=( `/opt/vyatta/bin/vyatta-show-protocols deleted` )
    local -a added=( `/opt/vyatta/bin/vyatta-show-protocols added` )

    # nothing lost, nothing gained
    [ ${#deleted[*]} -eq 0 -a ${#added[*]} -eq 0 ] && exit 0

    # Stop watcher (or it will restart daemons!)
    stop watchquagga

    # Cleanup any daemons no longer needed
    for p in ${deleted[*]}
    do
	# Need daemon to hold state of interface
	if okay_to_stop $p
	then stop $p
	fi
    done

    start watchquagga
}


case "$1" in
    start)   start $2;;
    stop)    stop $2;;
    status)  status $2;;
    update)  update;;
    reload)  start $2; reload_config $2;;
    restart) stop $2; start $2;;
    *)       usage;;
esac