Beispiel #1
0
    def ack(self):
        '''
        Acknowledge an update message
        '''
        if 'uuid' not in self.mesosUpdate['status']:
            self.logger.debug('Mesos:Ack:Skip')
            return
        self.logger.debug('Mesos:Update:Status:%s:%s' %
                          (self.mesosUpdate['status']['task_id']['value'],
                           self.mesosUpdate['status']['state']))
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Mesos-Stream-Id': self.streamId
        }

        acknowledge = {
            "framework_id": {
                "value": self.frameworkId
            },
            "type": "ACKNOWLEDGE",
            "acknowledge": {
                "agent_id": self.mesosUpdate['status']['agent_id'],
                "task_id": {
                    "value": self.mesosUpdate['status']['task_id']['value']
                },
                "uuid": self.mesosUpdate['status']['uuid']
            }
        }
        try:
            requests.post(self.mesos_url + '/api/v1/scheduler',
                          json.dumps(acknowledge),
                          headers=headers)
        except Exception as e:
            raise MesosException(e)
Beispiel #2
0
    def decline(self):
        '''
        Decline offer
        '''
        if not self.offer:
            return
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Mesos-Stream-Id': self.streamId
        }
        offers_decline = {
            "framework_id": {"value": self.frameworkId},
            "type": "DECLINE",
            "decline": {
                "offer_ids": []
            }
        }

        self.logger.debug('Mesos:Decline:Offer:' + self.offer['id']['value'])
        offers_decline['decline']['offer_ids'].append(
            {'value': self.offer['id']['value']}
        )
        try:
            self.r = requests.post(
                self.mesos_url + '/api/v1/scheduler',
                json.dumps(offers_decline),
                headers=headers
            )
        except Exception as e:
            raise MesosException(e)
        return True
Beispiel #3
0
        def request(self, requests):
            '''
            Send a REQUEST message

            :param requests: list of resources request [{'agent_id': : XX, 'resources': {}}]
            :type requests: list
            '''
            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Mesos-Stream-Id': self.streamId
            }

            revive = {
                "framework_id": {
                    "value": self.frameworkId
                },
                "type": "REQUEST",
                "requests": requests
            }

            try:
                requests.post(self.mesos_url + '/api/v1/scheduler',
                              json.dumps(revive),
                              headers=headers)
            except Exception as e:
                raise MesosException(e)
Beispiel #4
0
        def revive(self):
            '''
            Send REVIVE request
            '''
            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Mesos-Stream-Id': self.streamId
            }

            revive = {
                "framework_id": {
                    "value": self.frameworkId
                },
                "type": "REVIVE"
            }

            try:
                requests.post(self.mesos_url + '/api/v1/scheduler',
                              json.dumps(revive),
                              headers=headers,
                              auth=self.requests_auth,
                              verify=self.verify)
            except Exception as e:
                raise MesosException(e)
Beispiel #5
0
        def reconcile(self, tasks):
            '''
            Reconcile tasks

            :type tasks: list
            :param tasks: list of dict { "agent_id": xx, "task_id": yy }
            '''
            self.logger.debug('Reconcile %s' % (str(tasks)))

            if not tasks:
                return True

            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Mesos-Stream-Id': self.streamId
            }
            message = {
                "framework_id": {
                    "value": self.frameworkId
                },
                "type": "RECONCILE",
                "reconcile": {
                    "tasks": tasks
                }
            }

            try:
                requests.post(self.mesos_url + '/api/v1/scheduler',
                              json.dumps(message),
                              headers=headers)
            except Exception as e:
                raise MesosException(e)
            return True
Beispiel #6
0
    def combine_offers(self, offers, operations, options=None):
        '''
        Accept offers with task operations

        :param offers: offers to be accepted
        :type offers: list
        :param operations: JSON TaskInfo instances to accept
        :type operations: list of json TaskInfo
        :param options: optional filters
        :type options: JSON filters instances

        This method does not check if the operations are valid
        JSON TaskInfo instances and if conform with the offers.
        '''
        if not operations:
            self.logger.debug('Mesos:Accept:no operation to accept')
            return True

        if not offers:
            self.logger.debug('Mesos:Accept:no offers to accept')
            return True

        ids = [o.get_offer()['id']['value'] for o in offers]
        offer_ids = [{'value': oid} for oid in ids]
        self.logger.debug('Mesos:COMBINE Offer ids:' + ','.join(ids))

        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Mesos-Stream-Id': self.streamId
        }

        message = {
            "framework_id": {"value": self.frameworkId},
            "type": "ACCEPT",
            "accept": {
                "offer_ids": offer_ids,
                "operations": [{
                    'type': 'LAUNCH',
                    'launch': {'task_infos': operations}
                }]
            }
        }
        if options and options.get('filters'):
            message["accept"]["filters"] = options.get('filters')

        message = json.dumps(message)
        try:
            r = requests.post(
                self.mesos_url + '/api/v1/scheduler',
                message,
                headers=headers,
                auth=self.requests_auth,
                verify=self.verify
            )
            self.logger.debug('Mesos:Accept:' + str(message))
            self.logger.debug('Mesos:Accept:Anwser:%d:%s' % (r.status_code, r.text))
        except Exception as e:
            raise MesosException(e)
        return True
Beispiel #7
0
        def shutdown(self, agent_id, executor_id):
            '''
            Shutdown an executor

            :param agent_id: slave identifier
            :type agent_id: str
            :param executor_id: executor identifier
            :type executor_id: str
            '''
            self.logger.debug('Shutdown executor %s' % (str(executor_id)))
            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Mesos-Stream-Id': self.streamId
            }
            message = {
                "framework_id": {"value": self.frameworkId},
                "type": "SHUTDOWN",
                "shutdown": {
                    "executor_id": {'value': executor_id},
                    "agent_id": {'value': agent_id}
                }
            }
            try:
                requests.post(
                    self.mesos_url + '/api/v1/scheduler',
                    json.dumps(message),
                    headers=headers,
                    auth=self.requests_auth,
                    verify=self.verify
                )
            except Exception as e:
                raise MesosException(e)
            return True
Beispiel #8
0
        def kill(self, agent_id, task_id):
            '''
            Kill specified task

            :param agent_id: slave agent_id
            :type agent_id: str
            :param task_id: task identifier
            :type task_id: str
            '''
            self.logger.debug('Kill task %s' % (str(task_id)))
            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Mesos-Stream-Id': self.streamId
            }
            message = {
                "framework_id": {"value": self.frameworkId},
                "type": "KILL",
                "kill": {
                    "task_id": {'value': task_id},
                    "agent_id": {'value': agent_id}
                }
            }
            try:
                requests.post(
                    self.mesos_url + '/api/v1/scheduler',
                    json.dumps(message),
                    headers=headers,
                    auth=self.requests_auth,
                    verify=self.verify
                )
            except Exception as e:
                self.logger.error('Mesos:Kill:Exception:' + str(e))
                raise MesosException(e)
            return True
Beispiel #9
0
    def accept(self, operations, options=None):
        '''
        Accept offer with task operations

        :param operations: JSON TaskInfo instances to accept in current offer
        :type operations: list of json TaskInfo
        :param options: Optional offer additional params (filters, ...)
        :type options: dict
        '''
        if not operations:
            self.logger.debug('Mesos:Accept:no operation to accept')
            return True

        offer_ids = [{'value': self.offer['id']['value']}]
        self.logger.debug('Mesos:ACCEPT Offer ids:' + str(offer_ids))

        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Mesos-Stream-Id': self.streamId
        }

        tasks = []
        for operation in operations:
            if 'slave_id' not in operation:
                operation['slave_id'] = {
                    'value': self.offer['agent_id']['value']
                }
            tasks.append(operation)

        message = {
            "framework_id": {
                "value": self.frameworkId
            },
            "type": "ACCEPT",
            "accept": {
                "offer_ids": offer_ids,
                "operations": [{
                    'type': 'LAUNCH',
                    'launch': {
                        'task_infos': tasks
                    }
                }]
            }
        }
        if options and options.get('filters'):
            message["accept"]["filters"] = options.get('filters')

        message = json.dumps(message)
        try:
            r = requests.post(self.mesos_url + '/api/v1/scheduler',
                              message,
                              headers=headers)
            self.logger.debug('Mesos:Accept:' + str(message))
            self.logger.debug('Mesos:Accept:Anwser:%d:%s' %
                              (r.status_code, r.text))
        except Exception as e:
            raise MesosException(e)
        return True
Beispiel #10
0
        def message(self, agent_id, executor_id, message):
            '''
            Send message to an executor

            :param agent_id: slave identifier
            :type agent_id: str
            :param executor_id: executor identifier
            :type executor_id: str
            :param message: message to send, raw bytes encoded as Base64
            :type message: str
            '''
            self.logger.debug('Send message to executor %s' %
                              (str(executor_id)))
            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'Mesos-Stream-Id': self.streamId
            }
            message = {
                "framework_id": {
                    "value": self.frameworkId
                },
                "type": "MESSAGE",
                "message": {
                    "executor_id": {
                        'value': executor_id
                    },
                    "agent_id": {
                        'value': agent_id
                    },
                    "data": message
                }
            }
            try:
                requests.post(self.mesos_url + '/api/v1/scheduler',
                              json.dumps(message),
                              headers=headers,
                              auth=self.requests_auth,
                              verify=self.verify)
            except Exception as e:
                raise MesosException(e)
            return True
Beispiel #11
0
    def decline(self, options=None):
        '''
        Decline offer

        :param options: Optional offer additional params (filters, ...)
        :type options: dict
        '''
        if not self.offer:
            return
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Mesos-Stream-Id': self.streamId
        }
        offers_decline = {
            "framework_id": {"value": self.frameworkId},
            "type": "DECLINE",
            "decline": {
                "offer_ids": []
            }
        }
        if options and options.get('filters'):
            offers_decline["decline"]["filters"] = options.get('filters')

        self.logger.debug('Mesos:Decline:Offer:' + self.offer['id']['value'])
        offers_decline['decline']['offer_ids'].append(
            {'value': self.offer['id']['value']}
        )
        try:
            self.r = requests.post(
                self.mesos_url + '/api/v1/scheduler',
                json.dumps(offers_decline),
                headers=headers,
                auth=self.requests_auth,
                verify=self.verify
            )
        except Exception as e:
            raise MesosException(e)
        return True