• Recent
    • Unsolved
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Change Image ID Number

    Scheduled Pinned Locked Moved Solved
    FOG Problems
    3
    44
    12.2k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Wayne WorkmanW
      Wayne Workman
      last edited by

      Tom’s right. I have several ideas on how to make it better but I’ve just not worked on it.

      Please help us build the FOG community with everyone involved. It's not just about coding - way more we need people to test things, update documentation and most importantly work on uniting the community of people enjoying and working on FOG!
      Daily Clean Installation Results:
      https://fogtesting.fogproject.us/
      FOG Reporting:
      https://fog-external-reporting-results.fogproject.us/

      1 Reply Last reply Reply Quote 0
      • RobTitian16R
        RobTitian16 @Tom Elliott
        last edited by

        @Tom-Elliott Ah that makes sense. Thanks for clarifying!

        1 Reply Last reply Reply Quote 0
        • Wayne WorkmanW
          Wayne Workman
          last edited by Wayne Workman

          New script that works no matter what, plus resets the auto-number too.

          Keep in mind the nodeBB bug that removes spaces after [[ and before ]].

          Where you see double left brackets, a space must come after them. Where you see double right brackets, a space must come before them. You’ll need to add these spaces to the script if they are not there.

          #!/bin/bash
          
          #----- MySQL Credentials -----#
          snmysqluser=""
          snmysqlpass=""
          snmysqlhost=""
          # If user and pass is blank, leave just a set of double quotes like ""
          # if the db is local, set the host to just double quotes "" or "127.0.0.1" or "localhost"
          
          
          #----- Begin Program -----#
          
          selectAllImageIDs="SELECT imageID FROM images ORDER BY imageID"
          selectLowestImageID="SELECT imageID FROM images ORDER BY imageID ASC LIMIT 1"
          selectHighestImageID="SELECT imageID FROM images ORDER BY imageID DESC LIMIT 1"
          
          options="-sN"
          if [[ $snmysqlhost != "" ]]; then
                  options="$options -h$snmysqlhost"
          fi
          if [[ $snmysqluser != "" ]]; then
                  options="$options -u$snmysqluser"
          fi
          if [[ $snmysqlpass != "" ]]; then
                  options="$options -p$snmysqlpass"
          fi
          options="$options -D fog -e"
          
          
          #Information gathering.
          lowestID=$(mysql $options "$selectLowestImageID")
          highestID=$(mysql $options "$selectHighestImageID")
          newAutoIncrement=$((highestID + 1))
          
          
          ####### Basic logic flow ########
          
          #If there is an image id of 1, move it to the new auto increment number.
          #After re-numbering is complete, the new auto increment number will not be taken.
          #Then reset the auto-increment to the new auto increment number, which is the first free number.
          
          
          
          #Move any images that have an ID of 1 to the next free number.
          if [[ "$lowestID" -eq "1" ]]; then
              echo "-------------------"
              echo "Attempting to change Image ID $lowestID to $newAutoIncrement"
              mysql $options "UPDATE images SET imageID = $newAutoIncrement WHERE imageID = $lowestID"
              mysql $options "UPDATE imageGroupAssoc SET igaImageID = $newAutoIncrement WHERE igaImageID = $lowestID"
              mysql $options "UPDATE hosts SET hostImage = $newAutoIncrement WHERE hostImage = $lowestID"
              echo "Attempt completed"
          fi
          
          
          #Re-number all images sequentially.
          count=1
          mysql $options "$selectAllImageIDs" | while read imageID; do
              echo "-------------------"
              echo "Attempting to change Image ID $imageID to $count"
              mysql $options "UPDATE images SET imageID = $count WHERE imageID = $imageID"
              mysql $options "UPDATE imageGroupAssoc SET igaImageID = $count WHERE igaImageID = $imageID"
              mysql $options "UPDATE hosts SET hostImage = $count WHERE hostImage = $imageID"
              echo "Attempt completed"
              count=$((count + 1))
          done
          
          
          #set new auto-increment.
          echo "-------------------"
          highestID=$(mysql $options "$selectHighestImageID")
          newAutoIncrement=$((highestID + 1))
          echo "Attempting to change the auto_increment for the images table to $newAutoIncrement"
          mysql $options "ALTER TABLE images AUTO_INCREMENT = $newAutoIncrement"
          echo "Attempt completed"
          
          

          Please help us build the FOG community with everyone involved. It's not just about coding - way more we need people to test things, update documentation and most importantly work on uniting the community of people enjoying and working on FOG!
          Daily Clean Installation Results:
          https://fogtesting.fogproject.us/
          FOG Reporting:
          https://fog-external-reporting-results.fogproject.us/

          RobTitian16R 1 Reply Last reply Reply Quote 0
          • RobTitian16R
            RobTitian16 @Wayne Workman
            last edited by

            @Wayne-Workman Perfect - thanks for this!

            1 Reply Last reply Reply Quote 0
            • 1
            • 2
            • 3
            • 3 / 3
            • First post
              Last post

            154

            Online

            12.0k

            Users

            17.3k

            Topics

            155.2k

            Posts
            Copyright © 2012-2024 FOG Project