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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 argparse
  8. import docker
  9. import os
  10. import platform
  11. import pprint
  12. import requests
  13. import shutil
  14. import sys
  15. # Figure out latest release
  16. url = 'https://api.github.com/repos/matrix-org/synapse/releases/latest'
  17. resp = requests.get(url)
  18. latest_release = resp.json()['tag_name']
  19. from dulwich import porcelain
  20. from dulwich.repo import Repo
  21. # Prep source dir, checkout sources, figure out latest version
  22. git_src = 'src'
  23. git_url = 'https://github.com/AVENTER-UG/docker-matrix.git'
  24. git_refs = None
  25. repo = None
  26. if not os.path.isdir(git_src):
  27. repo = porcelain.clone(git_url, git_src)
  28. porcelain.pull(git_src, git_url, git_refs)
  29. git_repo = Repo(git_src)
  30. import fileinput
  31. for line in fileinput.input('./src/Dockerfile', inplace=True):
  32. print(line.replace('linux-headers-amd64', '').replace('git clone --branch $BV_SYN --depth 1 https://github.com/matrix-org/synapse.git', 'git clone --branch $BV_SYN https://github.com/matrix-org/synapse.git'), end='')
  33. # Client to connect to docker on localhost
  34. client = docker.from_env()
  35. processor = platform.machine()
  36. architecture = platform.architecture()
  37. if processor == 'aarch64':
  38. if 'arch=arm32' in client.info()['Labels']:
  39. processor='arm32v7'
  40. else:
  41. processor = 'arm64v8'
  42. elif processor == 'x86 64' or processor == 'x86_64':
  43. processor = 'x86_64'
  44. elif processor == 'armv7l':
  45. processor = 'arm32v7'
  46. else:
  47. sys.exit(1)
  48. docker_repository = 'arm/matrix'
  49. print('Building ' + docker_repository + ':' + processor + '-' + latest_release)
  50. build_args = {
  51. 'TAG_SYN' : latest_release
  52. }
  53. for line in docker.APIClient(base_url='unix://var/run/docker.sock').build(path=git_src,
  54. tag=docker_repository + ':' + processor + '-' + latest_release,
  55. labels={'os': 'linux', 'arch': processor},
  56. pull=True,
  57. buildargs=build_args
  58. ):
  59. print(line)
  60. image = client.images.get(docker_repository + ':' + processor + '-' + latest_release,)
  61. # Ensure latest tag can be created and fail build otherwise
  62. if not image.tag(repository=docker_repository, tag=processor + '-latest'):
  63. if not image.tag(repository=docker_repository, tag=processor + '-latest', force=True):
  64. print('Could not create "latest" tag!')
  65. sys.exit(1)
  66. # Reload the image tags/data from the server now that the build is done *and* we've tagged the lastest release
  67. image.reload()
  68. print('Build complete')
  69. print('Pushing build to registry.lollipopcloud.solutions')
  70. LOLLIPOP_REGISTRY = 'registry.lollipopcloud.solutions'
  71. LOLLIPOP_REGISTRY_URL = 'https://' + LOLLIPOP_REGISTRY
  72. client.login(username='build-server',
  73. registry=LOLLIPOP_REGISTRY_URL,
  74. reauth=True)
  75. upstream_repo = LOLLIPOP_REGISTRY + '/' + 'arm' + '/' + 'matrix'
  76. for image_tag in image.tags:
  77. print(' Tag: ' + image_tag)
  78. lc_tag = image_tag.split(':')[1]
  79. image.tag(repository=upstream_repo, tag=lc_tag, force=True)
  80. image.tag(repository='registry.lollipopcloud.tech' + '/' + 'arm' + '/' + 'matrix', tag=lc_tag, force=True)
  81. #Don't need to check upstream as it's smart enough not to re-transfer existing files
  82. # TODO: Update this to pre-load the upstream images and not waste the HTTP requests / responses telling Docker it was already pushed
  83. client.images.push(upstream_repo, tag=lc_tag)
  84. client.images.push('registry.lollipopcloud.tech' + '/' + 'arm' + '/' + 'matrix', tag=lc_tag)
  85. print('Build complete')