Skip to content

Merge --base-docker-image and --docker-image flag #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 37 additions & 38 deletions src/xpk/core/docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import datetime
import os
import random
import re
import string

from ..utils.console import xpk_exit, xpk_print
Expand All @@ -26,6 +27,11 @@
DEFAULT_DOCKER_IMAGE = 'python:3.10'
DEFAULT_SCRIPT_DIR = os.getcwd()
PLATFORM = 'linux/amd64'
CLOUD_PREFIXES = [
r'^gcr\.io',
r'^docker\.pkg\.dev',
r'^([a-z0-9-]+)-docker\.pkg\.dev',
]


def validate_docker_image(docker_image, args) -> int:
Expand Down Expand Up @@ -161,50 +167,43 @@ def setup_docker_image(args) -> tuple[int, str]:
0 if successful and 1 otherwise.
Name of the docker image to use.
"""
use_base_docker_image = use_base_docker_image_or_docker_image(args)

docker_image = args.base_docker_image
if use_base_docker_image:
validate_docker_image_code = validate_docker_image(docker_image, args)
if validate_docker_image_code != 0:
xpk_exit(validate_docker_image_code)
build_docker_image_code, docker_image = build_docker_image_from_base_image(
args
)
if build_docker_image_code != 0:
xpk_exit(build_docker_image_code)
else:
docker_image = args.docker_image
validate_docker_image_code = validate_docker_image(args.docker_image, args)
if validate_docker_image_code != 0:
xpk_exit(validate_docker_image_code)

return 0, docker_image
docker_image = args.docker_image

if not docker_image and args.base_docker_image:
docker_image = args.base_docker_image # fallback for legacy users

def use_base_docker_image_or_docker_image(args) -> bool:
"""Checks for correct docker image arguments.
if not docker_image:
xpk_print(
f'No docker image specified, using default: {DEFAULT_DOCKER_IMAGE}'
)
docker_image = DEFAULT_DOCKER_IMAGE

Args:
args: user provided arguments for running the command.
is_cloud_image = any(
re.match(prefix, docker_image) for prefix in CLOUD_PREFIXES
)

Returns:
True if intended to use base docker image, False to use docker image.
"""
use_base_docker_image = True
# Check if (base_docker_image and script_dir) or (docker_image) is set.
if args.docker_image is not None:
if is_cloud_image:
if args.script_dir is not DEFAULT_SCRIPT_DIR:
xpk_print(
'`--script-dir` and --docker-image can not be used together. Please'
' see `--help` command for more details.'
'Error: `--script-dir` cannot be used with a cloud docker'
' image.\nHint: If you need to customize the image with local'
' scripts, use a local base image (e.g., `ubuntu:20.04`) instead of a'
' prebuilt cloud image.'
)
xpk_exit(1)
if args.base_docker_image is not DEFAULT_DOCKER_IMAGE:
xpk_print(
'`--base-docker-image` and --docker-image can not be used together.'
' Please see `--help` command for more details.'
)
xpk_exit(1)
use_base_docker_image = False
return use_base_docker_image

validate_code = validate_docker_image(docker_image, args)
if validate_code != 0:
xpk_exit(validate_code)

else:
validate_code = validate_docker_image(docker_image, args)
if validate_code != 0:
xpk_exit(validate_code)

build_code, docker_image = build_docker_image_from_base_image(args)
if build_code != 0:
xpk_exit(build_code)

return 0, docker_image
Loading