From 1001b331f96d37cea2c18350060a53435c1f11f4 Mon Sep 17 00:00:00 2001
From: Alexandre MERLIN <alexandre.merlin@inria.fr>
Date: Tue, 8 Jun 2021 10:38:07 +0200
Subject: [PATCH] [microsteps+config] Allow to use a script for formating
 operations

---
 lib/kadeploy3/server/config.rb                |  3 +
 lib/kadeploy3/server/microsteps.rb            | 74 ++++++++++++++-----
 vagrant-env/kadeploy_conf/sample-cluster.conf |  1 +
 vagrant-env/scripts/formating.sh              | 33 +++++++++
 4 files changed, 91 insertions(+), 20 deletions(-)
 create mode 100644 vagrant-env/scripts/formating.sh

diff --git a/lib/kadeploy3/server/config.rb b/lib/kadeploy3/server/config.rb
index f0e28dfc..f834cf5e 100644
--- a/lib/kadeploy3/server/config.rb
+++ b/lib/kadeploy3/server/config.rb
@@ -848,6 +848,7 @@ module Configuration
     attr_reader :decompress_environment
     attr_reader :group_of_nodes #Hashtable (key is a command name)
     attr_reader :partitioning_script
+    attr_reader :formating_script
     attr_reader :kexec_server_precmd
     attr_reader :kexec_script
     attr_reader :bootloader_script
@@ -982,6 +983,8 @@ module Configuration
           end
           @partitioning_script = cp.value('script',String,nil,
             { :type => 'file', :readable => true, :prefix => Config.dir() })
+          @formating_script = cp.value('formating_script',String,'',
+            { :type => 'file', :readable => true, :prefix => Config.dir() })
         end
 
         cp.parse('kexec', true) do
diff --git a/lib/kadeploy3/server/microsteps.rb b/lib/kadeploy3/server/microsteps.rb
index f0016076..8f19ba6c 100644
--- a/lib/kadeploy3/server/microsteps.rb
+++ b/lib/kadeploy3/server/microsteps.rb
@@ -1802,19 +1802,30 @@ class Microstep < Automata::QueueTask
   # Output
   # * return true if the format has been successfully performed, false otherwise
   def ms_format_deploy_part()
-    if context[:common].mkfs_options.has_key?(context[:execution].environment.filesystem) then
-      opts = context[:common].mkfs_options[context[:execution].environment.filesystem]
-      return parallel_exec(
-        "mkdir -p #{context[:common].environment_extraction_dir}; "\
-        "umount #{get_deploy_part_str()} 2>/dev/null; "\
-        "mkfs -t #{context[:execution].environment.filesystem} #{opts} #{get_deploy_part_str()}"
+    if context[:cluster].formating_script && !context[:cluster].formating_script.empty?
+      debug(5,"Using the formating script for \"format_deploy_part\" step")
+      parallel_exec(
+        run_script("KADEPLOY_FORMAT_PART=DEPLOY"),
+        {
+          :input_file => context[:cluster].formating_script,
+          :scattering => :tree
+        }
       )
     else
-      return parallel_exec(
-        "mkdir -p #{context[:common].environment_extraction_dir}; "\
-        "umount #{get_deploy_part_str()} 2>/dev/null; "\
-        "mkfs -t #{context[:execution].environment.filesystem} #{get_deploy_part_str()}"
-      )
+      if context[:common].mkfs_options.has_key?(context[:execution].environment.filesystem) then
+        opts = context[:common].mkfs_options[context[:execution].environment.filesystem]
+        return parallel_exec(
+          "mkdir -p #{context[:common].environment_extraction_dir}; "\
+          "umount #{get_deploy_part_str()} 2>/dev/null; "\
+          "mkfs -t #{context[:execution].environment.filesystem} #{opts} #{get_deploy_part_str()}"
+        )
+      else
+        return parallel_exec(
+          "mkdir -p #{context[:common].environment_extraction_dir}; "\
+          "umount #{get_deploy_part_str()} 2>/dev/null; "\
+          "mkfs -t #{context[:execution].environment.filesystem} #{get_deploy_part_str()}"
+        )
+      end
     end
   end
 
@@ -1824,25 +1835,48 @@ class Microstep < Automata::QueueTask
   # Output
   # * return true if the format has been successfully performed, false otherwise
   def ms_format_tmp_part()
-    fstype = context[:execution].reformat_tmp
-    if context[:common].mkfs_options.has_key?(fstype) then
-      opts = context[:common].mkfs_options[fstype]
-      tmp_part = get_block_device_str() + context[:cluster].tmp_part
-      return parallel_exec("mkdir -p /tmp; umount #{tmp_part} 2>/dev/null; mkfs.#{fstype} #{opts} #{tmp_part};mount #{tmp_part} /mnt/tmp && chmod 1777 /mnt/tmp; umount /mnt/tmp")
+    if context[:cluster].formating_script && !context[:cluster].formating_script.empty?
+      debug(5,"Using the formating script for \"format_tmp_part\" step")
+      parallel_exec(
+        run_script("KADEPLOY_FORMAT_PART=TMP"),
+        {
+          :input_file => context[:cluster].formating_script,
+          :scattering => :tree
+        }
+      )
     else
-      tmp_part = get_block_device_str() + context[:cluster].tmp_part
-      return parallel_exec("mkdir -p /tmp; umount #{tmp_part} 2>/dev/null; mkfs.#{fstype} #{tmp_part};mount #{tmp_part} /mnt/tmp && chmod 1777 /mnt/tmp; umount /mnt/tmp")
+      fstype = context[:execution].reformat_tmp
+      if context[:common].mkfs_options.has_key?(fstype) then
+        opts = context[:common].mkfs_options[fstype]
+        tmp_part = get_block_device_str() + context[:cluster].tmp_part
+        return parallel_exec("mkdir -p /tmp; umount #{tmp_part} 2>/dev/null; mkfs.#{fstype} #{opts} #{tmp_part};mount #{tmp_part} /mnt/tmp && chmod 1777 /mnt/tmp; umount /mnt/tmp")
+      else
+        tmp_part = get_block_device_str() + context[:cluster].tmp_part
+        return parallel_exec("mkdir -p /tmp; umount #{tmp_part} 2>/dev/null; mkfs.#{fstype} #{tmp_part};mount #{tmp_part} /mnt/tmp && chmod 1777 /mnt/tmp; umount /mnt/tmp")
+      end
     end
   end
 
+
   # Format the swap part on the nodes
   #
   # Arguments
   # Output
   # * return true if the format has been successfully performed, false otherwise
   def ms_format_swap_part()
-    swap_part = get_block_device_str() + context[:cluster].swap_part
-    return parallel_exec("mkswap #{swap_part}")
+    if context[:cluster].formating_script && !context[:cluster].formating_script.empty?
+      debug(5,"Using the formating script for \"format_swap_part\" step")
+      parallel_exec(
+        run_script("KADEPLOY_FORMAT_PART=SWAP"),
+        {
+          :input_file => context[:cluster].formating_script,
+          :scattering => :tree
+        }
+      )
+    else
+      swap_part = get_block_device_str() + context[:cluster].swap_part
+      return parallel_exec("mkswap #{swap_part}")
+    end
   end
 
   # Mount the deployment part on the nodes
diff --git a/vagrant-env/kadeploy_conf/sample-cluster.conf b/vagrant-env/kadeploy_conf/sample-cluster.conf
index f620d11a..2f70ed92 100644
--- a/vagrant-env/kadeploy_conf/sample-cluster.conf
+++ b/vagrant-env/kadeploy_conf/sample-cluster.conf
@@ -8,6 +8,7 @@ partitioning:
   disable_swap: false
   block_device: /dev/sda
   script: /vagrant/scripts/partitioning.sh
+  formating_script: /vagrant/scripts/formating.sh
 trusted_deployment:
   user: deploy
   partition: "/dev/sda3"
diff --git a/vagrant-env/scripts/formating.sh b/vagrant-env/scripts/formating.sh
new file mode 100644
index 00000000..3e69f133
--- /dev/null
+++ b/vagrant-env/scripts/formating.sh
@@ -0,0 +1,33 @@
+#!/bin/bash -e
+
+if [ "$KADEPLOY_FORMAT_PART" = "DEPLOY" ]; then
+    echo "Kadeploy start deploy partition formating ($KADEPLOY_DEPLOY_PART in $KADEPLOY_FS_TYPE)" > /dev/kmsg
+    mkdir -p "$KADEPLOY_ENV_EXTRACTION_DIR"
+    umount "$KADEPLOY_DEPLOY_PART" 2>/dev/null || true
+    mkfs -t "$KADEPLOY_FS_TYPE" -F -b 4096 -O sparse_super,filetype,resize_inode,dir_index -q "$KADEPLOY_DEPLOY_PART"
+    echo "Deploy partition formating done" > /dev/kmsg
+elif [ "$KADEPLOY_FORMAT_PART" = "TMP" ]; then
+    TMP_PART="${KADEPLOY_BLOCK_DEVICE}${KADEPLOY_TMP_PART_NUM}"
+    echo "Kadeploy start tmp partition formating ($TMP_PART in $KADEPLOY_FS_TYPE_TMP)" > /dev/kmsg
+    umount "$TMP_PART" 2>/dev/null || true
+    # preserve UUID for tmp partition since it's shared between prod & deploy
+    if [ -b "$TMP_PART" ] && [[ -n $(lsblk "$TMP_PART" -o UUID -n) ]]; then
+        mkfs -t "$KADEPLOY_FS_TYPE_TMP" -U $(lsblk "$TMP_PART" -o UUID -n) -F -b 4096 -O sparse_super,filetype,resize_inode,dir_index -q "$TMP_PART"
+    else
+        mkfs -t "$KADEPLOY_FS_TYPE_TMP" -F -b 4096 -O sparse_super,filetype,resize_inode,dir_index -q "$TMP_PART"
+    fi
+    mount "$TMP_PART" /mnt/tmp
+    chmod 1777 /mnt/tmp
+    umount /mnt/tmp
+    echo "Tmp partition formating done" > /dev/kmsg
+elif [ "$KADEPLOY_FORMAT_PART" = "SWAP" ]; then
+    SWAP_PART="${KADEPLOY_BLOCK_DEVICE}${KADEPLOY_SWAP_PART_NUM}"
+    echo "Kadeploy start swap partition formating ($SWAP_PART)" > /dev/kmsg
+    # preserve UUID for swap partition since it's shared between prod & deploy
+    if [ -b "$SWAP_PART" ] && [[ -n $(lsblk "$SWAP_PART" -o UUID -n) ]]; then
+        mkswap --uuid $(lsblk "$SWAP_PART" -o UUID -n) "$SWAP_PART"
+    else
+        mkswap "$SWAP_PART"
+    fi
+    echo "Swap partition formating done" > /dev/kmsg
+fi
-- 
GitLab