Exemplo n.º 1
0
    def _ensure_net_exists(self):
        """
            Ensures that the network image and docker network exists.
        """

        try:
            self.get_client().images.get(config.net_container_name())
            self.get_client().networks.get(config.net_name())
        except ImageNotFound as _:
            console.info(
                f'network image [bold]{config.net_container_name()}[/] does not exist, quickly building it'
            )
            _, logs = self.get_client().images.build(
                path=str(NETWORK_CONTAINER_PATH),
                pull=True,
                tag=config.net_container_name(),
                rm=True,
                forcerm=True)

            for log in logs:
                console.debug(log)

            console.info(
                f'network container [bold]{config.net_container_name()}[/] built'
            )
            self._ensure_net_exists()

        except NotFound as _:
            console.info(
                f'docker network [bold]{config.net_name()}[/] does not exist, creating it'
            )
            self.get_client().networks.create(name=config.net_name(),
                                              check_duplicate=True)
            self._ensure_net_exists()
Exemplo n.º 2
0
def check():
    """
        Check plans and Docker environment
    """

    # plans
    loader = Loader()
    console.info(f'loaded [bold]{len(loader.valid_plans())}[/] valid plans')

    # docker
    try:
        client = docker.from_env()
        info = client.info()
        console.info(f'docker server version: [bold]{info.get("ServerVersion")}[/]')

        # network container
        client.images.get(config.net_container_name())
        console.info(f'network image [bold]\'{config.net_container_name()}\'[/] exists')

        # dwn docker  network
        client.networks.get(config.net_name())
        console.info(f'docker network [bold]\'{config.net_name()}\'[/] exists')

    except ImageNotFound as _:
        console.warn(f'network image [bold]\'{config.net_container_name()}\'[/] does not exist. '
                     f'build it with the [bold]\'network build-container\'[/] command')

    except NotFound as _:
        console.warn(f'docker network [bold]\'{config.net_name()}\'[/] not found.'
                     f'use  [bold]\'docker network create {config.net_name()}\'[/] to should solve that.')

    except DockerException as e:
        console.error(f'docker client error type [dim]{type(e)}[/]: [bold]{e}[/]')

    console.info('[green]everything seems to be ok to use dwn![/]')
Exemplo n.º 3
0
    def run_net(self, outside: int, inside: int):
        """
            Run a network container for a plan
        """

        self._ensure_net_exists()

        console.debug(
            f'starting network proxy [green]{inside}[/]<-{self.get_container_name()}<-'
            f'[red]{outside}[/] for plan [bold]{self.plan.name}[/]')

        self.get_client(). \
            containers.run(config.net_container_name(), detach=True,
                           environment={
                               'REMOTE_HOST': self.get_container_name(),
                               'REMOTE_PORT': inside, 'LOCAL_PORT': outside,
                           }, stderr=True, stdout=True, remove=True,
                           network=config.net_name(), ports={outside: outside},
                           name=self.get_net_container_name_with_ports(outside, inside))
Exemplo n.º 4
0
def build_container():
    """
        Builds the network container
    """

    console.info('building network container')

    try:
        client = docker.from_env()
    except DockerException as e:
        console.error(f'docker client failed: [bold]{e}[/]')
        return

    console.debug(f'path to docker context is: [bold]{NETWORK_CONTAINER_PATH}[/]')
    console.debug(f'network container will be called [bold]\'{config.net_container_name()}\'[/]')

    image, logs = client.images.build(
        path=str(NETWORK_CONTAINER_PATH), pull=True, tag=config.net_container_name())

    for log in logs:
        console.debug(log)

    console.info(f'network container \'{config.net_container_name()}\' built')