1
+ #!/usr/bin/env python3
2
+ #
3
+ # This script was copied as-is from:
4
+ # Source: https://github.com/canonical/packer-maas
5
+ # Original Author: Alexsander de Souza <[email protected] >
6
+ #
7
+
8
+
9
+ import os
10
+ import platform
11
+ import shutil
12
+ import sys
13
+
14
+ from curtin import distro , util
15
+ from curtin .commands import apt_config , curthooks
16
+ from curtin .config import load_command_config
17
+ from curtin .log import DEBUG , LOG , basicConfig
18
+ from curtin .paths import target_path
19
+ from curtin .util import ChrootableTarget , load_command_environment
20
+
21
+
22
+ def run_hook_in_target (target , hook ):
23
+ """Look for "hook" in "target" and run in a chroot"""
24
+ target_hook = target_path (target , "/curtin/" + hook )
25
+ if os .path .isfile (target_hook ):
26
+ LOG .debug ("running %s" % target_hook )
27
+ with ChrootableTarget (target = target ) as in_chroot :
28
+ in_chroot .subp (["/curtin/" + hook ])
29
+ return True
30
+ return False
31
+
32
+
33
+ def curthook (cfg , target , state ):
34
+ """Configure network and bootloader"""
35
+ LOG .info ("Running curtin builtin curthooks" )
36
+ state_etcd = os .path .split (state ["fstab" ])[0 ]
37
+ machine = platform .machine ()
38
+
39
+ distro_info = distro .get_distroinfo (target = target )
40
+ if not distro_info :
41
+ raise RuntimeError ("Failed to determine target distro" )
42
+ osfamily = distro_info .family
43
+ LOG .info (
44
+ "Configuring target system for distro: %s osfamily: %s" ,
45
+ distro_info .variant ,
46
+ osfamily ,
47
+ )
48
+
49
+ sources = cfg .get ("sources" , {})
50
+ dd_image = len (util .get_dd_images (sources )) > 0
51
+
52
+ curthooks .disable_overlayroot (cfg , target )
53
+ curthooks .disable_update_initramfs (cfg , target , machine )
54
+ curthooks .install_missing_packages (cfg , target , osfamily = osfamily )
55
+
56
+ if not dd_image :
57
+ curthooks .configure_iscsi (cfg , state_etcd , target , osfamily = osfamily )
58
+ curthooks .configure_mdadm (cfg , state_etcd , target , osfamily = osfamily )
59
+ curthooks .copy_fstab (state .get ("fstab" ), target )
60
+ curthooks .add_swap (cfg , target , state .get ("fstab" ))
61
+
62
+ run_hook_in_target (target , "install-custom-packages" )
63
+
64
+ if not dd_image :
65
+ curthooks .setup_kernel_img_conf (target )
66
+
67
+ crypttab_location = os .path .join (os .path .split (state ["fstab" ])[0 ], "crypttab" )
68
+ if os .path .exists (crypttab_location ):
69
+ curthooks .copy_crypttab (crypttab_location , target )
70
+
71
+ udev_rules_d = os .path .join (state ["scratch" ], "rules.d" )
72
+ if os .path .isdir (udev_rules_d ):
73
+ curthooks .copy_dname_rules (udev_rules_d , target )
74
+
75
+ apt_config .apply_debconf_selections (cfg , target )
76
+
77
+ curthooks .apply_networking (target , state )
78
+ curthooks .handle_pollinate_user_agent (cfg , target )
79
+
80
+ # re-enable update_initramfs
81
+ curthooks .enable_update_initramfs (cfg , target , machine )
82
+ curthooks .update_initramfs (target , all_kernels = True )
83
+
84
+ run_hook_in_target (target , "setup-bootloader" )
85
+
86
+
87
+ def cleanup ():
88
+ """Remove curtin-hooks so its as if we were never here."""
89
+ curtin_dir = os .path .dirname (__file__ )
90
+ shutil .rmtree (curtin_dir )
91
+
92
+
93
+ def main ():
94
+ state = load_command_environment ()
95
+ config = load_command_config (None , state )
96
+ target = state ["target" ]
97
+
98
+ basicConfig (stream = sys .stderr , verbosity = DEBUG )
99
+
100
+ curthook (config , target , state )
101
+ cleanup ()
102
+
103
+
104
+ if __name__ == "__main__" :
105
+ main ()
0 commit comments