author Rich Burridge <>
Tue, 02 May 2017 17:33:26 -0700
changeset 7964 d9801318ed3d
parent 6848 8e252a37ed0d
permissions -rw-r--r--
25981468 Build ilmbase and openexr with the GNU compilers

# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import ConfigParser
import os
import re
from subprocess import CalledProcessError, Popen, PIPE, check_call
import sys

from openstack_common import kill_contract
import smf_include

def set_hostmodel(value):
    cmd = ["/usr/sbin/ipadm", "show-prop", "-p", "hostmodel",
           "-co", "current", "ipv4"]
    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
    output, error = p.communicate()
    if p.returncode != 0:
        print "failed to retrieve hostmodel ipadm property"
        return False
    if output.strip() == value:
        return True
    cmd = ["/usr/bin/pfexec", "/usr/sbin/ipadm", "set-prop", "-t", "-p",
           "hostmodel=%s" % value, "ipv4"]
    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
    output, error = p.communicate()
    if p.returncode != 0:
        print "failed to set ipadm hostmodel property to %s" % value
        return False
    return True

def cleanup_dhcp_agent_datalinks():
    cmd = ["/usr/sbin/dladm", "show-link", "-p", "-o", "link"]
    p = Popen(cmd, stdout=PIPE, stderr=PIPE)
    output, error = p.communicate()
    if p.returncode != 0:
        print "failed to retrieve datalink names"
        return smf_include.SMF_EXIT_ERR_FATAL

    dlnames = output.splitlines()
    # DHCP agent datalinks are always 15 characters in length. They start with
    # 'dh', end with '_0', and in between they are hexadecimal digits.
    prog = re.compile('dh[0-9A-Fa-f\_]{11}_0')
    ret_code = smf_include.SMF_EXIT_OK
    ovs_bridge = get_ovs_bridge()
    for dlname in dlnames:
        if is None:
            # first remove the IP
            check_call(["/usr/bin/pfexec", "/usr/sbin/ipadm", "delete-ip",
            # It is possible that the IP was already deleted but not the
            # datalink. So we continue and try and delete the datalink.
            # next remove the VNIC
            check_call(["/usr/bin/pfexec", "/usr/sbin/dladm", "delete-vnic",
            # remove the OVS Port
            if ovs_bridge:
                check_call(["/usr/bin/pfexec", "/usr/sbin/ovs-vsctl", "--",
                            "--if-exists", "del-port", ovs_bridge, dlname])
        except CalledProcessError as err:
            print "failed to remove datalink '%s' used by DHCP agent: %s" % \
                (dlname, err)
            ret_code = smf_include.SMF_EXIT_ERR_FATAL
    return ret_code

def start():
    # verify paths are valid
    for f in sys.argv[2:4]:
        if not os.path.exists(f) or not os.access(f, os.R_OK):
            print '%s does not exist or is not readable' % f
            return smf_include.SMF_EXIT_ERR_CONFIG

    # remove VNICs associated with DHCP agent if any were left over.
    ret_code = cleanup_dhcp_agent_datalinks()
    if ret_code != smf_include.SMF_EXIT_OK:
        return ret_code

    # set the hostmodel property if necessary
    if not set_hostmodel("src-priority"):
        return smf_include.SMF_EXIT_ERR_FATAL

    cmd = "/usr/bin/pfexec /usr/lib/neutron/neutron-dhcp-agent " \
        "--config-file %s --config-file %s" % tuple(sys.argv[2:4])

def get_ovs_bridge():
    parser = ConfigParser.ConfigParser()"/etc/neutron/plugins/ml2/openvswitch_agent.ini")
        ovs_bridge = parser.get("ovs", "integration_bridge")
    except ConfigParser.NoOptionError:
        ovs_bridge = None
    return ovs_bridge

def stop():
    # Keep issuing SIGTERM until the contract is empty. This way we will catch
    # any child processes missed because they were getting forked.
    # 50 attempts will be made at intervals of 2 seconds. Typically, we
    # will only need 0 or 1 additional attempt before the contract is empty but
    # we chose to err on the side of caution. In the worst case, we will use
    # 100 seconds in the below loop which will leave 500 seconds (timeout is
    # 600s) for the other cleanup tasks, after which the service will be put to
    # maintenance state if the contract was not killed successfully.
    if not kill_contract(50, 2, sys.argv[2]):
        return smf_include.SMF_EXIT_ERR_FATAL

    # remove VNICs associated with DHCP agent
    ret_code = cleanup_dhcp_agent_datalinks()

    # finally reset the hostmodel property
    if not set_hostmodel("weak") or ret_code != smf_include.SMF_EXIT_OK:
        return smf_include.SMF_EXIT_ERR_FATAL
    return smf_include.SMF_EXIT_OK

if __name__ == "__main__":
    os.putenv("LC_ALL", "C")