Browse Source

Base gestion mode debug

feature/python-rkt
William Petit 2 years ago
parent
commit
448a16f659
5 changed files with 52 additions and 44 deletions
  1. 2
    0
      .gitignore
  2. 7
    6
      lib/rkt.py
  3. 3
    2
      lib/system.py
  4. 10
    8
      lib/tamarin.py
  5. 30
    28
      package

+ 2
- 0
.gitignore View File

@@ -3,3 +3,5 @@ packages
3 3
 *~
4 4
 __pycache__
5 5
 *.pyc
6
+*.changes
7
+*.deb

+ 7
- 6
lib/rkt.py View File

@@ -1,21 +1,22 @@
1 1
 import system, subprocess, os, tamarin, json, re
2 2
 
3
-def run(args, as_root = False, capture_output=False):
3
+def run(args, as_root = False, capture_output=False, debug=False):
4 4
     """Run rkt with the specified args (use the local copy if rkt is not found in the $PATH)"""
5 5
     rkt_bin = system.which('rkt', tamarin.get_workspace_subdir('rkt'))
6 6
     cmd = ( ["sudo", "-E", rkt_bin] if os.geteuid() != 0 and as_root == True else [rkt_bin] ) + args
7
-    print(" ".join(cmd))
7
+    if debug:
8
+        print(" ".join(cmd))
8 9
     if capture_output:
9 10
         return subprocess.check_output(cmd, stdin=subprocess.PIPE)
10 11
     else:
11 12
         return subprocess.call(cmd, stdin=subprocess.PIPE)
12 13
 
13
-def get_images_list(rkt_flags = []):
14
+def get_images_list(rkt_flags = [], debug=False):
14 15
     output = run([
15 16
         "image",
16 17
         "list",
17 18
         "--format=json"
18
-    ] + rkt_flags, capture_output=True)
19
+    ] + rkt_flags, capture_output=True, debug=debug)
19 20
     # Fetch the list of installed images
20 21
     return json.loads(output.decode('utf-8'))
21 22
 
@@ -28,10 +29,10 @@ def find_image_by_name(name_pattern, rkt_flags = []):
28 29
             return image
29 30
     return None
30 31
 
31
-def export_image(image_id, dest_file, rkt_flags = []):
32
+def export_image(image_id, dest_file, rkt_flags = [], debug=False):
32 33
     run([
33 34
         "image",
34 35
         "export",
35 36
         image_id,
36 37
         dest_file,
37
-    ] + rkt_flags)
38
+    ] + rkt_flags, debug=debug)

+ 3
- 2
lib/system.py View File

@@ -1,7 +1,8 @@
1 1
 import tarfile, os
2 2
 
3
-def extract_tar(file_path, dest_dir = "."):
4
-    print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir))
3
+def extract_tar(file_path, dest_dir = ".", debug=False):
4
+    if debug:
5
+        print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir))
5 6
     with tarfile.open(file_path) as tar:
6 7
         tar.extractall(dest_dir)
7 8
         tar.close()

+ 10
- 8
lib/tamarin.py View File

@@ -2,7 +2,7 @@ import os, glob, subprocess, configparser
2 2
 import web, system
3 3
 import codecs
4 4
 
5
-def run_profile_hooks(profile, step, cwd=None, env=None):
5
+def run_profile_hooks(profile, step, cwd=None, env=None, debug=False):
6 6
   hooks_dir = get_hooks_dir()
7 7
   step_hooks = profile[step]["hooks"]
8 8
   if not step_hooks:
@@ -22,7 +22,7 @@ def get_hooks_dir():
22 22
 def get_lib_dir():
23 23
   return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../lib")
24 24
 
25
-def load_profile(profile_name):
25
+def load_profile(profile_name, debug=False):
26 26
   profile_filename = profile_name+".conf"
27 27
   for profile_file in get_available_profiles():
28 28
     if profile_filename == os.path.basename(profile_file):
@@ -63,25 +63,27 @@ def get_rkt_achive_dest_dir():
63 63
     workspace_tmp = get_workspace_subdir('tmp')
64 64
     return glob.glob(os.path.join(os.sep, workspace_tmp, 'rkt-v*'))[0]
65 65
 
66
-def download_rkt():
66
+def download_rkt(debug=False):
67 67
     """Download a local copy of rkt in the tamarin workspace and return the absolute path to the archive"""
68 68
     url = "https://github.com/coreos/rkt/releases/download/v1.22.0/rkt-v1.22.0.tar.gz"
69 69
     file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "rkt.tar.gz")
70 70
     web.download_file(file_url=url, dest_path=file_path)
71 71
     return file_path
72 72
 
73
-def download_acbuild():
73
+def download_acbuild(debug=False):
74 74
     """Download a local copy of acbuild in the tamarin workspace and return the absolute path to the archive"""
75 75
     url = "https://github.com/containers/build/releases/download/v0.4.0/acbuild-v0.4.0.tar.gz"
76 76
     file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "acbuild.tar.gz")
77 77
     web.download_file(file_url=url, dest_path=file_path)
78 78
     return file_path
79 79
 
80
-def run_acbuild(args, captureOutput=False):
80
+def run_acbuild(args, captureOutput=False, as_root=False, debug=False):
81 81
     """Run acbuild with the specified args (use the local copy if acbuild is not found in the $PATH)"""
82 82
     acbuild_bin = system.which('acbuild', get_workspace_subdir('acbuild'))
83
-    print(" ".join([acbuild_bin] + args))
83
+    cmd = ( ["sudo", "-E", acbuild_bin] if os.geteuid() != 0 and as_root == True else [acbuild_bin] ) + args
84
+    if debug:
85
+        print(" ".join(cmd))
84 86
     if captureOutput:
85
-        return subprocess.check_output([acbuild_bin] + args, stdin=subprocess.PIPE)
87
+        return subprocess.check_output(cmd, stdin=subprocess.PIPE)
86 88
     else:
87
-        return subprocess.call([acbuild_bin] + args, stdin=subprocess.PIPE)
89
+        return subprocess.call(cmd, stdin=subprocess.PIPE)

+ 30
- 28
package View File

@@ -23,23 +23,23 @@ def create_args_parser():
23 23
 
24 24
     return parser
25 25
 
26
-def download_and_extract_rkt(dest_dir, verbose=True):
26
+def download_and_extract_rkt(dest_dir, debug=False):
27 27
     '''Download and extract rkt to the given destination directory'''
28
-    rkt_archive_path = tamarin.download_rkt()
29
-    system.extract_tar(rkt_archive_path, workspace_tmp)
28
+    rkt_archive_path = tamarin.download_rkt(debug=debug)
29
+    system.extract_tar(rkt_archive_path, workspace_tmp, debug=debug)
30 30
     rkt_archive_dir = tamarin.get_rkt_achive_dest_dir()
31 31
     shutil.rmtree(local_rkt_dir, ignore_errors=True)
32 32
     os.rename(rkt_archive_dir, dest_dir)
33 33
 
34
-def download_and_extract_acbuild(dest_dir, verbose=True):
34
+def download_and_extract_acbuild(dest_dir, debug=False):
35 35
     '''Download and extract acbuild to the given destination directory'''
36
-    acbuild_archive_path = tamarin.download_acbuild()
37
-    system.extract_tar(acbuild_archive_path, workspace_tmp)
36
+    acbuild_archive_path = tamarin.download_acbuild(debug=debug)
37
+    system.extract_tar(acbuild_archive_path, workspace_tmp, debug=debug)
38 38
     acbuild_archive_dir = tamarin.get_acbuild_achive_dest_dir()
39 39
     shutil.rmtree(local_acbuild_dir, ignore_errors=True)
40 40
     os.rename(acbuild_archive_dir, dest_dir)
41 41
 
42
-def get_cached_image_path(profile):
42
+def get_cached_image_path(profile, debug=False):
43 43
     '''Compute and return the path for an hypothetic cached image for the given profile'''
44 44
     containerbuild_hooks = profile['containerbuild']['hooks']
45 45
     hasher = hashlib.sha1()
@@ -49,23 +49,23 @@ def get_cached_image_path(profile):
49 49
     cache_dir = tamarin.get_workspace_subdir('cache')
50 50
     return os.path.join(os.sep, cache_dir, '{:s}.aci'.format(image_hash[:12]));
51 51
 
52
-def build_image(build_workspace, aci_file, base_image, profile):
52
+def build_image(build_workspace, aci_file, base_image, profile, debug=False):
53 53
 
54 54
     acbuild_flags = ["--work-path", build_workspace]
55 55
 
56 56
     # Find and export base image from rkt' store
57 57
     name_pattern = base_image.split('/')[-1] + '$'
58 58
     image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags)
59
-    rkt.export_image(image['id'], aci_file, rkt_flags=rkt_flags);
59
+    rkt.export_image(image['id'], aci_file, rkt_flags=rkt_flags, debug=debug);
60 60
 
61 61
     # Build image
62
-    tamarin.run_acbuild(acbuild_flags+["begin", aci_file])
63
-    tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)])
64
-    tamarin.run_acbuild(acbuild_flags+["mount", "add", "src", "/src", "--read-only"])
65
-    tamarin.run_acbuild(acbuild_flags+["mount", "add", "dist", "/dist"])
66
-    tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-hooks", "/tamarin/hooks", "--read-only"])
67
-    tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-lib", "/tamarin/lib", "--read-only"])
68
-    tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-profiles", "/tamarin/profiles", "--read-only"])
62
+    tamarin.run_acbuild(acbuild_flags+["begin", aci_file], debug=debug)
63
+    tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)], debug=debug)
64
+    tamarin.run_acbuild(acbuild_flags+["mount", "add", "src", "/src", "--read-only"], debug=debug)
65
+    tamarin.run_acbuild(acbuild_flags+["mount", "add", "dist", "/dist"], debug=debug)
66
+    tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-hooks", "/tamarin/hooks", "--read-only"], debug=debug)
67
+    tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-lib", "/tamarin/lib", "--read-only"], debug=debug)
68
+    tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-profiles", "/tamarin/profiles", "--read-only"], debug=debug)
69 69
 
70 70
     # Configure "containerbuild" hooks environment
71 71
     hooks_env = os.environ.copy()
@@ -74,26 +74,26 @@ def build_image(build_workspace, aci_file, base_image, profile):
74 74
     hooks_env["TAMARIN_ACBUILD_ENGINE"] = "chroot" if not system.which('systemctl') else "systemd-nspawn"
75 75
 
76 76
     # Run hooks
77
-    tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=hooks_env)
77
+    tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=hooks_env, debug=debug)
78 78
 
79
-    tamarin.run_acbuild(acbuild_flags+["write", "--overwrite", aci_file])
80
-    tamarin.run_acbuild(acbuild_flags+["end"])
79
+    tamarin.run_acbuild(acbuild_flags+["write", "--overwrite", aci_file], as_root=True, debug=debug)
80
+    tamarin.run_acbuild(acbuild_flags+["end"], as_root=True, debug=debug)
81 81
 
82 82
     return aci_file
83 83
 
84
-def cleanup(build_workspace, rkt_flags):
84
+def cleanup(build_workspace, rkt_flags, debug=False):
85 85
 
86 86
     # Nettoyage des conteneurs
87 87
     rkt.run([
88 88
         "gc",
89 89
         "--grace-period=0"
90
-    ] + rkt_flags, as_root=True)
90
+    ] + rkt_flags, as_root=True, debug=debug)
91 91
 
92 92
     # Nettoyage des images obsolètes du store
93 93
     rkt.run([
94 94
         "image",
95 95
         "gc"
96
-    ] + rkt_flags, as_root=True)
96
+    ] + rkt_flags, as_root=True, debug=debug)
97 97
 
98 98
     # Suppression de l'espace de travail de build
99 99
     shutil.rmtree(build_workspace, ignore_errors=True)
@@ -113,7 +113,7 @@ if __name__ == "__main__":
113 113
     output_dir = os.path.abspath(args.output)
114 114
 
115 115
     # Load build profile
116
-    profile = tamarin.load_profile(args.profile)
116
+    profile = tamarin.load_profile(args.profile, debug=args.debug)
117 117
 
118 118
     workspace = tamarin.get_workspace_dir()
119 119
     workspace_tmp = tamarin.get_workspace_subdir('tmp')
@@ -140,16 +140,16 @@ if __name__ == "__main__":
140 140
             "fetch",
141 141
             "--insecure-options=image",
142 142
             base_image
143
-        ] + rkt_flags)
143
+        ] + rkt_flags, debug=args.debug)
144 144
 
145 145
     aci_file = os.path.join(os.sep, build_workspace, 'image.aci')
146
-    cached_image_file = get_cached_image_path(profile)
146
+    cached_image_file = get_cached_image_path(profile, debug=args.debug)
147 147
 
148 148
     if not args.rebuild and os.path.exists(cached_image_file):
149 149
         # Copy cached image
150 150
         shutil.copyfile(cached_image_file, aci_file)
151 151
     else:
152
-        build_image(build_workspace, aci_file, base_image, profile)
152
+        build_image(build_workspace, aci_file, base_image, profile, debug=args.debug)
153 153
         # Cache image
154 154
         shutil.copyfile(aci_file, cached_image_file)
155 155
 
@@ -172,11 +172,13 @@ if __name__ == "__main__":
172 172
 
173 173
     if args.debug:
174 174
       rkt_args += ["--interactive", "--exec", "/bin/bash"]
175
+      helper_cmd = " ".join(["/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture])
176
+      print("Executer '{:s}' pour lancer la construction du paquet.".format(helper_cmd))
175 177
     else:
176 178
       rkt_args += ["--exec", "/usr/bin/python3", "--", "/tamarin/lib/build.py", args.profile, args.architecture]
177 179
 
178 180
     # Start container
179
-    rkt.run(rkt_flags+rkt_args, as_root=True)
181
+    rkt.run(rkt_flags+rkt_args, as_root=True, debug=args.debug)
180 182
 
181 183
     # Cleanup
182
-    cleanup(build_workspace, rkt_flags)
184
+    cleanup(build_workspace, rkt_flags, debug=args.debug)

Loading…
Cancel
Save