def show(): """ Show running plans """ loader = Loader() table = Table(title='running plan report') table.add_column('plan') table.add_column('container(s)') table.add_column('port(s)') table.add_column('volume(s)') for plan in loader.valid_plans(): if not len(plan.container.containers()) > 0: continue table.add_row( f'[bold]{plan.name}[/]', '\n'.join(f'[cyan]{c.name}[/]' for c in plan.container.containers()), '\n'.join(f'[blue]{p[1]}<-{p[0]}[/]' for p in plan.container.ports()), f"[green]{','.join(f'{v[0]}->{v[1]}' for v in plan.volumes.items())}[/]", ) console.print(table)
def remove(name, outside, inside): """ Removes a port mapping from a plan """ loader = Loader() if not (plan := loader.get_plan(name)): console.error(f'unable to find plan [bold]{name}[/]') return
def add(name, outside, inside): """ Add a port to a plan """ loader = Loader() if not (plan := loader.get_plan(name)): console.error(f'unable to find plan [bold]{name}[/]') return
def run(name, extra_args): """ Run a plan """ loader = Loader() if not (plan := loader.get_plan(name)): console.error(f'unable to find plan [bold]{name}[/]') return
def info(name): """ Displays detailed information about a plan. """ loader = Loader() if not (plan := loader.get_plan(name)): console.error(f'unable to find plan: [bold]{name}[/]') return
def stop(name, yes): """ Stop a plan """ if not yes: if not click.confirm(f'are you sure you want to stop containers for plan {name}?'): console.info('not stopping any plans') return loader = Loader() if not (plan := loader.get_plan(name)): console.error(f'unable to find plan [bold]{name}[/]') return
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![/]')
def show(detail): """ Shows all of the available plans """ loader = Loader() if detail: table = Table(title='dwn plans', show_lines=True, caption=f'{len(loader.valid_plans())} plans') table.add_column('name') table.add_column('path', overflow='fold') table.add_column('volumes', overflow='fold') table.add_column('ports') table.add_column('yaml', no_wrap=True) for p in loader.valid_plans(): table.add_row( f'[bold]{p.name}[/]', f'[dim]{p.plan_path}[/]', f"[green]{','.join(f'{v[0]}->{v[1]}' for v in p.volumes.items())}[/]", f"[blue]{','.join(f'{o[0]}<-{o[1]}' for o in p.exposed_ports)}[/]", f'{open(p.plan_path).read()}', ) console.print(table) return table = Table(title='dwn plans', caption=f'{len(loader.valid_plans())} plans') table.add_column('name') table.add_column('path', overflow='fold') for p in loader.valid_plans(): table.add_row( f'[bold]{p.name}[/]', f'[dim]{p.plan_path}[/]', ) console.print(table)
def pull(name): """ Pull plan images. """ plan_targets = [] if not name and not click.confirm('> a plan name was not specified, ' 'pull all valid plan images?'): return try: client = docker.from_env() except DockerException as e: console.error(f'failed to connect to docker: [bold]{e}[/e]') return loader = Loader() if name: plan_targets.append(loader.get_plan(name)) else: [plan_targets.append(n) for n in loader.valid_plans()] for p in plan_targets: try: console.info(f'pulling image [bold]{p.image}:{p.version}[/]') client.images.pull(p.image, tag=p.version) except ImageNotFound as e: console.error(f'failed to pull image: [bold]{e}[/]') continue except DockerException as e: console.error(f'a docker exception occurred: [bold]{e}[/]') continue console.info( f'image [bold]{p.image}:{p.version}[/] for plan [cyan]{p.name}[/] pulled' )
def update(name): """ Update plan images. """ plan_targets = [] if not name and not click.confirm('> a plan name was not specified, ' 'pull all valid plan images?'): return try: client = docker.from_env() except DockerException as e: console.error(f'failed to connect to docker: [bold]{e}[/e]') return loader = Loader() if name: plan_targets.append(loader.get_plan(name)) else: [plan_targets.append(n) for n in loader.valid_plans()] for p in plan_targets: if p is None: continue try: # build the image if we have an inline dockerfile if p.has_dockerfile(): console.info(f'building image [bold]{p.image_version()}[/]') dockerfile = BytesIO(p.dockerfile.encode('utf-8')) _, logs = client.images.build(fileobj=dockerfile, pull=True, tag=p.image_version(), rm=True, forcerm=True, nocache=True) for log in logs: console.debug(log) console.info( f'container for [bold]{p.image_version()}[/] built') # pull the image instead else: console.info(f'pulling image [bold]{p.image_version()}[/]') client.images.pull(p.image, tag=p.version) except ImageNotFound as e: console.error(f'failed to pull image: [bold]{e}[/]') continue except DockerException as e: console.error(f'a docker exception occurred: [bold]{e}[/]') continue console.info( f'image [bold]{p.image_version()}[/] for plan [cyan]{p.name}[/] updated' )