{% extends 'base.jinja2' %}

{% set console_device = console_device | default('ttyS0') %}
{% set baud_rate = baud_rate | default(115200) %}
{% set grub_installed_device = grub_installed_device | default('(hd1,gpt1)') %}
{% set efi_grub = efi_grub | default(False) %}
{% set pxe_net_configured = pxe_net_configured | default(True) %}
{% set net_interface = net_interface | default('') %}

{# allow job context override - use a different variable name, undefined if not in job context #}
{% set base_kernel_args = extra_kernel_args | default(base_kernel_args) %}

{% if efi_grub %}

{% if net_commands is not defined %}
{% if static_ip %}
{% set net_commands = "          - net_add_addr lava efinet0 " + static_ip + "
          - net_add_route lava 0.0.0.0 gw " + static_gateway + "
          - net_add_dns " + static_dns %}
{% else %}
{% if pxe_net_configured %}

{% set net_commands = "          - net_ls_addr" %}

{% else %}

{% if net_interface %}
{% set net_commands = "          - net_bootp " + net_interface %}
{% else %}
{% set net_commands = "          - net_bootp" %}
{% endif %}

{% endif %}

{# end static_ip #}
{% endif %}
{% endif %}

{% if efinet %}

{% set net_commands = "          - insmod efinet
" + net_commands %}

{% endif %}

{% else %}
{# not efi_grub #}

{% if net_commands is not defined %}
{% if net_interface %}
{% set net_commands = "          - net_bootp " + net_interface %}
{% else %}
{% set net_commands = "          - net_bootp" %}
{% endif %}
{% endif %}

{% endif %}

{% if console_device %}
{% set console_args = "console=" ~ console_device ~ "," ~ baud_rate %}
{% else %}
{% set console_args = "" %}
{% endif %}
{% if has_dtb %}
{% set dtb_commands = "          - clear
          - devicetree (tftp,{SERVER_IP})/{DTB}" %}
{% else %}
{% set dtb_commands = "" %}
{% endif %}

{% if has_nodebooter %}
{% set nodebooter_action = "      nodebooter:" %}
{% else %}
{% set nodebooter_action = "" %}
{% endif %}

{% block body %}

actions:
  deploy:
    connections:
      serial:
    methods:
      tftp:
      nbd:
  boot:
    connections:
      serial:
    methods:
      minimal:
{{ nodebooter_action }}
      grub:
        parameters:
          bootloader_prompt: {{ bootloader_prompt|default('grub>') }}
          needs_interrupt: {{ grub_needs_interrupt|default(true) }}
        ramdisk:
          commands:
{% block ramdisk_commands %}
{{ net_commands }}
          - clear
          - insmod linux
          - clear
          - insmod tftp
          - clear
          - 'linux (tftp,{SERVER_IP})/{KERNEL} {{ console_args }} {{ base_ip_args }} {{ base_kernel_args }}'
          - clear
          - initrd (tftp,{SERVER_IP})/{RAMDISK}
{{ dtb_commands }}
          - clear
          - boot
{% endblock ramdisk_commands %}
        nbd:
          commands:
          - clear
          - insmod linux
          - clear
          - insmod tftp
          - clear
          - 'linux (tftp,{SERVER_IP})/{KERNEL} {{ console_args }} {{ base_ip_args }} {{ base_nbdroot_args }} rw {{ base_kernel_args }}'
          - clear
          - initrd (tftp,{SERVER_IP})/{RAMDISK}
          - boot
        nfs:
          commands:
{% block nfs_commands %}
{{ net_commands }}
          - 'linux (tftp,{SERVER_IP})/{KERNEL} {{ console_args }} root=/dev/nfs rw {{ base_nfsroot_args }} {{ base_ip_args }} {{ base_kernel_args }}'
          - initrd (tftp,{SERVER_IP})/{RAMDISK}
{{ dtb_commands }}
          - boot
{% endblock nfs_commands %}
        debian-installer:
          expect_shell: False
          commands:
{{ net_commands }}
          - 'linux (tftp,{SERVER_IP})/{KERNEL} auto=true interface=eth0 priority=critical noshell BOOT_DEBUG=1 DEBIAN_FRONTEND=text url=tftp://{SERVER_IP}/{PRESEED_CONFIG} --- {{ console_args }} {{ base_kernel_args }} debug verbose'
          - initrd (tftp,{SERVER_IP})/{RAMDISK}
{{ dtb_commands }}
          - boot
        debian-installed:
          commands:
          - insmod part_gpt
          - insmod ext2
          - insmod part_msdos
          - insmod chain
          - ls
          - ls {{ grub_installed_device }}
          - sleep 10
          - set
          - set root={{ grub_installed_device }}
          - sleep 10
          - ls {{ grub_installed_device }}
          - sleep 10
          - chainloader {{ grub_installed_device }}/efi/debian/grubaa64.efi
          - sleep 10
          - boot
  test:
    methods:
      docker:
        global_options:
        - {{ docker_shell_global_arguments }}
        options:
        - {{ docker_shell_extra_arguments }}
{% endblock body %}
