Skip to content

Commit

Permalink
add support for building image from source code into wis2box-ctl.py ...
Browse files Browse the repository at this point in the history
  • Loading branch information
maaikelimper committed Jan 29, 2025
1 parent a42ab2a commit 5e81c7a
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions wis2box-ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

GITHUB_RELEASE_REPO = 'wmo-im/wis2box-release'

LOCAL_BUILD_IMAGES = ['wis2box-broker', 'wis2box-management', 'wis2box-mqtt-metrics-collector']

parser = argparse.ArgumentParser(
description='manage a composition of docker containers to implement a WIS2-in-a-box',
formatter_class=argparse.RawTextHelpFormatter)
Expand Down Expand Up @@ -259,6 +261,24 @@ def remove_old_docker_images() -> None:
if response == 'y':
os.remove(file_)

def build_local_images() -> None:
for image in LOCAL_BUILD_IMAGES:
print(f'Building {image}')
run(split(f'docker build -t {image} ./{image}'))

image_file = glob.glob('docker-compose.images-*.yml')[0]
# overwrite the image tag with the local image
with open(image_file, 'r') as f:
data = f.readlines()
with open(image_file, 'w') as f:
for line in data:
if 'image: ' in line:
image = line.split(':', 1)[1].strip()
image_name = image.split(':')[0].split('/')[-1]
if image_name in LOCAL_BUILD_IMAGES:
line = f' image: {image_name}\n'
f.write(line)

def update_images_yml() -> str:
"""
Expand All @@ -284,6 +304,15 @@ def update_images_yml() -> str:

if current_version == version:
print(f'Using latest version {version}, no update of images file required')
# docker pull the images to ensure they are up to date
with open(f'docker-compose.images-{version}.yml', 'r') as f:
for line in f:
if 'image:' in line:
image = line.split(':', 1)[1].strip()
if image in LOCAL_BUILD_IMAGES:
continue
print(f'Pulling {image}')
run(split(f'docker pull {image}'))
return

if version not in ['LOCAL_BUILD', 'Undefined']:
Expand All @@ -306,7 +335,7 @@ def update_images_yml() -> str:
data = fetch_data_from_url(f'https://{url_host}{url_path}')
with open(f'docker-compose.images-{version}.yml', 'wb') as f:
f.write(data)

# rename any other docker-compose.images-*.yml file to .bak
image_files = glob.glob('docker-compose.images-*.yml')
for file in image_files:
Expand Down Expand Up @@ -358,8 +387,7 @@ def make(args) -> None:
if args.command == "config":
run(split(f'{DOCKER_COMPOSE_COMMAND} {docker_compose_args} config'))
elif args.command == "build":
run(split(
f'{DOCKER_COMPOSE_COMMAND} {docker_compose_args} build {containers}'))
build_local_images()
elif args.command in ["up", "start", "start-dev"]:
# if no docker-compose.images-*.yml files exist, run get_images()
run(split(
Expand Down Expand Up @@ -394,7 +422,6 @@ def make(args) -> None:
# update docker_compose_args with the latest docker-compose.images-*.yml file
docker_image_file = glob.glob('docker-compose.images-*.yml')[0]
docker_compose_args = DOCKER_COMPOSE_ARGS + f' --file {docker_image_file}'
run(split(f'{DOCKER_COMPOSE_COMMAND} {docker_compose_args} pull'))
# if the argument "--restart" is passed, restart all containers and clean old images
if "--restart" in args.args:
run(split(
Expand Down

0 comments on commit 5e81c7a

Please sign in to comment.