Skip to content

Commit

Permalink
adapt host docker_build for using a docker registry
Browse files Browse the repository at this point in the history
If the host is the docker registry used globally, it should build the
docker image. Otherwise, if the host is not using a registry to pull the
images, it should build them locally.

In either cases, it should push the local images to the local registry
as each runbot builder is hosting a Docker registry.
  • Loading branch information
d-fence authored and Xavier-Do committed Jul 26, 2024
1 parent 2e44ce2 commit 2f97c68
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions runbot/models/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from odoo import models, fields, api
from odoo.tools import config, ormcache, file_open
from ..common import fqdn, local_pgadmin_cursor, os, list_local_dbs, local_pg_cursor
from ..container import docker_build
from ..container import docker_build, docker_push

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -129,34 +129,39 @@ def _docker_build(self):
self._docker_build_dockerfile(dockerfile)
_logger.info('Done...')

def _docker_build_dockerfile(self, dockerfile):
start = time.time()
docker_build_path = self.env['runbot.runbot']._path('docker', dockerfile.image_tag)
os.makedirs(docker_build_path, exist_ok=True)

user = getpass.getuser()

docker_append = f"""
RUN groupadd -g {os.getgid()} {user} \\
&& useradd -u {os.getuid()} -g {user} -G audio,video {user} \\
&& mkdir /home/{user} \\
&& chown -R {user}:{user} /home/{user}
USER {user}
ENV COVERAGE_FILE /data/build/.coverage
"""
with open(self.env['runbot.runbot']._path('docker', dockerfile.image_tag, 'Dockerfile'), 'w') as Dockerfile:
Dockerfile.write(dockerfile.dockerfile + docker_append)

docker_build_success, msg = docker_build(docker_build_path, dockerfile.image_tag)
if not docker_build_success:
dockerfile.to_build = False
dockerfile.message_post(body=f'Build failure:\n{msg}')
# self.env['runbot.runbot']._warning(f'Dockerfile build "{dockerfile.image_tag}" failed on host {self.name}')
else:
duration = time.time() - start
if duration > 1:
_logger.info('Dockerfile %s finished build in %s', dockerfile.image_tag, duration)

def _docker_build_dockerfile(self, dockerfile, workdir):
icp = self.env['ir.config_parameter']
docker_registry_host_id = icp.get_param('runbot.docker_registry_host_id', default=False)
if not self.use_docker_registry or docker_registry_host_id == self.id:
start = time.time()
# _logger.info('Building %s, %s', dockerfile.name, hash(str(dockerfile.dockerfile)))
docker_build_path = self.env['runbot.runbot']._path('docker', dockerfile.image_tag)
os.makedirs(docker_build_path, exist_ok=True)
user = getpass.getuser()

docker_append = f"""
RUN groupadd -g {os.getgid()} {user} \\
&& useradd -u {os.getuid()} -g {user} -G audio,video {user} \\
&& mkdir /home/{user} \\
&& chown -R {user}:{user} /home/{user}
USER {user}
ENV COVERAGE_FILE /data/build/.coverage
"""
with open(self.env['runbot.runbot']._path('docker', dockerfile.image_tag, 'Dockerfile'), 'w') as Dockerfile:
Dockerfile.write(dockerfile.dockerfile + docker_append)

docker_build_success, msg = docker_build(docker_build_path, dockerfile.image_tag)
if not docker_build_success:
dockerfile.to_build = False
dockerfile.message_post(body=f'Build failure:\n{msg}')
# self.env['runbot.runbot']._warning(f'Dockerfile build "{dockerfile.image_tag}" failed on host {self.name}')
else:
duration = time.time() - start
if duration > 1:
_logger.info('Dockerfile %s finished build in %s', dockerfile.image_tag, duration)
# In all cases we want to push image on the local registry as every runbot builder is running a registry
docker_push(dockerfile.image_tag)

@ormcache()
def _host_list(self):
return {host.name: host.id for host in self.search([])}
Expand Down

0 comments on commit 2f97c68

Please sign in to comment.