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