components/openstack/neutron/files/neutron-dhcp-agent
changeset 3998 5bd484384122
parent 3524 ad6a9e0880b9
child 4049 150852e281c4
equal deleted inserted replaced
3997:0ca3f3d6c919 3998:5bd484384122
     1 #!/usr/bin/python2.6
     1 #!/usr/bin/python2.6
     2 
     2 
     3 # Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     3 # Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
     4 #
     4 #
     5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
     5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
     6 #    not use this file except in compliance with the License. You may obtain
     6 #    not use this file except in compliance with the License. You may obtain
     7 #    a copy of the License at
     7 #    a copy of the License at
     8 #
     8 #
    21 import smf_include
    21 import smf_include
    22 
    22 
    23 from subprocess import CalledProcessError, Popen, PIPE, check_call
    23 from subprocess import CalledProcessError, Popen, PIPE, check_call
    24 
    24 
    25 
    25 
       
    26 def set_hostmodel(value):
       
    27     cmd = ["/usr/sbin/ipadm", "show-prop", "-p", "hostmodel",
       
    28            "-co", "current", "ipv4"]
       
    29     p = Popen(cmd, stdout=PIPE, stderr=PIPE)
       
    30     output, error = p.communicate()
       
    31     if p.returncode != 0:
       
    32         print "failed to retrieve hostmodel ipadm property"
       
    33         return False
       
    34     if output.strip() == value:
       
    35         return True
       
    36     cmd = ["/usr/sbin/ipadm", "set-prop", "-t", "-p", "hostmodel=%s" % value,
       
    37            "ipv4"]
       
    38     p = Popen(cmd, stdout=PIPE, stderr=PIPE)
       
    39     output, error = p.communicate()
       
    40     if p.returncode != 0:
       
    41         print "failed to set ipadm hostmodel property to %s" % value
       
    42         return False
       
    43     return True
       
    44 
       
    45 
    26 def start():
    46 def start():
    27     # verify paths are valid
    47     # verify paths are valid
    28     for f in sys.argv[2:4]:
    48     for f in sys.argv[2:4]:
    29         if not os.path.exists(f) or not os.access(f, os.R_OK):
    49         if not os.path.exists(f) or not os.access(f, os.R_OK):
    30             print '%s does not exist or is not readable' % f
    50             print '%s does not exist or is not readable' % f
    31             return smf_include.SMF_EXIT_ERR_CONFIG
    51             return smf_include.SMF_EXIT_ERR_CONFIG
       
    52 
       
    53     # set the hostmodel property if necessary
       
    54     if not set_hostmodel("src-priority"):
       
    55         return smf_include.SMF_EXIT_ERR_FATAL
    32 
    56 
    33     cmd = "/usr/lib/neutron/neutron-dhcp-agent --config-file %s " \
    57     cmd = "/usr/lib/neutron/neutron-dhcp-agent --config-file %s " \
    34         "--config-file %s" % tuple(sys.argv[2:4])
    58         "--config-file %s" % tuple(sys.argv[2:4])
    35     smf_include.smf_subprocess(cmd)
    59     smf_include.smf_subprocess(cmd)
    36 
    60 
    50         print "failed to retrieve IP interface names"
    74         print "failed to retrieve IP interface names"
    51         return smf_include.SMF_EXIT_ERR_FATAL
    75         return smf_include.SMF_EXIT_ERR_FATAL
    52 
    76 
    53     ifnames = output.splitlines()
    77     ifnames = output.splitlines()
    54     # DHCP agent datalinks are always 15 characters in length. They start with
    78     # DHCP agent datalinks are always 15 characters in length. They start with
    55     # 'evs', end with '_0', and in between they are hexadecimal digits.
    79     # 'dh', end with '_0', and in between they are hexadecimal digits.
    56     prog = re.compile('evs[0-9A-Fa-f\_]{10}_0')
    80     prog = re.compile('dh[0-9A-Fa-f\_]{11}_0')
    57     for ifname in ifnames:
    81     for ifname in ifnames:
    58         if not prog.search(ifname):
    82         if not prog.search(ifname):
    59             continue
    83             continue
    60 
    84 
    61         try:
    85         try:
    62             # first remove the IP
    86             # first remove the IP
    63             check_call(["/usr/bin/pfexec", "/usr/sbin/ipadm", "delete-ip",
    87             check_call(["/usr/bin/pfexec", "/usr/sbin/ipadm", "delete-ip",
    64                         ifname])
    88                         ifname])
    65             # get the tenant, evs, and vport name for the VNIC
       
    66             cmd = ["/usr/sbin/dladm", "show-vnic", "-po",
       
    67                    "tenant,evs,vport", ifname]
       
    68             p = Popen(cmd, stdout=PIPE, stderr=PIPE)
       
    69             output, error = p.communicate()
       
    70             if p.returncode != 0:
       
    71                 print "failed to retrieve Tenant, EVS," \
       
    72                       " and VPort info for a VNIC"
       
    73                 return smf_include.SMF_EXIT_ERR_FATAL
       
    74             tenant, evs, vport = output.strip().split(':')
       
    75             # next remove the VNIC
    89             # next remove the VNIC
    76             check_call(["/usr/bin/pfexec", "/usr/sbin/dladm", "delete-vnic",
    90             check_call(["/usr/bin/pfexec", "/usr/sbin/dladm", "delete-vnic",
    77                         ifname])
    91                         ifname])
    78             # remove the EVS VPort
       
    79             check_call(["/usr/sbin/evsadm", "remove-vport", "-T", tenant,
       
    80                         "%s/%s" % (evs, vport)])
       
    81         except CalledProcessError as err:
    92         except CalledProcessError as err:
    82             print "failed to remove datalinks used by DHCP agent: %s" % err
    93             print "failed to remove datalinks used by DHCP agent: %s" % err
    83             return smf_include.SMF_EXIT_ERR_FATAL
    94             return smf_include.SMF_EXIT_ERR_FATAL
       
    95 
       
    96     # finally reset the hostmodel property
       
    97     if not set_hostmodel("weak"):
       
    98         return smf_include.SMF_EXIT_ERR_FATAL
    84     return smf_include.SMF_EXIT_OK
    99     return smf_include.SMF_EXIT_OK
    85 
   100 
    86 if __name__ == "__main__":
   101 if __name__ == "__main__":
    87     os.putenv("LC_ALL", "C")
   102     os.putenv("LC_ALL", "C")
    88     smf_include.smf_main()
   103     smf_include.smf_main()