def __init__(self, profile=None, **settings): # TODO: recognise IPv6 addresses explicitly self.__protocol = DEFAULT_PROTOCOL self.__secure = DEFAULT_SECURE self.__verify = DEFAULT_VERIFY self.__user = DEFAULT_USER self.__password = DEFAULT_PASSWORD self.__address = Address.parse("") self._apply_env_vars() if profile is None: pass elif isinstance(profile, string_types): self._apply_uri(profile) elif isinstance(profile, self.__class__): self._apply_settings(**{k: profile[k] for k in self._hash_keys}) elif isinstance(profile, Mapping): self._apply_settings(**profile) else: raise TypeError("Profile %r is neither a ConnectionProfile " "nor a string URI" % profile) self._apply_settings(**settings) if not self.address.port: addr = list(self.address) if self.protocol == "http": addr[1] = DEFAULT_HTTPS_PORT if self.secure else DEFAULT_HTTP_PORT else: addr[1] = DEFAULT_BOLT_PORT self.__address = Address(addr)
def open(cls, address, timeout=None, keep_alive=False, on_broken=None): """ Open a connection to a given network :class:`.Address`. :param address: :param timeout: :param keep_alive: :param on_broken: callback for when the wire is broken after a successful connection has first been established (this does not trigger if the connection never opens successfully) :returns: :class:`.Wire` object :raises WireError: if connection fails to open """ from socket import socket, SOL_SOCKET, SO_KEEPALIVE address = Address(address) s = socket(family=address.family) if keep_alive: s.setsockopt(SOL_SOCKET, SO_KEEPALIVE, 1) s.settimeout(timeout) try: s.connect(address) except (IOError, OSError) as error: raise_from(WireError("Cannot connect to %r" % (address, )), error) return cls(s, on_broken=on_broken)
def _apply_settings(self, uri=None, scheme=None, protocol=None, secure=None, verify=None, address=None, host=None, port=None, port_number=None, auth=None, user=None, password=None, **other): if uri: self._apply_uri(uri) if scheme: self._apply_scheme(scheme) if protocol: self._apply_protocol(protocol) if secure is not None: self.__secure = secure if verify is not None: self.__verify = verify if isinstance(address, tuple): self.__address = Address(address) elif address: self.__address = Address.parse(address) if host and port: self.__address = Address.parse("%s:%s" % (host, port)) elif host: self.__address = Address.parse("%s:%s" % (host, self.port)) elif port: self.__address = Address.parse("%s:%s" % (self.host, port)) if isinstance(auth, tuple): self.__user, self.__password = auth elif auth: self.__user, _, self.__password = auth.partition(":") if user: self.__user = user if password: self.__password = password if other: raise ValueError("The following settings are not supported: %r" % other)
def __init__(self, spec, image, auth, user): self.spec = spec self.image = image self.address = Address(("localhost", self.spec.bolt_port)) self.auth = auth self.profiles = { "bolt": ConnectionProfile(scheme="bolt", port=self.spec.bolt_port, auth=self.auth), "http": ConnectionProfile(scheme="http", port=self.spec.http_port, auth=self.auth), "https": ConnectionProfile(scheme="https", port=self.spec.https_port, auth=self.auth), } environment = {} if self.auth: environment["NEO4J_AUTH"] = "/".join(self.auth) environment["NEO4J_ACCEPT_LICENSE_AGREEMENT"] = "yes" for key, value in self.spec.config.items(): fixed_key = "NEO4J_" + key.replace("_", "__").replace(".", "_") environment[fixed_key] = value for key, value in self.spec.env.items(): environment[key] = value ports = { "7474/tcp": self.spec.http_port, "7473/tcp": self.spec.https_port, "7687/tcp": self.spec.bolt_port } if self.spec.debug_opts.port is not None: ports["5100/tcp"] = self.spec.debug_opts.port if self.spec.dir_spec: volumes = self.spec.dir_spec.volumes(self.spec.name) for path in volumes: try: makedirs(path) except OSError: pass else: volumes = None try: user = int(user) except TypeError: user = None except ValueError: # Note: this will only work on Unix. from pwd import getpwnam user = getpwnam(user).pw_uid def create_container(img): return docker.containers.create( img, detach=True, environment=environment, hostname=self.spec.fq_name, name=self.spec.fq_name, network=self.spec.service_name, ports=ports, user=user, volumes=volumes, ) try: self.container = create_container(self.image) except ImageNotFound: log.info("Downloading Docker image %r", self.image) docker.images.pull(self.image) self.container = create_container(self.image)
def test_address_from_address(): a1 = Address.parse("[::1]:http") a2 = Address(a1) assert a2 is a1
def test_address_with_unknown_port_name(): a = Address(("localhost", "x")) with raises(ValueError): _ = a.port_number
def test_address_with_wrong_number_of_parts(): with raises(ValueError): _ = Address((1, 2, 3, 4, 5))
def remote_address(self): """ The remote :class:`.Address` to which this connection is bound. """ return Address(self.__socket.getpeername())
def local_address(self): """ The local :class:`.Address` to which this connection is bound. """ return Address(self.__socket.getsockname())