Ejemplo n.º 1
0
    def start(self,
              host: str,
              port: int,
              web_services: bool = True,
              distribution: bool = True,
              crash_on_error: bool = False):

        self.crash_on_error = crash_on_error

        if self.start_time is not NOT_RUNNING:
            raise RuntimeError("Felix is already running.")

        self.start_time = maya.now()
        payload = {"wsgi": self.rest_app, "http_port": port}
        deployer = HendrixDeploy(action="start", options=payload)
        click.secho(f"Running {self.__class__.__name__} on {host}:{port}")

        if distribution is True:
            self.start_distribution()

        if web_services is True:
            deployer.run()  # <-- Blocking call (Reactor)
Ejemplo n.º 2
0
def test_max_upload_bytes():

    statics_path = 'path_on_disk/to/files'
    os.makedirs(statics_path, exist_ok=True)

    options = {
        'wsgi': application,
        'max_upload_bytes': 200,
        'http_port': 9876,
        'resources': [MediaResource(statics_path, namespace='statics')]
    }
    deployer = HendrixDeploy(options=options)
    deployer.addServices()
    deployer.start()

    def reject_large_uploads():

        # uploading 50 bytes is fine.
        byte_count = 50
        ok_data = io.BytesIO(os.urandom(byte_count))
        response = requests.post("http://localhost:9876/",
                                 files={'data': ok_data})
        assert 200 == response.status_code

        # upload more than our max bytes and we fail with a 413
        byte_count = 201
        too_big = io.BytesIO(os.urandom(byte_count))
        response = requests.post("http://localhost:9876/",
                                 files={'data': too_big})
        assert 413 == response.status_code
        assert response.reason == "Request Entity Too Large"

    def test_static_files():

        js_file = 'showmethisfile.js'
        filepath = os.path.join(statics_path, js_file)

        open(filepath, 'w').write('//console.log("Hello World");')
        response = requests.get(f"http://localhost:9876/statics/{js_file}", )
        assert response.status_code == 200
        assert '//console.log("Hello World");' in response.text
        os.remove(filepath)
        os.removedirs(statics_path)

        response = requests.get(f"http://localhost:9876/statics/{js_file}", )
        assert response.status_code == 404

    yield threads.deferToThread(reject_large_uploads)
    yield threads.deferToThread(test_static_files)
Ejemplo n.º 3
0
    def start(self, ws_port: int, http_port: int, dry_run: bool = False):

        #
        # Websocket Service
        #

        def send_states(subscriber):
            message = ["states", self.known_nodes.abridged_states_dict()]
            subscriber.sendMessage(json.dumps(message).encode())

        def send_nodes(subscriber):
            message = ["nodes", self.known_nodes.abridged_nodes_dict()]
            subscriber.sendMessage(json.dumps(message).encode())

        websocket_service = hey_joe.WebSocketService("127.0.0.1", ws_port)
        websocket_service.register_followup("states", send_states)
        websocket_service.register_followup("nodes", send_nodes)

        #
        # WSGI Service
        #

        self.rest_app = Flask("fleet-monitor", template_folder=TEMPLATES_DIR)
        rest_app = self.rest_app

        @rest_app.route("/")
        def status():
            try:
                return render_template('monitor.html')
            except Exception as e:
                self.log.debug(str(e))

        #
        # Server
        #

        deployer = HendrixDeploy(action="start",
                                 options={
                                     "wsgi": rest_app,
                                     "http_port": http_port
                                 })
        deployer.add_non_tls_websocket_service(websocket_service)

        click.secho(f"Running Moe on 127.0.0.1:{http_port}")

        if not dry_run:
            deployer.run()
Ejemplo n.º 4
0
tp = reactor.suggestThreadPoolSize(1000)

# begin chdir armor
up = os.path.dirname(os.path.abspath(__file__))
testdir = os.path.dirname(up)
hendrix_app_dir = os.path.dirname(testdir)
hendrix_package_dir = os.path.dirname(hendrix_app_dir)
sys.path.insert(0, hendrix_package_dir)
# end chdir armor

from hendrix.deploy.base import HendrixDeploy

import threading


def delay(request):
    delay_time = float(request.matchdict['seconds'])
    print "Thread %s sleeping for %s seconds" % (threading.current_thread(), delay_time)
    time.sleep(delay_time)
    return Response('')

if __name__ == '__main__':
    config = Configurator()
    config.add_route('delay', '/{seconds}')
    config.add_view(delay, route_name='delay')

    app = config.make_wsgi_app()
    tp = ThreadPool(name="Big and Slow.", maxthreads=1000)
    deployer = HendrixDeploy(options={'wsgi': app, 'http_port': 8010}, threadpool=tp)
    deployer.run()
Ejemplo n.º 5
0
    avg_duration = float(previous_duration + duration) / float(total_requests)

    print("Average duration after %s: %s" % (total_requests, avg_duration))


class PerformanceTest(object):
    pool = Pool(20)

    def view(self, request):
        # option 1
        # @crosstown_traffic()
        # def wait():
        #     long()

        # option 2

        self.pool.apply_async(long)

        return Response()


config = Configurator()
config.add_route('test_view', '/')
config.add_view(PerformanceTest().view, route_name='test_view')

app = config.make_wsgi_app()

if __name__ == '__main__':
    deployer = HendrixDeploy(options={'wsgi': app})
    deployer.run()
Ejemplo n.º 6
0
from hendrix.deploy.base import HendrixDeploy
from hendrix.experience import hey_joe

deployer = HendrixDeploy(options={'wsgi': 'example_app.wsgi.application', 'http_port': 7575})

websocket_service = hey_joe.WebSocketService("127.0.0.1", 9000)
deployer.add_non_tls_websocket_service(websocket_service)

deployer.run()

Ejemplo n.º 7
0
monitor.start_learning_loop()


def send_states(subscriber):
    message = ["states", monitor.known_nodes.abridged_states_dict()]
    subscriber.sendMessage(json.dumps(message).encode())


def send_nodes(subscriber):
    message = ["nodes", monitor.known_nodes.abridged_nodes_dict()]
    subscriber.sendMessage(json.dumps(message).encode())


websocket_service.register_followup("states", send_states)
websocket_service.register_followup("nodes", send_nodes)


@rest_app.route("/")
def status():
    return render_template('monitor.html')


deployer = HendrixDeploy(action="start",
                         options={
                             "wsgi": rest_app,
                             "http_port": 9750
                         })
deployer.add_non_tls_websocket_service(websocket_service)
deployer.run()
Ejemplo n.º 8
0
hendrix_app_dir = os.path.dirname(testdir)
hendrix_package_dir = os.path.dirname(hendrix_app_dir)
sys.path.insert(0, hendrix_package_dir)
# end chdir armor

from hendrix.deploy.base import HendrixDeploy

import threading


def delay(request):
    delay_time = float(request.matchdict['seconds'])
    print "Thread %s sleeping for %s seconds" % (threading.current_thread(),
                                                 delay_time)
    time.sleep(delay_time)
    return Response('')


if __name__ == '__main__':
    config = Configurator()
    config.add_route('delay', '/{seconds}')
    config.add_view(delay, route_name='delay')

    app = config.make_wsgi_app()
    tp = ThreadPool(name="Big and Slow.", maxthreads=1000)
    deployer = HendrixDeploy(options={
        'wsgi': app,
        'http_port': 8010
    },
                             threadpool=tp)
    deployer.run()
Ejemplo n.º 9
0
'''
Runs the test WSGI application on a real HendrixDeploy.

Useful for poking and prodding to discover how to cover those hard-to-reach
parts of the service flow.
'''

from resources import application

from hendrix.deploy.base import HendrixDeploy

from twisted.internet import reactor

if __name__ == "__main__":
    threadPool = reactor.getThreadPool()
    threadPool.adjustPoolsize(3, 5)
    options = {'wsgi': application}
    deployer = HendrixDeploy(options=options)
    deployer.run()
Ejemplo n.º 10
0
def bob(click_config, action, quiet, teacher_uri, min_stake, http_port,
        discovery_port, federated_only, network, config_root, config_file,
        provider_uri, registry_filepath, dev, force, dry_run, label,
        policy_encrypting_key, alice_encrypting_key):
    """
    Start and manage a "Bob" character.
    """

    if not quiet:
        click.secho(BOB_BANNER)

    if action == 'init':
        """Create a brand-new persistent Bob"""

        if dev and not quiet:
            click.secho("WARNING: Using temporary storage area", fg='yellow')

        if not config_root:  # Flag
            config_root = click_config.config_file  # Envvar

        bob_config = BobConfiguration.generate(
            password=click_config.get_password(confirm=True),
            config_root=config_root,
            rest_host="localhost",
            domains={network} if network else None,
            federated_only=federated_only,
            no_registry=True,  # Yes we have no registry,
            registry_filepath=registry_filepath,
            provider_uri=provider_uri,
        )

        if not quiet:
            click.secho("Generated keyring {}".format(bob_config.keyring_dir),
                        fg='green')
            click.secho("Saved configuration file {}".format(
                bob_config.config_file_location),
                        fg='green')

            # Give the use a suggestion as to what to do next...
            how_to_run_message = "\nTo run an Bob node from the default configuration filepath run: \n\n'{}'\n"
            suggested_command = 'nucypher bob run'
            if config_root is not None:
                config_file_location = os.path.join(
                    config_root, config_file
                    or BobConfiguration.CONFIG_FILENAME)
                suggested_command += ' --config-file {}'.format(
                    config_file_location)
            click.secho(how_to_run_message.format(suggested_command),
                        fg='green')
            return  # FIN

        else:
            click.secho("OK")

    elif action == "destroy":
        """Delete all configuration files from the disk"""

        if dev:
            message = "'nucypher ursula destroy' cannot be used in --dev mode"
            raise click.BadOptionUsage(option_name='--dev', message=message)

        destroy_system_configuration(config_class=BobConfiguration,
                                     config_file=config_file,
                                     network=network,
                                     config_root=config_root,
                                     force=force)
        if not quiet:
            click.secho("Destroyed {}".format(config_root))
        return

    #
    # Get Bob Configuration
    #

    if dev:
        bob_config = BobConfiguration(
            dev_mode=True,
            domains={network},
            provider_uri=provider_uri,
            federated_only=True,
        )
    else:
        bob_config = BobConfiguration.from_configuration_file(
            filepath=config_file,
            domains={network or GLOBAL_DOMAIN},
            rest_port=discovery_port,
            provider_uri=provider_uri)

    # Teacher
    teacher_nodes = list()
    if teacher_uri:
        teacher_node = Ursula.from_teacher_uri(
            teacher_uri=teacher_uri,
            min_stake=min_stake,
            federated_only=bob_config.federated_only)
        teacher_nodes.append(teacher_node)

    # Produce
    BOB = bob_config(known_nodes=teacher_nodes)

    if action == "run":

        if not dev:
            # Keyring
            try:
                click.secho("Decrypting keyring...", fg='blue')
                bob_config.keyring.unlock(password=click_config.get_password())
            except CryptoError:
                raise bob_config.keyring.AuthenticationFailed
            finally:
                click_config.bob_config = bob_config

        # Bob Control
        bob_control = BOB.make_wsgi_app()
        click.secho("Starting Bob Character Control...")

        click.secho(f"Bob Verifying Key {bytes(BOB.stamp).hex()}",
                    fg="green",
                    bold=True)
        click.secho(
            f"Bob Encrypting Key {bytes(BOB.public_keys(DecryptingPower)).hex()}",
            fg="blue",
            bold=True)

        # Run
        if dry_run:
            return

        hx_deployer = HendrixDeploy(action="start",
                                    options={
                                        "wsgi": bob_control,
                                        "http_port": http_port
                                    })
        hx_deployer.run()  # <--- Blocking Call to Reactor

    elif action == "view":
        """Paint an existing configuration to the console"""
        paint_configuration(
            config_filepath=config_file or bob_config.config_file_location)
        return

    elif action == "retrieve":
        bob_request_data = {
            'label': b64encode(label).decode(),
            'policy_encrypting_pubkey': policy_encrypting_key,
            'alice_signing_pubkey': alice_encrypting_key,
            # 'message_kit': b64encode(bob_message_kit.to_bytes()).decode(),  # TODO
        }

        response = requests.post('/retrieve',
                                 data=json.dumps(bob_request_data))
        click.secho(response)
        return

    else:
        raise click.BadArgumentUsage(f"No such argument {action}")
Ejemplo n.º 11
0
from hendrix.contrib. async .messaging import hxdispatcher
from hendrix.deploy.base import HendrixDeploy

# from txsockjs.factory import SockJSResource
from hendrix.contrib. async .resources import MessageHandlerProtocol
from hendrix.facilities.resources import NamedResource
from twisted.internet.protocol import Factory

# message_resource = NamedResource('hendrix-demo')
# message_resource.putChild('messages', SockJSResource(Factory.forProtocol(MessageHandlerProtocol)))

from twisted.conch.telnet import TelnetTransport, TelnetProtocol
from twisted.internet.protocol import ServerFactory

# class TelnetToWebsocket(TelnetProtocol):

#     def dataReceived(self, data):
#         hxdispatcher.send('ladder_channels', data)

# telnet_server_factory = ServerFactory()
# telnet_server_factory.protocol = lambda: TelnetTransport(TelnetToWebsocket)

deployer = HendrixDeploy(options={'wsgi': 'ladder.wsgi.application'})
# deployer.resources.append(message_resource)
# deployer.reactor.listenTCP(6565, telnet_server_factory)
deployer.run()