Dockerize all chef sample apps in cloud build (#23551)

* Change create_docker script to use all device type

* search tar failes for devices to dockerize

* Use argparse instead of optparser

* simplify arguments
diff --git a/examples/chef/create_docker.py b/examples/chef/create_docker.py
new file mode 100755
index 0000000..688f782
--- /dev/null
+++ b/examples/chef/create_docker.py
@@ -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)
diff --git a/examples/chef/create_docker.sh b/examples/chef/create_docker.sh
deleted file mode 100755
index f38da15..0000000
--- a/examples/chef/create_docker.sh
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/usr/bin/env bash
-#
-# Copyright (c) 2021 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.
-#
-
-set -e # Exit on any script error.
-
-OUT_DIRECTORY="$WORKING_DIR/out/"
-
-# Clean up the out directory before extracting device files
-rm -r "$OUT_DIRECTORY"*
-
-# Extract device files
-tar -xf "$TAR_PATH" -C "$OUT_DIRECTORY"
-
-# Build the docker image
-docker build -f "$WORKING_DIR"/dockerfile -t "$IMAGE_NAME":latest -t "$IMAGE_NAME:short-sha_$SHORT_SHA" \
-    -t "$IMAGE_NAME:build-id_$BUILD_ID" -t "$IMAGE_NAME:commit-sha_$COMMIT_SHA" -t "$IMAGE_NAME:revision-id_$REVISION_ID" \
-    --build-arg DEVICE_NAME="$DEVICE_NAME" .
diff --git a/examples/chef/dockerfile b/examples/chef/dockerfile
index 0b0b320..f69d79b 100644
--- a/examples/chef/dockerfile
+++ b/examples/chef/dockerfile
@@ -8,10 +8,10 @@
     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"
 
diff --git a/integrations/cloudbuild/chef.yaml b/integrations/cloudbuild/chef.yaml
index 7abdf02..dcab965 100644
--- a/integrations/cloudbuild/chef.yaml
+++ b/integrations/cloudbuild/chef.yaml
@@ -42,15 +42,11 @@
 
     - 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
 
@@ -64,8 +60,6 @@
     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: