def execute(self, event):
        if not _should_handle(self, event):
            return

        if len(event.data.items) == 0:
            return utils.reply(event)

        item_names = []

        for item in event.data.items:
            # For development, don't let the server kill your agent
            if item.name != 'pyagent' or Config.config_update_pyagent():
                item_names.append(item.name)

        home = Config.home()

        env = dict(os.environ)
        env['CATTLE_ACCESS_KEY'] = Config.access_key()
        env['CATTLE_SECRET_KEY'] = Config.secret_key()
        env['CATTLE_CONFIG_URL'] = Config.config_url()
        env['CATTLE_HOME'] = home

        args = [Config.config_sh()] + item_names

        try:
            output = utils.get_command_output(args, cwd=home, env=env)
            return utils.reply(event, {
                'exitCode': 0,
                'output': output
            })
        except subprocess.CalledProcessError as e:
            Progress(event).update('Update Failed', data={
                'exitCode': e.returncode,
                'output': e.output
            })
    def execute(self, event):
        if not _should_handle(self, event):
            return

        if len(event.data.items) == 0:
            return utils.reply(event)

        item_names = []

        for item in event.data.items:
            # For development, don't let the server kill your agent
            if item.name != 'pyagent' or Config.config_update_pyagent():
                item_names.append(item.name)

        home = Config.home()

        env = dict(os.environ)
        env['CATTLE_ACCESS_KEY'] = Config.access_key()
        env['CATTLE_SECRET_KEY'] = Config.secret_key()
        env['CATTLE_CONFIG_URL'] = Config.config_url()
        env['CATTLE_HOME'] = home

        args = [Config.config_sh()] + item_names

        try:
            output = utils.get_command_output(args, cwd=home, env=env)
            return utils.reply(event, {'exitCode': 0, 'output': output})
        except subprocess.CalledProcessError as e:
            Progress(event).update('Update Failed',
                                   data={
                                       'exitCode': e.returncode,
                                       'output': e.output
                                   })
Exemple #3
0
    def delegate_request(self, req=None, event=None, instanceData=None, **kw):
        if instanceData.kind != 'container' or \
           instanceData.get('token') is None:
            return

        container = self.compute.get_container_by_name(instanceData.uuid)
        if container is None:
            return

        inspect = self.compute.inspect(container)

        try:
            ip = inspect['NetworkSettings']['IPAddress']
            running = inspect['State']['Running']
            if not running:
                log.error('Can not call [%s], container is not running',
                          instanceData.uuid)
                return
        except KeyError:
            log.error('Can not call [%s], container is not running',
                      instanceData.uuid)
            return

        progress = Progress(event, parent=req)
        exit_code, output, data = container_exec(ip, instanceData.token, event)

        if exit_code == 0:
            return reply(event, data, parent=req)
        else:
            progress.update('Update failed', data={
                'exitCode': exit_code,
                'output': output
            })
Exemple #4
0
    def _reply(self, req, response_data):
        if req is None:
            return None
        resp = utils.reply(req)
        resp.data = response_data

        return resp
Exemple #5
0
    def _reply(self, req, response_data):
        if req is None:
            return None
        resp = utils.reply(req)
        resp.data = response_data

        return resp
Exemple #6
0
    def delegate_request(self, req=None, event=None, instanceData=None, **kw):
        if instanceData.kind != 'container' or \
           instanceData.get('token') is None:
            return

        container = self.compute.get_container(docker_client(), instanceData,
                                               by_agent=True)
        if container is None:
            log.info('Can not call [%s], container does not exists',
                     instanceData.uuid)
            return

        inspect = self.compute.inspect(container)

        try:
            running = inspect['State']['Running']
            if not running:
                log.error('Can not call [%s], container is not running',
                          instanceData.uuid)
                return
        except KeyError:
            log.error('Can not call [%s], container is not running',
                      instanceData.uuid)
            return

        progress = Progress(event, parent=req)
        exit_code, output, data = ns_exec(inspect['State']['Pid'], event)

        if exit_code == 0:
            return reply(event, data, parent=req)
        else:
            progress.update('Update failed', data={
                'exitCode': exit_code,
                'output': output
            })
Exemple #7
0
    def delegate_request(self, req=None, event=None, instanceData=None, **kw):
        if instanceData.kind != 'container' or \
           instanceData.get('token') is None:
            return

        container = self.compute.get_container(docker_client(), instanceData,
                                               by_agent=True)
        if container is None:
            log.info('Can not call [%s], container does not exists',
                     instanceData.uuid)
            return

        inspect = self.compute.inspect(container)

        try:
            running = inspect['State']['Running']
            if not running:
                log.error('Can not call [%s], container is not running',
                          instanceData.uuid)
                return
        except KeyError:
            log.error('Can not call [%s], container is not running',
                      instanceData.uuid)
            return

        progress = Progress(event, parent=req)
        exit_code, output, data = ns_exec(inspect['State']['Pid'], event)

        if exit_code == 0:
            return reply(event, data, parent=req)
        else:
            progress.update('Update failed', data={
                'exitCode': exit_code,
                'output': output
            })
Exemple #8
0
    def delegate_request(self, req=None, event=None, instanceData=None, **kw):
        if instanceData.kind != 'container' or \
           instanceData.get('token') is None:
            return

        container = self.compute.get_container_by_name(instanceData.uuid)
        if container is None:
            return

        inspect = self.compute.inspect(container)

        try:
            ip = inspect['NetworkSettings']['IPAddress']
            running = inspect['State']['Running']
            if not running:
                log.error('Can not call [%s], container is not running',
                          instanceData.uuid)
                return
        except KeyError:
            log.error('Can not call [%s], container is not running',
                      instanceData.uuid)
            return

        progress = Progress(event, parent=req)
        exit_code, output, data = container_exec(ip, instanceData.token, event)

        if exit_code == 0:
            return reply(event, data, parent=req)
        else:
            progress.update('Update failed',
                            data={
                                'exitCode': exit_code,
                                'output': output
                            })
Exemple #9
0
    def update(self, msg, progress=None, data=None):
        resp = utils.reply(self._req, data)
        resp['transitioning'] = 'yes'
        resp['transitioningMessage'] = msg
        resp['transitioningProgress'] = progress

        if self._parent is not None:
            resp = utils.reply(self._parent, resp)
            resp['transitioning'] = 'yes'
            resp['transitioningMessage'] = msg
            resp['transitioningProgress'] = progress

        publisher = get_type(PUBLISHER)
        try:
            publisher.publish(resp)
        except:
            pass
Exemple #10
0
    def delegate_request(self, req=None, event=None, instanceData=None, **kw):
        if instanceData.kind != 'container' or \
           instanceData.get('token') is None:
            return

        container = self.compute.get_container(docker_client(), instanceData)
        if container is None:
            return

        inspect = self.compute.inspect(container)

        try:
            ip = inspect['NetworkSettings']['IPAddress']
            running = inspect['State']['Running']
            if not running:
                log.error('Can not call [%s], container is not running',
                          instanceData.uuid)
                return
        except KeyError:
            log.error('Can not call [%s], container is not running',
                      instanceData.uuid)
            return

        try:
            # Optimization for empty config.updates, should really find a
            # better way to do this
            if event.name == 'config.update' and len(event.data.items) == 0:
                return reply(event, None, parent=req)
        except:
            pass

        progress = Progress(event, parent=req)
        if DockerConfig.is_host_pidns():
            exit_code, output, data = ns_exec(inspect['State']['Pid'], event)
        else:
            exit_code, output, data = container_exec(ip, instanceData.token,
                                                     event)

        if exit_code == 0:
            return reply(event, data, parent=req)
        else:
            progress.update('Update failed',
                            data={
                                'exitCode': exit_code,
                                'output': output
                            })
    def delegate_request(self, req=None, event=None, instanceData=None, **kw):
        if instanceData.kind != 'container' or \
           instanceData.get('token') is None:
            return

        container = self.compute.get_container_by_name(instanceData.uuid)
        if container is None:
            return

        inspect = self.compute.inspect(container)

        try:
            ip = inspect['NetworkSettings']['IPAddress']
            running = inspect['State']['Running']
            if not running:
                log.error('Can not call [%s], container is not running',
                          instanceData.uuid)
                return
        except KeyError:
            log.error('Can not call [%s], container is not running',
                      instanceData.uuid)
            return

        try:
            # Optimization for empty config.updates, should really find a
            # better way to do this
            if event.name == 'config.update' and len(event.data.items) == 0:
                return reply(event, None, parent=req)
        except:
            pass

        progress = Progress(event, parent=req)
        if DockerConfig.is_host_pidns():
            exit_code, output, data = ns_exec(inspect['State']['Pid'], event)
        else:
            exit_code, output, data = container_exec(ip, instanceData.token,
                                                     event)

        if exit_code == 0:
            return reply(event, data, parent=req)
        else:
            progress.update('Update failed', data={
                'exitCode': exit_code,
                'output': output
            })
Exemple #12
0
    def execute(self, event):
        if not _should_handle(self, event):
            return

        resp = utils.reply(event)
        if Config.do_ping():
            for type in types():
                if hasattr(type, 'on_ping'):
                    type.on_ping(event, resp)

        return resp
    def execute(self, event):
        if not _should_handle(self, event):
            return

        resp = utils.reply(event)
        if Config.do_ping():
            for type in types():
                if hasattr(type, 'on_ping'):
                    type.on_ping(event, resp)

        return resp
Exemple #14
0
def _worker_main(worker_name, queue, ppid):
    agent = Agent()
    marshaller = type_manager.get_type(type_manager.MARSHALLER)
    publisher = type_manager.get_type(type_manager.PUBLISHER)
    while True:
        try:
            req = None
            line = queue.get(True, 5)

            req = marshaller.from_string(line)

            utils.log_request(req, log, 'Request: %s', line)

            id = req.id
            start = time.time()
            try:
                utils.log_request(req, log, '%s : Starting request %s for %s',
                                  worker_name, id, req.name)
                resp = agent.execute(req)
                if resp is not None:
                    publisher.publish(resp)
            finally:
                duration = time.time() - start
                utils.log_request(req, log,
                                  '%s : Done request %s for %s [%s] seconds',
                                  worker_name, id, req.name, duration)
        except Empty:
            if not _should_run(ppid):
                break
        except FailedToLock as e:
            log.info("%s for %s", e, req.name)
            if not _should_run(ppid):
                break
        except Exception as e:
            error_id = str(uuid.uuid4())
            log.exception("%s : Unknown error", error_id)
            if not _should_run(ppid):
                break

            if req is not None:
                msg = "{0} : {1}".format(error_id, e)

                resp = utils.reply(req)
                if resp is not None:
                    resp["transitioning"] = "error"
                    resp["transitioningInternalMessage"] = msg
                    publisher.publish(resp)