Skip to content

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
samhotep committed Jul 18, 2024
1 parent ca7772a commit 4580763
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ Note that before changing the base image you should run
```bash
dotrun clean
```
To get rid of the old virtualenv and
to get rid of the old virtualenv.
26 changes: 13 additions & 13 deletions dotrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class Dotrun:
base_image_name = "canonicalwebteam/dotrun-image:latest"
BASE_IMAGE_NAME = "canonicalwebteam/dotrun-image:latest"

def __init__(self):
self.cwd = os.getcwd()
Expand Down Expand Up @@ -59,26 +59,26 @@ def _get_docker_client(self):

def _check_image_updates(self):
try:
self.docker_client.images.get(self.base_image_name)
self.docker_client.images.get(self.BASE_IMAGE_NAME)
# Pull the image in the background
print("Checking for dotrun image updates...")
threading.Thread(target=self._pull_image)
except docker.errors.ImageNotFound:
print("Getting the dotrun image...")
self._pull_image()

def _pull_image(self, image_name=None, no_exit=False):
def _pull_image(self, image_name=None, exit_on_download_error=True):
"""Pull the dotrun image (if updated) from Docker Hub"""
if not image_name:
image_name = self.base_image_name
image_name = self.BASE_IMAGE_NAME
image_uri = self._get_image_name(image_name)
repository, tag = image_uri.split(":")
try:
self.docker_client.images.pull(repository=repository, tag=tag)
except (docker.errors.APIError, docker.errors.ImageNotFound) as e:
print(f"Unable to download image: {image_name}")
# Optionally quit if image download fails
if not no_exit:
if exit_on_download_error:
print(e)
sys.exit(1)
print(f"Attempting to use local image: {image_name}")
Expand All @@ -91,7 +91,7 @@ def _create_cache_volume(self):

# We need to fix the volume ownership
self.docker_client.containers.run(
self.base_image_name,
self.BASE_IMAGE_NAME,
f"chown -R ubuntu:ubuntu {self.container_home}.cache",
user="root",
mounts=self._prepare_mounts([]),
Expand Down Expand Up @@ -176,7 +176,7 @@ def get_mount(command, mounts):

def create_container(self, command, image_name=None):
if not image_name:
image_name = self.base_image_name
image_name = self.BASE_IMAGE_NAME
ports = {self.project_port: self.project_port}
# Run on the same network mode as the host
network_mode = None
Expand Down Expand Up @@ -211,7 +211,7 @@ def create_container(self, command, image_name=None):
)


def _get_cli_command_arg(pattern, command_list):
def _extract_cli_command_arg(pattern, command_list):
"""
Return the value from the format
Expand All @@ -229,7 +229,7 @@ def _get_cli_command_arg(pattern, command_list):
print(f"Value for arg {command_arg} not supplied.")
sys.exit(1)

# Remove the image command from command list
# Remove the command from command list
new_command_list = (
" ".join(command_list).replace(command_arg, "").replace(" ", " ")
)
Expand All @@ -242,7 +242,7 @@ def _handle_image_cli_param(dotrun, command_list):
Handle the --image cli parameter, if supplied, and return the
created container and the modified command list.
"""
if result := _get_cli_command_arg("image", command_list):
if result := _extract_cli_command_arg("image", command_list):
image_name, commands = result
# Sanitize the image name
image_name = dotrun._get_image_name(image_name)
Expand All @@ -257,10 +257,10 @@ def _handle_release_cli_param(dotrun, command_list):
Handle the --release cli parameter, if supplied, and return the
created container and the modified command list.
"""
if result := _get_cli_command_arg("release", command_list):
if result := _extract_cli_command_arg("release", command_list):
image_tag, commands = result
# Get the release image uri
image_name, _ = dotrun.base_image_name.split(":")
image_name, _ = dotrun.BASE_IMAGE_NAME.split(":")
image_tag = f"{image_name}:{image_tag}"
return (
_start_container_with_image(dotrun, image_tag, commands),
Expand All @@ -277,7 +277,7 @@ def _start_container_with_image(dotrun, image_uri, command_list):
print(f"Using image: {image_uri}")

# Download the image
dotrun._pull_image(image_uri, no_exit=True)
dotrun._pull_image(image_uri, exit_on_download_error=True)

# Start dotrun from the supplied base image
try:
Expand Down

0 comments on commit 4580763

Please sign in to comment.