Exemple #1
0
def connect(options):
    # port 5671 is for SSL, port 5672 is for TCP
    if options.keyfile is not None or options.certfile is not None:
        if options.keyfile is None:
            print("you must specify a key file for user with your cert file")
            sys.exit(1)
        if options.certfile is None:
            print("you must specify a cert file for user with your key file")
            sys.exit(1)
        if options.ca_certfile is None:
            print("you must specify a ca_certs file")
            sys.exit(1)
        print("connecting to %s:5671 with certificate and key" % options.server)

        return amqp.Connection(host="%s:%d" % (options.server,5671),
                               login_method="EXTERNAL",
                               virtual_host=options.vhost,
                               ssl={"keyfile":options.keyfile,
                                    "certfile":options.certfile,
                                    "cert_reqs":ssl.CERT_REQUIRED,
                                    "ca_certs":options.ca_certfile},
                               heartbeat=60)
    if options.password is not None and options.ask_pass:
        print("don't specify -p and -P")
        sys.exit(1)
    if options.password is not None or options.ask_pass:
        if options.user is None:
            print("you must specify a user if you provide a password")
            sys.exit(1)
    if options.user is not None:
        if options.password is None:
            if options.ask_pass:
                options.password = getpass.getpass("password for %s:" % options.user)
            else:
                print("you must specify a password along with a user")
                sys.exit(1)
        if options.ca_certfile is not None:
            print("connecting over SSL to %s:5671 with username and password" % options.server)
            return amqp.Connection(host="%s:%d" % (options.server,5671),
                                   userid=options.user,
                                   password=options.password,
                                   virtual_host=options.vhost,
                                   ssl={"cert_reqs":ssl.CERT_REQUIRED,
                                        "ca_certs":options.ca_certfile},
                                   heartbeat=60)
        else:
            print("connecting over TCP to %s:5672 with username and password" % options.server)
            return amqp.Connection(host="%s:%d" % (options.server,5672),
                                   userid=options.user,
                                   password=options.password,
                                   virtual_host=options.vhost,
                                   heartbeat=60)
    else:
        print("connecting to %s:5672 anonymously" % options.server)
        return amqp.Connection(host="%s:%d" % (options.server,5672),
                               virtual_host=options.vhost,
                               heartbeat=60)
Exemple #2
0
def get_connection(
        hostname, port, vhost, use_tls=False, keyfile=None, certfile=None):
    host = '%s:%s' % (hostname, port)
    if use_tls:
        return amqp.Connection(host=host, vhost=vhost, ssl={
            'keyfile': keyfile,
            'certfile': certfile
        }
        )
    else:
        return amqp.Connection(host=host, vhost=vhost)
Exemple #3
0
    def __init__(self, uri="amqp://*****:*****@localhost:5672", exchange="is"):
        url = urllib.parse.urlparse(uri)

        self.connection = amqp.Connection(
            host="{}:{}".format(url.hostname or "localhost", url.port or 5672),
            userid=url.username or "guest",
            password=url.password or "guest",
            virtual_host='/'
            if not url.path or url.path == '/' else url.path[1:],
            connect_timeout=5.0,
        )
        self.connection.connect()

        self._channel = self.connection.channel()
        self._channel.auto_decode = False

        self._exchange = exchange
        self._channel.exchange_declare(
            exchange=self._exchange,
            type="topic",
            durable=False,
            auto_delete=False,
        )

        self.subscriptions = []
        self.amqp_message = None
Exemple #4
0
 def Connect_AMQP_UserPass(self):
     ssl_opts = {
         'ca_certs': os.environ.get('X509_USER_CERT'),
         'ssl_version': ssl.PROTOCOL_TLSv1_2
     }
     for host in [
             self.config['AMQP_PRIMARY'], self.config['AMQP_FALLBACK']
     ]:
         try:
             eprint('AMQP connecting to host={} as userid={}'.format(
                 host, self.config['AMQP_USERID']))
             self.AMQP_connection = amqp.Connection(
                 login_method='AMQPLAIN',
                 host=host,
                 virtual_host='xsede',
                 userid=self.config['AMQP_USERID'],
                 password=self.config['AMQP_PASSWORD'],
                 heartbeat=120,
                 ssl=ssl_opts)
             self.AMQP_connection.connect()
             self.AMQP_channel = self.AMQP_connection.channel()
             return self.AMQP_channel
         except Exception as ex:
             eprint('AMQP connect error: ' + format(ex))
     eprint('Failed to connect to all AMQP services')
     sys.exit(1)
def stopSCMB(host):
    logging.info("ovincidents: stopSCMB: Start")

    EXCHANGE_NAME = 'scmb'
    dest = host + ':5671'

    # Setup our ssl options
    ssl_options = ({
        'ca_certs': 'certs/' + host + '-caroot.pem',
        'certfile': 'certs/' + host + '-client.pem',
        'keyfile': 'certs/' + host + '-key.pem',
        'cert_reqs': ssl.CERT_REQUIRED,
        'ssl_version': ssl.PROTOCOL_TLSv1_1,
        'server_side': False
    })

    logging.info("ovincidents : recv: ssl_options %s", ssl_options)

    # Connect to RabbitMQ
    conn = amqp.Connection(dest, login_method='EXTERNAL', ssl=ssl_options)

    ch = conn.channel()
    qname, _, _ = ch.queue_declare()
    ch.queue_bind(qname, EXCHANGE_NAME, 'scmb.#')

    # Send a message to end this queue
    # This code written based on info provided by https://www.rabbitmq.com/consumer-cancel.html
    # basic_cancel(msg.consumer_tag)
    print(ch)
    ch.basic_cancel(None, None)
    ch.close()

    logging.info("ovincidents: stopSCMB: End")
Exemple #6
0
def recv(host, route):
    # Create and bind to queue
    EXCHANGE_NAME = 'scmb'
    dest = host + ':5671'

    # Setup our ssl options
    ssl_options = ({
        'ca_certs': 'caroot.pem',
        'certfile': 'client.pem',
        'keyfile': 'key.pem',
        'cert_reqs': ssl.CERT_REQUIRED,
        'server_side': False
    })

    # Connect to RabbitMQ
    conn = amqp.Connection(dest, login_method='EXTERNAL', ssl=ssl_options)
    conn.connect()

    ch = conn.channel()
    qname, _, _ = ch.queue_declare()
    ch.queue_bind(qname, EXCHANGE_NAME, route)
    ch.basic_consume(qname, callback=partial(callback, ch))

    # Start listening for messages
    while ch.callbacks:
        ch.wait(amqp.spec.Queue.BindOk)

    ch.close()
    conn.close()
Exemple #7
0
 def __connect(self):
     self.conn = amqp.Connection(**self.params)
     self.chan = self.conn.channel()
     self.chan.exchange_declare(exchange=self.exchange_name,
                                type=self.exchange_type,
                                auto_delete=False,
                                durable=True)
Exemple #8
0
    def connect(self):
        """
        Connects to the given AMQP instance, defined in `self._config`.

        :returns Bool: True if connection and channel is established successfully.
        """
        params = self._build_connection_params()

        self._connection = amqp.Connection(**params)

        # handle older and newer versions of amqp
        try:
            self._connection.connect()
        except:  # NOQA
            pass

        self._channel = self._connection.channel()

        is_valid_connection = self._connection and self._channel

        if is_valid_connection:
            __log__.info("Connection successfully created.")
        else:
            __log__.error("FAILED to create connection or channel!")

        return is_valid_connection
Exemple #9
0
def connection(request):
    host = '%s:%s' % (
        os.environ.get('RABBITMQ_HOST', 'localhost'),
        os.environ.get('RABBITMQ_5672_TCP', '5672')
    )
    vhost = getattr(request.config, "slaveinput", {}).get("slaveid", None)
    return amqp.Connection(host=host, vhost=vhost)
Exemple #10
0
def submit_job(app_id, app_key, task_id, mimetype, text_col, dedupe):
    """Submit a job to the queue for the Celery worker. Create the required JSON message and post it to RabbitMQ."""
    # These are the args that the Python function in the adjunct processor will use.
    kwargs = {
        "app_id": app_id,
        "app_key": app_key,
        "task_id": task_id,
        "format": mimetype,
        "text_col": text_col,
        "dedupe": dedupe,
        "s3_endpoint": S3ENDPOINT,
        "bucket": BUCKET,
        "redis_port": REDISPORT,
        "redis_host": REDISHOST
    }
    # Recreate a celery message manually so that we don't need to import celery_tasks.py which has heavy dependencies.
    job = {
        "id": task_id,
        # "task": "synapsify_adjunct.celery_tasks.synapsify_master",
        "task": "dc2_master",
        "kwargs": kwargs
    }
    # Connect to RabbitMQ and post.
    conn = amqp.Connection(host=RMQHOST,
                           port=RMQPORT,
                           userid=RMQUSERNAME,
                           password=RMQPASSWORD,
                           virtual_host=RMQVHOST,
                           insist=False)
    cha = conn.channel()
    msg = amqp.Message(json.dumps(job))
    msg.properties["content_type"] = "application/json"
    cha.basic_publish(routing_key=RMQEXCHANGE, msg=msg)
    cha.close()
    conn.close()
Exemple #11
0
    def _connect(self, service):
        if self.connection is not None:
            self._close()

        toks = service.split(":")
        host = toks[0]
        try:
            port = int(toks[1])
        except:
            if self.ssl_options is None:
                port = 5672
            else:
                port = 5671

        if self.ssl_options is None:
            ssl = False
            login_method = "AMQPLAIN"
        else:
            ssl = self.ssl_options
            if "certfile" in ssl:
                login_method = "EXTERNAL"
            else:
                login_method = "AMQPLAIN"

        self.connection = amqp.Connection(host="%s:%d" % (host, port),
                                          login_method=login_method,
                                          userid=self.username,
                                          password=self.password,
                                          virtual_host=self.vhost,
                                          ssl=ssl,
                                          heartbeat=60,
                                          confirm_publish=True)
        self.channel = self.connection.channel()
Exemple #12
0
 def connect(self):
     """
     连接rabbitmq服务器
     """
     k = self.symbol()
     if k not in connection_pool:
         self.connection = amqp.Connection(
             host="%s:%s" % (self.host, self.port),
             userid=self.username or 'guest',
             password=self.password or 'guest',
             virtual_host=unquote(self.path.lstrip('/') or '%2F'))
         self.connection.connect()
         self.channel = self.connection.channel()
         connection_pool[k] = self.connection
         channel_pool[k] = self.channel
     else:
         self.connection = connection_pool[k]
         if not self.connection.connected:
             del connection_pool[k]
             channel_pool[k].close()
             del channel_pool[k]
             self.connect()
         else:
             self.channel = channel_pool[k]
     try:
         self.channel.queue_declare(self.queuename, durable=True)
     except amqp.exceptions.PreconditionFailed:
         pass
Exemple #13
0
 def conn(self):
     """Find or create current greenlet's AMQP connection"""
     gid = self.greenlet_id
     if gid not in self.conns:
         log.debug("%s: Opening new AMQP connection.", self.lbl)
         self.conns[gid] = amqp.Connection(config.AMQP_URI)
     return self.conns[gid]
Exemple #14
0
 def connect(self):
     try:
         self._connection = amqp.Connection(**self.connkwargs)
         self._connection.connect()
         self._channel = self._connection.channel()
     except Exception:
         raise ConnectionError('Connect failed')
Exemple #15
0
def main():
    """Creates connection to amqp on HOST to channel DRIVE, """
    parser = ArgumentParser()
    parser.add_argument('--config', '-c', required=True, type=FileType('r'))
    parser.add_argument('--host', '-h', default=HOST)
    parser.add_argument('--queue', '-q', default=DRIVE_QUEUE)
    parser.add_argument('--device', '-d', default=None)
    args = parser.parse_args()

    connection = amqp.Connection(args.host)
    channel = connection.channel()
    channel.queue_declare(queue=args.queue)

    # maybe add controller callibration??
    if args.device is None:
        args.device = DEVICE
    device = evdev.device.InputDevice(args.device)  # get evdev device
    config = json.load(args.config)
    args.config.close()

    events = ((config[str(event.code)], event.value)
              for event in device.read_loop())
    events = (event for event in events if event[0] in EVENT_TO_ACTION)
    for msg in messages(events):
        print(msg)
        channel.basic_publish(msg, routing_key=args.queue)
Exemple #16
0
def publish_messages():
    with amqp.Connection('localhost',
                         userid="iotile",
                         password="******",
                         confirm_publish=True) as c:

        ch = c.channel()

        with open("/opt/src/messages.txt", "r") as input_messages:
            line = input_messages.readline()

            while len(line) > 0:
                signal, site, machine, value = line.split(",")
                body = {"value": float(value)}
                jbody = json.dumps(body)

                message = amqp.Message(body=jbody,
                                       application_headers={
                                           "timestamp_in_ms":
                                           int(time.time() * 1000)
                                       })
                ch.basic_publish(
                    message,
                    exchange="amq.topic",
                    routing_key=f'raw.arch.{site}.{machine}.{signal}')
                time.sleep(0.1)
                line = input_messages.readline()
def main():
    parser = optparse.OptionParser()
    parser.add_option('-c',
                      '--config-file',
                      help='Galaxy configuration file',
                      dest='config_file',
                      action='store')
    parser.add_option(
        '-s',
        '--http-server-section',
        help='Name of the HTTP server section in the Galaxy configuration file',
        dest='http_server_section',
        action='store')
    (opts, args) = parser.parse_args()
    log.debug("GALAXY LISTENER PID: " + str(os.getpid()) + " - " + str(opts))
    # read the Galaxy config file
    global config_file_name
    config_file_name = opts.config_file
    global config
    config = ConfigParser.ConfigParser()
    config.read(opts.config_file)
    global http_server_section
    http_server_section = opts.http_server_section
    amqp_config = {}
    for option in config.options("galaxy_amqp"):
        amqp_config[option] = config.get("galaxy_amqp", option)
    log.debug(str(amqp_config))
    # connect
    conn = amqp.Connection(host=amqp_config['host'] + ":" +
                           amqp_config['port'],
                           userid=amqp_config['userid'],
                           password=amqp_config['password'],
                           virtual_host=amqp_config['virtual_host'])
    chan = conn.channel()
    chan.queue_declare(queue=amqp_config['queue'],
                       durable=True,
                       exclusive=True,
                       auto_delete=False)
    chan.exchange_declare(
        exchange=amqp_config['exchange'],
        type="direct",
        durable=True,
        auto_delete=False,
    )
    chan.queue_bind(queue=amqp_config['queue'],
                    exchange=amqp_config['exchange'],
                    routing_key=amqp_config['routing_key'])

    chan.basic_consume(queue=amqp_config['queue'],
                       no_ack=True,
                       callback=recv_callback,
                       consumer_tag="testtag")
    log.debug('Connected to rabbitmq server - ' + amqp_config['host'] + ":" +
              amqp_config['port'])
    while True:
        chan.wait()
    chan.basic_cancel("testtag")
    chan.close()
    conn.close()
Exemple #18
0
    def run(self, config_path: str, broker: str, device: str) -> "IO ()":
        connection = amqp.Connection(broker)
        channel = connection.channel()
        for queue in self.queues:
            channel.queue_declare(queue=queue, arguments=QUEUE_ARGS)

        device = evdev.device.InputDevice(device)
        super().run(config_path, device.read_loop(), partial(sink, channel))
 def setUp(self):
     self.exchange = 'test_exchange'
     self.type = 'direct'
     self.msg_body = 'test_message'
     self.routing_key = 'test_routing_key'
     self.connection = amqp.Connection()
     self.channel = self.connection.channel()
     self.channel.exchange_declare(exchange=self.exchange, type=self.type)
Exemple #20
0
def main():
    parser = OptionParser(
        usage='usage: %prog [options] message\nexample: %prog hello world', )
    parser.add_option(
        '--host',
        dest='host',
        help='AMQP server to connect to (default: %default)',
        default='localhost',
    )
    parser.add_option(
        '-u',
        '--userid',
        dest='userid',
        help='userid to authenticate as (default: %default)',
        default='guest',
    )
    parser.add_option(
        '-p',
        '--password',
        dest='password',
        help='password to authenticate with (default: %default)',
        default='guest',
    )
    parser.add_option(
        '--ssl',
        dest='ssl',
        action='store_true',
        help='Enable SSL (default: not enabled)',
        default=False,
    )

    options, args = parser.parse_args()

    if not args:
        parser.print_help()
        sys.exit(1)

    msg_body = ' '.join(args)

    conn = amqp.Connection(options.host,
                           userid=options.userid,
                           password=options.password,
                           ssl=options.ssl)

    ch = conn.channel()
    ch.exchange_declare('myfan', 'fanout')

    msg = amqp.Message(msg_body,
                       content_type='text/plain',
                       application_headers={
                           'foo': 7,
                           'bar': 'baz'
                       })

    ch.basic_publish(msg, 'myfan')

    ch.close()
    conn.close()
Exemple #21
0
    def drive(self) -> "IO ()":
        """Opens connect, channel and consumes until channel closed"""
        connection = amqp.Connection(self.host)
        channel = connection.channel()
        channel.queue_declare(queue=self.queue)
        channel.basic_consume(queue=self.queue, callback=self.callback)

        while channel.callbacks:
            channel.wait()
Exemple #22
0
def get_connection(hostname,
                   port,
                   vhost,
                   use_tls=False,
                   keyfile=None,
                   certfile=None,
                   ca_certs=None):
    host = f'{hostname}:{port}'
    if use_tls:
        return amqp.Connection(host=host,
                               vhost=vhost,
                               ssl={
                                   'keyfile': keyfile,
                                   'certfile': certfile,
                                   'ca_certs': ca_certs,
                               })
    else:
        return amqp.Connection(host=host, vhost=vhost)
Exemple #23
0
 def _connect(self):
     try:
         self.close()
     except Exception:
         pass
     self.connection = amqp.Connection(**self.connection_parameters)
     self.channel = self.connection.channel()
     if self.subscription:
         self._subscribe()
Exemple #24
0
 def reconnect(self):
     parsed = urlparse.urlparse(self.amqp_url)
     port = parsed.port or 5672
     self.connection = amqp.Connection(
         host="%s:%s" % (parsed.hostname, port),
         userid=parsed.username,
         password=parsed.password,
         virtual_host=unquote(parsed.path.lstrip('/')))
     self.channel = self.connection.channel()
     self.channel.queue_declare(self.name)
Exemple #25
0
 def ensure_chan(self):
     '''获取channel'''
     if not isinstance(self.con, amqp.connection.Connection):
         self.con = amqp.Connection(host=config.MQ_HOST, userid=config.MQ_UID, \
                    password=config.MQ_PWD, virtual_host=config.MQ_VHOST, insit=False)
     else:
         for k in self.con.channels:
             if isinstance(self.con.channels[k], amqp.channel.Channel):
                 return self.con.channels[k]
     return self.con.channel()
 def connect(self):
     if self._connection:
         return
     try:
         self._connection = amqp.Connection(**self.connkwargs)
         self._connection.connect()
         self._channel = self._connection.channel()
         self._register_handlers()
     except Exception:
         raise ConnectionError('Connect failed')
Exemple #27
0
def main():
    roomba = Create(BluetoothController('00:0A:3A:2E:C9:BB'))
    roomba.control()
    connection = amqp.Connection(HOST)
    channel = connection.channel()
    channel.queue_declare(queue=DRIVE_QUEUE)
    channel.basic_consume(queue=DRIVE_QUEUE, callback=partial(drive, roomba))

    while channel.callbacks:
        channel.wait()
Exemple #28
0
 def connection(self):
     """Returns a new connection as a context manager."""
     conn = amqp.Connection(
         host="%s:%s" % (self.config.RABBIT_HOST, self.config.RABBIT_PORT),
         userid=self.config.RABBIT_USER,
         password=self.config.RABBIT_PASSWORD,
         virtual_host=self.config.RABBIT_VIRTUAL_HOST)
     logger.info('Connected to RabbitMQ')
     with closing(conn):
         yield conn
Exemple #29
0
 def conn(self):
     """Find or create current greenlet's AMQP connection"""
     gid = self.greenlet_id
     if gid not in self.conns:
         log.debug("%s: Opening new AMQP connection.", self.lbl)
         conn = amqp.Connection(config.AMQP_URI)
         # TODO: The following line is needed for later versions of amqp lib
         # conn.connect()
         self.conns[gid] = conn
     return self.conns[gid]
Exemple #30
0
def server(dir, rate, broker, loglevel):
    """
    The server CLI. See README.
    """
    logging.basicConfig(level=loglevel.upper())
    
    monitorer = DirMonitorer(dir, rate)  # no such word...
    print(f"  Monitoring { dir } ...")
    
    server_id = str(uuid.uuid4())
    print(f"  Server ID for client connection:  { server_id }")
    
    exchange_in = f"{ server_id }-in"
    exchange_out = f"{ server_id }-out"
    
    if broker:
        c = amqp.Connection(host=broker)
    else:
        c = amqp.Connection()
    with c:
        ch = c.channel()
        
        # Setup full-state exchange and queue
        ch.exchange_declare(exchange=exchange_in, type="fanout")
        queue_in, *_ = ch.queue_declare(exclusive=True)
        ch.queue_bind(queue_in, exchange=exchange_in)
        ch.basic_consume(queue=queue_in, callback=lambda msg: monitorer.full_state(ch, msg))
        
        # Setup updates exchange
        ch.exchange_declare(exchange=exchange_out, type="fanout")
        
        print("  (Ctrl+C to stop)\n")
        while True:
            
            # Reply to full-state requests (why does it raise?... i don't get it...)
            try:
                c.drain_events(timeout=monitorer.delay_seconds)
            except socket.timeout:
                pass
            
            # Publish updates
            monitorer.publish_updates(ch, exchange_out)