Utilitaires pour la construction de paquets.
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.

package 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. import argparse, sys, shutil, os, subprocess
  3. sys.path.append(os.path.dirname(__file__) + '/lib')
  4. import tamarin
  5. def create_args_parser():
  6. '''Return a new configured ArgumentParser'''
  7. profile_names = tamarin.get_available_profile_names()
  8. parser = argparse.ArgumentParser(description="Generate packages for various GNU/Linux distributions")
  9. # Define available/required arguments and flags
  10. parser.add_argument("project_directory", help="The path to your project to package")
  11. parser.add_argument("-o", "--output", help="The path to the generated packages destination directory", default=".")
  12. parser.add_argument("-p", "--profile", help="The profile to use to package this project (default: debian)", choices=profile_names, default='debian')
  13. parser.add_argument("-a", "--architecture", help="The target architecture for the package (default: amd64)", default='amd64')
  14. parser.add_argument("-b", "--base", help="Use the specified image instead of the profile's one", default='')
  15. parser.add_argument("--rebuild", help="Ignore cache and rebuild container's image", action="store_true", default=False)
  16. parser.add_argument("--debug", help="Will add extra output and start the container in interactive mode", action="store_true", default=False)
  17. parser.add_argument("--cleanup", help="Clear the workspace and remove obsolete Docker images before build", action="store_true", default=False)
  18. parser.add_argument("--override-docker-args", help="Override all 'docker run' arguments. Use '[IMAGE_TAG]', '[PROFILE]' and '[ARCH]' to insert the corresponding values into your command.", default="")
  19. return parser
  20. def build_image(build_workspace, base_image, profile_name, profile, debug=False, rebuild=False):
  21. with open("{:s}/Dockerfile".format(build_workspace), 'w') as dockerfile:
  22. dockerfile.write("FROM {:s}\n".format(base_image))
  23. # Configure "containerbuild" hooks environment
  24. hooks_env = os.environ.copy()
  25. hooks_env["PATH"] = os.environ['PATH'] + ':' + tamarin.get_lib_dir()
  26. # Run hooks
  27. tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=hooks_env, debug=debug)
  28. image_tag = "tamarin:{:s}_{:s}_{:d}".format(profile_name, base_image.replace(':', '_'), os.getpid())
  29. build_args = [ "build", "-t", image_tag ]
  30. if rebuild:
  31. build_args += [ "--no-cache" ]
  32. tamarin.run_docker(build_args + [build_workspace], debug=debug)
  33. return image_tag
  34. def cleanup(build_workspace=None, debug=False):
  35. if build_workspace == None:
  36. build_workspace = tamarin.get_workspace_subdir('tmp')
  37. # Suppression de l'espace de travail de build
  38. shutil.rmtree(build_workspace, ignore_errors=True)
  39. def validate_args(args):
  40. '''TODO'''
  41. if __name__ == "__main__":
  42. parser = create_args_parser()
  43. args = parser.parse_args()
  44. if args.cleanup:
  45. cleanup(debug=args.debug)
  46. # Verify project directory
  47. project_dir = os.path.abspath(args.project_directory)
  48. output_dir = os.path.abspath(args.output)
  49. # Load build profile
  50. profile = tamarin.load_profile(args.profile, debug=args.debug)
  51. workspace = tamarin.get_workspace_dir()
  52. pid = os.getpid()
  53. build_workspace = tamarin.get_workspace_subdir('tmp/build_{:d}'.format(pid))
  54. base_image = args.base if args.base != '' else profile['profile']['default_image']
  55. image_tag = build_image(build_workspace, base_image, args.profile, profile, debug=args.debug, rebuild=args.rebuild)
  56. kwargs = dict()
  57. kwargs['debug'] = args.debug
  58. docker_args = []
  59. # Append custom arguments
  60. if args.override_docker_args != "":
  61. docker_args = args.override_docker_args.replace('[IMAGE_TAG]', image_tag)
  62. docker_args = docker_args.replace('[PROFILE]', args.profile)
  63. docker_args = docker_args.replace('[ARCH]', args.architecture)
  64. else:
  65. docker_args += [ "run", "--rm" ]
  66. # volumes definition
  67. docker_args += [
  68. "-v", "{:s}:/src:ro".format(project_dir),
  69. "-v", "{:s}:/dist".format(output_dir),
  70. "-v", "{:s}:/tamarin/hooks:ro".format(tamarin.get_hooks_dir()),
  71. "-v", "{:s}:/tamarin/lib:ro".format(tamarin.get_lib_dir()),
  72. "-v", "{:s}:/tamarin/profiles:ro".format(tamarin.get_profiles_dir())
  73. ]
  74. # Use environment proxy if defined
  75. for proxy_var in ['HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy']:
  76. if proxy_var in os.environ:
  77. docker_args += ["-e", "{:s}={:s}".format(proxy_var, os.environ[proxy_var])]
  78. if args.debug:
  79. kwargs['pty'] = True
  80. docker_args += ["-it", image_tag, "/bin/sh"]
  81. helper_cmd = " ".join(["/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture])
  82. print("Executer '{:s}' pour lancer la construction du paquet.".format(helper_cmd))
  83. else:
  84. docker_args += [image_tag, "/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture]
  85. # Start container
  86. tamarin.run_docker(docker_args, **kwargs)
  87. cleanup(build_workspace, debug=args.debug)