neutron-dhcp-agent revision 2900
2605N/A#!/usr/bin/python2.6
2605N/A
2605N/A# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2605N/A#
2605N/A# Licensed under the Apache License, Version 2.0 (the "License"); you may
2605N/A# not use this file except in compliance with the License. You may obtain
2605N/A# a copy of the License at
2605N/A#
2605N/A# http://www.apache.org/licenses/LICENSE-2.0
2605N/A#
2605N/A# Unless required by applicable law or agreed to in writing, software
2605N/A# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
2605N/A# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
2605N/A# License for the specific language governing permissions and limitations
2605N/A# under the License.
2605N/A
2605N/Aimport os
2900N/Aimport re
2605N/Aimport sys
2605N/A
2605N/Aimport smf_include
2605N/A
2900N/Afrom subprocess import CalledProcessError, Popen, PIPE, check_call
2900N/A
2605N/A
2605N/Adef start():
2605N/A # verify paths are valid
2605N/A for f in sys.argv[2:4]:
2605N/A if not os.path.exists(f) or not os.access(f, os.R_OK):
2605N/A print '%s does not exist or is not readable' % f
2605N/A return smf_include.SMF_EXIT_ERR_CONFIG
2605N/A
2605N/A cmd = "/usr/lib/neutron/neutron-dhcp-agent --config-file %s " \
2605N/A "--config-file %s" % tuple(sys.argv[2:4])
2605N/A smf_include.smf_subprocess(cmd)
2605N/A
2900N/A
2900N/Adef stop():
2900N/A try:
2900N/A # first kill the SMF contract
2900N/A check_call(["/usr/bin/pkill", "-c", sys.argv[2]])
2900N/A except CalledProcessError as err:
2900N/A print "failed to kill the SMF contract: %s" % err
2900N/A return smf_include.SMF_EXIT_ERR_FATAL
2900N/A
2900N/A cmd = ["/usr/sbin/ipadm", "show-if", "-p", "-o", "ifname"]
2900N/A p = Popen(cmd, stdout=PIPE, stderr=PIPE)
2900N/A output, error = p.communicate()
2900N/A if p.returncode != 0:
2900N/A print "failed to retrieve IP interface names"
2900N/A return smf_include.SMF_EXIT_ERR_FATAL
2900N/A
2900N/A ifnames = output.splitlines()
2900N/A # DHCP agent datalinks are always 15 characters in length. They start with
2900N/A # 'evs', end with '_0', and in between they are hexadecimal digits.
2900N/A prog = re.compile('evs[0-9A-Fa-f\_]{10}_0')
2900N/A for ifname in ifnames:
2900N/A if not prog.search(ifname):
2900N/A continue
2900N/A
2900N/A try:
2900N/A # first remove the IP
2900N/A check_call(["/usr/bin/pfexec", "/usr/sbin/ipadm", "delete-ip",
2900N/A ifname])
2900N/A # next remove the VNIC
2900N/A check_call(["/usr/bin/pfexec", "/usr/sbin/dladm", "delete-vnic",
2900N/A ifname])
2900N/A except CalledProcessError as err:
2900N/A print "failed to remove datalinks used by DHCP agent: %s" % err
2900N/A return smf_include.SMF_EXIT_ERR_FATAL
2900N/A return smf_include.SMF_EXIT_OK
2900N/A
2605N/Aif __name__ == "__main__":
2605N/A os.putenv("LC_ALL", "C")
2605N/A smf_include.smf_main()