Skip to content

Commit

Permalink
refactor: format curtin-hook code (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsander-souza authored Jun 25, 2024
1 parent 9929d0b commit 012666a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
51 changes: 28 additions & 23 deletions ubuntu/scripts/curtin-hooks
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,71 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import shutil
import platform
import shutil
import sys

from curtin import distro, util
from curtin.commands import apt_config, curthooks
from curtin.config import load_command_config
from curtin.log import LOG
from curtin.paths import target_path
from curtin.util import load_command_environment, ChrootableTarget
from curtin.commands import curthooks, apt_config
from curtin.util import ChrootableTarget, load_command_environment


def run_hook_in_target(target, hook):
"""Look for "hook" in "target" and run in a chroot"""
target_hook = target_path(target, '/curtin/' + hook)
target_hook = target_path(target, "/curtin/" + hook)
if os.path.isfile(target_hook):
LOG.debug("running %s" % target_hook)
with ChrootableTarget(target=target) as in_chroot:
in_chroot.subp(['/curtin/' + hook])
in_chroot.subp(["/curtin/" + hook])
return True
return False


def curthook(cfg, target, state):
"""Configure network and bootloader"""
LOG.info('Running curtin builtin curthooks')
state_etcd = os.path.split(state['fstab'])[0]
LOG.info("Running curtin builtin curthooks")
state_etcd = os.path.split(state["fstab"])[0]
machine = platform.machine()

distro_info = distro.get_distroinfo(target=target)
if not distro_info:
raise RuntimeError('Failed to determine target distro')
raise RuntimeError("Failed to determine target distro")
osfamily = distro_info.family
LOG.info('Configuring target system for distro: %s osfamily: %s',
distro_info.variant, osfamily)

sources = cfg.get('sources', {})
LOG.info(
"Configuring target system for distro: %s osfamily: %s",
distro_info.variant,
osfamily,
)

sources = cfg.get("sources", {})
dd_image = len(util.get_dd_images(sources)) > 0

curthooks.disable_overlayroot(cfg, target)
curthooks.disable_update_initramfs(cfg, target, machine)
curthooks.install_missing_packages(cfg, target, osfamily=osfamily)

if not dd_image:
curthooks.configure_iscsi(cfg, state_etcd, target, osfamily=osfamily)
curthooks.configure_mdadm(cfg, state_etcd, target, osfamily=osfamily)
curthooks.copy_fstab(state.get('fstab'), target)
curthooks.add_swap(cfg, target, state.get('fstab'))
curthooks.copy_fstab(state.get("fstab"), target)
curthooks.add_swap(cfg, target, state.get("fstab"))

run_hook_in_target(target, 'install-custom-packages')
run_hook_in_target(target, "install-custom-packages")

if not dd_image:
curthooks.setup_kernel_img_conf(target)

crypttab_location = os.path.join(os.path.split(state['fstab'])[0],
"crypttab")
crypttab_location = os.path.join(os.path.split(state["fstab"])[0], "crypttab")
if os.path.exists(crypttab_location):
curthooks.copy_crypttab(crypttab_location, target)

udev_rules_d = os.path.join(state['scratch'], "rules.d")
udev_rules_d = os.path.join(state["scratch"], "rules.d")
if os.path.isdir(udev_rules_d):
curthooks.copy_dname_rules(udev_rules_d, target)

apt_config.apply_debconf_selections(cfg, target)

curthooks.apply_networking(target, state)
Expand All @@ -88,8 +93,8 @@ def curthook(cfg, target, state):
# re-enable update_initramfs
curthooks.enable_update_initramfs(cfg, target, machine)
curthooks.update_initramfs(target, all_kernels=True)
run_hook_in_target(target, 'setup-bootloader')

run_hook_in_target(target, "setup-bootloader")


def cleanup():
Expand All @@ -101,7 +106,7 @@ def cleanup():
def main():
state = load_command_environment()
config = load_command_config(None, state)
target = state['target']
target = state["target"]

curthook(config, target, state)
cleanup()
Expand Down
2 changes: 1 addition & 1 deletion ubuntu/scripts/networking.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export DEBIAN_FRONTEND=noninteractive

apt-get install -qy netplan.io cloud-init

cat > /etc/sysctl.d/99-cloudimg-ipv6.conf <<EOF
cat >/etc/sysctl.d/99-cloudimg-ipv6.conf <<EOF
net.ipv6.conf.all.use_tempaddr = 0
net.ipv6.conf.default.use_tempaddr = 0
EOF
Expand Down

0 comments on commit 012666a

Please sign in to comment.