brew.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import logging
  3. import docker
  4. import git
  5. DEFAULT_REPOSITORY = 'git://github.com/dotcloud/docker'
  6. DEFAULT_BRANCH = 'library'
  7. logger = logging.getLogger(__name__)
  8. logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
  9. level='DEBUG')
  10. def fetch_buildlist(repository=None, branch=None):
  11. if repository is None:
  12. repository = DEFAULT_REPOSITORY
  13. if branch is None:
  14. branch = DEFAULT_BRANCH
  15. logger.info('Cloning docker repo from {0}, branch: {1}'.format(
  16. repository, branch))
  17. #FIXME: set destination folder and only pull latest changes instead of
  18. # cloning the whole repo everytime
  19. dst_folder = git.clone_branch(repository, branch)
  20. for buildfile in os.listdir(os.path.join(dst_folder, 'library')):
  21. f = open(os.path.join(dst_folder, 'library', buildfile))
  22. for line in f:
  23. logger.debug('{0} ---> {1}'.format(buildfile, line))
  24. args = line.split()
  25. try:
  26. #FIXME: delegate to workers instead?
  27. if len(args) == 1: # Just a URL, simple mode
  28. start_build(args[0], 'refs/heads/master', buildfile)
  29. elif len(args) == 3: # docker-tag url B:branch or T:tag
  30. ref = None
  31. if args[2].startswith('B:'):
  32. ref = 'refs/heads/' + args[2][2:]
  33. elif args[2].startswith('T:'):
  34. ref = 'refs/tags/' + args[2][2:]
  35. else:
  36. raise RuntimeError('Incorrect line format, '
  37. 'please refer to the docs')
  38. start_build(args[1], ref, buildfile, args[0])
  39. except Exception as e:
  40. logger.exception(e)
  41. f.close()
  42. def start_build(repository, ref, docker_repo, docker_tag=None):
  43. logger.info('Cloning {0} (ref: {1})'.format(repository, ref))
  44. dst_folder = git.clone(repository, ref)
  45. if not 'Dockerfile' in os.listdir(dst_folder):
  46. raise RuntimeError('Dockerfile not found in cloned repository')
  47. f = open(os.path.join(dst_folder, 'Dockerfile'))
  48. logger.info('Building using dockerfile...')
  49. #img_id, logs = docker.build_context(dst_folder)
  50. logger.info('Committing to library/{0}:{1}'.format(docker_repo,
  51. docker_tag or 'latest'))
  52. #docker.commit(img_id, 'library/' + docker_repo, docker_tag)
  53. logger.info('Pushing result to the main registry')
  54. #docker.push('library/' + docker_repo)
  55. f.close()