You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. # Docker API documentation : https://docker-py.readthedocs.io
  3. # Requirements
  4. # apt install libffi-dev python3-dev
  5. # pip3 install docker[tls]
  6. # pip3 installshutil.rmtree('src')
  7. import docker
  8. import os
  9. import platform
  10. import pprint
  11. import requests
  12. import shutil
  13. import sys
  14. # Figure out latest matterbridge release
  15. url = 'https://api.github.com/repos/42wim/matterbridge/releases/latest'
  16. resp = requests.get(url)
  17. latest_release = resp.json()['tag_name']
  18. from dulwich import porcelain
  19. from dulwich.repo import Repo
  20. # Prep source dir, checkout sources, figure out latest version
  21. git_src = 'src'
  22. git_url = 'https://github.com/42wim/matterbridge.git'
  23. git_refs = None
  24. repo = None
  25. if not os.path.isdir(git_src):
  26. repo = porcelain.clone(git_url, git_src, checkout=latest_release)
  27. else:
  28. repo = Repo(git_src)
  29. porcelain.pull(repo, git_url, git_refs)
  30. # Ensure we have the latest sources & swap to most recent tag
  31. repo.refs.set_symbolic_ref(b'HEAD', b'refs/tags/' + latest_release.encode('utf-8'))
  32. # Client to connect to docker on localhost
  33. client = docker.from_env()
  34. processor = platform.machine()
  35. architecture = platform.architecture()
  36. if processor == 'aarch64':
  37. if 'arch=arm32' in client.info()['Labels']:
  38. processor='arm32v7'
  39. else:
  40. processor = 'arm64v8'
  41. elif processor == 'x86 64' or processor == 'x86_64':
  42. processor = 'x86_64'
  43. elif processor == 'armv7l':
  44. processor = 'arm32v7'
  45. else:
  46. sys.exit(1)
  47. docker_repository = 'arm/matterbridge'
  48. print('Building ' + docker_repository + ':' + processor + '-' + latest_release)
  49. build_args = None
  50. for line in docker.APIClient(base_url='unix://var/run/docker.sock').build(path=git_src,
  51. tag=docker_repository + ':' + processor + '-' + latest_release,
  52. labels={'os': 'linux', 'arch': processor},
  53. pull=True,
  54. buildargs=build_args
  55. ):
  56. print(line)
  57. image = client.images.get(docker_repository + ':' + processor + '-' + latest_release)
  58. # Ensure latest tag can be created and fail build otherwise
  59. if not image.tag(repository=docker_repository, tag=processor + '-latest'):
  60. if not image.tag(repository=docker_repository, tag=processor + '-latest', force=True):
  61. print('Could not create "latest" tag!')
  62. sys.exit(1)
  63. # Reload the image tags/data from the server now that the build is done *and* we've tagged the lastest release
  64. image.reload()
  65. print('Build complete')
  66. print('Pushing build to registry.lollipopcloud.solutions')
  67. LOLLIPOP_REGISTRY = 'registry.lollipopcloud.solutions'
  68. LOLLIPOP_REGISTRY_URL = 'https://' + LOLLIPOP_REGISTRY
  69. client.login(username='build-server',
  70. registry=LOLLIPOP_REGISTRY_URL,
  71. reauth=True)
  72. upstream_repo = LOLLIPOP_REGISTRY + '/' + 'arm' + '/' + 'matterbridge'
  73. for image_tag in image.tags:
  74. print(' Tag: ' + image_tag)
  75. lc_tag = image_tag.split(':')[1]
  76. image.tag(repository=upstream_repo, tag=lc_tag, force=True)
  77. image.tag(repository='registry.lollipopcloud.tech' + '/' + 'arm' + '/' + 'matterbridge', tag=lc_tag, force=True)
  78. #Don't need to check upstream as it's smart enough not to re-transfer existing files
  79. # TODO: Update this to pre-load the upstream images and not waste the HTTP requests / responses telling Docker it was already pushed
  80. client.images.push(upstream_repo, tag=lc_tag)
  81. client.images.push('registry.lollipopcloud.tech' + '/' + 'arm' + '/' + 'matterbridge', tag=lc_tag)
  82. print('Build complete')