From bb61c1f7459e8286be926d887c504bd78f90ee75 Mon Sep 17 00:00:00 2001 From: Jason Ertel Date: Fri, 11 Dec 2020 15:33:27 -0500 Subject: [PATCH] Cleanup bash imports/sources, function definitions, and variables --- setup/so-common-functions | 52 -------------------------------------- setup/so-functions | 53 +++++++++++++++++++++++++++++++++------ setup/so-setup | 12 ++++++++- setup/so-variables | 2 ++ setup/so-whiptail | 3 --- 5 files changed, 59 insertions(+), 63 deletions(-) delete mode 100644 setup/so-common-functions diff --git a/setup/so-common-functions b/setup/so-common-functions deleted file mode 100644 index 8bdf09374..000000000 --- a/setup/so-common-functions +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash - -source ./so-variables -source ../salt/common/tools/sbin/so-common -source ../salt/common/tools/sbin/so-image-common - -# Helper functions - -filter_unused_nics() { - - if [[ $MNIC ]]; then local grep_string="$MNIC\|bond0"; else local grep_string="bond0"; fi - - # If we call this function and NICs have already been assigned to the bond interface then add them to the grep search string - if [[ $BNICS ]]; then - grep_string="$grep_string" - for BONDNIC in "${BNICS[@]}"; do - grep_string="$grep_string\|$BONDNIC" - done - fi - - # Finally, set filtered_nics to any NICs we aren't using (and ignore interfaces that aren't of use) - filtered_nics=$(ip link | awk -F: '$0 !~ "lo|vir|veth|br|docker|wl|^[^0-9]"{print $2}' | grep -vwe "$grep_string" | sed 's/ //g') - readarray -t filtered_nics <<< "$filtered_nics" - - nic_list=() - for nic in "${filtered_nics[@]}"; do - case $(cat "/sys/class/net/${nic}/carrier" 2>/dev/null) in - 1) - nic_list+=("$nic" "Link UP " "OFF") - ;; - 0) - nic_list+=("$nic" "Link DOWN " "OFF") - ;; - *) - nic_list+=("$nic" "Link UNKNOWN " "OFF") - ;; - esac - done - - export nic_list -} - -calculate_useable_cores() { - - # Calculate reasonable core usage - local cores_for_zeek=$(( (num_cpu_cores/2) - 1 )) - local lb_procs_round - lb_procs_round=$(printf "%.0f\n" $cores_for_zeek) - - if [ "$lb_procs_round" -lt 1 ]; then lb_procs=1; else lb_procs=$lb_procs_round; fi - export lb_procs -} diff --git a/setup/so-functions b/setup/so-functions index 66450a6bb..9f90138f3 100755 --- a/setup/so-functions +++ b/setup/so-functions @@ -15,13 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -source ./so-whiptail -source ./so-variables -source ./so-common-functions - -CONTAINER_REGISTRY=quay.io - -SOVERSION=$(cat ../VERSION) +# README - DO NOT DEFINE GLOBAL VARIABLES IN THIS FILE. Instead use so-variables. log() { msg=$1 @@ -48,6 +42,51 @@ logCmd() { $cmd >> "$setup_log" 2>&1 } +filter_unused_nics() { + + if [[ $MNIC ]]; then local grep_string="$MNIC\|bond0"; else local grep_string="bond0"; fi + + # If we call this function and NICs have already been assigned to the bond interface then add them to the grep search string + if [[ $BNICS ]]; then + grep_string="$grep_string" + for BONDNIC in "${BNICS[@]}"; do + grep_string="$grep_string\|$BONDNIC" + done + fi + + # Finally, set filtered_nics to any NICs we aren't using (and ignore interfaces that aren't of use) + filtered_nics=$(ip link | awk -F: '$0 !~ "lo|vir|veth|br|docker|wl|^[^0-9]"{print $2}' | grep -vwe "$grep_string" | sed 's/ //g') + readarray -t filtered_nics <<< "$filtered_nics" + + nic_list=() + for nic in "${filtered_nics[@]}"; do + case $(cat "/sys/class/net/${nic}/carrier" 2>/dev/null) in + 1) + nic_list+=("$nic" "Link UP " "OFF") + ;; + 0) + nic_list+=("$nic" "Link DOWN " "OFF") + ;; + *) + nic_list+=("$nic" "Link UNKNOWN " "OFF") + ;; + esac + done + + export nic_list +} + +calculate_useable_cores() { + + # Calculate reasonable core usage + local cores_for_zeek=$(( (num_cpu_cores/2) - 1 )) + local lb_procs_round + lb_procs_round=$(printf "%.0f\n" $cores_for_zeek) + + if [ "$lb_procs_round" -lt 1 ]; then lb_procs=1; else lb_procs=$lb_procs_round; fi + export lb_procs +} + airgap_rules() { # Copy the rules for suricata if using Airgap mkdir -p /nsm/repo/rules diff --git a/setup/so-setup b/setup/so-setup index ccfa87a59..1a821d405 100755 --- a/setup/so-setup +++ b/setup/so-setup @@ -24,9 +24,19 @@ fi cd "$(dirname "$0")" || exit 255 +# Source the generic function libraries that are also used by the product after +# setup. These functions are intended to be reusable outside of the setup process. +source ../salt/common/tools/sbin/so-common +source ../salt/common/tools/sbin/so-image-common + +# Setup bash functionality is divided into functions and user-facing prompts. +# Do not attempt to re-use any of this functionality outside of setup. Instead, +# if needed, migrated generic functions into so-common. source ./so-functions -source ./so-common-functions source ./so-whiptail + +# Finally, source the default variable definitions, which require availability of +# functions source above. source ./so-variables # Parse command line arguments diff --git a/setup/so-variables b/setup/so-variables index 17441dca0..09e0ebc46 100644 --- a/setup/so-variables +++ b/setup/so-variables @@ -1,5 +1,7 @@ #!/bin/bash +SOVERSION=$(cat ../VERSION) + total_mem=$(grep MemTotal /proc/meminfo | awk '{print $2}' | sed -r 's/.{3}$//') export total_mem diff --git a/setup/so-whiptail b/setup/so-whiptail index 7b105bb8e..791cceb76 100755 --- a/setup/so-whiptail +++ b/setup/so-whiptail @@ -15,9 +15,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -source ./so-variables -source ./so-common-functions - whiptail_airgap() { [ -n "$TESTING" ] && return