Skip to content

Commit

Permalink
Merge pull request #36 from EyeSeeTea/feature/command-rm-31
Browse files Browse the repository at this point in the history
Add rm command
  • Loading branch information
ifoche authored May 25, 2020
2 parents b4c9fc7 + 3557166 commit f360bd4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ $ d2-docker start dhis2-sierra.tgz

On the first run, the images will been created, but you can either run this command again or the standard `start DHIS2_DATA_IMAGE_NAME`.

### Delete image/containers linked to an instance

```
$ d2-docker rm eyeseetea/dhis2-data:2.30-sierra
```

### Copy Docker images to/from local directories

You can use a Docker image or a data directory (db + apps) as source, that will create a new Docker image _eyeseetea/dhis2-data:2.30-sierra2_ and a `sierra-data/` directory:
Expand Down
16 changes: 15 additions & 1 deletion src/d2_docker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
start,
logs,
stop,
rm,
commit,
push,
copy,
Expand All @@ -17,7 +18,20 @@
create,
)

COMMAND_MODULES = [start, logs, stop, commit, push, copy, export, import_, list_, run_sql, create]
COMMAND_MODULES = [
start,
logs,
stop,
rm,
commit,
push,
copy,
export,
import_,
list_,
run_sql,
create,
]


def get_parser():
Expand Down
24 changes: 24 additions & 0 deletions src/d2_docker/commands/rm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from d2_docker import utils

DESCRIPTION = "Remove dhis2-data docker images/containers"


def setup(parser):
parser.add_argument("images", metavar="IMAGE", nargs="+", help="Docker dhis2-data images")


def run(args):
for image in args.images:
remove_image(image)


def remove_image(image):
utils.logger.info("Delete image/containers: {}".format(image))
utils.run_docker_compose(["stop"], image)
result = utils.run_docker_compose(["ps", "-q"], data_image=image, capture_output=True)
container_ids = result.stdout.decode("utf-8").splitlines()
utils.logger.debug("Container IDs: {}".format(container_ids))
if container_ids:
utils.run_docker(["container", "rm", *container_ids])
utils.run_docker(["image", "rm", image])
utils.logger.info("Removed: {}".format(image))
7 changes: 7 additions & 0 deletions src/d2_docker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ def get_running_image_name():
raise D2DockerError("Multiple d2-docker images running, specify one:\n{}".format(names))


def run_docker(args):
"""Run docker."""
cmd = ["docker", *args]
result = run(cmd, capture_output=True)
return [s.strip() for s in result.stdout.decode("utf-8").splitlines()]


def run_docker_ps(args):
"""Run docker ps filtered by app label and return output lines."""
cmd = ["docker", "ps", "--filter", "label=" + IMAGE_NAME_LABEL, *args]
Expand Down

0 comments on commit f360bd4

Please sign in to comment.