예제 #1
0
    def cmd_sync(self, low, timeout=None):
        '''
        Execute a runner function synchronously; eauth is respected

        This function requires that :conf_master:`external_auth` is configured
        and the user is authorized to execute runner functions: (``@runner``).

        .. code-block:: python

            runner.eauth_sync({
                'fun': 'jobs.list_jobs',
                'username': '******',
                'password': '******',
                'eauth': 'pam',
            })
        '''
        event = salt.utils.event.get_master_event(self.opts,
                                                  self.opts['sock_dir'],
                                                  listen=True)
        job = self.master_call(**low)
        ret_tag = salt.utils.event.tagify('ret', base=job['tag'])

        if timeout is None:
            timeout = self.opts.get('rest_timeout', 300)
        ret = event.get_event(tag=ret_tag, full=True, wait=timeout)
        if ret is None:
            raise salt.exceptions.SaltClientTimeout(
                "RunnerClient job '{0}' timed out".format(job['jid']),
                jid=job['jid'])

        return ret['data']['return']
예제 #2
0
파일: eventlisten.py 프로젝트: bryson/salt
def listen(opts):
    """
    Attach to the pub socket and grab messages
    """
    event = salt.utils.event.get_event(
        opts["node"], sock_dir=opts["sock_dir"], transport=opts["transport"], opts=opts, listen=True
    )
    check_access_and_print_warning(opts["sock_dir"])
    print(event.puburi)
    jid_counter = 0
    found_minions = []
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        if opts["func_count"]:
            data = ret.get("data", False)
            if data:
                if "id" in six.iterkeys(data) and data.get("id", False) not in found_minions:
                    if data["fun"] == opts["func_count"]:
                        jid_counter += 1
                        found_minions.append(data["id"])
                        print(
                            "Reply received from [{0}]. Total replies now: [{1}].".format(
                                ret["data"]["id"], jid_counter
                            )
                        )
                    continue
        else:
            print("Event fired at {0}".format(time.asctime()))
            print("*" * 25)
            print("Tag: {0}".format(ret["tag"]))
            print("Data:")
            pprint.pprint(ret["data"])
예제 #3
0
def listen(sock_dir, node, id=None):
    '''
    Attach to the pub socket and grab messages
    '''
    event = salt.utils.event.SaltEvent(node, sock_dir, id=id)
    print(event.puburi)
    jid_counter = 0
    found_minions = []
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        if opts['func_count']:
            data = ret.get('data', False)
            if data:
                if 'id' in data.keys() and data.get(
                        'id', False) not in found_minions:
                    if data['fun'] == opts['func_count']:
                        jid_counter += 1
                        found_minions.append(data['id'])
                        print(
                            'Reply received from [{0}]. Total replies now: [{1}].'
                            .format(ret['data']['id'], jid_counter))
                    continue
        else:
            print('Event fired at {0}'.format(time.asctime()))
            print('*' * 25)
            print('Tag: {0}'.format(ret['tag']))
            print('Data:')
            pprint.pprint(ret['data'])
예제 #4
0
파일: queue.py 프로젝트: yy721746/salt
def process_queue(queue, quantity=1, backend='sqlite'):
    '''
    Pop items off a queue and create an event on the Salt event bus to be
    processed by a Reactor.

    CLI Example:

    .. code-block:: bash

        salt-run queue.process_queue myqueue
        salt-run queue.process_queue myqueue 6
        salt-run queue.process_queue myqueue all backend=sqlite
    '''
    # get ready to send an event
    event = get_event('master',
                      __opts__['sock_dir'],
                      __opts__['transport'],
                      opts=__opts__,
                      listen=False)
    try:
        items = pop(queue=queue, quantity=quantity, backend=backend)
    except SaltInvocationError as exc:
        error_txt = '{0}'.format(exc)
        __jid_event__.fire_event({'errors': error_txt}, 'progress')
        return False

    data = {
        'items': items,
        'backend': backend,
        'queue': queue,
    }
    event.fire_event(data, tagify([queue, 'process'], prefix='queue'))
    return data
예제 #5
0
파일: mixins.py 프로젝트: xia0204/salt
def _fetch_events(q):
    '''
    Collect events and store them
    '''
    def _clean_queue():
        print('Cleaning queue!')
        while not q.empty():
            queue_item = q.get()
            queue_item.task_done()

    atexit.register(_clean_queue)
    a_config = AdaptedConfigurationTestCaseMixin()
    event = salt.utils.event.get_event(
        'minion',
        sock_dir=a_config.get_config('minion')['sock_dir'],
        opts=a_config.get_config('minion'),
    )
    while True:
        try:
            events = event.get_event(full=False)
        except Exception as exc:
            # This is broad but we'll see all kinds of issues right now
            # if we drop the proc out from under the socket while we're reading
            log.exception("Exception caught while getting events %r", exc)
        q.put(events)
예제 #6
0
def listen(opts):
    '''
    Attach to the pub socket and grab messages
    '''
    event = salt.utils.event.get_event(opts['node'],
                                       sock_dir=opts['sock_dir'],
                                       transport=opts['transport'],
                                       opts=opts,
                                       listen=True)
    check_access_and_print_warning(opts['sock_dir'])
    print(event.puburi)
    jid_counter = 0
    found_minions = []
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        if opts['func_count']:
            data = ret.get('data', False)
            if data:
                if 'id' in six.iterkeys(data) and data.get(
                        'id', False) not in found_minions:
                    if data['fun'] == opts['func_count']:
                        jid_counter += 1
                        found_minions.append(data['id'])
                        print(
                            'Reply received from [{0}]. Total replies now: [{1}].'
                            .format(ret['data']['id'], jid_counter))
                    continue
        else:
            print('Event fired at {0}'.format(time.asctime()))
            print('*' * 25)
            print('Tag: {0}'.format(ret['tag']))
            print('Data:')
            pprint.pprint(ret['data'])
예제 #7
0
    def cmd_sync(self, low, timeout=None, full_return=False):
        """
        Execute a runner function synchronously; eauth is respected

        This function requires that :conf_master:`external_auth` is configured
        and the user is authorized to execute runner functions: (``@runner``).

        .. code-block:: python

            runner.eauth_sync({
                'fun': 'jobs.list_jobs',
                'username': '******',
                'password': '******',
                'eauth': 'pam',
            })
        """
        with salt.utils.event.get_master_event(
            self.opts, self.opts["sock_dir"], listen=True
        ) as event:
            job = self.master_call(**low)
            ret_tag = salt.utils.event.tagify("ret", base=job["tag"])

            if timeout is None:
                timeout = self.opts.get("rest_timeout", 300)
            ret = event.get_event(
                tag=ret_tag, full=True, wait=timeout, auto_reconnect=True
            )
            if ret is None:
                raise salt.exceptions.SaltClientTimeout(
                    "RunnerClient job '{}' timed out".format(job["jid"]),
                    jid=job["jid"],
                )

            return ret if full_return else ret["data"]["return"]
예제 #8
0
def listen(opts):
    '''
    Attach to the pub socket and grab messages
    '''
    event = salt.utils.event.get_event(
            opts['node'],
            sock_dir=opts['sock_dir'],
            transport=opts['transport'],
            opts=opts
            )
    check_access_and_print_warning(opts['sock_dir'])
    print(event.puburi)
    jid_counter = 0
    found_minions = []
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        if opts['func_count']:
            data = ret.get('data', False)
            if data:
                if 'id' in six.iterkeys(data) and data.get('id', False) not in found_minions:
                    if data['fun'] == opts['func_count']:
                        jid_counter += 1
                        found_minions.append(data['id'])
                        print('Reply received from [{0}]. Total replies now: [{1}].'.format(ret['data']['id'], jid_counter))
                    continue
        else:
            print('Event fired at {0}'.format(time.asctime()))
            print('*' * 25)
            print('Tag: {0}'.format(ret['tag']))
            print('Data:')
            pprint.pprint(ret['data'])
예제 #9
0
def _wait_for_signed_cert_request(timeout, opts={}):
    """Class to wait for cert via get_event"""
    fqdn = platform.node()
    opts['id'] = fqdn
    opts['node'] = 'minion'
    opts['transport'] = SALT_EVENT_TRANSPORT
    opts['sock_dir'] = os.path.join(SALT_SOCKET_DIR, opts['node'])
    stop_time = time.time() + timeout
    event = salt_event.get_event(opts['node'],
                                 sock_dir=opts['sock_dir'],
                                 transport=opts['transport'],
                                 opts=opts,
                                 listen=True)

    while time.time() < stop_time:
        ret = event.get_event(full=True)
        if ret is None:
            logger.debug('[_minion_event] No event data in packet')
            continue
        data = ret.get('data', False)
        if data and ret['tag'] == SALT_EVENT_RESPONSE_TAG:
            if _job_contains_cert_data(data):
                logger.debug('[_minion_event] Job contains cert data!')
                return data['data']
            else:
                logger.debug(
                    '[_minion_event] Job does not contains cert data. :(')
                continue
예제 #10
0
def _fetch_events(q, opts):
    """
    Collect events and store them
    """
    def _clean_queue():
        log.info("Cleaning queue!")
        while not q.empty():
            queue_item = q.get()
            queue_item.task_done()

    atexit.register(_clean_queue)
    event = salt.utils.event.get_event("minion",
                                       sock_dir=opts["sock_dir"],
                                       opts=opts)

    # Wait for event bus to be connected
    while not event.connect_pull(30):
        time.sleep(1)

    # Notify parent process that the event bus is connected
    q.put("CONNECTED")

    while True:
        try:
            events = event.get_event(full=False)
        except Exception as exc:  # pylint: disable=broad-except
            # This is broad but we'll see all kinds of issues right now
            # if we drop the proc out from under the socket while we're reading
            log.exception("Exception caught while getting events %r", exc)
        q.put(events)
예제 #11
0
파일: eventlisten.py 프로젝트: bemehow/salt
def listen(sock_dir, node, id=None):
    '''
    Attach to the pub socket and grab messages
    '''
    event = salt.utils.event.SaltEvent(
            node,
            sock_dir,
            id=id
            )
    print(event.puburi)
    jid_counter = 0
    found_minions = []
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        if opts['func_count']:
            data = ret.get('data', False)
            if data:
                if 'id' in data.keys() and data.get('id', False) not in found_minions:
                    if data['fun'] == opts['func_count']:
                        jid_counter += 1
                        found_minions.append(data['id'])
                        print('Reply received from [{0}]. Total replies now: [{1}].'.format(ret['data']['id'], jid_counter))
                    continue
        else:
            print('Event fired at {0}'.format(time.asctime()))
            print('*' * 25)
            print('Tag: {0}'.format(ret['tag']))
            print('Data:')
            pprint.pprint(ret['data'])
예제 #12
0
파일: mixins.py 프로젝트: dmyerscough/salt
    def cmd_sync(self, low, timeout=None):
        '''
        Execute a runner function synchronously; eauth is respected

        This function requires that :conf_master:`external_auth` is configured
        and the user is authorized to execute runner functions: (``@runner``).

        .. code-block:: python

            runner.eauth_sync({
                'fun': 'jobs.list_jobs',
                'username': '******',
                'password': '******',
                'eauth': 'pam',
            })
        '''
        event = salt.utils.event.get_master_event(self.opts, self.opts['sock_dir'])
        job = self.master_call(**low)
        ret_tag = salt.utils.event.tagify('ret', base=job['tag'])

        if timeout is None:
            timeout = 300
        ret = event.get_event(tag=ret_tag, full=True, wait=timeout)
        if ret is None:
            raise salt.exceptions.SaltClientTimeout(
                "RunnerClient job '{0}' timed out".format(job['jid']),
                jid=job['jid'])

        return ret['data']['return']
예제 #13
0
    def listen(self, node, socket, callback_func, controller):
        print('Listening with {0} filters'.format(len(self.filters)))

        sock = os.path.join(socket, node)
        event = salt.utils.event.SaltEvent(node, sock)

        while controller.get():
            print('Heartbeat!-!')
            ret = event.get_event(full=True)

            while len(self.jidsin) > 1000:
                self.jidsin.pop()

            while len(self.jidsout) > 10:
                self.jidsout.pop()

            if ret is None:
                continue

            data = ret.get('data', False)

            if data is None:
                continue

            if 'jid' in data.keys():
                if 'return' in data.keys():
                    if (data['jid'], data['id']) in self.jidsin:
                        continue
                    else:
                        self.jidsin.append((data['jid'], data['id']))
                else:
                    if data['jid'] in self.jidsout:
                        continue
                    else:
                        self.jidsout.append(data['jid'])

            has_returned = False

            for f in self.filters:
                return_data = True
                for k in f.get_filter().keys():
                    if k in data.keys():
                        if isinstance(data[k], basestring):
                            if data[k] in f.get_filter()[k]:
                                return_data = return_data and True
                            else:
                                return_data = False
                        else:
                            if set(f.get_filter()[k]).issubset(set(data[k])):
                                return_data = return_data and True
                            else:
                                return_data = False
                    else:
                        return_data = False

                if not has_returned:
                    if return_data is True:
                        callback_func(data)
                        has_returned = True
예제 #14
0
    def listen(self, node, socket, callback_func, controller):
        print('Listening with {0} filters'.format(len(self.filters)))

        sock = os.path.join(socket, node)
        event = salt.utils.event.SaltEvent(node, sock)
        
        while controller.get():
            print('Heartbeat!-!')
            ret = event.get_event(full=True)

            while len(self.jidsin) > 1000:
                self.jidsin.pop()

            while len(self.jidsout) > 10:
                self.jidsout.pop()

            if ret is None:
                continue
            
            data = ret.get('data', False)
            
            if data is None:
                continue
           
            if 'jid' in data.keys():
                if 'return' in data.keys():
                    if (data['jid'], data['id']) in self.jidsin:
                        continue
                    else:
                        self.jidsin.append((data['jid'], data['id']))
                else:
                    if data['jid'] in self.jidsout:
                        continue
                    else:
                        self.jidsout.append(data['jid'])

            has_returned = False

            for f in self.filters:
                return_data = True
                for k in f.get_filter().keys():
                    if k in data.keys():
                        if isinstance(data[k], basestring):
                            if data[k] in f.get_filter()[k]:
                                return_data = return_data and True
                            else:
                                return_data = False
                        else:
                            if set(f.get_filter()[k]).issubset(set(data[k])):
                                return_data = return_data and True
                            else:
                                return_data = False
                    else:
                        return_data = False

                if not has_returned: 
                    if return_data is True:
                        callback_func(data)
                        has_returned = True
예제 #15
0
 def setUp(self):
     super(TestAsyncEventPublisher, self).setUp()
     self.opts = {'sock_dir': SOCK_DIR}
     self.publisher = event.AsyncEventPublisher(
         self.opts,
         self.io_loop,
     )
     self.event = event.get_event('minion', opts=self.opts, io_loop=self.io_loop)
     self.event.subscribe('')
     self.event.set_event_handler(self._handle_publish)
예제 #16
0
 def run(self):
     import salt.utils.event
     event = salt.utils.event.MasterEvent('/var/run/salt/master')
     
     log.debug("salt listener has started.")
     while self.__running:
         evdata = event.get_event(wait=1, full=True)
         if evdata is not None:
             tag, data = evdata['tag'], evdata['data']
             log.debug("tag %s data %s" % (tag, data))
             if tag=="salt/auth":
                 self.process_auth_event(data)
         time.sleep(1)
     log.debug("salt listener has quit.")
예제 #17
0
    def run(self):
        opts = salt.config.client_config(
            strongr.core.Core.config().clouddomain.OpenNebula.salt_config +
            '/master')
        inter_domain_event_factory = strongr.clouddomain.model.gateways.Gateways.inter_domain_event_factory(
        )

        event = salt.utils.event.get_event('master',
                                           sock_dir=opts['sock_dir'],
                                           transport=opts['transport'],
                                           opts=opts)

        while True:
            ret = event.get_event(full=True)
            if ret is None:
                continue

            try:
                if fnmatch.fnmatch(ret['tag'], 'salt/job/*/ret/*'):
                    data = ret['data']
                    if 'jid' in data and 'return' in data and 'retcode' in data:
                        job_finished_event = inter_domain_event_factory.newJobFinishedEvent(
                            data['jid'], data['return'], data['retcode'])
                        strongr.core.Core.inter_domain_events_publisher(
                        ).publish(job_finished_event)
                elif fnmatch.fnmatch(ret['tag'], 'salt/cloud/*/creating'):
                    data = ret['data']
                    if 'name' in data:
                        vmcreated_event = inter_domain_event_factory.newVmCreatedEvent(
                            data['name'])
                        strongr.core.Core.inter_domain_events_publisher(
                        ).publish(vmcreated_event)
                elif fnmatch.fnmatch(ret['tag'], 'salt/cloud/*/created'):
                    data = ret['data']
                    if 'name' in data:
                        vmready_event = inter_domain_event_factory.newVmReadyEvent(
                            data['name'])
                        strongr.core.Core.inter_domain_events_publisher(
                        ).publish(vmready_event)
                elif fnmatch.fnmatch(ret['tag'], 'salt/cloud/*/destroyed'):
                    data = ret['data']
                    if 'name' in data:
                        vmdestroyed_event = inter_domain_event_factory.newVmDestroyedEvent(
                            data['name'])
                        strongr.core.Core.inter_domain_events_publisher(
                        ).publish(vmdestroyed_event)
            except Exception as e:  # thread must always continue running
                logging.getLogger('SaltEventTranslator').warning(str(e))
                pass
예제 #18
0
파일: eventlisten.py 프로젝트: herlo/salt
def listen(sock_dir, node, id=None):
    """
    Attach to the pub socket and grab messages
    """
    event = salt.utils.event.SaltEvent(node, sock_dir, id=id)
    print event.puburi
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        print ("Event fired at {0}".format(time.asctime()))
        print ("*" * 25)
        print ("Tag: {0}".format(ret["tag"]))
        print ("Data:")
        pprint.pprint(ret["data"])
예제 #19
0
        def _run():
            event = salt.utils.event.get_event(
                'master',
                sock_dir=self._salt_opts['sock_dir'],
                transport=self._salt_opts['transport'],
                opts=self._salt_opts)

            wait = 10
            tag = ''
            full = True
            while True:
                data = event.get_event(wait=wait, tag=tag, full=full)
                if data is None:
                    continue
                self._sensor_service.dispatch(trigger='salt.event', payload=data)
예제 #20
0
def listen(sock_dir, node, id=None):
    '''
    Attach to the pub socket and grab messages
    '''
    event = salt.utils.event.SaltEvent(node, sock_dir, id=id)
    print event.puburi
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        print('Event fired at {0}'.format(time.asctime()))
        print('*' * 25)
        print('Tag: {0}'.format(ret['tag']))
        print('Data:')
        pprint.pprint(ret['data'])
예제 #21
0
 async def wait_reply():
     event = salt.utils.event.MinionEvent(__opts__)
     # Generate a random reply channel
     reply_channel = action['reply_channel'] = uuid.uuid4().hex
     started = time.time()
     # Now send s signal to fire!
     waiter.set()
     # Wait until timeout.
     while started + timeout > time.time():
         ret = event.get_event(
             no_block=True, full=True,
             tag='ami_reply/{}'.format(reply_channel))
         if ret is None:
             await sleep(0.01)
         else:
             # Get a reply from the other side and return it.
             return ret['data']['Reply']
예제 #22
0
def listen(sock_dir, node):
    '''
    Attach to the pub socket and grab messages
    '''
    event = salt.utils.event.SaltEvent(
            node,
            sock_dir,
            )
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        print('Event fired at {0}'.format(time.asctime()))
        print('*' * 25)
        print('Tag: {0}'.format(ret['tag']))
        print('Data:')
        pprint.pprint(ret['data'])
예제 #23
0
    def schnueffel(self):
        event = salt.utils.event.SaltEvent(
            self._config.salt_node,
            os.path.join(self._config.salt_sock_dir, self._config.salt_node))
        if self._config.debug:
            print "listening on: %s" % event.puburi
        while True:
            ret = event.get_event(full=True)
            if ret is None:
                continue
            if self._config.debug:
                print 'Event fired at {0}'.format(time.asctime())
                print '*' * 25
                print 'Tag: {0}'.format(ret['tag'])
                print 'Data:'
                pprint.pprint(ret['data'])

            self._logger.info(ret['tag'], extra={'gelfProps': ret['data']})
예제 #24
0
def main():
    opts = salt.config.client_config('/etc/salt/master')
    event = salt.utils.event.get_event('master',
                                       sock_dir=opts['sock_dir'],
                                       transport=opts['transport'],
                                       opts=opts)

    while True:
        ret = event.get_event(full=True)
        if ret is None:
            print('*')
            continue
        if fnmatch.fnmatch(ret['tag'], 'salt/job/*/ret/*'):
            print(ret)
            payload = ret['data']
            r = requests.post('http://manager/events',
                              data=json.dumps(payload))
            print(r.status_code)
  def schnueffel(self):
    event = salt.utils.event.SaltEvent(
            self._config.salt_node,
            os.path.join(self._config.salt_sock_dir, self._config.salt_node)
            )
    if self._config.debug:
      print "listening on: %s" % event.puburi
    while True:
      ret = event.get_event(full=True)
      if ret is None:
        continue
      if self._config.debug:
        print 'Event fired at {0}'.format(time.asctime())
        print '*' * 25
        print 'Tag: {0}'.format(ret['tag'])
        print 'Data:'
        pprint.pprint(ret['data'])

      self._logger.info(ret['tag'], extra={'gelfProps': ret['data']})
예제 #26
0
def check_auth(name, pub_key=None, sock_dir=None, queue=None, timeout=300):
    '''
    This function is called from a multiprocess instance, to wait for a minion
    to become available to receive salt commands
    '''
    event = salt.utils.event.SaltEvent('master', sock_dir)
    starttime = time.mktime(time.localtime())
    newtimeout = timeout
    log.debug(
        'In check_auth, waiting for {0} to become available'.format(name))
    while newtimeout > 0:
        newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
        ret = event.get_event(full=True)
        if ret is None:
            continue
        if ret['tag'] == 'minion_start' and ret['data']['id'] == name:
            queue.put(name)
            newtimeout = 0
            log.debug('Minion {0} is ready to receive commands'.format(name))
예제 #27
0
def check_auth(name, pub_key=None, sock_dir=None, queue=None, timeout=300):
    '''
    This function is called from a multiprocess instance, to wait for a minion
    to become available to receive salt commands
    '''
    event = salt.utils.event.SaltEvent('master', sock_dir)
    starttime = time.mktime(time.localtime())
    newtimeout = timeout
    log.debug(
        'In check_auth, waiting for {0} to become available'.format(
            name
        )
    )
    while newtimeout > 0:
        newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
        ret = event.get_event(full=True)
        if ret is None:
            continue
        if ret['tag'] == 'minion_start' and ret['data']['id'] == name:
            queue.put(name)
            newtimeout = 0
            log.debug('Minion {0} is ready to receive commands'.format(name))
예제 #28
0
def event_listener(accepted_minions, cached_ret):
    opts = salt.config.client_config('/etc/salt/master')
    event = salt.utils.event.get_event('master',
                                       sock_dir=opts['sock_dir'],
                                       transport=opts['transport'],
                                       opts=opts)

    while 1:
        data = event.get_event()
        if data:
            if 'act' in data:
                mid = data['id']
                if mid not in accepted_minions:
                    accepted_minions[mid] = True

            elif 'return' in data and 'fun' in data and data[
                    'fun'] == 'legion.cache':
                mid = data['id']
                if mid not in cached_ret:
                    cached_ret[mid] = True

        time.sleep(0.1)
예제 #29
0
def process_queue(queue, quantity=1, backend="sqlite", is_runner=False):
    """
    Pop items off a queue and create an event on the Salt event bus to be
    processed by a Reactor.

    CLI Example:

    .. code-block:: bash

        salt-run queue.process_queue myqueue
        salt-run queue.process_queue myqueue 6
        salt-run queue.process_queue myqueue all backend=sqlite
    """
    # get ready to send an event
    with get_event(
            "master",
            __opts__["sock_dir"],
            __opts__["transport"],
            opts=__opts__,
            listen=False,
    ) as event_bus:
        try:
            items = pop(queue=queue,
                        quantity=quantity,
                        backend=backend,
                        is_runner=is_runner)
        except SaltInvocationError as exc:
            error_txt = "{}".format(exc)
            __jid_event__.fire_event({"errors": error_txt}, "progress")
            return False

        data = {
            "items": items,
            "backend": backend,
            "queue": queue,
        }
        event_bus.fire_event(data, tagify([queue, "process"], prefix="queue"))
    return data
예제 #30
0
def listen(opts):
    """
    Attach to the pub socket and grab messages
    """
    event = salt.utils.event.get_event(
        opts["node"],
        sock_dir=opts["sock_dir"],
        transport=opts["transport"],
        opts=opts,
        listen=True,
    )
    check_access_and_print_warning(opts["sock_dir"])
    print(event.puburi)
    jid_counter = 0
    found_minions = []
    while True:
        ret = event.get_event(full=True)
        if ret is None:
            continue
        if opts["func_count"]:
            data = ret.get("data", False)
            if data:
                if "id" in data.keys() and data.get(
                        "id", False) not in found_minions:
                    if data["fun"] == opts["func_count"]:
                        jid_counter += 1
                        found_minions.append(data["id"])
                        print(
                            "Reply received from [{}]. Total replies now: [{}]."
                            .format(ret["data"]["id"], jid_counter))
                    continue
        else:
            print("Event fired at {}".format(time.asctime()))
            print("*" * 25)
            print("Tag: {}".format(ret["tag"]))
            print("Data:")
            pprint.pprint(ret["data"])
예제 #31
0
    def get_async_returns(self, tag, timeout=None, event=None):
        '''
        Yield all events from a given tag until "ret" is recieved or timeout is
        reached.
        '''
        if timeout is None:
            timeout = self.opts['timeout'] * 2

        if event is None:
            event = salt.utils.event.get_master_event(self.opts, self.opts['sock_dir'])
        timeout_at = time.time() + timeout
        last_progress_timestamp = time.time()
        basetag_depth = tag.count('/') + 1

        # no need to have a sleep, get_event has one inside
        while True:
            raw = event.get_event(timeout, tag=tag, full=True)
            # If we saw no events in the event bus timeout
            # OR
            # we have reached the total timeout
            # AND
            # have not seen any progress events for the length of the timeout.
            now = time.time()
            if raw is None and (now > timeout_at and
                                now - last_progress_timestamp > timeout):
                # Timeout reached
                break
            try:
                tag_parts = raw['tag'].split('/')
                suffix = '/'.join(tag_parts[basetag_depth:])
                last_progress_timestamp = now
                yield suffix, raw['data']
                if tag_parts[3] == 'ret':
                    raise StopIteration()  # we are done, we got return
            except (IndexError, KeyError):
                continue
예제 #32
0
파일: mixins.py 프로젝트: sleminov-tc/salt
def _fetch_events(q):
    '''
    Collect events and store them
    '''
    def _clean_queue():
        print('Cleaning queue!')
        while not q.empty():
            queue_item = q.get()
            queue_item.task_done()

    atexit.register(_clean_queue)

    opts = RUNTIME_VARS.RUNTIME_CONFIGS['minion']
    event = salt.utils.event.get_event('minion',
                                       sock_dir=opts['sock_dir'],
                                       opts=opts)
    while True:
        try:
            events = event.get_event(full=False)
        except Exception:
            # This is broad but we'll see all kinds of issues right now
            # if we drop the proc out from under the socket while we're reading
            pass
        q.put(events)
예제 #33
0
    async def action_event_loop(self):
        log.debug('AMI action event loop started.')
        event = salt.utils.event.MinionEvent(__opts__)
        trace_actions = __opts__.get('ami_trace_actions')
        while True:
            try:
                evdata = event.get_event(tag='ami_action', no_block=True)
                # TODO: Connect to Salt's tornado eventloop.
                try:
                    await asyncio.sleep(0.01)
                except concurrent.futures._base.CancelledError:
                    return
                if evdata:
                    # Remove salt's event item
                    evdata.pop('_stamp', False)
                    reply_channel = evdata.pop('reply_channel', False)
                    # Trace the request if set.
                    if trace_actions and isinstance(trace_actions, bool):
                        log.info('Action request: %s', evdata)
                    elif isinstance(
                            trace_actions,
                            list) and evdata['Action'] in trace_actions:
                        log.info('Action request: %s', evdata)
                    else:
                        log.debug('Action request: %s', evdata)
                    try:
                        # Send action and get action result.
                        res = await asyncio.wait_for(
                            self.manager.send_action(evdata), timeout=1.0)
                        if trace_actions and isinstance(trace_actions, bool):
                            log.info('Action result: %s', res)
                        elif isinstance(
                                trace_actions,
                                list) and evdata['Action'] in trace_actions:
                            log.info('Action result: %s', res)
                        else:
                            log.debug('Action result: %s', res)
                    except asyncio.TimeoutError:
                        log.error('Send action timeout: %s', evdata)
                        res = {
                            'Message': 'Action Timeout',
                            'Response': 'Error'
                        }
                    except concurrent.futures._base.CancelledError:
                        log.info('AMI action event loop quitting.')
                        return
                    except Exception as e:
                        log.exception('Send action error:')
                        res = str(e)
                    # Make a list of results to unify.
                    if not isinstance(res, list):
                        res = [res]
                    if reply_channel:
                        # Send back the result.
                        __salt__['event.fire']({
                            'Reply': [dict(k) for k in res]
                        }, 'ami_reply/{}'.format(reply_channel))

            except Exception as e:
                if "'int' object is not callable" in str(e):
                    # Reaction on CTRL+C :-)
                    log.info('AMI events action lister quitting.')
                    return
                else:
                    log.exception('AMI action event loop error:')
                    await asyncio.sleep(1)  # Protect log flood.
예제 #34
0
import salt.utils.event

event = salt.utils.event.MasterEvent('/var/run/salt/master')

data = event.get_event()
예제 #35
0
#f.close()
###
__opts__ = salt.config.client_config('/etc/salt/master')
print __opts__['mysql.host']
# Create MySQL connect
#conn = MySQLdb.connect(host="127.0.0.1",user="******",passwd="qwert",db="cmdb",port=3306)
conn = MySQLdb.connect(host=__opts__['mysql.host'],
                       user=__opts__['mysql.user'],
                       passwd=__opts__['mysql.pass'],
                       db=__opts__['mysql.db'],
                       port=__opts__['mysql.port'])
cursor = conn.cursor()

# Listen Salt Master Event System
event = salt.utils.event.MasterEvent(__opts__['sock_dir'])
print "event", event.get_event()
for eachevent in event.iter_events(full=True):
    ret = eachevent['data']
    if "salt/job/" in eachevent['tag']:
        # Return Event
        if ret.has_key('id') and ret.has_key('return'):
            # Igonre saltutil.find_job event
            if ret['fun'] == "saltutil.find_job":
                continue

            sql = '''INSERT INTO `app_salt_return`
                (`fun`, `jid`, `result`, `host`, `success`, `full_ret` )
                VALUES ( %s, %s, %s, %s, %s, %s)'''
            cursor.execute(sql,
                           (ret['fun'], ret['jid'], json.dumps(ret['return']),
                            ret['id'], ret['success'], json.dumps(ret)))
예제 #36
0
파일: event.py 프로젝트: iTraceur/goms
#!/usr/bin/python
# -*- coding: utf-8 -*-

import fnmatch
import salt.config
import salt.utils.event

opts = salt.config.client_config('/etc/salt/master')

event = salt.utils.event.get_event(
    'master',
    sock_dir = opts['sock_dir'],
    transport = opts['transport'],
    opts=opts)

while True:

    ret = event.get_event(full=True)
    if ret is None:
        continue
    else:
        if fnmatch.fnmatch(ret['tag'], 'server/resource/allocation/information'):
            print ret['data']['data']
예제 #37
0
    if 'cert' in data['data']:
        return True
    else:
        return False


opts = {}
opts['func_count'] = ''
opts['id'] = socket.getfqdn()
opts['node'] = 'minion'
opts['transport'] = SALT_EVENT_TRANSPORT
opts['sock_dir'] = os.path.join(SALT_SOCKET_DIR, opts['node'])
event = salt_event.get_event(opts['node'],
                             sock_dir=opts['sock_dir'],
                             transport=opts['transport'],
                             opts=opts,
                             listen=True)

job_counter = 0
while True:
    print("Attempt number: {}".format(job_counter))
    ret = event.get_event(full=True)

    print("{}".format(ret))

    if ret is None:
        print('[_minion_event] No event data in packet')
        job_counter += 1
        continue
    data = ret.get('data', False)
예제 #38
0
    def listen(self):
        '''
        the main event loop where we receive the events and
        start the workers that dump our data into the database
        '''
        # log on to saltstacks event-bus
        event = salt.utils.event.SaltEvent(self.node,
                                           self.sock_dir)

        # we store our events in a list, we dont really care about an order
        # or what kind of data is put in there. all that is configured with the
        # sql-template configured in the configfile
        event_queue = []

        # start our dump_timer
        self.ev_timer.start()

        # this is for logline chronology so the timer-message always comes
        # _before_ the actual startup-message of the listening loop below :-)
        time.sleep(1)

        log.info("entering main event loop")
        log.info("listening on: {0}".format(event.puburi))

        # read everything we can get our hands on
        while True:

            # the zmq-socket does not like ^C very much, make the error
            # a little more graceful. alright, alright, ignore the damn thing,
            # we're exiting anyways...
            try:
                ret = event.get_event(full=True)
            except zmq.ZMQError:
                pass

            if ret is None:
                continue

            # if the timer has expired, we may have not received enough
            # events in the queue to reach event_limit, in that case we dump
            # the data anyway to have it in the database
            if(self.ev_timer_ev):
                if (len(self.running_workers) < self.max_workers) and \
                   (len(event_queue) > 0):

                    self._init_worker(event_queue)

                    # reset our queue to prevent duplicate entries
                    del event_queue[:]

                    # we reset the timer.ev_timer_ev  at the end of the loop 
                    # so we can update the stats that are logged


            # filter only the events we're interested in. all events have a tag 
            # we can filter them by. we match with a precompiled regex
            if( 'tag' in ret ):

                # filter out events with an empty tag. those are special
                if( ret['tag'] != '' ):
                       
                    # run through our configured events and try to match the 
                    # current events tag against the ones we're interested in
                    for key in self.event_map.keys():
                        if( self.event_map[key]['tag'].match(ret['tag'])):
                            log.debug("matching on {0}:{1}".format(key, 
                                                                   ret['tag']))

                            prio = self.event_map[key].get('prio', 0)

                            # push prio1-events directly into a worker
                            if prio > 0:
                                log.debug('Prio1 event found, pushing immediately!')
                                self.events_han += 1
                                self._init_worker([ret])
                            else:
                                event_queue.append(ret)
                                self.events_han += 1

            # once we reach the event_limit, start a worker that
            # writes that data in to the database
            if len(event_queue) >= self.event_limit:

                # only start a worker if not too many workers are running
                if len(self.running_workers) < self.max_workers:
                    self._init_worker(event_queue)
                    # reset the timer
                    self.ev_timer.reset()

                    # reset our queue to prevent duplicate entries
                    del event_queue[:]

                else:
                    # FIXME: we need to handle this situation somehow if
                    # too many workers are running. just flush the events?
                    # there really is no sane way except queueing more and more
                    # until some sort of limit is reached and we care more about
                    # our saltmaster than about the collected events!
                    log.critical("too many workers running, loosing data!!!")
                   
            # a list for the workers that are still running
            clean_workers = []

            # run through all the workers and join() the ones
            # that have finished dumping their data and keep
            # the running ones on our list
            for worker in self.running_workers:
                if worker.isAlive():
                    clean_workers.append(worker)
                else:
                    worker.join()
                    log.debug("joined worker #{0}".format(worker.getName()))
                    self.threads_join += 1

            # get rid of the old reference  and set a new one
            # FIXME: is this really neccessary?
            del self.running_workers

            self.running_workers = clean_workers
            self.events_rec += 1

            # we update the stats every 'received div handled == 0'
            # or if we recevied a timer event from our ResetTimer
            if( (self.events_rec % self.state_upd) == 0 ):
                self._write_state()
            elif(self.ev_timer_ev):
                self._write_state()
                self.ev_timer_ev = False

        log.info("listen loop ended...")                
예제 #39
0
        sys.exit(1)

    event = salt.utils.event.get_event('minion',
                                       sock_dir=opts['sock_dir'],
                                       transport=opts['transport'],
                                       listen=True,
                                       opts=opts)
    event.subscribe(tag=RESPONSE_TAG, match_type='fnmatch')

    for idx in range(RETRIES):
        print("Requesting certificate from server. [{0}/{1}]".format(
            idx + 1, RETRIES))
        event.fire_master({}, REQUEST_TAG)  # send event to master
        data = event.get_event(full=False,
                               auto_reconnect=True,
                               no_block=False,
                               match_type='fnmatch',
                               tag=RESPONSE_TAG,
                               wait=WAIT_RESPONSE)
        if data:
            try:
                with open(args.destination, 'wb') as _file:
                    _file.write(data['data'].encode('utf8'))
                    print("Certificate saved to: {0}".format(args.destination))
            except Exception as ex:
                print("Unable to write to destination: " + ex.message)
                sys.exit(1)
            sys.exit(0)
    print("Certificate not received from server. Exit.")
    sys.exit(1)
예제 #40
0
import warnings
import requests
import urllib3

requests.packages.urllib3.disable_warnings()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

local = salt.client.LocalClient()
caller = salt.client.Caller()
pillardata = caller.function('pillar.items')

event_trigger = sys.argv[1]
env = sys.argv[2]

opts = salt.config.client_config('/etc/salt/master')
event = salt.utils.event.get_event('master',
                                   sock_dir=opts['sock_dir'],
                                   transport=opts['transport'],
                                   opts=opts)

for minion in pillardata[env].iteritems():
    while True:
        data = event.get_event(tag=event_trigger)
        if data is None:
            continue
        else:
            data = local.cmd('saltmaster*', 'mine.get',
                             [data['id'], 'network.ip_addrs'])
            print data
            break
예제 #41
0
    :return: if the event occured atleast <amount> of times (bool)
    '''
    opts = salt.config.client_config('/etc/salt/master')

    event = salt.utils.event.get_event('master',
                                       sock_dir=opts['sock_dir'],
                                       transport=opts['transport'],
                                       opts=opts)
    counter = 0
    timeout = time.time() + wait
    if case is not None:
        oatspsql.update_case(case,
                             solution='Waiting for {0} `{1}` events.'.format(
                                 amount, error))
    while time.time() < timeout:
        if event.get_event(wait=3, tag=tag):
            counter += 1
    success = counter >= amount
    if case is not None:
        oatspsql.update_case(
            case,
            solution='Result: {0} events. Wait success: {1}.'.format(
                counter, success))
    return success


def wait_for_event(tag, wait=10, case=None):
    '''
    Wait the given time for an event.
    :param tag: the event tag to wait for.
    :param wait: the amount of time to wait for.
예제 #42
0
import MySQLdb
###
#f = open('track_num.conf')
#track = f.read().strip('\n')
#f.close()
###
__opts__ = salt.config.client_config('/etc/salt/master')
print __opts__['mysql.host']
# Create MySQL connect
#conn = MySQLdb.connect(host="127.0.0.1",user="******",passwd="qwert",db="cmdb",port=3306)
conn = MySQLdb.connect(host=__opts__['mysql.host'], user=__opts__['mysql.user'], passwd=__opts__['mysql.pass'], db=__opts__['mysql.db'], port=__opts__['mysql.port'])
cursor = conn.cursor()

# Listen Salt Master Event System
event = salt.utils.event.MasterEvent(__opts__['sock_dir'])
print "event",event.get_event()
for eachevent in event.iter_events(full=True):
    ret = eachevent['data']
    if "salt/job/" in eachevent['tag']:
        # Return Event
        if ret.has_key('id') and ret.has_key('return'):
            # Igonre saltutil.find_job event
            if ret['fun'] == "saltutil.find_job":
                continue

            sql = '''INSERT INTO `app_salt_return`
                (`fun`, `jid`, `result`, `host`, `success`, `full_ret` )
                VALUES ( %s, %s, %s, %s, %s, %s)'''
            cursor.execute(sql, (ret['fun'], ret['jid'],
                                 json.dumps(ret['return']), ret['id'],
                                 ret['success'], json.dumps(ret)))