|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + # |
| 6 | + # Copyright (C) 2010 Cloud.com, Inc. All rights reserved. |
| 7 | + # |
| 8 | + # This software is licensed under the GNU General Public License v3 or later. |
| 9 | + # |
| 10 | + # It is free software: you can redistribute it and/or modify |
| 11 | + # it under the terms of the GNU General Public License as published by |
| 12 | + # the Free Software Foundation, either version 3 of the License, or any later version. |
| 13 | + # This program is distributed in the hope that it will be useful, |
| 14 | + # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + # GNU General Public License for more details. |
| 17 | + # |
| 18 | + # You should have received a copy of the GNU General Public License |
| 19 | + # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + # |
| 21 | + |
| 22 | +# edithosts.sh -- edit the dhcphosts file on the routing domain |
| 23 | +# $1 : the mac address |
| 24 | +# $2 : the associated ip address |
| 25 | +# $3 : the hostname |
| 26 | + |
| 27 | +source /root/func.sh |
| 28 | + |
| 29 | +lock="biglock" |
| 30 | +locked=$(getLockFile $lock) |
| 31 | +if [ "$locked" != "1" ] |
| 32 | +then |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +lock_rr="rrouter" |
| 37 | +locked_rr=$(getLockFile $lock_rr) |
| 38 | +if [ "$locked_rr" != "1" ] |
| 39 | +then |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +grep "redundant_router=1" /var/cache/cloud/cmdline > /dev/null |
| 44 | +no_redundant=$? |
| 45 | + |
| 46 | +wait_for_dnsmasq () { |
| 47 | + local _pid=$(pidof dnsmasq) |
| 48 | + for i in 0 1 2 3 4 5 6 7 8 9 10 |
| 49 | + do |
| 50 | + sleep 1 |
| 51 | + _pid=$(pidof dnsmasq) |
| 52 | + [ "$_pid" != "" ] && break; |
| 53 | + done |
| 54 | + [ "$_pid" != "" ] && return 0; |
| 55 | + logger -t cloud "edithosts: timed out waiting for dnsmasq to start" |
| 56 | + return 1 |
| 57 | +} |
| 58 | + |
| 59 | +logger -t cloud "edithosts: update $1 $2 $3 to hosts" |
| 60 | + |
| 61 | +[ ! -f /etc/dhcphosts.txt ] && touch /etc/dhcphosts.txt |
| 62 | +[ ! -f /var/lib/misc/dnsmasq.leases ] && touch /var/lib/misc/dnsmasq.leases |
| 63 | + |
| 64 | +#delete any previous entries from the dhcp hosts file |
| 65 | +sed -i /$1/d /etc/dhcphosts.txt |
| 66 | +sed -i /$2,/d /etc/dhcphosts.txt |
| 67 | +sed -i /$3,/d /etc/dhcphosts.txt |
| 68 | + |
| 69 | +#put in the new entry |
| 70 | +echo "$1,$2,$3,infinite" >>/etc/dhcphosts.txt |
| 71 | + |
| 72 | +#delete leases to supplied mac and ip addresses |
| 73 | +sed -i /$1/d /var/lib/misc/dnsmasq.leases |
| 74 | +sed -i /"$2 "/d /var/lib/misc/dnsmasq.leases |
| 75 | +sed -i /"$3 "/d /var/lib/misc/dnsmasq.leases |
| 76 | + |
| 77 | +#put in the new entry |
| 78 | +echo "0 $1 $2 $3 *" >> /var/lib/misc/dnsmasq.leases |
| 79 | + |
| 80 | +#edit hosts file as well |
| 81 | +sed -i /"$2 "/d /etc/hosts |
| 82 | +sed -i /"$3"/d /etc/hosts |
| 83 | +echo "$2 $3" >> /etc/hosts |
| 84 | + |
| 85 | +# make dnsmasq re-read files |
| 86 | +pid=$(pidof dnsmasq) |
| 87 | +if [ "$pid" != "" ] |
| 88 | +then |
| 89 | + service dnsmasq restart |
| 90 | +else |
| 91 | + if [ $no_redundant -eq 1 ] |
| 92 | + then |
| 93 | + wait_for_dnsmasq |
| 94 | + else |
| 95 | + logger -t cloud "edithosts: skip wait dnsmasq due to redundant virtual router" |
| 96 | + fi |
| 97 | +fi |
| 98 | + |
| 99 | +ret=$? |
| 100 | +releaseLockFile $lock_rr $locked_rr |
| 101 | +unlock_exit $ret $lock $locked |
0 commit comments