Esempio n. 1
0
    def _rest_call(self, action, uri, custom_headers=None, session=None):
        LOG.debug('Vyatta Router REST Request: {0} {1}'.format(action, uri))
        if session is None:
            session = requests

        auth = tuple(config.VROUTER.vrouter_credentials.split(':'))
        if len(auth) != 2:
            raise v_exc.InvalidParameter(
                cause=_("Invalid vrouter_credentials %s") % len(auth))

        headers = {'Accept': 'application/json', 'Content-Length': 0}

        if custom_headers:
            headers.update(custom_headers)

        try:
            uri = 'https://{0}{1}'.format(self.address, uri)
            return session.request(action,
                                   uri,
                                   auth=auth,
                                   headers=headers,
                                   verify=False)
        except requests.ConnectionError:
            LOG.error(
                _LE('Vyatta vRouter REST API: '
                    'Could not establish HTTP connection to %s'), self.address)
            with excutils.save_and_reraise_exception():
                raise v_exc.VRouterConnectFailure(ip_address=self.address)
Esempio n. 2
0
 def configure_gateway(self, context, router_id, interface_infos):
     LOG.debug("Vyatta vRouter Driver::Configure gateway")
     if len(interface_infos) != 1:
         raise v_exc.InvalidParameter(
             cause=_("Only one external gateway interface expected. "
                     "Given interfaces = %s") % len(interface_infos))
     vrouter_api = self._get_router_api(context, router_id)
     vrouter_api.update_router(external_gateway_info=interface_infos[0])
Esempio n. 3
0
 def _make_entity_name(self, data):
     idnr = uuid.UUID(data['id'])
     name = data['name'].lower()
     name = ''.join((x if x.isalnum() else '') for x in name)
     name = '{0}-{1}'.format(name, idnr.get_hex())
     if self.NAME_LENGTH_LIMIT < len(name):
         raise v_exc.InvalidParameter(
             cause=('Can\'t make vyatta resource identifier, result exceed '
                    'length limit'))
     return name
Esempio n. 4
0
 def _lookup(self, context):
     q = context.session.query(models_v2.Network)
     q = q.filter_by(
         id=vyatta_config.VROUTER.management_network_id)
     try:
         value = q.one()
     except orm_exc.NoResultFound:
         raise v_exc.InvalidParameter(
             cause=_('Management network {0} specified in vRouter plugin '
                     'configuration file does not exist').format(
                 vyatta_config.VROUTER.management_network_id))
     return value
Esempio n. 5
0
    def get_vrouter_configuration(self, mode=CFG_FMT_GENERIC):
        cmd = ['configuration']
        if mode == self.CFG_FMT_GENERIC:
            pass
        elif mode == self.CFG_FMT_COMMANDS:
            cmd.append('commands')
        else:
            raise v_exc.InvalidParameter(
                cause='unsupported configuration dump format')

        cmd = '/'.join(cmd)
        return self._show_cmd(cmd)
Esempio n. 6
0
    def _launch_routerVM(self, context):
        LOG.debug("Vyatta vRouter Driver::Launch router")
        router_name = 'vrouter_{0}'.format(os.urandom(6).encode('hex'))
        LOG.info(
            _LI("Vyatta vRouter Driver::Creating the vRouter instance %s"),
            router_name)

        try:
            router = self._nova_client.servers.create(
                router_name,
                config.VROUTER.image_id,
                config.VROUTER.flavor,
                nics=[{
                    'net-id': self._management_network_id
                }])
        except (nova_exc.UnsupportedVersion, nova_exc.CommandError,
                nova_exc.AuthorizationFailure, nova_exc.NoUniqueMatch,
                nova_exc.AuthSystemNotFound, nova_exc.NoTokenLookupException,
                nova_exc.EndpointNotFound, nova_exc.AmbiguousEndpoints,
                nova_exc.ConnectionRefused, nova_exc.ClientException,
                Exception):
            with excutils.save_and_reraise_exception():
                LOG.error(
                    _LE("Vyatta vRouter Driver::Create server %s failed"),
                    router_name)
                raise v_exc.InstanceSpawnError()

        LOG.info(
            _LI("Vyatta vRouter Driver::Waiting for the vRouter "
                "instance %s to start"), router_name)

        def _router_spawn():
            while True:
                try:
                    instance = self._nova_client.servers.get(router.id)
                except (nova_exc.UnsupportedVersion, nova_exc.CommandError,
                        nova_exc.AuthorizationFailure, nova_exc.NoUniqueMatch,
                        nova_exc.AuthSystemNotFound,
                        nova_exc.NoTokenLookupException,
                        nova_exc.EndpointNotFound, nova_exc.AmbiguousEndpoints,
                        nova_exc.ConnectionRefused, nova_exc.ClientException,
                        Exception):
                    yield config.VROUTER.nova_poll_interval
                    continue
                LOG.debug("Vyatta vRouter Driver::vRouter instance {0} "
                          "Spawn Status: {1}".format(router_name,
                                                     instance.status))
                if instance.status not in ('ACTIVE', 'ERROR'):
                    yield config.VROUTER.nova_poll_interval
                elif instance.status == 'ERROR':
                    raise v_exc.InstanceSpawnError()
                else:
                    break

        try:
            # Wait for Nova to spawn VM instance
            self._wait(_router_spawn,
                       timeout=config.VROUTER.nova_spawn_timeout)
        except (v_exc.InstanceSpawnError, v_exc.WaitTimeoutError) as ex:
            with excutils.save_and_reraise_exception():
                LOG.error(
                    _LE("Vyatta vRouter Driver::vRouter {0} spawn issue. "
                        "Exception {1}").format(router_name, ex))
                self._delete_routerVM(context, router.id)

        try:
            ifs = router.interface_list()
            if len(ifs) != 1:
                raise v_exc.InvalidParameter(
                    cause=_("Management interface expected "
                            "in router: %s") % router.id)
        except (nova_exc.UnsupportedVersion, nova_exc.CommandError,
                nova_exc.AuthorizationFailure, nova_exc.NoUniqueMatch,
                nova_exc.AuthSystemNotFound, nova_exc.NoTokenLookupException,
                nova_exc.EndpointNotFound, nova_exc.AmbiguousEndpoints,
                nova_exc.ConnectionRefused, nova_exc.ClientException,
                v_exc.InvalidParameter, Exception):
            with excutils.save_and_reraise_exception():
                self._delete_routerVM(context, router.id)

        def _router_boot():
            router_api = None
            while router_api is None:
                try:
                    router_api = self._get_router_api(context, router.id)
                except (v_exc.VRouterConnectFailure,
                        v_exc.VRouterOperationError):
                    yield config.VROUTER.vrouter_poll_interval
                    continue
                if router_api is not None:
                    break

        LOG.info(
            _LI("Vyatta vRouter Driver::Waiting for the vRouter {0} "
                "to boot.").format(router_name))
        try:
            # Now wait for router to boot
            self._wait(_router_boot,
                       timeout=config.VROUTER.vrouter_boot_timeout)
        except (v_exc.WaitTimeoutError, v_exc.VRouterConnectFailure,
                v_exc.VRouterOperationError, v_exc.InvalidVRouterInstance,
                v_exc.InvalidInstanceConfiguration) as ex:
            with excutils.save_and_reraise_exception():
                LOG.error(
                    _LE("Vyatta vRouter Driver::vRouter {0} boot issue. "
                        "Exception: {1}").format(router_name, ex))
                self._delete_routerVM(context, router.id)

        LOG.info(_LI("Vyatta vRouter Driver::vRouter instance %s is ready"),
                 router_name)

        return router