From f2797ac5b0d4414be0a49d98b0dba24df9929b3a Mon Sep 17 00:00:00 2001
From: Alexandre MERLIN <alexandre.merlin@inria.fr>
Date: Tue, 18 Jan 2022 09:10:41 +0100
Subject: [PATCH] add startup script step

---
 doc/Kadeploy.tex                              |  1 +
 lib/kadeploy3/server/config.rb                |  3 +++
 lib/kadeploy3/server/macrostep.rb             |  3 ++-
 lib/kadeploy3/server/microsteps.rb            | 10 ++++++++++
 lib/kadeploy3/server/stepdeploy.rb            |  4 ++++
 vagrant-env/kadeploy_conf/sample-cluster.conf |  1 +
 vagrant-env/scripts/startup.sh                |  1 +
 7 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 vagrant-env/scripts/startup.sh

diff --git a/doc/Kadeploy.tex b/doc/Kadeploy.tex
index 42a640f3..51173ebd 100644
--- a/doc/Kadeploy.tex
+++ b/doc/Kadeploy.tex
@@ -1169,6 +1169,7 @@ automata:
   \begin{itemize}
     \item \yfieldd{sleep\_time\_before\_boot}{Integer}{20} seconds of sleep before the first ping request to detect when a node is ready. Generally, it represents the average time during the node power up until it sends a dhcp request.
     \item \yfield{install\_bootloader}{String} Path to a script that install a bootloader on the deployment partition. You can use Kadeploy3 environment variables (see section \ref{sec:env_vars}) in this script. Please refer to section \ref{sec:bootscript}) for further informations.
+    \item \yfield{startup\_script}{String} An admin script to be executed on each node at the boot of the deployment kernel. Can be ommited. You can use Kadeploy3 environment variables (see section \ref{sec:env_vars}) in this script. Please refer to section \ref{sec:bootscript}) for further informations.
   \end{itemize}
 
   \item \ypath{/boot/kernels} Options of OS kernel's that are booted by Kadeploy3
diff --git a/lib/kadeploy3/server/config.rb b/lib/kadeploy3/server/config.rb
index 521a85a2..dae2259a 100644
--- a/lib/kadeploy3/server/config.rb
+++ b/lib/kadeploy3/server/config.rb
@@ -878,6 +878,7 @@ module Configuration
     attr_reader :nfsroot_params
     attr_reader :admin_pre_install
     attr_reader :admin_post_install
+    attr_reader :startup_script
     attr_reader :use_ip_to_deploy
     attr_reader :cmd_ext
     attr_reader :trusted_deployment_user
@@ -1033,6 +1034,8 @@ module Configuration
           @sleep_time_before_ping = cp.value('sleep_time_before_ping', Integer, 20)
           @bootloader_script = cp.value('install_bootloader', String, nil,
             { :type => 'file', :readable => true, :prefix => Config.dir() })
+          @startup_script = cp.value('startup_script', String, '',
+            { :type => 'file', :readable => true, :prefix => Config.dir() })
           cp.parse('kernels', true) do
             cp.parse('user') do
               @kernel_params = cp.value('params', String, '')
diff --git a/lib/kadeploy3/server/macrostep.rb b/lib/kadeploy3/server/macrostep.rb
index 7995ea88..3e30b2fe 100644
--- a/lib/kadeploy3/server/macrostep.rb
+++ b/lib/kadeploy3/server/macrostep.rb
@@ -318,8 +318,9 @@ module Macrostep
 
       delete_task(:manage_user_post_install, "no user postinstall") if cexec.environment.postinstall.nil?
 
-      delete_task(:set_default_vlan, "no vlan cmd set") if cexec.vlan_id.nil? && context[:common].set_vlan_cmd.nil? && context[:common].set_vlan_script.empty?
+      delete_task(:set_default_vlan, "no vlan cmd set") if cexec.vlan_id.nil? || context[:common].set_vlan_cmd.nil? || context[:common].set_vlan_script.empty?
       delete_task(:set_vlan, "no vlan set") if cexec.vlan_id.nil?
+      delete_task(:startup_script, "no startup script set") if context[:cluster].startup_script.nil? || context[:cluster].startup_script.empty?
 
       # Do not reformat deploy partition
       if !cexec.deploy_part.nil? and cexec.deploy_part != ""
diff --git a/lib/kadeploy3/server/microsteps.rb b/lib/kadeploy3/server/microsteps.rb
index 8779f2bb..898cf592 100644
--- a/lib/kadeploy3/server/microsteps.rb
+++ b/lib/kadeploy3/server/microsteps.rb
@@ -1947,6 +1947,16 @@ class Microstep < Automata::QueueTask
     return parallel_exec("mount #{tmp_part} /tmp")
   end
 
+  # Custom script executed just after Deployment Mini OS boot
+  def ms_startup_script
+    parallel_exec(
+      run_script(),
+      {
+        :input_file => context[:cluster].startup_script,
+        :scattering => :tree
+      })
+  end
+
   # Send the SSH key in the deployed environment
   #
   # Arguments
diff --git a/lib/kadeploy3/server/stepdeploy.rb b/lib/kadeploy3/server/stepdeploy.rb
index 1959add7..7f020147 100644
--- a/lib/kadeploy3/server/stepdeploy.rb
+++ b/lib/kadeploy3/server/stepdeploy.rb
@@ -16,6 +16,7 @@ module Macrostep
         [:set_default_vlan],
         [:reboot, "soft"],
         [:wait_reboot],
+        [:startup_script],
         [:send_key_in_deploy_env, :tree],
         [:create_partition_table],
         [:format_deploy_part],
@@ -34,6 +35,7 @@ module Macrostep
         [:set_default_vlan],
         [:kexec, :to_deploy_kernel],
         [:wait_reboot],
+        [:startup_script],
         [:send_key_in_deploy_env, :tree],
         [:create_partition_table],
         [:format_deploy_part],
@@ -56,6 +58,7 @@ module Macrostep
         [:set_default_vlan],
         [:reboot, "soft"],
         [:wait_reboot],
+        [:startup_script],
         [:send_key_in_deploy_env, :tree],
         [:manage_admin_pre_install, :tree],
       ]
@@ -79,6 +82,7 @@ module Macrostep
         [:set_default_vlan],
         [:reboot, "soft"],
         [:wait_reboot],
+        [:startup_script],
         [:send_key_in_deploy_env, :tree],
         [:create_partition_table],
         [:format_deploy_part],
diff --git a/vagrant-env/kadeploy_conf/sample-cluster.conf b/vagrant-env/kadeploy_conf/sample-cluster.conf
index 4eef65d6..3db47caa 100644
--- a/vagrant-env/kadeploy_conf/sample-cluster.conf
+++ b/vagrant-env/kadeploy_conf/sample-cluster.conf
@@ -25,6 +25,7 @@ arch:
 boot:
   sleep_time_before_ping: 0
   install_bootloader: /vagrant/scripts/install-grub2-dev.sh
+  startup_script: /vagrant/scripts/startup.sh
   kernels:
     deploy:
       initrd: kernels/kadeploy3-deploy-kernel-buster.initrd.img
diff --git a/vagrant-env/scripts/startup.sh b/vagrant-env/scripts/startup.sh
new file mode 100644
index 00000000..fc9d44b5
--- /dev/null
+++ b/vagrant-env/scripts/startup.sh
@@ -0,0 +1 @@
+echo "Execution of startup script" > /dev/kmsg
-- 
GitLab