Changing IP address post fog install is problematic


  • Moderator

    Once fog is installed it is difficult to change the IP address of the fog server.

    I updated the /opt/fog/.fogsettings with the new IP address and then reran the installer script.

    The first issue I ran into was the background wouldn’t load for the iPXE menu. This was addressed by updating the fog setting FOG_WEB_HOST to the new IP address.

    The second issue I ran into was not being able to connect to the NFS share. This was corrected by updating the default storage node, default member, IP address.

    Also I see FOG_TFTP_HOST setting still points to the old IP address

    I would be great if the installer script would check these values to see if they were different then update them to reflect the new setting in the .fogsettings file.


  • Moderator

    @george1421 said:

    @Tom-Elliott The probably the easiest solution is to create a procedure (wiki page) that if you need to update your fog server you need to do:

    1. Update the settings in the /opt/fog/.fogsettings
    2. Rerun the installer
    3. Update the IP address for the storage node on the FOG system where you changed the IP address
    4. Update the IP address on a any master storage node that may reference this FOG server
    5. Update the FOG_WEB_HOST value
    6. update the FOG_TFTP_HOST value

    I can I’ll marked this as addressed (Solved), since its not a technical solution but a procedural one that resolves the issue.
    [Edit] I would, but it appears I can no longer mark topics as solved[/Edit]

    Added here: https://wiki.fogproject.org/wiki/index.php?title=Change_FOG_Server_IP_Address


  • Moderator

    @george1421 Thanks. I hope it’ll get the job done.

    Here’s the latest copy: https://sourceforge.net/p/fogupdateip/code/HEAD/tree/

    And this is how you’d check out using svn:

    svn checkout svn://svn.code.sf.net/p/fogupdateip/code/ /svn/fogupdateip
    

    I’ve fixed an issue there that had to do with the PATH variable - and I’ve made an installer and attached the GPL 3.0 license.

    Of course it can be improved further by being made more efficient and better organized and with wider range of OS support, but right now it’s 100% complete for Fedora 23 and CentOS 7.


  • Moderator

    @Wayne-Workman Very nice job. I’ll be sure to test this out on both Centos 7 and 6.5 after the holiday. But overall this looks great. It checks all of the boxes on the wish list.


  • Moderator

    This one provides messages, and outputs them to a log file. It also creates it’s own settings file instead of storing settings inside of .fogsettings.

    The web directory work is also dynamically based on the settings in .fogsettings

    bldnsmasq now defaults to 1 if it’s not previously set - this only makes sense to build the correct file if it doesn’t exist, that way if the user wants to start up dnsmasq, they can just simply start it.

    I’ve also fixed some minor errors. I’ve tested this only on CentOS 7 - seems to be working good.

    Given that dnsmasq has been added to the /opt/fog/.fogsettings “productlist”, and is installed, and this script is named: /opt/fog/utils/updateIP.sh and made executable, and a root crontab event is made such as */5 * * * * /opt/fog/utils/updateIP.sh or */3 * * * * /opt/fog/utils/updateIP.sh (every 5 minutes, or every 3 minutes)

    Then this will keep you’re fog server and it’s “defaultmember” storage node’s IP addresses totally up to date.
    And - if you’re using the dodnsmasq and bldnsmasq settings, it’ll keep those updated with the right IP as well. With those set and this script running via Cron - you have a 100% Mobile FOG Server that auto-configures itself every time it’s IP changes.

    This still needs further polishing, like a installer and uninstaller, a readme and a GNU GPL license attached. I’ll also make it into a sourceforge project too.

    #---- Notes ----#
    
    # This script is for changing the FOG Server's IP address and configure dnsmasq automatically.
    # It's intended to run as a root  cron event every 5 minutes, should work fine on Red Hat and RH variants. 
    # Thanks to forums.fogproject.org @sudburr for doing a ton of initial work.
    # Thanks to forums.fogproject.org @george1421 for continually providing feedback.
    # Updated by forums.fogproject.org @Wayne-Workman
    # Version 6.
    
    #Fog settings location.
    fogsettings=/opt/fog/.fogsettings
    
    #Custom settings location.
    customfogsettings=/opt/fog/.customfogsettings
    
    #Log for this script, to store messages and such in.
    log=/opt/fog/log/updateIP.log
    
    
    #---- Set Command Paths ----#
    
    #Store previous contents of IFS.
    previousIFS=$IFS
    
    IFS=:
      for p in $PATH; do
    
            #Get grep path
            if [[ -f $p/grep ]]; then
                    grep=$p/grep
            fi
    
            #Get awk path
            if [[ -f $p/awk ]]; then
                    awk=$p/awk
            fi
    
            #Get cut path
            if [[ -f $p/cut ]]; then
                    cut=$p/cut
            fi
    
            #Get ip path
            if [[ -f $p/ip ]]; then
                    ip=$p/ip
            fi
    
            #Get sed path
            if [[ -f $p/sed ]]; then
                    sed=$p/sed
            fi
    
            #Get mysql path
            if [[ -f $p/mysql ]]; then
                    mysql=$p/mysql
            fi
    
            #Get cp path
            if [[ -f $p/cp ]]; then
                    cp=$p/cp
            fi
    
            #Get echo path
            if [[ -f $p/echo ]]; then
                    echo=$p/echo
            fi
    
            #Get mv path
            if [[ -f $p/mv ]]; then
                    mv=$p/mv
            fi
    
            #Get rm path
            if [[ -f $p/rm ]]; then
                    rm=$p/rm
            fi
    
            #Get systemctl path
            if [[ -f $p/systemctl ]]; then
                    systemctl=$p/systemctl
            fi
    
            #Get date path
            if [[ -f $p/date ]]; then
                    date=$p/date
            fi
    
      done
    
    #Restore previous contents of IFS
    IFS=$previousIFS
    
    
    #---- Check contents of command variables ----#
    
    if [[ -z $echo ]]; then
            echo The path for grep was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $grep ]]; then
            $echo The path for grep was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $awk ]]; then
            $echo The path for awk was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $cut ]]; then
            $echo The path for cut was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $ip ]]; then
            $echo The path for ip was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $sed ]]; then
            $echo The path for sed was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $mysql ]]; then
            $echo The path for mysql was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $cp ]]; then
            $echo The path for cp was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $mv ]]; then
            $echo The path for mv was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $rm ]]; then
            $echo The path for rm was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $systemctl ]]; then
            $echo The path for systemctl was not found, exiting. >> $log
            exit
    fi
    
    if [[ -z $date ]]; then
            $echo The path for date was not found, exiting. >> $log
            exit
    fi
    
    
    
    #Record the date.
    NOW=$($date +"%m-%d-%Y")
    $echo -------------------- >> $log
    $echo $NOW >> $log
    $echo -------------------- >> $log
    
    
    
    #check for .fogsettings existence, if it isn't there, exit the script.
    if ! [[ -f $fogsettings ]]; then
            $echo The file $fogsettings does not exist, exiting. >> $log
            exit
    fi
    
    
    
    #---- Get interface name and last IP from .fogsettings ---#
    
    interface="$($grep 'interface=' $fogsettings | $awk -F'"' '{$0=$2}1')"
    fogsettingsIP="$($grep 'ipaddress=' $fogsettings | $awk -F'"' '{$0=$2}1')"
    
    #Check if the interface setting is good.
    if [[ -z $interface ]]; then
            $echo The interface setting inside $fogsettings either doesn't exist or isn't as expected, exiting. >> $log
            exit
    fi
    
    #Check if the ipaddress setting is good.
    if [[ -z $fogsettingsIP ]]; then
            $echo The ipaddress setting inside $fogsettings either doesn't exist or isn't as expected, exiting. >> $log
            exit
    fi
    
    
    
    #---- Wait for an IP address ----#
    
    IP=`$ip addr list ${interface} | $grep "inet " |$cut -d" " -f6|$cut -d/ -f1`
    
    while [[ -z $IP ]]
    
    do
            $echo The IP address for $interface was not found, waiting 5 seconds. >> $log
            sleep 5
            IP=`$ip addr list ${interface} | $grep "inet " |$cut -d" " -f6|$cut -d/ -f1`
    done
    
    
    
    
    if [[ "$IP" != "$fogsettingsIP" ]]; then
    #If the interface IP doesn't match the .fogsettings IP, do the below.
    
    
            #-------------- Update the IP settings --------------#
    
            $echo The IP address for $interface does not match the ipaddress setting in $fogsettings, updating the IP Settings server-wide. >> $log
    
            #---- SQL ----#
    
            snmysqluser="$($grep 'snmysqluser=' $fogsettings | $awk -F'"' '{$0=$2}1')"
            snmysqlpass="$($grep 'snmysqlpass=' $fogsettings | $cut -d \' -f2 )"
    
            #These are the SQL statements to run against the DB
            statement1="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_TFTP_HOST';"
            statement2="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_WOL_HOST';"
            statement3="UPDATE \`nfsGroupMembers\` SET \`ngmHostname\` = '$IP' WHERE \`ngmMemberName\` ='DefaultMember';"
            statement4="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_WEB_HOST';"
    
    
            #This puts all the statements into one variable. If you add more statments above, add the extra ones to this too.
            sqlStatements=$statement1$statement2$statement3$statement4
    
    
            #This builds the proper MySQL Connection Statement and runs it.
            if [ "$snmysqlpass" != "" ]; then
                    #If there is a password set
                    $echo A password was set for snmysqlpass in $fogsettings, using the password. >> $log
                    $mysql --user=$snmysqluser --password=$snmysqlpass --database='fog' -e "$sqlStatements"
    
            elif [ "$snmysqluser" != "" ]; then
                    #If there is a user set but no password
                    $echo A username was set for snmysqluser in $fogsettings, but no password was found. Using the username. >> $log
                    $mysql --user $snmysqluser --database='fog' -e "$sqlStatements"
    
            else
                    $echo There was no username or password set for the database in $fogsettings, trying without credentials. >> $log
                    #If there is no user or password set
                    $mysql --database='fog' -e "$sqlStatements"
            fi
    
    
    
            #---- Update IP address in file default.ipxe ----#
    
            $echo Updating the IP in /tftpboot/default.ipxe >> $log
            $sed -i "s|http://\([^/]\+\)/|http://$IP/|" /tftpboot/default.ipxe
            $sed -i "s|http:///|http://$IP/|" /tftpboot/default.ipxe
    
    
            #---- Backup config.class.php and then updae IP ----#
    
            #read the docroot and webroot settings.
            docroot="$($grep 'docroot=' $fogsettings | $awk -F'"' '{$0=$2}1')"
            webroot="$($grep 'webroot=' $fogsettings | $awk -F'"' '{$0=$2}1')"
    
    
            #check if docroot is blank.
            if [[ -z $docroot ]]; then
                    $echo There is no docroot set inside $fogsettings exiting the script. >> $log
                    exit
            fi
    
            #check if webroot is blank.
            if [[ -z $webroot ]]; then
                    $echo There is no webroot set inside $fogsettings exiting the script. >> $log
                    exit
            fi
    
    
    
            $echo Backing up ${docroot}$webroot'lib/fog/config.class.php' >> $log
            $cp -f ${docroot}$webroot'lib/fog/config.class.php' ${docroot}$webroot'lib/fog/config.class.php.old'
    
            $echo Updating the IP inside ${docroot}$webroot'lib/fog/config.class.php' >> $log
            $sed -i "s|\".*\..*\..*\..*\"|\$_SERVER['SERVER_ADDR']|" ${docroot}$webroot'lib/fog/config.class.php'
    
            #---- Update .fogsettings IP ----#
    
            $echo Updating the ipaddress field inside of $fogsettings >> $log
            $sed -i "s|ipaddress=\".*\"|ipaddress=\"$IP\"|" $fogsettings
    
    
            #check if customfogsettings exists, if not, create it.
            if [[ ! -f $customfogsettings ]]; then
                    $echo $customfogsettings was not found, creating it. >> $log
                    touch $customfogsettings
            fi
    
    
            #Check if the dodnsmasq setting exists in $customfogsettings If not, create it and set it to false.
            if ! $grep -q dodnsmasq "$customfogsettings"; then
                    $echo The dodnsmasq setting was not found in $customfogsettings, adding it. >> $log
                    #Remove any blank lines at the end of customfogsettings, then rewrite file.
                    $sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $customfogsettings > $customfogsettings.new
                    $mv $customfogsettings.new $customfogsettings
                    #Add dodnsmasq setting.
                    $echo dodnsmasq="\""0"\"" >> $customfogsettings
                    #Add a blank line at the end of customfogsettings.
                    $echo "" >> $customfogsettings
            fi
    
    
            #Check if the bldnsmasq setting exists in $customfogsettings. If not, create it and set it to true.
            if ! grep -q bldnsmasq "$customfogsettings"; then
                    $echo The bldnsmasq setting was not found in $customfogsettings, adding it. >> $log
                    #Remove any blank lines at the end of customfogsettings, then rewrite file.
                    $sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $customfogsettings > $customfogsettings.new
                    $mv $customfogsettings.new $customfogsettings
                    #Add bldnsmasq setting.
                    $echo bldnsmasq="\""1"\"" >> $customfogsettings
                    #Add a blank line at the end of customfogsettings.
                    $echo "" >> $customfogsettings
            fi
    
    
            #Read the dodnsmasq and bldnsmasq settings.
            dodnsmasq="$($grep 'dodnsmasq=' $customfogsettings | $awk -F'"' '{$0=$2}1')"
            bldnsmasq="$($grep 'bldnsmasq=' $customfogsettings | $awk -F'"' '{$0=$2}1')"
    
            #If either of the dnsmasq fogsettings are empty, exit the script.
            if [[ -z dodnsmasq ]]; then
                    $echo The dodnsmasq setting in $customfogsettings either doesn't exist or isn't as expected, exiting the script. >> $log
                    exit
            fi
    
            if [[ -z bldnsmasq ]]; then
                    $echo The bldnsmasq setting in $customfogsettings either doesn't exist or isn't as expected, exiting the script. >> $log
                    exit
            fi
    
            #If bldnsmasq is seto as 1, build the config file.
            if [[ "$bldnsmasq" == "1" ]]; then
    
                    #set the ltsp.conf path.
                    ltsp=/etc/dnsmasq.d/ltsp.conf
    
                    $echo bldnsmasq inside $customfogsettings was set to 1, recreating $ltsp >> $log
    
                    #Read what boot file is set in fogsettings, use that in ltsp.conf
                    bootfilename="$($grep 'bootfilename=' $fogsettings | $awk -F'"' '{$0=$2}1')"
    
                    #If bootfilename is blank, set it to undionly.kkpxe
                    if [[ -z "$bootfilename" ]]; then
                            $echo The bootfilename setting inside of $fogsettings is either doesn't exist or isn't as expected, defaulting to undionly.kkpxe >> $log
                            bootfilename=undionly.kkpxe
                    fi
    
                    #Check for existence of the bootfile copy ".0" file. If it exists, delete it and recreate it.
                    bootfileCopy="${bootfilename%.*}.0"
                    if [[ -f $bootfileCopy ]]; then
                            $echo $bootfileCopy was found, deleting it. >> $log
                            $rm -f $bootfileCopy
                    fi
                    $echo Copying /tftpboot/$bootfilename to /tftpboot/$bootfileCopy for dnsmasq to use. >> $log
                    $cp /tftpboot/$bootfilename /tftpboot/$bootfileCopy
    
    
                    #this config overwrites anything in ltsp.conf because "bldnsmasq" was set to 1.
    
                    $echo Recreating $ltsp for use with dnsmasq. >> $log
                    $echo port=0 > $ltsp
                    $echo log-dhcp >> $ltsp
                    $echo tftp-root=/tftpboot >> $ltsp
                    $echo dhcp-boot=$bootfileCopy,$IP,$IP >> $ltsp
                    $echo dhcp-option=17,/images >> $ltsp
                    $echo dhcp-option=vendor:PXEClient,6,2b >> $ltsp
                    $echo dhcp-no-override >> $ltsp
                    $echo pxe-prompt="Press F8 for boot menu", 1 >> $ltsp
                    $echo pxe-service=X86PC, “Boot from network”, undionly >> $ltsp
                    $echo pxe-service=X86PC, "Boot from local hard disk", 0 >> $ltsp
                    $echo dhcp-range=$IP,proxy >> $ltsp
    
            fi
    
    
    
            #if dodnsmasq is set to 1, restart and enable dnsmasq. ELSE disable and stop.
            if [[ "$dodnsmasq" == "1" ]]; then
                    $echo dodnsmasq was set to 1 inside of $customfogsettings - starting it and enabling it to run at boot. >> $log
                    $echo You may manually set this to 0 if you like, and manually stop and disable dnsmasq with these commands: >> $log
                    $echo systemctl disable dnsmasq >> $log
                    $echo systemctl stop dnsmasq >> $log
                    $systemctl enable dnsmasq
                    $systemctl restart dnsmasq
            else
                    $echo dodnsmasq was set to 0 inside of $customfogsettings - stopping it and disabling it from running at boot. >> $log
                    $echo You may manually set this to 1 if you like, and manually start and enable dnsmasq with these commands: >> $log
                    $echo systemctl enable dnsmasq >> $log
                    $echo systemctl restart dnsmasq >> $log
                    $systemctl disable dnsmasq
                    $systemctl stop dnsmasq
    
            fi
    
    else
            $echo The IP address found on $interface matches the IP set in $fogsettings, assuming all is good, exiting. >> $log
            exit
    fi
    

  • Moderator

    Version 5. This one will build the dnsmasq config file.

    #---- Notes ----#
    
    # This script is for changing the FOG Server's IP address and configure dnsmasq automatically.
    # It's intended to run as a cron event every 5 minutes, should work fine on Red Hat and RH variants. 
    # Thanks to forums.fogproject.org @sudburr for doing a ton of initial work.
    # Updated by forums.fogproject.org @Wayne-Workman
    # Version 5.
    # If you want to use dnsmasq, you must first set this up as a cron script to run every so oftne (every 5 minutes)
    # And add dnsmasq to the packages list inside /opt/fog/.fogsettings
    # and when "dodnsmasq" and "bldnsmasq" show up (after script's first run), set those to 1 or whatever you prefer.
    #
    # the final "undionly" line still needs fixed to be created from the bootfile variable in the ltsp.conf file.
    
    fogsettings=/opt/fog/.fogsettings
    
    
    #check for .fogsettings existence, if it isn't there, exit the script.
    if ! [[ -f $fogsettings ]]; then
            exit
    fi
    
    #---- Set Command Paths ----#
    
    #Store previous contents of IFS.
    previousIFS=$IFS
    
    IFS=:
      for p in $PATH; do
    
            #Get grep path
            if [[ -f $p/grep ]]; then
                    grep=$p/grep
            fi
    
            #Get awk path
            if [[ -f $p/awk ]]; then
                    awk=$p/awk
            fi
    
            #Get cut path
            if [[ -f $p/cut ]]; then
                    cut=$p/cut
            fi
    
            #Get ip path
            if [[ -f $p/ip ]]; then
                    ip=$p/ip
            fi
    
            #Get sed path
            if [[ -f $p/sed ]]; then
                    sed=$p/sed
            fi
    
            #Get mysql path
            if [[ -f $p/mysql ]]; then
                    mysql=$p/mysql
            fi
    
            #Get cp path
            if [[ -f $p/cp ]]; then
                    cp=$p/cp
            fi
    
            #Get echo path
            if [[ -f $p/echo ]]; then
                    echo=$p/echo
            fi
    
            #Get mv path
            if [[ -f $p/mv ]]; then
                    mv=$p/mv
            fi
    
            #Get rm path
            if [[ -f $p/rm ]]; then
                    rm=$p/rm
            fi
    
            #Get systemctl path
            if [[ -f $p/systemctl ]]; then
                    systemctl=$p/systemctl
            fi
    
      done
    
    #Restore previous contents of IFS
    IFS=$previousIFS
    
    #If any command variables don't have content, exit the script.
    if ! [[ -z $grep ]] && [[ -z $awk ]] && [[ -z $cut ]] && [[ -z $ip ]] && [[ -z $sed ]] && [[ -z $mysql ]] && [[ -z $cp ]] && [[ -z $echo ]] && [[ -z $mv ]] && [[ -z $rm ]] && [[ -z $systemctl ]]; then
            exit
    fi
    
    #---- Get interface name and last IP from .fogsettings ---#
    
    interface="$($grep 'interface=' $fogsettings | $awk -F'"' '{$0=$2}1')"
    fogsettingsIP="$($grep 'ipaddress=' $fogsettings | $awk -F'"' '{$0=$2}1')"
    
    #If there isn't an interface and IP set in .fogsettings, exit the script.
    if ! [[ -z $interface ]] && [[ -z $fogsettingsIP ]]; then
            exit
    fi
    
    
    #---- Wait for an IP address ----#
    
    IP=`$ip addr list ${interface} | $grep "inet " |$cut -d" " -f6|$cut -d/ -f1`
    
    while [ -z $IP ]
    
    do
        sleep 5
        IP=`$ip addr list ${interface} | $grep "inet " |$cut -d" " -f6|$cut -d/ -f1`
    done
    
    
    
    if [[ "$IP" != "$fogsettingsIP" ]]; then
    #If the interface IP doesn't match the .fogsettings IP, do the below.
    
            #---- SQL ----#
    
            snmysqluser="$($grep 'snmysqluser=' $fogsettings | $awk -F'"' '{$0=$2}1')"
            snmysqlpass="$($grep 'snmysqlpass=' $fogsettings | $cut -d \' -f2 )"
    
            #These are the SQL statements to run against the DB
            statement1="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_TFTP_HOST';"
            statement2="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_WOL_HOST';"
            statement3="UPDATE \`nfsGroupMembers\` SET \`ngmHostname\` = '$IP' WHERE \`ngmMemberName\` ='DefaultMember';"
            statement4="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_WEB_HOST';"
    
    
            #This puts all the statements into one variable. If you add more statments above, add the extra ones to this too.
            sqlStatements=$statement1$statement2$statement3$statement4
    
    
            #This builds the proper MySQL Connection Statement.
            if [ "$snmysqlpass" != "" ]; then
                    #If there is a password set
                    $mysql --user=$snmysqluser --password=$snmysqlpass --database='fog' -e "$sqlStatements"
    
            elif [ "$snmysqluser" != "" ]; then
                    #If there is a user set but no password
                    $mysql --user $snmysqluser --database='fog' -e "$sqlStatements"
    
            else
                    #If there is no user or password set
                    $mysql --database='fog' -e "$sqlStatements"
            fi
    
            #---- Update IP address in file default.ipxe ----#
    
            $sed -i "s|http://\([^/]\+\)/|http://$IP/|" /tftpboot/default.ipxe
            $sed -i "s|http:///|http://$IP/|" /tftpboot/default.ipxe
    
    
            #---- Backup config.class.php and then updae IP ----#
    
            $cp -f /var/www/html/fog/lib/fog/config.class.php /var/www/html/fog/lib/fog/config.class.php.old
            $sed -i "s|\".*\..*\..*\..*\"|\$_SERVER['SERVER_ADDR']|" /var/www/html/fog/lib/fog/config.class.php
    
            #---- Update .fogsettings IP ----#
    
            $sed -i "s|ipaddress=\".*\"|ipaddress=\"$IP\"|" $fogsettings
    
    
            #Check if the dodnsmasq setting exists in fog settings. If not, create it and set it to false.
            if ! $grep -q dodnsmasq "$fogsettings"; then
                    #Remove any blank lines at the end of fogsettings, then rewrite file.
                    $sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $fogsettings > $fogsettings.new
                    $mv $fogsettings.new $fogsettings
                    #Add dodnsmasq setting.
                    $echo dodnsmasq="\""0"\"" >> $fogsettings
                    #Add a blank line at the end of fogsettings.
                    $echo "" >> $fogsettings
            fi
    
    
            #Check if the bldnsmasq setting exists in fog settings. If no, create it and set it to false.
            if ! grep -q bldnsmasq "$fogsettings"; then
                    #Remove any blank lines at the end of fogsettings, then rewrite file.
                    $sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $fogsettings > $fogsettings.new
                    $mv $fogsettings.new $fogsettings
                    #Add bldnsmasq setting.
                    $echo bldnsmasq="\""0"\"" >> $fogsettings
                    #Add a blank line at the end of fogsettings.
                    $echo "" >> $fogsettings
            fi
    
    
            #Read the dodnsmasq and bldnsmasq settings.
            dodnsmasq="$($grep 'dodnsmasq=' $fogsettings | $awk -F'"' '{$0=$2}1')"
            bldnsmasq="$($grep 'bldnsmasq=' $fogsettings | $awk -F'"' '{$0=$2}1')"
    
            #If either of the dnsmasq fogsettings are empty, exit the script.
            if ! [[ -z dodnsmasq ]] && [[ -z bldnsmasq ]]; then
                    exit
            fi
    
            #If bldnsmasq is seto as 1, build the config file.
            if [[ "$bldnsmasq" == "1" ]]; then
    
                    #set the ltsp.conf path.
                    ltsp=/etc/dnsmasq.d/ltsp.conf
    
                    #Read what boot file is set in fogsettings, use that in ltsp.conf
                    bootfilename="$($grep 'bootfilename=' $fogsettings | $awk -F'"' '{$0=$2}1')"
    
                    #If bootfilename is blank, set it to undionly.kkpxe
                    if [[ -z "$bootfilename" ]]; then
                            bootfilename=undionly.kkpxe
                    fi
    
                    #Check for existence of the bootfile copy ".0" file. If it exists, delete it and recreate it.
                    bootfileCopy="${bootfilename%.*}.0"
                    if [[ -f $bootfileCopy ]]; then
                            $rm -f $bootfileCopy
                    fi
                    $cp /tftpboot/$bootfilename /tftpboot/$bootfileCopy
    
    
                    #this config overwrites anything in ltsp.conf because "bldnsmasq" was set to 1.
                    $echo port=0 > $ltsp
                    $echo log-dhcp >> $ltsp
                    $echo tftp-root=/tftpboot >> $ltsp
                    $echo dhcp-boot=$bootfileCopy,$IP,$IP >> $ltsp
                    $echo dhcp-option=17,/images >> $ltsp
                    $echo dhcp-option=vendor:PXEClient,6,2b >> $ltsp
                    $echo dhcp-no-override >> $ltsp
                    $echo pxe-prompt="Press F8 for boot menu", 3 >> $ltsp
                    $echo pxe-service=X86PC, “Boot from network”, undionly >> $ltsp
                    $echo pxe-service=X86PC, "Boot from local hard disk", 0 >> $ltsp
                    $echo dhcp-range=$IP,proxy >> $ltsp
    
            fi
    
    
    
            #if dodnsmasq is set to 1, restart and enable dnsmasq. ELSE disable and stop.
            if [[ "$dodnsmasq" == "1" ]]; then
                    $systemctl enable dnsmasq
                    $systemctl restart dnsmasq
            else
                    $systemctl disable dnsmasq
                    $systemctl stop dnsmasq
            fi
    
    
    fi
    

  • Moderator

    This changes all fog settings and the “defaultmember” storage node to whatever the current IP of the fog server is. I’ve tested it, it works.

    I’ve added extensive fail-safes as @george1421 was talking about - if anything isn’t right, it just quits.

    I’ll work on adding dnsmasq now and the earlier check to run the cron event or not from @mrayzies

    #---- Notes ----#
    
    # This script is for changing the FOG Server's IP address and configure dnsmasq automatically.
    # It's intended to run as a cron event every 5 minutes, should work fine on Red Hat and RH variants. 
    # Thanks to forums.fogproject.org @sudburr for doing a ton of initial work.
    # Updated by forums.fogproject.org @Wayne-Workman
    # Version 4.
    
    
    #check for .fogsettings existence, if it isn't there, exit the script.
    if ! [[ -f /opt/fog/.fogsettings ]]; then
            exit
    fi
    
    #---- Set Command Paths ----#
    
    #Store previous contents of IFS.
    previousIFS=$IFS
    
    IFS=:
      for p in $PATH; do
    
            #Get grep path
            if [[ -f $p/grep ]]; then
                    grep=$p/grep
            fi
    
            #Get awk path
            if [[ -f $p/awk ]]; then
                    awk=$p/awk
            fi
    
            #Get cut path
            if [[ -f $p/cut ]]; then
                    cut=$p/cut
            fi
    
            #Get ip path
            if [[ -f $p/ip ]]; then
                    ip=$p/ip
            fi
    
            #Get sed path
            if [[ -f $p/sed ]]; then
                    sed=$p/sed
            fi
    
            #Get mysql path
            if [[ -f $p/mysql ]]; then
                    mysql=$p/mysql
            fi
    
            #Get cp path
            if [[ -f $p/cp ]]; then
                    cp=$p/cp
            fi
    
      done
    
    #Restore previous contents of IFS
    IFS=$previousIFS
    
    #If any command variables don't have content, exit the script.
    if ! [[ -z $grep ]] && [[ -z $awk ]] && [[ -z $cut ]] && [[ -z $ip ]] && [[ -z $sed ]] && [[ -z $mysql ]] && [[ -z $cp ]]; then
            exit
    fi
    
    #---- Get interface name and last IP from .fogsettings ---#
    
    interface="$($grep 'interface=' /opt/fog/.fogsettings | $awk -F'"' '{$0=$2}1')"
    fogsettingsIP="$($grep 'ipaddress=' /opt/fog/.fogsettings | $awk -F'"' '{$0=$2}1')"
    
    #If there isn't an interface and IP set in .fogsettings, exit the script.
    if ! [[ -z $interface ]] && [[ -z $fogsettingsIP ]]; then
            exit
    fi
    
    
    #---- Wait for an IP address ----#
    
    IP=`$ip addr list ${interface} | $grep "inet " |$cut -d" " -f6|$cut -d/ -f1`
    
    while [ -z $IP ]
    
    do
        sleep 5
        IP=`$ip addr list ${interface} | $grep "inet " |$cut -d" " -f6|$cut -d/ -f1`
    done
    
    
    
    if [[ "$IP" != "$fogsettingsIP" ]]; then
    #If the interface IP doesn't match the .fogsettings IP, do the below.
    
            #---- SQL ----#
    
            snmysqluser="$($grep 'snmysqluser=' /opt/fog/.fogsettings | $awk -F'"' '{$0=$2}1')"
            snmysqlpass="$($grep 'snmysqlpass=' /opt/fog/.fogsettings | $cut -d \' -f2 )"
    
            #These are the SQL statements to run against the DB
            statement1="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_TFTP_HOST';"
            statement2="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_WOL_HOST';"
            statement3="UPDATE \`nfsGroupMembers\` SET \`ngmHostname\` = '$IP' WHERE \`ngmMemberName\` ='DefaultMember';"
            statement4="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_WEB_HOST';"
    
    
            #This builds the proper MySQL Connection Statement.
            if [ "$snmysqlpass" != "" ]; then
                    #If there is a password set
                    $mysql --user=$snmysqluser --password=$snmysqlpass --database='fog' -e "$statement1$statement2$statement3$statement4"
    
            elif [ "$snmysqluser" != "" ]; then
                    #If there is a user set but no password
                    $mysql --user $snmysqluser --database='fog' -e "$statement1$statement2$statement3$statement4"
    
            else
                    #If there is no user or password set
                    $mysql --database='fog' -e "$statement1$statement2$statement3$statement4"
            fi
    
            #---- Update IP address in file default.ipxe ----#
    
            $sed -i "s|http://\([^/]\+\)/|http://$IP/|" /tftpboot/default.ipxe
            $sed -i "s|http:///|http://$IP/|" /tftpboot/default.ipxe
    
    
            #---- Backup config.class.php and then updae IP ----#
    
            $cp -f /var/www/html/fog/lib/fog/config.class.php /var/www/html/fog/lib/fog/config.class.php.old
            $sed -i "s|\".*\..*\..*\..*\"|\$_SERVER['SERVER_ADDR']|" /var/www/html/fog/lib/fog/config.class.php
    
            #---- Update .fogsettings IP ----#
    
            $sed -i "s|ipaddress=\".*\"|ipaddress=\"$IP\"|" /opt/fog/.fogsettings
    
    fi
    

  • Moderator

    @george1421 This script should knock your socks right off.

    It dynamically finds the paths for all the called programs by searching through the directories in $PATH. The result is CRON friendly commands that are easy to type. :-)

    Still 100% untested, I’ve not even run this script myself yet.

    #---- Notes ----#
    
    # This script is for changing the FOG Server's IP address and configure dnsmasq automatically.
    
    # Thanks to forums.fogproject.org @sudburr for doing a ton of initial work.
    
    # Updated by forums.fogproject.org @Wayne-Workman
    
    # This is an early copy, stay tuned for updates.
    
    # December 23th, 2015.
    
    
    
    
    #---- Set Command Paths ----#
    
    ( IFS=:
      for p in $PATH; do
    
            #Get grep path
            if [[ -f $p/grep ]]; then
                    grep=$p/grep
            fi
    
            #Get awk path
            if [[ -f $p/awk ]]; then
                    awk=$p/awk
            fi
    
            #Get cut path
            if [[ -f $p/cut ]]; then
                    cut=$p/cut
            fi
    
            #Get ip path
            if [[ -f $p/ip ]]; then
                    ip=$p/ip
            fi
    
            #Get sed path
            if [[ -f $p/sed ]]; then
                    sed=$p/sed
            fi
    
            #Get mysql path
            if [[ -f $p/mysql ]]; then
                    mysql=$p/mysql
            fi
    
            #Get cp path
            if [[ -f $p/cp ]]; then
                    cp=$p/cp
            fi
    
      done
    )
    
    
    #---- Get interface name and last IP from .fogsettings ---#
    
    interface="$($grep 'interface=' /opt/fog/.fogsettings | $awk -F'"' '{$0=$2}1')"
    fogsettingsIP="$($grep 'ipaddress=' /opt/fog/.fogsettings | $awk -F'"' '{$0=$2}1')"
    
    
    #---- Wait for an IP address ----#
    
    IP=`$ip addr list ${interface} | $grep "inet " |$cut -d" " -f6|$cut -d/ -f1`
    
    while [ -z $IP ]
    
    do
        sleep 5
        IP=`$ip addr list ${interface} | $grep "inet " |$cut -d" " -f6|$cut -d/ -f1`
    done
    
    
    
    
    if [[ "$IP" != "$fogsettingsIP" ]]; then
    #If the actual IP doesn't match the .fogsettings IP
    
            #Update .fogsettings IP
            $sed -i "s|ipaddress=\".*\"|ipaddress=\"$IP\"|" /opt/fog/.fogsettings
    
            #Get MySQL credentials
            snmysqluser="$($grep 'snmysqluser=' /opt/fog/.fogsettings | $awk -F'"' '{$0=$2}1')"
            snmysqlpass="$($grep 'snmysqlpass=' /opt/fog/.fogsettings | $cut -d \' -f2 )"
    
            #---- SQL ----#
    
            #These are the SQL statements to run against the DB
            statement1="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_TFTP_HOST';"
            statement2="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_WOL_HOST';"
            statement3="UPDATE \`nfsGroupMembers\` SET \`ngmHostname\` = '$IP' WHERE \`ngmMemberName\` ='DefaultMember';"
    
            if [ "$snmysqlpass" != "" ]; then
                    #If there is a password set
                    $mysql --user=$snmysqluser --password=$snmysqlpass --database='fog'  << EOF
                    $statement1
                    $statement2
                    $statement3
                    EOF
    
            elif [ "$snmysqluser != "" ]; then
                    #If there is a user set but no password
                    $mysql --user $snmysqluser --database='fog' << EOF
                    $statement1
                    $statement2
                    $statement3
                    EOF
    
            else
                    #If there is no user or password set
                    $mysql --database='fog' << EOF
                    $statement1
                    $statement2
                    $statement3
                    EOF
    
            fi
    
    
            #Updating IP address in file default.ipxe
            $sed -i "s|http://\([^/]\+\)/|http://$IP/|" /tftpboot/default.ipxe
            $sed -i "s|http:///|http://$IP/|" /tftpboot/default.ipxe
    
    
            #---- Backup config.class.php and then updae IP ----#
            $cp -f /var/www/html/fog/lib/fog/config.class.php /var/www/html/fog/lib/fog/config.class.php.old
            $sed -i "s|\".*\..*\..*\..*\"|\$_SERVER['SERVER_ADDR']|" /var/www/html/fog/lib/fog/config.class.php
    
    
    
    fi
    

  • Moderator

    @mrayzies By all means, give your thoughts and even contribute. :-) FOG is a community project.



  • I was talking about the CRON job.

    And OK – sorry to barge in on the conversation.


  • Moderator

    @mrayzies Are you talking about the cron job? We can add a simple setting into the .fogsettings file like keepIPupdated="1" / "0"

    What I’m working on is a total custom thing - it’ll likely never be implemented into FOG Trunk officially.



  • @Wayne-Workman
    I think there should be an option in .fogsettings to skip this altogether, not because of system resources, but occasionally I might want to poke at the system (e.g. add a temporary VIP for testing something else) and unless the coding is air-tight, it might kick off some horrible changes.


  • Moderator

    @george1421 My thought was just to setup a cron job that runs every 5 minutes.

    If the actual IP is the same as what’s in .fogsettings then nothing is done - and to find that out, it’s not much resources used.


  • Moderator

    @Wayne-Workman

    Some other things I just thought of while walking about.

    How does dhcp lease time come into play in regards to script execution. Especially if this will be a cron job. Consider what is a shortest duration lease and a typical duration lease time.

    should the physical location of this script be location in a fog related directory like /opt/fog/utils?

    Is there any way to trap an interface going up/down and then to call your script? hint: http://www.cyberciti.biz/tips/how-do-i-run-firewall-script-as-soon-as-eth0-interface-brings-up.html

    Does the interface go down and then up on a dhcp renewal.

    We would assume that the path statement points correctly for bash to run without full paths on the called applications especially if they are system type applications.


  • Moderator

    @george1421 All great points - The paths in there are for Fedora 23.

    I’ll see about using the $PATH variable instead of hard coded paths - and I’ll work on better variable handling.


  • Moderator

    @Wayne-Workman This is going to be a critical review of the code. Don’t take it personally (my guys hate me when I do this type of review, but the finished product is generally better).

    Some lines use the full path to the application and some only use the application name. (i.e. /usr/share/awk)

    Since you are using the full path to the application you assume that is the proper location. I just checked on my ubuntu based system awk is in /usr/bin/awk under Centos 7 its again /usr/bin/awk but in your script it references /usr/share/awk which I assume would cause your script to fail on these systems.

    What would happen if interface or fogsettingsIP returned nothing instead of a value what would happen to your script?

    The same question if the mysql credentials were not returned.

    In sudburr’s post also references a wol host field to be updated, is this important?

    What happens if config.class.php.old already exists when the script is run a second or third time. Will anything important be lost?

    Do any running fog services maintain a cached copy of the server’s IP address or do the fog services need to be stopped when you update these settings to make them stick?


  • Moderator

    100% NOT tested :-) :+1:

    #---- Notes ----#
    
    # This script is for changing the FOG Server's IP address and configure dnsmasq automatically.
    
    # Thanks to forums.fogproject.org @sudburr for doing a ton of initial work.
    
    # Updated by forums.fogproject.org @Wayne-Workman
    
    # This is an early copy, stay tuned for updates.
    
    # December 22th, 2015.
    
    
    #---- Get interface name and last IP from .fogsettings ---#
    
    interface="$(/usr/bin/grep 'interface=' /opt/fog/.fogsettings | /usr/share/awk -F'"' '{$0=$2}1')"
    fogsettingsIP="$(/usr/bin/grep 'ipaddress=' /opt/fog/.fogsettings | /usr/share/awk -F'"' '{$0=$2}1')"
    
    
    #---- Wait for an IP address ----#
    
    IP=`/sbin/ip addr list ${interface} | /usr/bin/grep "inet " |/usr/bin/cut -d" " -f6|/usr/bin/cut -d/ -f1`
    
    while [ -z $IP ]
    
    do
        sleep 5
        IP=`ip addr list ${interface} | grep "inet " |cut -d" " -f6|cut -d/ -f1`
    done
    
    
    
    
    if [[ "$IP" != "$fogsettingsIP" ]]; then
    #If the actual IP doesn't match the .fogsettings IP
    
            #Update .fogsettings IP
            sed -i "s|ipaddress=\".*\"|ipaddress=\"$IP\"|" /opt/fog/.fogsettings
    
            #Get MySQL credentials
            snmysqluser="$(/usr/bin/grep 'snmysqluser=' /opt/fog/.fogsettings | /usr/share/awk -F'"' '{$0=$2}1')"
            snmysqlpass="$(/usr/bin/grep 'snmysqlpass=' /opt/fog/.fogsettings | /usr/bin/cut -d \' -f2 )"
    
            #---- SQL ----#
    
            #These are the SQL statements to run against the DB
            statement1="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_TFTP_HOST';"
            statement2="UPDATE \`globalSettings\` SET \`settingValue\` = '$IP' WHERE \`settingKey\` ='FOG_WOL_HOST';"
            statement3="UPDATE \`nfsGroupMembers\` SET \`ngmHostname\` = '$IP' WHERE \`ngmMemberName\` ='DefaultMember';"
    
            if [ "$snmysqlpass" != "" ]; then
                    #If there is a password set
                    /usr/bin/mysql --user=$snmysqluser --password=$snmysqlpass --database='fog'  << EOF
                    $statement1
                    $statement2
                    $statement3
                    EOF
    
            elif [ "$snmysqluser != "" ]; then
                    #If there is a user set but no password
                    /usr/bin/mysql --user $snmysqluser --database='fog' << EOF
                    $statement1
                    $statement2
                    $statement3
                    EOF
    
            else
                    #If there is no user or password set
                    /usr/bin/mysql --database='fog' << EOF
                    $statement1
                    $statement2
                    $statement3
                    EOF
    
            fi
    
    
            #Updating IP address in file default.ipxe
            sed -i "s|http://\([^/]\+\)/|http://$IP/|" /tftpboot/default.ipxe
            sed -i "s|http:///|http://$IP/|" /tftpboot/default.ipxe
    
    
            #---- Backup config.class.php and then updae IP ----#
            cp -f /var/www/html/fog/lib/fog/config.class.php /var/www/html/fog/lib/fog/config.class.php.old
            sed -i "s|\".*\..*\..*\..*\"|\$_SERVER['SERVER_ADDR']|" /var/www/html/fog/lib/fog/config.class.php
    
    
    
    fi
    

  • Moderator

    I’m going to take a stab at making all of that below into one single cron-friendly script - and have it run via Cron every 5 minutes.

    I’ll add logic to it so that it’ll only run if the IP has changed since the last run. It’ll use settings set in the FOG config file where appropriate.

    I’ll also work dnsmasq into it as well, along with service restarts for dnsmasq.


  • Moderator

    @george1421 said:

    The only thing that jumps out that may be an issue in the script is that for the Centos 7 OS the ethernet adapters are no longer just eth0, eth1 and so on. The Intel NUC I’m working on the default ethernet adapter is enp3s0.

    If the interface name is known, you can use this to get the IP in CentOS 7.

    interface1ip="$(/sbin/ip addr show | grep $interface1name | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")"

    This is a piece from a little project I’m working on:
    https://sourceforge.net/p/openvpnrouter/code/HEAD/tree/identifyInterfaces.sh#l26


  • Moderator

    I agree this is great! I’m glad we have the community involvement in the FOG community to help everyone solve these basic problems. Well done @sudburr !!

    The only thing that jumps out that may be an issue in the script is that for the Centos 7 OS the ethernet adapters are no longer just eth0, eth1 and so on. The Intel NUC I’m working on the default ethernet adapter is enp3s0.

    I just checked and this value interface="enp3s0" can be extracted from the /opt/fog/.fogsettings file. This would then tie into what interface FOG is using too. There may be a case where there is a system with one or more network interfaces that the script would have to take into account.


Log in to reply
 

426
Online

39001
Users

10717
Topics

101762
Posts

Looks like your connection to FOG Project was lost, please wait while we try to reconnect.