components/openstack/neutron/files/agent/solaris/dhcp.py
author Girish Moodalbail <Girish.Moodalbail@oracle.COM>
Mon, 08 Dec 2014 15:34:49 -0800
changeset 3524 ad6a9e0880b9
parent 1987 6fa18b7a0af6
child 3998 5bd484384122
permissions -rw-r--r--
20089330 Horizon reports "Error: Unable to contact Neutron" on large data set 20103259 unable to delete instance, reports "EVS controller: vport in use" 19970607 disabling neutron smf services does not remove services' vports 18091479 unable delete a port that was associated with incomplete zone
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
     1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
     2
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
     3
# Copyright 2012 OpenStack Foundation
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
     4
# All Rights Reserved.
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
     5
#
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
     6
# Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
     7
#
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
     8
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
     9
#    not use this file except in compliance with the License. You may obtain
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    10
#    a copy of the License at
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    11
#
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    12
#         http://www.apache.org/licenses/LICENSE-2.0
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    13
#
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    14
#    Unless required by applicable law or agreed to in writing, software
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    15
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    16
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    17
#    License for the specific language governing permissions and limitations
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    18
#    under the License.
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    19
#
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    20
# @author: Girish Moodalbail, Oracle, Inc.
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    21
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    22
import abc
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    23
import os
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    24
import re
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    25
import shutil
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    26
import socket
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    27
import StringIO
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    28
import sys
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    29
import uuid
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    30
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    31
import netaddr
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    32
from oslo.config import cfg
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    33
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    34
from neutron.agent.linux import utils
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    35
from neutron.agent.solaris import net_lib
3524
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
    36
from neutron.common import constants
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    37
from neutron.common import exceptions
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    38
from neutron.openstack.common import importutils
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    39
from neutron.openstack.common import jsonutils
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    40
from neutron.openstack.common import log as logging
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    41
from neutron.openstack.common import uuidutils
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    42
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    43
LOG = logging.getLogger(__name__)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    44
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    45
OPTS = [
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    46
    cfg.StrOpt('dhcp_confs',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    47
               default='$state_path/dhcp',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    48
               help=_('Location to store DHCP server config files')),
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    49
    cfg.StrOpt('dhcp_domain',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    50
               default='openstacklocal',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    51
               help=_('Domain to use for building the hostnames')),
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    52
    cfg.StrOpt('dnsmasq_config_file',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    53
               default='',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    54
               help=_('Override the default dnsmasq settings with this file')),
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    55
    cfg.StrOpt('dnsmasq_dns_server',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    56
               help=_('Use another DNS server before any in '
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    57
                      '/etc/resolv.conf.')),
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    58
    cfg.IntOpt(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    59
        'dnsmasq_lease_max',
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    60
        default=(2 ** 24),
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    61
        help=_('Limit number of leases to prevent a denial-of-service.')),
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    62
    cfg.StrOpt('interface_driver',
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    63
               help=_("The driver used to manage the virtual interface.")),
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    64
]
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    65
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    66
IPV4 = 4
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    67
IPV6 = 6
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    68
UDP = 'udp'
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    69
TCP = 'tcp'
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    70
DNS_PORT = 53
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
    71
DHCPV4_PORT = 67
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    72
DHCPV6_PORT = 547
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    73
METADATA_DEFAULT_PREFIX = 16
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    74
METADATA_DEFAULT_IP = '169.254.169.254'
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    75
METADATA_DEFAULT_CIDR = '%s/%d' % (METADATA_DEFAULT_IP,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    76
                                   METADATA_DEFAULT_PREFIX)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    77
METADATA_PORT = 80
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    78
WIN2k3_STATIC_DNS = 249
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    79
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    80
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    81
class DictModel(object):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    82
    """Convert dict into an object that provides attribute access to values."""
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    83
    def __init__(self, d):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    84
        for key, value in d.iteritems():
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    85
            if isinstance(value, list):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    86
                value = [DictModel(item) if isinstance(item, dict) else item
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    87
                         for item in value]
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    88
            elif isinstance(value, dict):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    89
                value = DictModel(value)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    90
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    91
            setattr(self, key, value)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    92
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    93
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    94
class NetModel(DictModel):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    95
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    96
    def __init__(self, use_namespaces, d):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    97
        super(NetModel, self).__init__(d)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    98
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
    99
        self._ns_name = None
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   100
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   101
    @property
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   102
    def namespace(self):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   103
        return self._ns_name
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   104
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   105
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   106
class DhcpBase(object):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   107
    __metaclass__ = abc.ABCMeta
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   108
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   109
    def __init__(self, conf, network, root_helper='sudo',
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   110
                 version=None, plugin=None):
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   111
        self.conf = conf
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   112
        self.network = network
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   113
        self.root_helper = None
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   114
        self.device_manager = DeviceManager(self.conf,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   115
                                            self.root_helper, plugin)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   116
        self.version = version
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   117
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   118
    @abc.abstractmethod
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   119
    def enable(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   120
        """Enables DHCP for this network."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   121
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   122
    @abc.abstractmethod
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   123
    def disable(self, retain_port=False):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   124
        """Disable dhcp for this network."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   125
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   126
    def restart(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   127
        """Restart the dhcp service for the network."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   128
        self.disable(retain_port=True)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   129
        self.enable()
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   130
        self.device_manager.update(self.network)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   131
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   132
    @abc.abstractproperty
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   133
    def active(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   134
        """Boolean representing the running state of the DHCP server."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   135
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   136
    @abc.abstractmethod
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   137
    def release_lease(self, mac_address, removed_ips):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   138
        """Release a DHCP lease."""
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   139
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   140
    @abc.abstractmethod
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   141
    def reload_allocations(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   142
        """Force the DHCP server to reload the assignment database."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   143
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   144
    @classmethod
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   145
    def existing_dhcp_networks(cls, conf, root_helper):
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   146
        """Return a list of existing networks ids that we have configs for."""
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   147
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   148
        raise NotImplementedError
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   149
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   150
    @classmethod
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   151
    def check_version(cls):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   152
        """Execute version checks on DHCP server."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   153
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   154
        raise NotImplementedError
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   155
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   156
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   157
class DhcpLocalProcess(DhcpBase):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   158
    PORTS = []
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   159
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   160
    def _enable_dhcp(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   161
        """check if there is a subnet within the network with dhcp enabled."""
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   162
        for subnet in self.network.subnets:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   163
            if subnet.enable_dhcp:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   164
                return True
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   165
        return False
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   166
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   167
    def enable(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   168
        """Enables DHCP for this network by spawning a local process."""
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   169
        interface_name = self.device_manager.setup(self.network,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   170
                                                   reuse_existing=True)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   171
        if self.active:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   172
            self.restart()
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   173
        elif self._enable_dhcp():
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   174
            self.interface_name = interface_name
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   175
            self.spawn_process()
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   176
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   177
    def disable(self, retain_port=False):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   178
        """Disable DHCP for this network by killing the local process."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   179
        pid = self.pid
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   180
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   181
        if self.active:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   182
            cmd = ['kill', '-9', pid]
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   183
            utils.execute(cmd, self.root_helper)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   184
            if not retain_port:
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   185
                self.device_manager.destroy(self.network, self.interface_name)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   186
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   187
        elif pid:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   188
            LOG.debug(_('DHCP for %(net_id)s pid %(pid)d is stale, ignoring '
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   189
                        'command'), {'net_id': self.network.id, 'pid': pid})
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   190
        else:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   191
            LOG.debug(_('No DHCP started for %s'), self.network.id)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   192
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   193
        self._remove_config_files()
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   194
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   195
    def _remove_config_files(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   196
        confs_dir = os.path.abspath(os.path.normpath(self.conf.dhcp_confs))
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   197
        conf_dir = os.path.join(confs_dir, self.network.id)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   198
        shutil.rmtree(conf_dir, ignore_errors=True)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   199
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   200
    def get_conf_file_name(self, kind, ensure_conf_dir=False):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   201
        """Returns the file name for a given kind of config file."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   202
        confs_dir = os.path.abspath(os.path.normpath(self.conf.dhcp_confs))
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   203
        conf_dir = os.path.join(confs_dir, self.network.id)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   204
        if ensure_conf_dir:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   205
            if not os.path.isdir(conf_dir):
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   206
                os.makedirs(conf_dir, 0o755)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   207
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   208
        return os.path.join(conf_dir, kind)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   209
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   210
    def _get_value_from_conf_file(self, kind, converter=None):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   211
        """A helper function to read a value from one of the state files."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   212
        file_name = self.get_conf_file_name(kind)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   213
        msg = _('Error while reading %s')
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   214
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   215
        try:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   216
            with open(file_name, 'r') as f:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   217
                try:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   218
                    return converter and converter(f.read()) or f.read()
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   219
                except ValueError:
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   220
                    msg = _('Unable to convert value in %s')
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   221
        except IOError:
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   222
            msg = _('Unable to access %s')
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   223
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   224
        LOG.debug(msg % file_name)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   225
        return None
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   226
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   227
    @property
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   228
    def pid(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   229
        """Last known pid for the DHCP process spawned for this network."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   230
        return self._get_value_from_conf_file('pid', int)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   231
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   232
    @property
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   233
    def active(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   234
        pid = self.pid
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   235
        if pid is None:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   236
            return False
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   237
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   238
        cmd = ['/usr/bin/pargs', pid]
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   239
        try:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   240
            return self.network.id in utils.execute(cmd)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   241
        except RuntimeError:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   242
            return False
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   243
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   244
    @property
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   245
    def interface_name(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   246
        return self._get_value_from_conf_file('interface')
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   247
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   248
    @interface_name.setter
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   249
    def interface_name(self, value):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   250
        interface_file_path = self.get_conf_file_name('interface',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   251
                                                      ensure_conf_dir=True)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   252
        utils.replace_file(interface_file_path, value)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   253
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   254
    @abc.abstractmethod
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   255
    def spawn_process(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   256
        pass
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   257
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   258
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   259
class Dnsmasq(DhcpLocalProcess):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   260
    # The ports that need to be opened when security policies are active
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   261
    # on the Neutron port used for DHCP.  These are provided as a convenience
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   262
    # for users of this class.
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   263
    PORTS = {IPV4: [(UDP, DNS_PORT), (TCP, DNS_PORT), (UDP, DHCPV4_PORT)],
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   264
             IPV6: [(UDP, DNS_PORT), (TCP, DNS_PORT), (UDP, DHCPV6_PORT)],
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   265
             }
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   266
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   267
    _TAG_PREFIX = 'tag%d'
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   268
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   269
    NEUTRON_NETWORK_ID_KEY = 'NEUTRON_NETWORK_ID'
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   270
    NEUTRON_RELAY_SOCKET_PATH_KEY = 'NEUTRON_RELAY_SOCKET_PATH'
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   271
    MINIMUM_VERSION = 2.59
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   272
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   273
    @classmethod
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   274
    def check_version(cls):
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   275
        ver = 0
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   276
        try:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   277
            cmd = ['/usr/lib/inet/dnsmasq', '--version']
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   278
            out = utils.execute(cmd)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   279
            ver = re.findall("\d+.\d+", out)[0]
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   280
            is_valid_version = float(ver) >= cls.MINIMUM_VERSION
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   281
            # For Solaris, we rely on the packaging system to ensure a
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   282
            # matching/supported version of dnsmasq.
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   283
            if not is_valid_version:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   284
                LOG.warning(_('FAILED VERSION REQUIREMENT FOR DNSMASQ. '
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   285
                              'DHCP AGENT MAY NOT RUN CORRECTLY! '
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   286
                              'Please ensure that its version is %s '
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   287
                              'or above!'), cls.MINIMUM_VERSION)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   288
        except (OSError, RuntimeError, IndexError, ValueError):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   289
            LOG.warning(_('Unable to determine dnsmasq version. '
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   290
                          'Please ensure that its version is %s '
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   291
                          'or above!'), cls.MINIMUM_VERSION)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   292
        return float(ver)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   293
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   294
    @classmethod
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   295
    def existing_dhcp_networks(cls, conf, root_helper):
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   296
        """Return a list of existing networks ids that we have configs for."""
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   297
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   298
        confs_dir = os.path.abspath(os.path.normpath(conf.dhcp_confs))
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   299
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   300
        return [
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   301
            c for c in os.listdir(confs_dir)
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   302
            if uuidutils.is_uuid_like(c)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   303
        ]
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   304
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   305
    def spawn_process(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   306
        """Spawns a Dnsmasq process for the network."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   307
        env = {
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   308
            self.NEUTRON_NETWORK_ID_KEY: self.network.id,
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   309
        }
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   310
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   311
        cmd = [
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   312
            '/usr/lib/inet/dnsmasq',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   313
            '--no-hosts',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   314
            '--no-resolv',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   315
            '--strict-order',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   316
            '--bind-interfaces',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   317
            '--interface=%s' % self.interface_name,
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   318
            '--except-interface=lo0',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   319
            '--pid-file=%s' % self.get_conf_file_name(
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   320
                'pid', ensure_conf_dir=True),
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   321
            '--dhcp-hostsfile=%s' % self._output_hosts_file(),
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   322
            '--dhcp-optsfile=%s' % self._output_opts_file(),
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   323
            '--leasefile-ro',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   324
        ]
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   325
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   326
        possible_leases = 0
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   327
        for i, subnet in enumerate(self.network.subnets):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   328
            # if a subnet is specified to have dhcp disabled
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   329
            if not subnet.enable_dhcp:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   330
                continue
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   331
            if subnet.ip_version == 4:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   332
                mode = 'static'
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   333
            else:
1872
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   334
                # TODO(gmoodalb): how do we indicate other options
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   335
                # ra-only, slaac, ra-nameservers, and ra-stateless.
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   336
                # We need to also set the DUID for the DHCPv6 server to use
1872
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   337
                macaddr_cmd = ['/usr/sbin/dladm', 'show-linkprop',
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   338
                               '-co', 'value', '-p', 'mac-address',
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   339
                               self.interface_name]
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   340
                stdout = utils.execute(macaddr_cmd)
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   341
                uid = stdout.splitlines()[0].strip()
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   342
                # IANA assigned ID for Oracle
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   343
                enterprise_id = '111'
0b81e3d9f3ae 18698425 DHCP and L3 agent are unable to plumb VNICs with IPv6 address
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1760
diff changeset
   344
                cmd.append('--dhcp-duid=%s,%s' % (enterprise_id, uid))
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   345
                mode = 'static'
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   346
            if self.version >= self.MINIMUM_VERSION:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   347
                set_tag = 'set:'
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   348
            else:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   349
                set_tag = ''
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   350
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   351
            cidr = netaddr.IPNetwork(subnet.cidr)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   352
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   353
            cmd.append('--dhcp-range=%s%s,%s,%s,%ss' %
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   354
                       (set_tag, self._TAG_PREFIX % i,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   355
                        cidr.network,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   356
                        mode,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   357
                        self.conf.dhcp_lease_duration))
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   358
            possible_leases += cidr.size
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   359
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   360
        # Cap the limit because creating lots of subnets can inflate
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   361
        # this possible lease cap.
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   362
        cmd.append('--dhcp-lease-max=%d' %
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   363
                   min(possible_leases, self.conf.dnsmasq_lease_max))
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   364
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   365
        cmd.append('--conf-file=%s' % self.conf.dnsmasq_config_file)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   366
        if self.conf.dnsmasq_dns_server:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   367
            cmd.append('--server=%s' % self.conf.dnsmasq_dns_server)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   368
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   369
        if self.conf.dhcp_domain:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   370
            cmd.append('--domain=%s' % self.conf.dhcp_domain)
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   371
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   372
        # TODO(gmoodalb): prepend the env vars before command
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   373
        utils.execute(cmd, self.root_helper)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   374
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   375
    def release_lease(self, mac_address, removed_ips):
1987
6fa18b7a0af6 19158668 associating a new floating ip removes existing ipnat rules and re-adds them
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1944
diff changeset
   376
        """Release a DHCP lease."""
6fa18b7a0af6 19158668 associating a new floating ip removes existing ipnat rules and re-adds them
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1944
diff changeset
   377
        for ip in removed_ips or []:
6fa18b7a0af6 19158668 associating a new floating ip removes existing ipnat rules and re-adds them
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1944
diff changeset
   378
            cmd = ['/usr/lib/inet/dhcp_release', self.interface_name,
6fa18b7a0af6 19158668 associating a new floating ip removes existing ipnat rules and re-adds them
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1944
diff changeset
   379
                   ip, mac_address]
6fa18b7a0af6 19158668 associating a new floating ip removes existing ipnat rules and re-adds them
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1944
diff changeset
   380
            utils.execute(cmd, self.root_helper)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   381
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   382
    def reload_allocations(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   383
        """Rebuild the dnsmasq config and signal the dnsmasq to reload."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   384
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   385
        # If all subnets turn off dhcp, kill the process.
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   386
        if not self._enable_dhcp():
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   387
            self.disable()
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   388
            LOG.debug(_('Killing dhcpmasq for network since all subnets have '
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   389
                        'turned off DHCP: %s'), self.network.id)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   390
            return
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   391
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   392
        self._output_hosts_file()
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   393
        self._output_opts_file()
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   394
        if self.active:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   395
            cmd = ['kill', '-HUP', self.pid]
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   396
            utils.execute(cmd, self.root_helper)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   397
        else:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   398
            LOG.debug(_('Pid %d is stale, relaunching dnsmasq'), self.pid)
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   399
        LOG.debug(_('Reloading allocations for network: %s'), self.network.id)
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   400
        self.device_manager.update(self.network)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   401
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   402
    def _output_hosts_file(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   403
        """Writes a dnsmasq compatible hosts file."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   404
        r = re.compile('[:.]')
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   405
        buf = StringIO.StringIO()
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   406
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   407
        for port in self.network.ports:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   408
            for alloc in port.fixed_ips:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   409
                name = 'host-%s.%s' % (r.sub('-', alloc.ip_address),
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   410
                                       self.conf.dhcp_domain)
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   411
                set_tag = ''
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   412
                if getattr(port, 'extra_dhcp_opts', False):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   413
                    if self.version >= self.MINIMUM_VERSION:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   414
                        set_tag = 'set:'
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   415
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   416
                    buf.write('%s,%s,%s,%s%s\n' %
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   417
                              (port.mac_address, name, alloc.ip_address,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   418
                               set_tag, port.id))
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   419
                else:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   420
                    buf.write('%s,%s,%s\n' %
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   421
                              (port.mac_address, name, alloc.ip_address))
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   422
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   423
        name = self.get_conf_file_name('host')
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   424
        utils.replace_file(name, buf.getvalue())
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   425
        return name
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   426
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   427
    def _output_opts_file(self):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   428
        """Write a dnsmasq compatible options file."""
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   429
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   430
        if self.conf.enable_isolated_metadata:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   431
            subnet_to_interface_ip = self._make_subnet_interface_ip_map()
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   432
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   433
        options = []
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   434
        for i, subnet in enumerate(self.network.subnets):
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   435
            if not subnet.enable_dhcp:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   436
                continue
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   437
            if subnet.dns_nameservers:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   438
                options.append(
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   439
                    self._format_option(i, 'dns-server',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   440
                                        ','.join(subnet.dns_nameservers)))
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   441
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   442
            gateway = subnet.gateway_ip
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   443
            host_routes = []
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   444
            for hr in subnet.host_routes:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   445
                if hr.destination == "0.0.0.0/0":
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   446
                    gateway = hr.nexthop
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   447
                else:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   448
                    host_routes.append("%s,%s" % (hr.destination, hr.nexthop))
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   449
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   450
            # Add host routes for isolated network segments
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   451
            enable_metadata = (
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   452
                self.conf.enable_isolated_metadata
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   453
                and not subnet.gateway_ip
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   454
                and subnet.ip_version == 4)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   455
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   456
            if enable_metadata:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   457
                subnet_dhcp_ip = subnet_to_interface_ip[subnet.id]
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   458
                host_routes.append(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   459
                    '%s/32,%s' % (METADATA_DEFAULT_IP, subnet_dhcp_ip)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   460
                )
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   461
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   462
            if host_routes:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   463
                options.append(
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   464
                    self._format_option(i, 'classless-static-route',
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   465
                                        ','.join(host_routes)))
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   466
                options.append(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   467
                    self._format_option(i, WIN2k3_STATIC_DNS,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   468
                                        ','.join(host_routes)))
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   469
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   470
            if subnet.ip_version == 4:
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   471
                if gateway:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   472
                    options.append(self._format_option(i, 'router', gateway))
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   473
                else:
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   474
                    options.append(self._format_option(i, 'router'))
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   475
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   476
        for port in self.network.ports:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   477
            if getattr(port, 'extra_dhcp_opts', False):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   478
                options.extend(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   479
                    self._format_option(port.id, opt.opt_name, opt.opt_value)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   480
                    for opt in port.extra_dhcp_opts)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   481
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   482
        name = self.get_conf_file_name('opts')
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   483
        utils.replace_file(name, '\n'.join(options))
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   484
        return name
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   485
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   486
    def _make_subnet_interface_ip_map(self):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   487
        # TODO(gmoodalb): need to complete this when we support metadata
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   488
        pass
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   489
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   490
    def _format_option(self, tag, option, *args):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   491
        """Format DHCP option by option name or code."""
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   492
        if self.version >= self.MINIMUM_VERSION:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   493
            set_tag = 'tag:'
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   494
        else:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   495
            set_tag = ''
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   496
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   497
        option = str(option)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   498
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   499
        if isinstance(tag, int):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   500
            tag = self._TAG_PREFIX % tag
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   501
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   502
        if not option.isdigit():
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   503
            option = 'option:%s' % option
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   504
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   505
        return ','.join((set_tag + tag, '%s' % option) + args)
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   506
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   507
    @classmethod
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   508
    def lease_update(cls):
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   509
        network_id = os.environ.get(cls.NEUTRON_NETWORK_ID_KEY)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   510
        dhcp_relay_socket = os.environ.get(cls.NEUTRON_RELAY_SOCKET_PATH_KEY)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   511
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   512
        action = sys.argv[1]
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   513
        if action not in ('add', 'del', 'old'):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   514
            sys.exit()
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   515
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   516
        mac_address = sys.argv[2]
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   517
        ip_address = sys.argv[3]
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   518
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   519
        if action == 'del':
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   520
            lease_remaining = 0
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   521
        else:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   522
            lease_remaining = int(os.environ.get('DNSMASQ_TIME_REMAINING', 0))
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   523
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   524
        data = dict(network_id=network_id, mac_address=mac_address,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   525
                    ip_address=ip_address, lease_remaining=lease_remaining)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   526
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   527
        if os.path.exists(dhcp_relay_socket):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   528
            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   529
            sock.connect(dhcp_relay_socket)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   530
            sock.send(jsonutils.dumps(data))
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   531
            sock.close()
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   532
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   533
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   534
class DeviceManager(object):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   535
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   536
    def __init__(self, conf, root_helper, plugin):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   537
        self.conf = conf
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   538
        self.root_helper = root_helper
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   539
        self.plugin = plugin
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   540
        if not conf.interface_driver:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   541
            raise SystemExit(_('You must specify an interface driver'))
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   542
        try:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   543
            self.driver = importutils.import_object(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   544
                conf.interface_driver, conf)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   545
        except Exception as e:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   546
            msg = (_("Error importing interface driver '%(driver)s': "
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   547
                   "%(inner)s") % {'driver': conf.interface_driver,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   548
                                   'inner': e})
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   549
            raise SystemExit(msg)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   550
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   551
    def get_interface_name(self, network, port):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   552
        """Return interface(device) name for use by the DHCP process."""
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   553
        return self.driver.get_device_name(port)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   554
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   555
    def get_device_id(self, network):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   556
        """Return a unique DHCP device ID for this host on the network."""
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   557
        # There could be more than one dhcp server per network, so create
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   558
        # a device id that combines host and network ids
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   559
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   560
        host_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, socket.gethostname())
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   561
        return 'dhcp%s-%s' % (host_uuid, network.id)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   562
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   563
    def setup_dhcp_port(self, network):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   564
        """Create/update DHCP port for the host if needed and return port."""
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   565
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   566
        device_id = self.get_device_id(network)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   567
        subnets = {}
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   568
        dhcp_enabled_subnet_ids = []
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   569
        for subnet in network.subnets:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   570
            if subnet.enable_dhcp:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   571
                dhcp_enabled_subnet_ids.append(subnet.id)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   572
                subnets[subnet.id] = subnet
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   573
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   574
        dhcp_port = None
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   575
        for port in network.ports:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   576
            port_device_id = getattr(port, 'device_id', None)
3524
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
   577
            port_device_owner = getattr(port, 'device_owner', None)
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
   578
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
   579
            # if the agent is started on a different node, then the
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
   580
            # device_ids will be different since they are based off
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
   581
            # hostname.
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
   582
            if (port_device_id == device_id or
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
   583
                    (port_device_owner == constants.DEVICE_OWNER_DHCP and
ad6a9e0880b9 20089330 Horizon reports "Error: Unable to contact Neutron" on large data set
Girish Moodalbail <Girish.Moodalbail@oracle.COM>
parents: 1987
diff changeset
   584
                     port_device_id.startswith('dhcp'))):
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   585
                port_fixed_ips = []
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   586
                for fixed_ip in port.fixed_ips:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   587
                    port_fixed_ips.append({'subnet_id': fixed_ip.subnet_id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   588
                                           'ip_address': fixed_ip.ip_address})
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   589
                    if fixed_ip.subnet_id in dhcp_enabled_subnet_ids:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   590
                        dhcp_enabled_subnet_ids.remove(fixed_ip.subnet_id)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   591
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   592
                # If there are dhcp_enabled_subnet_ids here that means that
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   593
                # we need to add those to the port and call update.
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   594
                if dhcp_enabled_subnet_ids:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   595
                    port_fixed_ips.extend(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   596
                        [dict(subnet_id=s) for s in dhcp_enabled_subnet_ids])
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   597
                    dhcp_port = self.plugin.update_dhcp_port(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   598
                        port.id, {'port': {'fixed_ips': port_fixed_ips}})
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   599
                else:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   600
                    dhcp_port = port
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   601
                # break since we found port that matches device_id
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   602
                break
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   603
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   604
        # DHCP port has not yet been created.
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   605
        if dhcp_port is None:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   606
            LOG.debug(_('DHCP port %(device_id)s on network %(network_id)s'
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   607
                        ' does not yet exist.'), {'device_id': device_id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   608
                                                  'network_id': network.id})
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   609
            port_dict = dict(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   610
                name='',
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   611
                admin_state_up=True,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   612
                device_id=device_id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   613
                network_id=network.id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   614
                tenant_id=network.tenant_id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   615
                fixed_ips=[dict(subnet_id=s) for s in dhcp_enabled_subnet_ids])
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   616
            dhcp_port = self.plugin.create_dhcp_port({'port': port_dict})
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   617
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   618
        # Convert subnet_id to subnet dict
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   619
        fixed_ips = [dict(subnet_id=fixed_ip.subnet_id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   620
                          ip_address=fixed_ip.ip_address,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   621
                          subnet=subnets[fixed_ip.subnet_id])
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   622
                     for fixed_ip in dhcp_port.fixed_ips]
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   623
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   624
        ips = [DictModel(item) if isinstance(item, dict) else item
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   625
               for item in fixed_ips]
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   626
        dhcp_port.fixed_ips = ips
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   627
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   628
        return dhcp_port
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   629
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   630
    def setup(self, network, reuse_existing=False):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   631
        """Create and initialize a device for network's DHCP on this host."""
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   632
        port = self.setup_dhcp_port(network)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   633
        interface_name = self.get_interface_name(network, port)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   634
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   635
        if net_lib.Datalink.datalink_exists(interface_name):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   636
            if not reuse_existing:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   637
                raise exceptions.PreexistingDeviceFailure(
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   638
                    dev_name=interface_name)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   639
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   640
                LOG.debug(_('Reusing existing device: %s.'), interface_name)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   641
        else:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   642
            self.driver.plug(network.tenant_id, network.id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   643
                             port.id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   644
                             interface_name)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   645
        ip_cidrs = []
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   646
        for fixed_ip in port.fixed_ips:
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   647
            subnet = fixed_ip.subnet
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   648
            net = netaddr.IPNetwork(subnet.cidr)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   649
            ip_cidr = '%s/%s' % (fixed_ip.ip_address, net.prefixlen)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   650
            ip_cidrs.append(ip_cidr)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   651
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   652
        if (self.conf.enable_isolated_metadata and self.conf.use_namespaces):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   653
            ip_cidrs.append(METADATA_DEFAULT_CIDR)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   654
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   655
        self.driver.init_l3(interface_name, ip_cidrs)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   656
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   657
        return interface_name
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   658
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   659
    def update(self, network):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   660
        """Update device settings for the network's DHCP on this host."""
1760
353323c7bdc1 PSARC/2013/350 OpenStack for Solaris (Umbrella)
Drew Fisher <drew.fisher@oracle.com>
parents:
diff changeset
   661
        pass
1944
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   662
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   663
    def destroy(self, network, device_name):
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   664
        """Destroy the device used for the network's DHCP on this host."""
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   665
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   666
        self.driver.fini_l3(device_name)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   667
        self.driver.unplug(device_name)
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   668
        self.plugin.release_dhcp_port(network.id,
56ac2df1785b PSARC/2014/207 OpenStack Glance Update to Havana
Drew Fisher <drew.fisher@oracle.com>
parents: 1872
diff changeset
   669
                                      self.get_device_id(network))