Skip to content

Commit

Permalink
Dockerize all chef sample apps in cloud build (#23551)
Browse files Browse the repository at this point in the history
* Change create_docker script to use all device type

* search tar failes for devices to dockerize

* Use argparse instead of optparser

* simplify arguments
  • Loading branch information
FernandoLGuzman authored and pull[bot] committed Jun 26, 2023
1 parent 801875a commit 1729890
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 45 deletions.
68 changes: 68 additions & 0 deletions examples/chef/create_docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3

# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import argparse
import tarfile
import shutil
import docker

client = docker.from_env()

_CREATE_DOCKER_SCRIPT_PATH = os.path.dirname(__file__)
_SUPPORTED_PLATFORM = 'linux_x86'

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--commit_sha', required=True)
parser.add_argument('-s', '--short_sha', required=True)
parser.add_argument('-r', '--revision_id', required=True)
parser.add_argument('-b', '--build_id', required=True)
parser.add_argument('-i', '--image_name', required=True)
parser.add_argument('-t', '--tar_path', required=True)

args = parser.parse_args()

out_directory = f'{_CREATE_DOCKER_SCRIPT_PATH}/out'

for device_file_name in os.listdir(args.tar_path):
platform, device = device_file_name.split('-')
if _SUPPORTED_PLATFORM not in platform:
continue

# Clean up the out directory before extracting device files
shutil.rmtree(out_directory)
os.mkdir(out_directory)

device = device.replace('.tar.gz', '')

print(f'Extracting {platform} files of {device}')
my_tar = tarfile.open(f'{args.tar_path}/{device_file_name}')
my_tar.extractall(out_directory)
my_tar.close()

docker_image_name = f'{args.image_name}/{platform}/{device}'.lower()

print(f'Building {platform} docker image for {device}')
image = client.images.build(path=_CREATE_DOCKER_SCRIPT_PATH, buildargs={
'DEVICE_NAME': f'{device}'})
image[0].tag(docker_image_name, tag='latest')
image[0].tag(docker_image_name, tag=f'short-sha_{args.short_sha}')
image[0].tag(docker_image_name, tag=f'build-id_{args.build_id}')
image[0].tag(docker_image_name, tag=f'commit-sha_{args.commit_sha}')
image[0].tag(docker_image_name, tag=f'revision-id_{args.revision_id}')

print(f'Pushing image: {docker_image_name}')
client.images.push(docker_image_name)
31 changes: 0 additions & 31 deletions examples/chef/create_docker.sh

This file was deleted.

8 changes: 4 additions & 4 deletions examples/chef/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ RUN apt-get update && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

COPY examples/chef/out/${DEVICE_NAME} /chef_device
COPY examples/chef/out/${DEVICE_NAME}.map /chef_device.map
COPY examples/chef/out/${DEVICE_NAME}.matter /chef_device.matter
COPY examples/chef/out/${DEVICE_NAME}_hashmeta.yaml /chef_device_hashmeta.yaml
COPY out/${DEVICE_NAME} /chef_device
COPY out/${DEVICE_NAME}.map /chef_device.map
COPY out/${DEVICE_NAME}.matter /chef_device.matter
COPY out/${DEVICE_NAME}_hashmeta.yaml /chef_device_hashmeta.yaml

ENV DBUS_SYSTEM_BUS_ADDRESS="unix:path=/var/run/dbus/system_bus_socket"

Expand Down
14 changes: 4 additions & 10 deletions integrations/cloudbuild/chef.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ steps:

- name: "gcr.io/cloud-builders/docker"
args:
- "-c"
- COMMIT_SHA=$COMMIT_SHA SHORT_SHA=$SHORT_SHA REVISION_ID=$REVISION_ID
BUILD_ID=$BUILD_ID IMAGE_NAME=$_DOCKER_IMAGE_NAME
DEVICE_NAME=$_DOCKER_DEVICE_NAME
WORKING_DIR=/workspace/examples/chef
TAR_PATH=/workspace/artifacts/linux_x86-$_DOCKER_DEVICE_NAME.tar.gz
./examples/chef/create_docker.sh
['/workspace/examples/chef/create_docker.py', '--commit_sha', '$COMMIT_SHA' ,'--short_sha', '$SHORT_SHA',
'--revision_id', '$REVISION_ID', '--build_id', '$BUILD_ID', '--image_name', '$_DOCKER_IMAGE_NAME',
'--tar_path', '/workspace/artifacts']
id: DockerAll
entrypoint: /usr/bin/bash
entrypoint: python3
waitFor:
- CompileNoip

Expand All @@ -64,8 +60,6 @@ artifacts:
objects:
location: "gs://matter-build-automation-artifacts/$PROJECT_ID/$COMMIT_SHA/"
paths: ["/workspace/artifacts/*.tar.gz"]
images:
- $_DOCKER_IMAGE_NAME
# Using higher CPU machines generally speeds up builds, except bootstrap is always
# slow.
options:
Expand Down

0 comments on commit 1729890

Please sign in to comment.