← Back to team overview

canonical-hw-cert team mailing list archive

[Merge] ~rodsmith/maas-cert-server:improve-maniacs-setup-code into maas-cert-server:master

 

Rod Smith has proposed merging ~rodsmith/maas-cert-server:improve-maniacs-setup-code into maas-cert-server:master.

Commit message:
Fix shellcheck warnings on maniacs-setup

Requested reviews:
  hardware-certification-users (hardware-certification)

For more details, see:
https://code.launchpad.net/~rodsmith/maas-cert-server/+git/maas-cert-server/+merge/433309

This MR clears all the shellcheck warnings on maniacs-setup. (Using the -x option to shellcheck is necessary for one warnings.) I've done a test run and it worked fine.

This MR also clears out a couple of code segments that are no longer relevant because they relate to old versions of Ubuntu. (The current version is officially supported only on Ubuntu 22.04.) Other such code remains, though. Clearing these out was just convenient because they were triggering shellcheck warnings.
-- 
Your team hardware-certification-users is requested to review the proposed merge of ~rodsmith/maas-cert-server:improve-maniacs-setup-code into maas-cert-server:master.
diff --git a/debian/changelog b/debian/changelog
index 0b01607..0513a08 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+maas-cert-server (0.7.2-0ppa1) jammy; urgency=medium
+
+  * Clean up maniacs-setup code
+
+ -- Rod Smith <rod.smith@xxxxxxxxxxxxx>  Fri, 18 Nov 2022 12:33:27 -0500
+
 maas-cert-server (0.7.1-0ppa1) jammy; urgency=medium
 
   * Remove references to certification-docs, which has been deprecated.
diff --git a/usr/sbin/maniacs-setup b/usr/sbin/maniacs-setup
index 4345f13..2ca34ca 100755
--- a/usr/sbin/maniacs-setup
+++ b/usr/sbin/maniacs-setup
@@ -123,7 +123,7 @@ get_release_info() {
 check_set_progress() {
     local mystatus=incomplete
     # Is our current op already in the tracker?
-    if [ "$(grep "$1" "$PROGRESS_TRACKER")" ] ; then
+    if grep -q "$1" "$PROGRESS_TRACKER" ; then
         mystatus=completed
     else
        echo "$1" >> "$PROGRESS_TRACKER"
@@ -180,8 +180,7 @@ setup_globals() {
     if [ $USE_SNAPS == 0 ] ; then
         MAAS_VERSION=$(dpkg -s maas | grep -m 1 Version | cut -d " " -f 2)
         set +e
-        dpkg --compare-versions "2.0.0~beta" "ge" "$MAAS_VERSION"
-        if [ "$?" = 0 ] ; then
+        if dpkg --compare-versions "2.0.0~beta" "ge" "$MAAS_VERSION" ; then
             echo "This script requires MAAS 2.0 or greater!"
             echo "Exiting!"
             exit 1
@@ -208,8 +207,9 @@ setup_progress_tracker() {
 }
 
 setup_network_addresses() {
-    local internal_with_route=$(route | grep default | grep $INTERNAL_NET)
-    if [ ! -z "$internal_with_route" ] ; then
+    local internal_with_route
+    internal_with_route=$(route | grep default | grep $INTERNAL_NET) || true
+    if [ -n "$internal_with_route" ] ; then
         echo "Your default route goes through $INTERNAL_NET, but you've set INTERNAL_NET"
         echo "to $INTERNAL_NET in /etc/maas-cert-server/config. Your internal network"
         echo "should NOT have a default route set! Please fix it!"
@@ -219,7 +219,9 @@ setup_network_addresses() {
     INTERNAL_IP=$(ip -4 addr show "$INTERNAL_NET" | grep inet | tr -s " " | cut -d" " -f3 | cut -d"/" -f1)
     INTERNAL_BROADCAST=$(ip -4 addr show "$INTERNAL_NET" | grep inet | tr -s " " | cut -d" " -f5)
     INTERNAL_NETMASK=$(ip -4 addr show "$INTERNAL_NET" | grep inet | tr -s " " | cut -d" " -f3 | cut -d"/" -f2)
-    INTERNAL_NETSTART=$(ipcalc -n "$INTERNAL_IP"/"$INTERNAL_NETMASK" | grep Network | tr -s " " | cut -d " " -f 2 | cut -d "/" -f 1)
+    # INTERNAL_NETSTART is currently unused; but if necessary in the future,
+    # uncomment the below line....
+    # INTERNAL_NETSTART=$(ipcalc -n "$INTERNAL_IP"/"$INTERNAL_NETMASK" | grep Network | tr -s " " | cut -d " " -f 2 | cut -d "/" -f 1)
     EXTERNAL_IP=$(ip -4 addr show "$EXTERNAL_NET" | grep inet | tr -s " " | cut -d" " -f3 | cut -d"/" -f1)
     MAAS_URL="http://$INTERNAL_IP:5240/MAAS";
     if [ -z "$INTERNAL_IP" ] ; then
@@ -287,7 +289,7 @@ setup_postgresql() {
     # as happens by default with Ubuntu 20.04 and later.
     echo
     echo "***************************************************************************"
-    if [ "$(check_set_progress "$FUNCNAME")" == "completed" ] ; then
+    if [ "$(check_set_progress "${FUNCNAME[0]}")" == "completed" ] ; then
         echo "* PostgresQL has already been configured."
         RERUN=yes
         return
@@ -312,7 +314,7 @@ setup_postgresql() {
 reconfigure_controllers() {
     echo 
     echo "***************************************************************************"
-    if [ "$(check_set_progress "$FUNCNAME")" == "completed" ] ; then
+    if [ "$(check_set_progress "${FUNCNAME[0]}")" == "completed" ] ; then
         echo "* Region and Cluster controllers have previously been reconfigured."
         RERUN=yes
         return
@@ -366,7 +368,7 @@ login_maas_admin() {
         echo "Attempting a login...."
         maas login admin "$MAAS_URL" "$APIKEY" &> /dev/null
         RETURN_CODE=$?
-        let TRIES_LEFT=$TRIES_LEFT-1
+        (( TRIES_LEFT-- )) || true
         if [ $RETURN_CODE != 0 ] ; then
             sleep 3
         fi
@@ -381,12 +383,12 @@ setup_maas_admin() {
     # so ignore errors for idempotence
     echo
     echo "***************************************************************************"
-    if [ "$(check_set_progress "$FUNCNAME")" == "completed" ] ; then
+    if [ "$(check_set_progress "${FUNCNAME[0]}")" == "completed" ] ; then
         echo "* MAAS Admin user has already been created for $DEFAULT_USER."
         RERUN=yes
         return
     fi
-    if [ -z $DB_PASS ] ; then
+    if [ -z "$DB_PASS" ] ; then
         echo -n "* Please enter the PostgresQL password: "
         read -sr DB_PASS
     fi
@@ -409,7 +411,7 @@ setup_maas_admin() {
 setup_ssh_keys() {
     echo
     echo "***************************************************************************"
-    if [ "$(check_set_progress "$FUNCNAME")" == "completed" ] ; then
+    if [ "$(check_set_progress "${FUNCNAME[0]}")" == "completed" ] ; then
         echo "* SSH Keys have already been created for $DEFAULT_USER."
         RERUN=yes
 	return
@@ -436,50 +438,56 @@ setup_ssh_keys() {
 
 write_starting_mirror_config() {
     base_path="/var/spool/apt-mirror"
-    echo "############# config ##################" > $MIRROR_LIST
-    echo "#" >> $MIRROR_LIST
-    echo "# set base_path    /var/spool/apt-mirror" >> $MIRROR_LIST
-    echo "#" >> $MIRROR_LIST
-    echo "# set mirror_path  $base_path/mirror" >> $MIRROR_LIST
-    echo "# set skel_path    $base_path/skel" >> $MIRROR_LIST
-    echo "# set var_path     $base_path/var" >> $MIRROR_LIST
-    echo "# set cleanscript $base_path/var/clean.sh" >> $MIRROR_LIST
-    echo "# set defaultarch  <running host architecture>" >> $MIRROR_LIST
-    echo "# set postmirror_script $base_path/var/postmirror.sh" >> $MIRROR_LIST
-    echo "# set run_postmirror 0" >> $MIRROR_LIST
-    echo "set nthreads     20" >> $MIRROR_LIST
-    echo "set _tilde 0" >> $MIRROR_LIST
-    echo "set base_path $ARCHIVE_MIRROR" >> $MIRROR_LIST
-    echo "#" >> $MIRROR_LIST
-    echo "############# end config ##############" >> $MIRROR_LIST
+    {
+        echo "############# config ##################"
+        echo "#"
+        echo "# set base_path    /var/spool/apt-mirror"
+        echo "#"
+        echo "# set mirror_path  $base_path/mirror"
+        echo "# set skel_path    $base_path/skel"
+        echo "# set var_path     $base_path/var"
+        echo "# set cleanscript $base_path/var/clean.sh"
+        echo "# set defaultarch  <running host architecture>"
+        echo "# set postmirror_script $base_path/var/postmirror.sh"
+        echo "# set run_postmirror 0"
+        echo "set nthreads     20"
+        echo "set _tilde 0"
+        echo "set base_path $ARCHIVE_MIRROR"
+        echo "#"
+        echo "############# end config ##############"
+    } > $MIRROR_LIST
 }
 
 
 write_partial_mirror_config() {
     release=$1
     architecture=$2
-    echo >> $MIRROR_LIST
-    echo "## $release on $architecture archives" >> $MIRROR_LIST
-    echo >> $MIRROR_LIST
-    echo "deb-$architecture $MIRROR_FROM_ARCHIVE $release main restricted universe multiverse" >> $MIRROR_LIST
-    echo "deb-$architecture $MIRROR_FROM_ARCHIVE $release-security main restricted universe multiverse" >> $MIRROR_LIST
-    echo "deb-$architecture $MIRROR_FROM_ARCHIVE $release-backports main restricted universe multiverse" >> $MIRROR_LIST
-    echo "deb-$architecture $MIRROR_FROM_ARCHIVE $release-updates main restricted universe multiverse" >> $MIRROR_LIST
-    echo >> $MIRROR_LIST
-    echo "deb-$architecture http://ppa.launchpad.net/hardware-certification/public/ubuntu $release main" >> $MIRROR_LIST
-    echo "deb-$architecture http://ppa.launchpad.net/checkbox-dev/ppa/ubuntu $release main" >> $MIRROR_LIST
-    echo "deb-$architecture http://ppa.launchpad.net/firmware-testing-team/ppa-fwts-stable/ubuntu $release main" >> $MIRROR_LIST
+    {
+        echo
+        echo "## $release on $architecture archives"
+        echo
+        echo "deb-$architecture $MIRROR_FROM_ARCHIVE $release main restricted universe multiverse"
+        echo "deb-$architecture $MIRROR_FROM_ARCHIVE $release-security main restricted universe multiverse"
+        echo "deb-$architecture $MIRROR_FROM_ARCHIVE $release-backports main restricted universe multiverse"
+        echo "deb-$architecture $MIRROR_FROM_ARCHIVE $release-updates main restricted universe multiverse"
+        echo
+        echo "deb-$architecture http://ppa.launchpad.net/hardware-certification/public/ubuntu $release main"
+        echo "deb-$architecture http://ppa.launchpad.net/checkbox-dev/ppa/ubuntu $release main"
+        echo "deb-$architecture http://ppa.launchpad.net/firmware-testing-team/ppa-fwts-stable/ubuntu $release main"
+    } >> $MIRROR_LIST
 }
 
 
 write_closing_mirror_config() {
-    echo >> "$MIRROR_LIST"
-    echo "## Clean up archives" >> "$MIRROR_LIST"
-    echo >> "$MIRROR_LIST"
-    echo "clean $MIRROR_FROM_ARCHIVE" >> "$MIRROR_LIST"
-    echo "clean http://ppa.launchpad.net/hardware-certification/public/ubuntu"; >> "$MIRROR_LIST"
-    echo "clean http://ppa.launchpad.net/checkbox-dev/ppa/ubuntu"; >> "$MIRROR_LIST"
-    echo "clean http://ppa.launchpad.net/firmware-testing-team/ppa-fwts-stable/ubuntu"; >> "$MIRROR_LIST"
+    {
+        echo
+        echo "## Clean up archives"
+        echo
+        echo "clean $MIRROR_FROM_ARCHIVE"
+        echo "clean http://ppa.launchpad.net/hardware-certification/public/ubuntu";
+        echo "clean http://ppa.launchpad.net/checkbox-dev/ppa/ubuntu";
+        echo "clean http://ppa.launchpad.net/firmware-testing-team/ppa-fwts-stable/ubuntu";
+    } >> "$MIRROR_LIST"
 }
 
 
@@ -492,7 +500,7 @@ mirror_archive() {
     echo "* time and disk space, though -- about 150 GiB per release mirrored."
     echo "* To defer this task, respond 'N' to the following question."
     echo "*"
-    if [[ -d /srv/mirror && /srv/skel && /srv/var ]]; then
+    if [[ -d /srv/mirror && -d /srv/skel && -d /srv/var ]]; then
         echo "* It appears you have an existing mirror in a deprecated location."
         echo "* Continuing here will move the old mirror directories into the  new"
         echo "* location, overwriting anything in the new location. Be careful"
@@ -563,12 +571,12 @@ mirror_archive() {
             if [[ "$release" == "$INSTALLED_RELEASE" && "$USE_LOCAL_MIRROR" == "Y" ]]; then
                 echo "* Mirroring $release automatically"
                 releases[i]="$release"
-                let i=i+1
+                (( i++ )) || true
             else
                 get_yn "* Do you want to mirror $release" "Y"
                 if [ $YN = "Y" ] ; then
                     releases[i]="$release"
-                    let i=i+1
+                    (( i++ )) || true
                 fi
             fi
         done
@@ -724,7 +732,7 @@ retrieve_virtualization_image() {
 setup_nat() {
     echo
     echo "***************************************************************************"
-    if [ "$(check_set_progress "$FUNCNAME")" == "completed" ] ; then
+    if [ "$(check_set_progress "${FUNCNAME[0]}")" == "completed" ] ; then
         echo "* NAT has already been configured."
         RERUN=yes
         return
@@ -749,8 +757,7 @@ setup_nat() {
         systemctl disable certification-nat
     fi
     set +e
-    iptables -L &> /dev/null
-    if [ "$?" != 0 ] ; then
+    if ! iptables -L &> /dev/null ; then
         echo "* WARNING: There is a problem with the iptables/NAT configuration!"
         echo "* This must be investigated and fixed before MAAS will be able to"
         echo "* commission or deploy nodes!"
@@ -762,21 +769,28 @@ setup_nat() {
 
 
 setup_ip_ranges() {
+    local third_octet
+    local third_octet_plus1
+    local third_octet_plus2
+    local third_octet_plus3
+    local internal16
+    local internal24
+    local cidr
     echo
     echo "***************************************************************************"
-    if [ "$(check_set_progress "$FUNCNAME")" == "completed" ] ; then
+    if [ "$(check_set_progress "${FUNCNAME[0]}")" == "completed" ] ; then
         echo "* MAAS network ranges have already been configured."
         RERUN=yes
         return
     fi
 
-    local internal24=$(echo "$INTERNAL_IP" | cut -d "." -f 1-3)
-    local internal16=$(echo "$INTERNAL_IP" | cut -d "." -f 1-2)
-    local third_octet=$(echo "$INTERNAL_IP" | cut -d "." -f 3)
-    let local third_octet_plus1="$third_octet"+1
-    let local third_octet_plus2="$third_octet"+2
-    let local third_octet_plus3="$third_octet"+3
-    local cidr=$(ipcalc -n "$INTERNAL_IP"/"$INTERNAL_NETMASK" | grep Netmask | tr -s " " | cut -d " " -f4)
+    internal24=$(echo "$INTERNAL_IP" | cut -d "." -f 1-3)
+    internal16=$(echo "$INTERNAL_IP" | cut -d "." -f 1-2)
+    third_octet=$(echo "$INTERNAL_IP" | cut -d "." -f 3)
+    ((third_octet_plus1="$third_octet"+1))
+    ((third_octet_plus2="$third_octet"+2))
+    ((third_octet_plus3="$third_octet"+3))
+    cidr=$(ipcalc -n "$INTERNAL_IP"/"$INTERNAL_NETMASK" | grep Netmask | tr -s " " | cut -d " " -f4) || true
     if [ -z "$cidr" ] ; then
         local is_valid=false
         local numbers='^[0-9]+$'
@@ -845,7 +859,8 @@ setup_ip_ranges() {
     echo "*    Low auto-assign IP address (implicit) = $AUTO_ASSIGN_LOW"
     echo "*    High auto-assign IP address (implicit) = $AUTO_ASSIGN_HIGH"
 
-    local RANGES=$(maas admin ipranges read | grep end_ip)
+    local RANGES
+    RANGES=$(maas admin ipranges read | grep end_ip) || true
     if [ -z "$RANGES" ] ; then
         echo "* Initializing rack controller"
         maas admin ipranges create type=dynamic start_ip="$DHCP_RANGE_LOW" end_ip="$DHCP_RANGE_HIGH" > /dev/null
@@ -853,7 +868,8 @@ setup_ip_ranges() {
         INTERNAL_FABRIC=$(maas admin ipranges read | jshon -a -e subnet -e vlan -e fabric | tr -d '"' | head -n 1)
         PRIMARY_RACK=$(maas admin rack-controllers read | jshon -a -e hostname | tr -d '"')
         maas admin vlan update "$INTERNAL_FABRIC" untagged dhcp_on=True primary_rack="$PRIMARY_RACK" > /dev/null
-        local SUBNET_ID=$(maas admin ipranges read | jshon -a -e subnet -e id | head -n 1)
+        local SUBNET_ID
+        SUBNET_ID=$(maas admin ipranges read | jshon -a -e subnet -e id | head -n 1)
         maas admin subnet update "$SUBNET_ID" gateway_ip="$INTERNAL_IP"
     else
         echo "* Rack controller DHCP configuration already exists; leaving it alone!"
@@ -866,7 +882,6 @@ setup_ip_ranges() {
 # Add escapes ("\") before ".", "[", and "]" characters in the passed string.
 # Return modified string as modified_string
 add_escapes() {
-#     local original_string="$1"
     modified_string=${1//\./\\\.}
     modified_string=${modified_string//\[/\\\[}
     modified_string=${modified_string//\]/\\\]}
@@ -875,32 +890,19 @@ add_escapes() {
 setup_dns() {
     echo
     echo "***************************************************************************"
-    if [ "$(check_set_progress "$FUNCNAME")" == "completed" ] ; then
+    if [ "$(check_set_progress "${FUNCNAME[0]}")" == "completed" ] ; then
         echo "* Upstread DNS has already been configured."
         RERUN=yes
         return
     fi
 
     # Set MAAS upstream DNS
-    local dns=$(cat /etc/resolv.conf | grep -v "$INTERNAL_IP" | grep -v "#" | grep -i nameserver | head -n1 | cut -d ' ' -f2)
+    local dns
+    dns=$(grep -v "$INTERNAL_IP" /etc/resolv.conf | grep -v "#" | grep -i nameserver | head -n1 | cut -d ' ' -f2) || true
     # Below signals that NetworkManager is in charge of DNS, so extract real
     # upstream DNS using nmcli....
     if [ "$dns" = "127.0.1.1" ] || [ "$dns" = "127.0.0.53" ] ; then
-        if dpkg --compare-versions "$(nmcli -v | grep -oE '[^ ]+$')" "lt" "0.9.10" ; then
-            dns=$(nmcli dev list iface $EXTERNAL_NET | grep -m 1 domain_name_servers | tr -s " " | grep -oE '[^ ]+$') || true
-        else
-            dns=$(nmcli dev show $EXTERNAL_NET | grep -m 1 DNS | tr -s " " | grep -oE '[^ ]+$') || true
-        fi
-        # Ubuntu 18.04's NetPlan can look like NetworkManager, but NetPlan
-        # doesn't use nmcli, so we must instead use systemd-resolve....
-        if [ -z "$dns" ] ; then
-            dns=$(systemd-resolve --status $EXTERNAL_NET | grep "DNS Servers" | cut -d ":" -f 2 | xargs)
-        fi
-        echo "dns is $dns"
-    fi
-    # None of the above works in Ubuntu 22.04, so try yet another approach....
-    if [ -z "$dns" ] ; then
-        dns=$(resolvectl status | grep "Current DNS Server" | head -n 1 | cut -d ":" -f 2 | sed 's/ //g')
+        dns=$(resolvectl status | grep "Current DNS Server" | head -n 1 | cut -d ":" -f 2 | xargs)
     fi
     if [ -z "$dns" ] ; then
         dns="8.8.8.8"
@@ -915,11 +917,11 @@ setup_dns() {
     elif [ -f 01-netcfg.yaml ] ; then # New style (NetPlan); Ubuntu 18.04
         nameservers=$(grep -A 1 nameservers /etc/netplan/01-netcfg.yaml | tail -n 1 | cut -d ":" -f 2)
         if [ -z "$nameservers" ] ; then # No name servers defined
-            if [ "$(grep addresses /etc/netplan/01-netcfg.yaml)" ] ; then
+            if grep -q addresses /etc/netplan/01-netcfg.yaml ; then
                 echo "      nameservers:" >> /etc/netplan/01-netcfg.yaml
                 echo "        addresses: [$INTERNAL_IP]" >> /etc/netplan/01-netcfg.yaml
             fi
-        elif [ ! "$(echo "$nameservers" | grep "$INTERNAL_IP")" ] ; then
+        elif ! echo "$nameservers" | grep -q "$INTERNAL_IP" ; then
             new_nameservers=${nameservers/\[/\[$INTERNAL_IP,}
             add_escapes "$new_nameservers"
             new_nameservers="$modified_string"
@@ -936,17 +938,11 @@ setup_dns() {
 
 
 setup_flat_storage() {
-    set +e
-    dpkg --compare-versions "1.9.0" "le" "$MAAS_VERSION"
-    if [ "$?" = 0 ] ; then
-        set -e
-        echo
-        echo "***************************************************************************"
-        echo "* Now configuring MAAS to use the 'flat' storage model by default...."
-        echo "*"
-        maas admin maas set-config name=default_storage_layout value=flat
-    fi
-    set -e
+    echo
+    echo "***************************************************************************"
+    echo "* Now configuring MAAS to use the 'flat' storage model by default...."
+    echo "*"
+    maas admin maas set-config name=default_storage_layout value=flat
 }
 
 
@@ -954,7 +950,7 @@ setup_node_archive_site() {
     echo
     echo "***************************************************************************"
     echo "* MAAS tells nodes to look to an Ubuntu repository on the Internet. You"
-    if [ "$(check_set_progress "$FUNCNAME")" == "completed" ] ; then
+    if [ "$(check_set_progress "${FUNCNAME[0]}")" == "completed" ] ; then
         echo "* MAAS has already been configured with a repository."
         RERUN=yes
         return

Follow ups