~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to bin/dhcpleasor.py

  • Committer: Todd Willey
  • Date: 2010-07-15 04:21:17 UTC
  • mfrom: (131.1.4)
  • Revision ID: git-v1:fb2ea2cafd67fc1ef67edc969a1edf3a1d2fd7f7
fix merge errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
3
 
 
4
# Copyright 2010 United States Government as represented by the
 
5
# Administrator of the National Aeronautics and Space Administration.
 
6
# All Rights Reserved.
 
7
#
 
8
# Copyright 2010 Anso Labs, LLC
 
9
#
 
10
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
11
#    not use this file except in compliance with the License. You may obtain
 
12
#    a copy of the License at
 
13
#
 
14
#         http://www.apache.org/licenses/LICENSE-2.0
 
15
#
 
16
#    Unless required by applicable law or agreed to in writing, software
 
17
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
18
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
19
#    License for the specific language governing permissions and limitations
 
20
#    under the License.
 
21
 
 
22
"""
 
23
dhcpleasor.py
 
24
 
 
25
Handle lease database updates from DHCP servers.
 
26
"""
 
27
 
 
28
import sys
 
29
import os
 
30
import logging
 
31
sys.path.append(os.path.abspath(os.path.join(__file__, "../../")))
 
32
 
 
33
logging.debug(sys.path)
 
34
import getopt
 
35
from os import environ
 
36
from nova.compute import linux_net
 
37
from nova.compute import network
 
38
from nova import rpc
 
39
 
 
40
from nova import flags
 
41
FLAGS = flags.FLAGS
 
42
 
 
43
 
 
44
def add_lease(mac, ip, hostname, interface):
 
45
    if FLAGS.fake_rabbit:
 
46
        network.lease_ip(ip)
 
47
    else:
 
48
        rpc.cast(FLAGS.cloud_topic, {"method": "lease_ip",
 
49
                "args" : {"address": ip}})
 
50
 
 
51
def old_lease(mac, ip, hostname, interface):
 
52
    logging.debug("Adopted old lease or got a change of mac/hostname")
 
53
 
 
54
def del_lease(mac, ip, hostname, interface):
 
55
    if FLAGS.fake_rabbit:
 
56
        network.release_ip(ip)
 
57
    else:
 
58
        rpc.cast(FLAGS.cloud_topic, {"method": "release_ip",
 
59
                "args" : {"address": ip}})
 
60
 
 
61
def init_leases(interface):
 
62
    net = network.get_network_by_interface(interface)
 
63
    res = ""
 
64
    for host_name in net.hosts:
 
65
        res += "%s\n" % linux_net.hostDHCP(net, host_name, net.hosts[host_name])
 
66
    return res
 
67
 
 
68
 
 
69
def main(argv=None):
 
70
    if argv is None:
 
71
        argv = sys.argv
 
72
    interface = environ.get('DNSMASQ_INTERFACE', 'br0')
 
73
    if int(environ.get('TESTING', '0')):
 
74
        FLAGS.fake_rabbit = True
 
75
        FLAGS.redis_db = 8
 
76
        FLAGS.network_size = 32
 
77
        FLAGS.fake_libvirt=True
 
78
        FLAGS.fake_network=True
 
79
        FLAGS.fake_users = True
 
80
    action = argv[1]
 
81
    if action in ['add','del','old']:
 
82
        mac = argv[2]
 
83
        ip = argv[3]
 
84
        hostname = argv[4]
 
85
        logging.debug("Called %s for mac %s with ip %s and hostname %s on interface %s" % (action, mac, ip, hostname, interface))
 
86
        globals()[action+'_lease'](mac, ip, hostname, interface)
 
87
    else:
 
88
        print init_leases(interface)
 
89
    exit(0)
 
90
 
 
91
if __name__ == "__main__":
 
92
    sys.exit(main())