Exemplo n.º 1
0
    def execute(self, service, shared_data):
        if CONF.ntp_use_dhcp_config:
            osutils = osutils_factory.get_os_utils()
            dhcp_hosts = osutils.get_dhcp_hosts_in_use()

            ntp_option_data = None

            for (_, dhcp_host) in dhcp_hosts:
                options_data = dhcp.get_dhcp_options(dhcp_host,
                                                     [dhcp.OPTION_NTP_SERVERS])
                if options_data:
                    ntp_option_data = options_data.get(dhcp.OPTION_NTP_SERVERS)
                    if ntp_option_data:
                        break

            if not ntp_option_data:
                LOG.debug("Could not obtain the NTP configuration via DHCP")
                return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False)

            ntp_hosts = self._unpack_ntp_hosts(ntp_option_data)

            self.verify_time_service(osutils)
            osutils.set_ntp_client_config(ntp_hosts)

            LOG.info('NTP client configured. Server(s): %s' % ntp_hosts)

        return (base.PLUGIN_EXECUTION_DONE, False)
Exemplo n.º 2
0
    def test_get_dhcp_options(self, mock_parse_dhcp_reply,
                              mock_get_dhcp_request_data,
                              mock_get_mac_address_by_local_ip, mock_socket,
                              mock_randint):
        mock_randint.return_value = 'fake int'
        mock_socket().getsockname.return_value = ['fake local ip']
        mock_get_mac_address_by_local_ip.return_value = 'fake mac'
        mock_get_dhcp_request_data.return_value = 'fake data'
        mock_parse_dhcp_reply.return_value = (True, 'fake replied options')

        response = dhcp.get_dhcp_options(
            dhcp_host='fake host', requested_options=['fake option'])

        mock_randint.assert_called_once_with(0, 2 ** 32 - 1)
        mock_socket.assert_called_with(socket.AF_INET, socket.SOCK_DGRAM)
        mock_socket().setsockopt.assert_called_once_with(
            socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        mock_socket().bind.assert_called_once_with(('', 68))
        mock_socket().settimeout.assert_called_once_with(5)
        mock_socket().getsockname.assert_called_once_with()
        mock_get_mac_address_by_local_ip.assert_called_once_with(
            'fake local ip')
        mock_get_dhcp_request_data.assert_called_once_with('fake int',
                                                           'fake mac',
                                                           ['fake option'],
                                                           'cloudbase-init')
        mock_socket().sendto.assert_called_once_with(
            'fake data', ('fake host', 67))
        mock_socket().recv.assert_called_once_with(1024)
        mock_parse_dhcp_reply.assert_called_once_with(mock_socket().recv(),
                                                      'fake int')
        mock_socket().close.assert_called_once_with()
        self.assertEqual('fake replied options', response)
Exemplo n.º 3
0
    def execute(self, service, shared_data):
        reboot_required = False
        osutils = osutils_factory.get_os_utils()

        if osutils.is_real_time_clock_utc() != CONF.real_time_clock_utc:
            osutils.set_real_time_clock_utc(CONF.real_time_clock_utc)
            LOG.info('RTC set to UTC: %s', CONF.real_time_clock_utc)
            reboot_required = True

        if CONF.ntp_enable_service:
            self.verify_time_service(osutils)
            LOG.info('NTP client service enabled')

        if CONF.ntp_use_dhcp_config:
            dhcp_hosts = osutils.get_dhcp_hosts_in_use()

            ntp_option_data = None

            for (_, _, dhcp_host) in dhcp_hosts:
                options_data = dhcp.get_dhcp_options(dhcp_host,
                                                     [dhcp.OPTION_NTP_SERVERS])
                if options_data:
                    ntp_option_data = options_data.get(dhcp.OPTION_NTP_SERVERS)
                    if ntp_option_data:
                        break

            if not ntp_option_data:
                LOG.debug("Could not obtain the NTP configuration via DHCP")
                return base.PLUGIN_EXECUTE_ON_NEXT_BOOT, reboot_required

            ntp_hosts = self._unpack_ntp_hosts(ntp_option_data)
            osutils.set_ntp_client_config(ntp_hosts)
            LOG.info('NTP client configured. Server(s): %s' % ntp_hosts)

        return base.PLUGIN_EXECUTION_DONE, reboot_required
Exemplo n.º 4
0
    def execute(self, service, shared_data):
        if CONF.ntp_use_dhcp_config:
            osutils = osutils_factory.get_os_utils()
            dhcp_hosts = osutils.get_dhcp_hosts_in_use()

            ntp_option_data = None

            for (mac_address, dhcp_host) in dhcp_hosts:
                options_data = dhcp.get_dhcp_options(dhcp_host,
                                                     [dhcp.OPTION_NTP_SERVERS])
                if options_data:
                    ntp_option_data = options_data.get(dhcp.OPTION_NTP_SERVERS)
                    if ntp_option_data:
                        break

            if not ntp_option_data:
                LOG.debug("Could not obtain the NTP configuration via DHCP")
                return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False)

            # TODO(alexpilotti): support multiple NTP servers
            ntp_host = socket.inet_ntoa(ntp_option_data[:4])

            self._check_w32time_svc_status(osutils)
            osutils.set_ntp_client_config(ntp_host)

            LOG.info('NTP client configured. Server: %s' % ntp_host)

        return (base.PLUGIN_EXECUTION_DONE, False)
Exemplo n.º 5
0
    def execute(self, service, shared_data):
        if CONF.ntp_use_dhcp_config:
            osutils = osutils_factory.get_os_utils()
            dhcp_hosts = osutils.get_dhcp_hosts_in_use()

            ntp_option_data = None

            for (_, dhcp_host) in dhcp_hosts:
                options_data = dhcp.get_dhcp_options(dhcp_host,
                                                     [dhcp.OPTION_NTP_SERVERS])
                if options_data:
                    ntp_option_data = options_data.get(dhcp.OPTION_NTP_SERVERS)
                    if ntp_option_data:
                        break

            if not ntp_option_data:
                LOG.debug("Could not obtain the NTP configuration via DHCP")
                return base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False

            ntp_hosts = self._unpack_ntp_hosts(ntp_option_data)

            self.verify_time_service(osutils)
            osutils.set_ntp_client_config(ntp_hosts)

            LOG.info('NTP client configured. Server(s): %s' % ntp_hosts)

        return base.PLUGIN_EXECUTION_DONE, False
Exemplo n.º 6
0
    def execute(self, service, shared_data):
        reboot_required = False
        osutils = osutils_factory.get_os_utils()

        if osutils.is_real_time_clock_utc() != CONF.real_time_clock_utc:
            osutils.set_real_time_clock_utc(CONF.real_time_clock_utc)
            LOG.info('RTC set to UTC: %s', CONF.real_time_clock_utc)
            reboot_required = True

        if CONF.ntp_enable_service:
            self.verify_time_service(osutils)
            LOG.info('NTP client service enabled')

        if CONF.ntp_use_dhcp_config:
            dhcp_hosts = osutils.get_dhcp_hosts_in_use()

            ntp_option_data = None

            for (_, dhcp_host) in dhcp_hosts:
                options_data = dhcp.get_dhcp_options(dhcp_host,
                                                     [dhcp.OPTION_NTP_SERVERS])
                if options_data:
                    ntp_option_data = options_data.get(dhcp.OPTION_NTP_SERVERS)
                    if ntp_option_data:
                        break

            if not ntp_option_data:
                LOG.debug("Could not obtain the NTP configuration via DHCP")
                return base.PLUGIN_EXECUTE_ON_NEXT_BOOT, reboot_required

            ntp_hosts = self._unpack_ntp_hosts(ntp_option_data)
            osutils.set_ntp_client_config(ntp_hosts)
            LOG.info('NTP client configured. Server(s): %s' % ntp_hosts)

        return base.PLUGIN_EXECUTION_DONE, reboot_required
Exemplo n.º 7
0
    def test_get_dhcp_options(self, mock_parse_dhcp_reply,
                              mock_get_dhcp_request_data,
                              mock_get_mac_address_by_local_ip, mock_socket,
                              mock_randint):
        mock_randint.return_value = 'fake int'
        mock_socket().getsockname.return_value = ['fake local ip']
        mock_get_mac_address_by_local_ip.return_value = 'fake mac'
        mock_get_dhcp_request_data.return_value = 'fake data'
        mock_parse_dhcp_reply.return_value = (True, 'fake replied options')

        response = dhcp.get_dhcp_options(
            dhcp_host='fake host', requested_options=['fake option'])

        mock_randint.assert_called_once_with(0, 2 ** 32 - 1)
        mock_socket.assert_called_with(socket.AF_INET, socket.SOCK_DGRAM)
        mock_socket().bind.assert_called_once_with(('', 68))
        mock_socket().settimeout.assert_called_once_with(5)
        mock_socket().connect.assert_called_once_with(('fake host', 67))
        mock_socket().getsockname.assert_called_once_with()
        mock_get_mac_address_by_local_ip.assert_called_once_with(
            'fake local ip')
        mock_get_dhcp_request_data.assert_called_once_with('fake int',
                                                           'fake mac',
                                                           ['fake option'],
                                                           'cloudbase-init')
        mock_socket().send.assert_called_once_with('fake data')
        mock_socket().recv.assert_called_once_with(1024)
        mock_parse_dhcp_reply.assert_called_once_with(mock_socket().recv(),
                                                      'fake int')
        mock_socket().close.assert_called_once_with()
        self.assertEqual('fake replied options', response)
Exemplo n.º 8
0
    def execute(self, service, shared_data):
        if CONF.ntp_use_dhcp_config:
            osutils = osutils_factory.get_os_utils()
            dhcp_hosts = osutils.get_dhcp_hosts_in_use()

            ntp_option_data = None

            for (mac_address, dhcp_host) in dhcp_hosts:
                options_data = dhcp.get_dhcp_options(dhcp_host,
                                                     [dhcp.OPTION_NTP_SERVERS])
                if options_data:
                    ntp_option_data = options_data.get(dhcp.OPTION_NTP_SERVERS)
                    if ntp_option_data:
                        break

            if not ntp_option_data:
                LOG.debug("Could not obtain the NTP configuration via DHCP")
                return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False)

            # TODO(alexpilotti): support multiple NTP servers
            ntp_host = socket.inet_ntoa(ntp_option_data[:4])

            self._check_w32time_svc_status(osutils)
            osutils.set_ntp_client_config(ntp_host)

            LOG.info('NTP client configured. Server: %s' % ntp_host)

        return (base.PLUGIN_EXECUTION_DONE, False)
Exemplo n.º 9
0
    def execute(self, service, shared_data):
        if CONF.mtu_use_dhcp_config:
            osutils = osutils_factory.get_os_utils()
            dhcp_hosts = osutils.get_dhcp_hosts_in_use()

            for (mac_address, dhcp_host) in dhcp_hosts:
                options_data = dhcp.get_dhcp_options(dhcp_host, [dhcp.OPTION_MTU])
                if options_data:
                    mtu_option_data = options_data.get(dhcp.OPTION_MTU)
                    if mtu_option_data:
                        mtu = struct.unpack("!H", mtu_option_data)[0]
                        osutils.set_network_adapter_mtu(mac_address, mtu)
                    else:
                        LOG.debug("Could not obtain the MTU configuration " 'via DHCP for interface "%s"' % mac_address)

        return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False)
Exemplo n.º 10
0
    def _get_wire_server_endpoint_address(self):
        total_time = 300
        poll_time = 5
        retries = total_time / poll_time

        while True:
            try:
                options = dhcp.get_dhcp_options()
                endpoint = (options or {}).get(WIRESERVER_DHCP_OPTION)
                if not endpoint:
                    raise exception.MetadaNotFoundException(
                        "Cannot find Azure WireServer endpoint address")
                return socket.inet_ntoa(endpoint)
            except Exception:
                if not retries:
                    raise
                time.sleep(poll_time)
                retries -= 1
Exemplo n.º 11
0
    def execute(self, service, shared_data):
        if CONF.mtu_use_dhcp_config:
            osutils = osutils_factory.get_os_utils()
            dhcp_hosts = osutils.get_dhcp_hosts_in_use()

            for (mac_address, dhcp_host) in dhcp_hosts:
                options_data = dhcp.get_dhcp_options(dhcp_host,
                                                     [dhcp.OPTION_MTU])
                if options_data:
                    mtu_option_data = options_data.get(dhcp.OPTION_MTU)
                    if mtu_option_data:
                        mtu = struct.unpack('!H', mtu_option_data)[0]
                        osutils.set_network_adapter_mtu(mac_address, mtu)
                    else:
                        LOG.debug('Could not obtain the MTU configuration '
                                  'via DHCP for interface "%s"' % mac_address)

        return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False)
Exemplo n.º 12
0
    def _get_wire_server_endpoint_address(self):
        total_time = 300
        poll_time = 5
        retries = total_time / poll_time

        while True:
            try:
                options = dhcp.get_dhcp_options()
                endpoint = (options or {}).get(WIRESERVER_DHCP_OPTION)
                if not endpoint:
                    raise exception.MetadaNotFoundException(
                        "Cannot find Azure WireServer endpoint address")
                return socket.inet_ntoa(endpoint)
            except Exception:
                if not retries:
                    raise
                time.sleep(poll_time)
                retries -= 1
Exemplo n.º 13
0
 def test_get_dhcp_options_timeout(self, mock_client_socket):
     mock_client_socket.side_effect = [socket.timeout]
     dhcp.get_dhcp_options(dhcp_host='fake host',
                           requested_options=['fake option'])
Exemplo n.º 14
0
 def test_get_dhcp_options_timeout(self, mock_client_socket):
     mock_client_socket.side_effect = [socket.timeout]
     dhcp.get_dhcp_options(dhcp_host='fake host',
                           requested_options=['fake option'])