create_docker.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2020 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import os
  17. import shutil
  18. import tarfile
  19. import docker
  20. client = docker.from_env()
  21. _CREATE_DOCKER_SCRIPT_PATH = os.path.dirname(__file__)
  22. _SUPPORTED_PLATFORM = 'linux_x86'
  23. parser = argparse.ArgumentParser()
  24. parser.add_argument('-c', '--commit_sha', required=True)
  25. parser.add_argument('-s', '--short_sha', required=True)
  26. parser.add_argument('-r', '--revision_id', required=True)
  27. parser.add_argument('-b', '--build_id', required=True)
  28. parser.add_argument('-i', '--image_name', required=True)
  29. parser.add_argument('-t', '--tar_path', required=True)
  30. args = parser.parse_args()
  31. out_directory = f'{_CREATE_DOCKER_SCRIPT_PATH}/out'
  32. for device_file_name in os.listdir(args.tar_path):
  33. platform, device = device_file_name.split('-')
  34. if _SUPPORTED_PLATFORM not in platform:
  35. continue
  36. # Clean up the out directory before extracting device files
  37. shutil.rmtree(out_directory, ignore_errors=True)
  38. os.mkdir(out_directory)
  39. device = device.replace('.tar.gz', '')
  40. print(f'Extracting {platform} files of {device}')
  41. my_tar = tarfile.open(f'{args.tar_path}/{device_file_name}')
  42. my_tar.extractall(out_directory)
  43. my_tar.close()
  44. docker_image_name = f'{args.image_name}/{platform}/{device}'.lower()
  45. print(f'Building {platform} docker image for {device}')
  46. image = client.images.build(path=_CREATE_DOCKER_SCRIPT_PATH, buildargs={
  47. 'DEVICE_NAME': f'{device}'})
  48. image[0].tag(docker_image_name, tag='latest')
  49. image[0].tag(docker_image_name, tag=f'short-sha_{args.short_sha}')
  50. image[0].tag(docker_image_name, tag=f'build-id_{args.build_id}')
  51. image[0].tag(docker_image_name, tag=f'commit-sha_{args.commit_sha}')
  52. image[0].tag(docker_image_name, tag=f'revision-id_{args.revision_id}')
  53. print(f'Pushing image: {docker_image_name}')
  54. response = client.images.push(docker_image_name, stream=True, decode=True)
  55. for line in response:
  56. print(line)