def schedule_and_build_instances(self, context, build_requests,
                                     request_specs, image,
                                     admin_password, injected_files,
                                     requested_networks, block_device_mapping):
        index = 0

        for build_request in build_requests:
            request_spec = request_specs[index]

            index += 1

            data = {'build_requests': [build_request],
                    'request_specs': [request_spec],
                    'image': image,
                    'admin_password': admin_password,
                    'injected_files': injected_files,
                    'requested_networks': requested_networks,
                    'block_device_mapping': block_device_mapping}

            req = {"context": context, "data": data,
                   "action": "schedule_and_build_instances"}

            request = Request.fromDict(req)

            self.nova_manager.notify(event_type="SERVER_CREATE",
                                     request=request)
    def schedule_and_build_instances(self, context, build_requests,
                                     request_specs, image, admin_password,
                                     injected_files, requested_networks,
                                     block_device_mapping):
        index = 0

        for build_request in build_requests:
            request_spec = request_specs[index]

            index += 1

            data = {
                'build_requests': [build_request],
                'request_specs': [request_spec],
                'image': image,
                'admin_password': admin_password,
                'injected_files': injected_files,
                'requested_networks': requested_networks,
                'block_device_mapping': block_device_mapping
            }

            req = {
                "context": context,
                "data": data,
                "action": "schedule_and_build_instances"
            }

            request = Request.fromDict(req)

            self.nova_manager.notify(event_type="SERVER_CREATE",
                                     request=request)
    def build_instances(self, context, instances, image, filter_properties,
                        admin_password, injected_files, requested_networks,
                        security_groups, block_device_mapping=None,
                        legacy_bdm=True):
        for instance in instances:
            data = {'instances': [instance],
                    'image': image,
                    'filter_properties': filter_properties,
                    'admin_password': admin_password,
                    'injected_files': injected_files,
                    'requested_networks': requested_networks,
                    'security_groups': security_groups,
                    'block_device_mapping': block_device_mapping,
                    'legacy_bdm': legacy_bdm}

            req = {"context": context, "data": data,
                   "action": "build_instances"}
            try:
                request = Request.fromDict(req)

                self.nova_manager.notify(event_type="SERVER_CREATE",
                                         request=request)
            except Exception as ex:
                LOG.info(ex)
    def build_instances(self,
                        context,
                        instances,
                        image,
                        filter_properties,
                        admin_password,
                        injected_files,
                        requested_networks,
                        security_groups,
                        block_device_mapping=None,
                        legacy_bdm=True):
        for instance in instances:
            data = {
                'instances': [instance],
                'image': image,
                'filter_properties': filter_properties,
                'admin_password': admin_password,
                'injected_files': injected_files,
                'requested_networks': requested_networks,
                'security_groups': security_groups,
                'block_device_mapping': block_device_mapping,
                'legacy_bdm': legacy_bdm
            }

            req = {
                "context": context,
                "data": data,
                "action": "build_instances"
            }
            try:
                request = Request.fromDict(req)

                self.nova_manager.notify(event_type="SERVER_CREATE",
                                         request=request)
            except Exception as ex:
                LOG.info(ex)
    def run(self):
        LOG.info("Worker %s running!" % self.name)
        queue_items = []
        last_release_time = SharedQuota.getLastReleaseTime()

        while not self.exit and not self.queue.isClosed():
            if last_release_time < SharedQuota.getLastReleaseTime():
                last_release_time = SharedQuota.getLastReleaseTime()

                while queue_items:
                    self.queue.reinsertItem(queue_items.pop(0))

            if len(queue_items) >= self.backfill_depth:
                SharedQuota.wait()
                continue

            queue_item = self.queue.getItem(blocking=False)

            if queue_item is None:
                if self.queue.getSize():
                    SharedQuota.wait()
                    continue
                else:
                    queue_item = self.queue.getItem(blocking=True)

            if queue_item is None:
                continue

            try:
                request = Request.fromDict(queue_item.getData())
                user_id = request.getUserId()
                prj_id = request.getProjectId()
                context = request.getContext()
                server = request.getServer()
                server_id = server.getId()
                quota = None

                try:
                    s = self.nova_manager.getServer(server_id, detail=True)

                    if s.getState() != "building":
                        # or server["OS-EXT-STS:task_state"] != "scheduling":
                        self.queue.deleteItem(queue_item)
                        continue
                except Exception as ex:
                    LOG.warn("the server %s is not anymore available!"
                             "(reason=%s)" % (server_id, ex))
                    self.queue.deleteItem(queue_item)

                    continue

                quota = self.projects[prj_id].getQuota()
                blocking = False

                if server.isEphemeral() and not SharedQuota.isEnabled():
                    blocking = True

                if quota.allocate(server, blocking=blocking):
                    found = False

                    try:
                        km = self.keystone_manager
                        trust = km.getTrust(context["trust_id"])
                        token = trust.getToken(km.getToken().getId())

                        context["auth_token"] = token.getId()
                        context["user_id"] = token.getUser().getId()
                    except Exception as ex:
                        LOG.error("error on getting the token for server "
                                  "%s (reason=%s)" % (server.getId(), ex))
                        raise ex

                    try:
                        self.nova_manager.buildServer(request)

                        LOG.info("building server %s (user_id=%s prj_id=%s quo"
                                 "ta=shared)" % (server_id, user_id, prj_id))

                        found = True
                    except Exception as ex:
                        LOG.error(
                            "error on building the server %s (reason=%s)" %
                            (server.getId(), ex))

                    if found:
                        self.queue.deleteItem(queue_item)
                    else:
                        quota.release(server)
                        queue_items.append(queue_item)
                else:
                    queue_items.append(queue_item)

            except Exception as ex:
                LOG.error("Exception has occured", exc_info=1)
                LOG.error("Worker %s: %s" % (self.name, ex))

                self.queue.deleteItem(queue_item)

        LOG.info("Worker %s destroyed!" % self.name)
    def run(self):
        LOG.info("Worker %s running!" % self.name)
        queue_items = []
        last_release_time = SharedQuota.getLastReleaseTime()

        while not self.exit and not self.queue.isClosed():
            try:
                if last_release_time < SharedQuota.getLastReleaseTime():
                    last_release_time = SharedQuota.getLastReleaseTime()

                    while queue_items:
                        self.queue.restore(queue_items.pop(0))

                    for project in self.project_manager.getProjects():
                        for user in project.getUsers():
                            self.queue.updatePriority(user)

                if len(queue_items) >= self.backfill_depth:
                    SharedQuota.wait()
                    continue

                queue_item = self.queue.dequeue(block=False)

                if queue_item is None:
                    if self.queue.getSize():
                        SharedQuota.wait()
                        continue
                    else:
                        queue_item = self.queue.dequeue(block=True)

                if queue_item is None:
                    continue

                request = Request.fromDict(queue_item.getData())
                user_id = request.getUserId()
                prj_id = request.getProjectId()
                context = request.getContext()
                server = request.getServer()
                server_id = server.getId()
                quota = None

                try:
                    s = self.nova_manager.getServer(server_id, detail=True)

                    if s.getState() != "building":
                        # or server["OS-EXT-STS:task_state"] != "scheduling":
                        self.queue.delete(queue_item)
                        continue
                except SynergyError as ex:
                    LOG.warn("the server %s is not anymore available!"
                             " (reason=%s)" % (server_id, ex))
                    self.queue.delete(queue_item)

                    continue

                project = self.project_manager.getProject(id=prj_id)

                if not project:
                    raise SynergyError("project %r not found!" % prj_id)

                quota = project.getQuota()
                blocking = False

                if server.isEphemeral() and not SharedQuota.isEnabled():
                    blocking = True

                if quota.allocate(server, blocking=blocking):
                    found = False

                    try:
                        km = self.keystone_manager
                        trust = km.getTrust(context["trust_id"])
                        token = trust.getToken(km.getToken().getId())

                        context["auth_token"] = token.getId()
                        context["user_id"] = token.getUser().getId()
                    except SynergyError as ex:
                        LOG.error("error on getting the token for server "
                                  "%s (reason=%s)" % (server.getId(), ex))
                        raise ex

                    try:
                        self.nova_manager.buildServer(request)

                        LOG.info("building server %s user_id=%s prj_id=%s quo"
                                 "ta=shared" % (server_id, user_id, prj_id))

                        found = True
                    except Exception as ex:
                        LOG.error("error on building the server %s (reason=%s)"
                                  % (server.getId(), ex))

                    if found:
                        self.queue.delete(queue_item)
                    else:
                        quota.release(server)
                        queue_items.append(queue_item)
                else:
                    queue_items.append(queue_item)

            except Exception as ex:
                LOG.error("Exception has occured", exc_info=1)
                LOG.error("Worker %s: %s" % (self.name, ex))

                self.queue.delete(queue_item)

        LOG.info("Worker %s destroyed!" % self.name)