Beispiel #1
0
def node_from_config(config):
    name = config['NodeName']
    signing_key = generate_signing_key(wifstr=read_key_file(config['KeyFile']))
    (gossip_host, gossip_port) = parse_listen_directives(config)['gossip']
    # stubbing endpoint address for now
    endpoint_addr = (None, None)
    nd = node.Node(address=(socket.gethostbyname(gossip_host), gossip_port),
                   identifier=generate_identifier(signing_key),
                   signingkey=signing_key,
                   name=name,
                   endpoint_address=endpoint_addr,
                   )
    return nd
Beispiel #2
0
def node_from_config(config):
    name = config['NodeName']
    signing_key = generate_signing_key(wifstr=read_key_file(config['KeyFile']))
    listen_info = config.get("Listen")
    (gossip_host, gossip_port) = parse_listen_directives(listen_info)['gossip']
    # stubbing endpoint address for now
    endpoint_addr = (None, None)
    nd = node.Node(
        address=(socket.gethostbyname(gossip_host), gossip_port),
        identifier=generate_identifier(signing_key),
        signingkey=signing_key,
        name=name,
        endpoint_address=endpoint_addr,
    )
    return nd
Beispiel #3
0
def initialize_web_server(listen_info, validator):
    # Parse the listen directives from the configuration so
    # we know what to bind HTTP protocol to
    listen_directives = parse_listen_directives(listen_info)

    if 'http' in listen_directives:
        static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                  "static_content")

        site = ApiSite(RootPage(validator, static_dir))
        interface = listen_directives['http'].host
        if interface is None:
            interface = ''
        LOGGER.info("listen for HTTP requests on (ip='%s', port=%s)",
                    interface, listen_directives['http'].port)
        reactor.listenTCP(listen_directives['http'].port,
                          site,
                          interface=interface)
Beispiel #4
0
def initialize_web_server(config, validator):
    # Parse the listen directives from the configuration so
    # we know what to bind HTTP protocol to
    listen_directives = parse_listen_directives(config)

    if 'http' in listen_directives:
        static_dir = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), "static_content")

        site = ApiSite(RootPage(validator, static_dir))
        interface = listen_directives['http'].host
        if interface is None:
            interface = ''
        LOGGER.info(
            "listen for HTTP requests on (ip='%s', port=%s)",
            interface,
            listen_directives['http'].port)
        reactor.listenTCP(
            listen_directives['http'].port,
            site,
            interface=interface)
    def launch(self, launch=True, genesis=False, daemon=False, delay=False,
               node=None):
        listen_directives = parse_listen_directives(self.config["Listen"])
        http_host = listen_directives['http'].host
        if "Endpoint" in self.config and self.config['Endpoint'] is not None:
            http_host = self.config["Endpoint"]["Host"]

        self.url = "http://{}:{}".format(http_host,
                                         listen_directives['http'].port)

        self.config['LogDirectory'] = self._data_dir
        self._log_file = os.path.join(self._data_dir,
                                      "{}-debug.log".format(self.name))

        if self.log_config:
            for v in self.log_config["handlers"].itervalues():
                if 'filename' in v:
                    filename = os.path.join(
                        self._data_dir,
                        "{}-{}".format(self.name,
                                       os.path.basename(v['filename'])))
                    v['filename'] = filename
                    # pick the last log file... not sure
                    # how we can pick a better one with out completely
                    # parsing the log config or adding our own entry
                    self._log_file = filename

            log_config_file = os.path.join(self._data_dir,
                                           "{}-log-config.js"
                                           .format(self.name))
            with open(log_config_file, 'w') as fp:
                json.dump(self.log_config, fp, sort_keys=True,
                          indent=4, separators=(',', ': '))
            self.config['LogConfigFile'] = log_config_file

        config_file_name = "{}.json".format(self.name)
        self._config_file = os.path.join(self._data_dir, config_file_name)
        with open(self._config_file, 'w') as fp:
            json.dump(self.config, fp, sort_keys=True,
                      indent=4, separators=(',', ': '))

        args = [
            sys.executable,  # Fix for windows, where script are not executable
            self._txn_validator,
            "--conf-dir",
            self._data_dir,
            "--config",
            config_file_name
        ]

        if genesis:
            args.append("--genesis")

        if daemon:
            args.append("--daemon")

        if delay:
            args.append("--delay-start")

        cmd_file = os.path.join(self._data_dir,
                                "{}.sh"
                                .format(self.name))
        with open(cmd_file, 'w') as fp:
            fp.write('#!/usr/bin/env bash\n')
            fp.write(' '.join(args))
            fp.write('\n')
        os.chmod(cmd_file, 0744)

        # redirect stdout and stderror
        self._stdout_file = os.path.join(self._data_dir,
                                         "{}.out".format(self.name))
        self._stderr_file = os.path.join(self._data_dir,
                                         "{}.err".format(self.name))

        self._command = " ".join(args)
        env = os.environ.copy()
        env["PYTHONPATH"] = os.pathsep.join(sys.path)
        if launch:
            self._output = open(self._stdout_file, 'w')
            self._outerr = open(self._stderr_file, 'w')
            self._handle = subprocess.Popen(args,
                                            stdout=self._output,
                                            stderr=self._outerr,
                                            env=env)
        else:
            self._handle = None
Beispiel #6
0
def parse_networking_info(config):
    '''
    Provides a DRY location for parsing a validator's intended network
    interface specifications from that validator's configuration
    Args:
        config: (dict) - fully resolved configuration dictionary (c.f.
            sawtooth.Config)
    Returns:
        (nd, http_port): an ordered pair, where:
            nd:         (gossip.Node)
            http_port:  (int) or (None)

    '''
    # Parse the listen directives from the configuration so
    # we know what to bind gossip protocol to
    listen_info = config.get("Listen")
    listen_directives = parse_listen_directives(listen_info)

    # If the gossip listen address is 0.0.0.0, then there must be
    # an Endpoint.Host entry as we don't know what to put in the
    # endpoint registry otherwise.
    if listen_directives['gossip'].host == '0.0.0.0' and \
            ('Endpoint' not in config or
             'Port' not in config['Endpoint']):
        raise Exception('gossip listen address is 0.0.0.0, but endpoint host '
                        'missing from configuration')

    gossip_host = listen_directives['gossip'].host
    gossip_port = listen_directives['gossip'].port

    # The endpoint host/port and HTTP port come from the listen data, but
    # can be overridden by the configuration.
    endpoint_host = gossip_host
    endpoint_port = gossip_port
    endpoint_http_port = None
    if 'http' in listen_directives:
        endpoint_http_port = listen_directives['http'].port

    # See if we need to override the endpoint data
    endpoint_cfg = config.get('Endpoint', None)
    if endpoint_cfg is not None:
        if 'Host' in endpoint_cfg:
            endpoint_host = endpoint_cfg['Host']
        if 'Port' in endpoint_cfg:
            endpoint_port = int(endpoint_cfg['Port'])
        if 'HttpPort' in endpoint_cfg:
            endpoint_http_port = int(endpoint_cfg['HttpPort'])

    # Finally, if the endpoint host is 'localhost', we need to convert it
    # because to another host, obviously 'localhost' won't mean "us"
    if endpoint_host == 'localhost':
        endpoint_host = socket.gethostbyname(endpoint_host)
    name = config['NodeName']
    addr = (socket.gethostbyname(gossip_host), gossip_port)
    endpoint_addr = (endpoint_host, endpoint_port)
    signingkey = signed_object.generate_signing_key(
        wifstr=config.get('SigningKey'))
    identifier = signed_object.generate_identifier(signingkey)
    nd = node.Node(address=addr,
                   identifier=identifier,
                   signingkey=signingkey,
                   name=name,
                   endpoint_address=endpoint_addr)
    return (nd, endpoint_http_port)
Beispiel #7
0
    def launch(self, launch=True, daemon=False, delay=False, node=None):
        listen_directives = parse_listen_directives(self.config["Listen"])
        http_host = listen_directives['http'].host
        if "Endpoint" in self.config and self.config['Endpoint'] is not None:
            http_host = self.config["Endpoint"]["Host"]

        self.url = "http://{}:{}".format(http_host,
                                         listen_directives['http'].port)

        self.config['LogDirectory'] = self._data_dir
        self._log_file = os.path.join(self._data_dir,
                                      "{}-debug.log".format(self.name))

        if self.log_config:
            for v in self.log_config["handlers"].itervalues():
                if 'filename' in v:
                    filename = os.path.join(
                        self._data_dir,
                        "{}-{}".format(self.name,
                                       os.path.basename(v['filename'])))
                    v['filename'] = filename
                    # pick the last log file... not sure
                    # how we can pick a better one with out completely
                    # parsing the log config or adding our own entry
                    self._log_file = filename

            log_config_file = os.path.join(
                self._data_dir, "{}-log-config.js".format(self.name))
            with open(log_config_file, 'w') as fp:
                json.dump(self.log_config,
                          fp,
                          sort_keys=True,
                          indent=4,
                          separators=(',', ': '))
            self.config['LogConfigFile'] = log_config_file

        config_file_name = "{}.json".format(self.name)
        self._config_file = os.path.join(self._data_dir, config_file_name)
        with open(self._config_file, 'w') as fp:
            json.dump(self.config,
                      fp,
                      sort_keys=True,
                      indent=4,
                      separators=(',', ': '))

        args = [
            sys.executable,  # Fix for windows, where script are not executable
            self._txn_validator,
            "--conf-dir",
            self._data_dir,
            "--config",
            config_file_name
        ]

        if daemon:
            args.append("--daemon")

        if delay:
            args.append("--delay-start")

        cmd_file = os.path.join(self._data_dir, "{}.sh".format(self.name))
        with open(cmd_file, 'w') as fp:
            fp.write('#!/usr/bin/env bash\n')
            fp.write(' '.join(args))
            fp.write('\n')
        os.chmod(cmd_file, 0744)

        # redirect stdout and stderror
        self._stdout_file = os.path.join(self._data_dir,
                                         "{}.out".format(self.name))
        self._stderr_file = os.path.join(self._data_dir,
                                         "{}.err".format(self.name))

        self._command = " ".join(args)
        env = os.environ.copy()
        env["PYTHONPATH"] = os.pathsep.join(sys.path)
        if launch:
            self._output = open(self._stdout_file, 'w')
            self._outerr = open(self._stderr_file, 'w')
            self._handle = subprocess.Popen(args,
                                            stdout=self._output,
                                            stderr=self._outerr,
                                            env=env)
        else:
            self._handle = None
Beispiel #8
0
def parse_networking_info(config):
    '''
    Provides a DRY location for parsing a validator's intended network
    interface specifications from that validator's configuration
    Args:
        config: (dict) - fully resolved configuration dictionary (c.f.
            sawtooth.Config)
    Returns:
        (nd, http_port): an ordered pair, where:
            nd:         (gossip.Node)
            http_port:  (int) or (None)

    '''
    # Parse the listen directives from the configuration so
    # we know what to bind gossip protocol to
    listen_info = config.get("Listen")
    listen_directives = parse_listen_directives(listen_info)

    # If the gossip listen address is 0.0.0.0, then there must be
    # an Endpoint.Host entry as we don't know what to put in the
    # endpoint registry otherwise.
    if listen_directives['gossip'].host == '0.0.0.0' and \
            ('Endpoint' not in config or
             'Port' not in config['Endpoint']):
        raise Exception(
            'gossip listen address is 0.0.0.0, but endpoint host '
            'missing from configuration')

    gossip_host = listen_directives['gossip'].host
    gossip_port = listen_directives['gossip'].port

    # The endpoint host/port and HTTP port come from the listen data, but
    # can be overridden by the configuration.
    endpoint_host = gossip_host
    endpoint_port = gossip_port
    endpoint_http_port = None
    if 'http' in listen_directives:
        endpoint_http_port = listen_directives['http'].port

    # See if we need to override the endpoint data
    endpoint_cfg = config.get('Endpoint', None)
    if endpoint_cfg is not None:
        if 'Host' in endpoint_cfg:
            endpoint_host = endpoint_cfg['Host']
        if 'Port' in endpoint_cfg:
            endpoint_port = int(endpoint_cfg['Port'])
        if 'HttpPort' in endpoint_cfg:
            endpoint_http_port = int(endpoint_cfg['HttpPort'])

    # Finally, if the endpoint host is 'localhost', we need to convert it
    # because to another host, obviously 'localhost' won't mean "us"
    if endpoint_host == 'localhost':
        endpoint_host = socket.gethostbyname(endpoint_host)
    name = config['NodeName']
    addr = (socket.gethostbyname(gossip_host), gossip_port)
    endpoint_addr = (endpoint_host, endpoint_port)
    signingkey = signed_object.generate_signing_key(
        wifstr=config.get('SigningKey'))
    identifier = signed_object.generate_identifier(signingkey)
    nd = node.Node(address=addr,
                   identifier=identifier,
                   signingkey=signingkey,
                   name=name,
                   endpoint_address=endpoint_addr)
    return (nd, endpoint_http_port)