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