#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)maintenance 3.2.0 03.1.2025 (c)2025 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # #@ Do some admin tasks # # Exit codes: # 01: Unsupported platform. # 02: No root privileges (needed to proceed). # 03: Needed tool missing in in PATH. # 04: Unknown parameter. # 04: Application error. # # Update history: # # V 3.2.0 03.1.2025 New version # # Usage: # # cloud-agent # Shows infos about oracle cloud agent rpm # cloud-agent status # Shows infos if oracle cloud agent is running or not # cloud-agent restart # Restart oracle cloud agent # cloud-agent renew # Delete old version and install new oracle cloud agent rpm # # Set some defaults exitcode=0 # Return code of the script script=`echo "$0" | sed 's|^[-]*||'` # Name of this script without any leading dashes progstr=`basename "$script"` # Just the name of the script pid="$$" # Get the process id from current shell tmpdir="/tmp" # Temporary directory timestamp=`date '+%y%m%d%H%M%S'` # Extension String with current date and time scratchfile="${tmpdir}/${progstr}.${timestamp}.${pid}.tmp" # Temporary file # Information from instance metadata imdsurl="http://169.254.169.254/opc/v2" insttags="instance/definedTags/Instance-Management" # Get current user and os PARAM1=`echo "$1" | tr '[:upper:]' '[:lower:]'` # First papameter PARAM2=${2} # Second papameter ME=`whoami` # Current user OS=`uname -s` # Infos about the host os (e.g. Darwin, SunOS, Linux) # Set PATH PATH="/bin:/.local/bin:/usr/local/bin:$PATH" # Check for some tools transfer=`which "transfer" 2>/dev/null | sed 's|^no transfer in .*||'` setuptools=`which "setup-tools" 2>/dev/null | sed 's|^no setup-tools in .*||'` ufw=`which "ufw" 2>/dev/null | sed 's|^no ufw in .*||'` systemctl=`which "systemctl" 2>/dev/null | sed 's|^no systemctl in .*||'` firewallcmd=`which "firewall-cmd" 2>/dev/null | sed 's|^no firewall-cmd in .*||'` # Delete file function DeleteFile { local filename=${1} if [ "$filename" != "" ]; then if [ -f "$filename" ]; then rm -f "$filename" fi fi } # Delete tempfiles function Cleanup { DeleteFile $scratchfile } # Get OS name and version from /etc/os-release. Examples: # OL6: NAME="Oracle Linux Server" / ID="rhel" / VERSION_ID="6.10" / ID="ol" / ID_LIKE="fedora" # OL7: NAME="Oracle Linux Server" / VERSION_ID="7.9" / ID="ol" # OL8: NAME="Oracle Linux Server" / VERSION_ID="8.10" / ID="ol" # OL9: NAME="Oracle Linux Server" / VERSION_ID="9.6" / ID="ol" # OL10: NAME="Oracle Linux Server" / VERSION_ID="10.0" / ID="ol" # RHEL8: NAME="Red Hat Enterprise Linux" / VERSION_ID="8.6" / ID="rhel" # UBUNTU: Name="Ubuntu" / VERSION_ID="25.04" / ID=ubuntu / ID_LIKE=debian / VERSION_CODENAME=plucky function GetOSVersion { NAME="" ID="" ID_LIKE="fedora" VERSION_ID="" VERSION_CODENAME="" VERSION_MAIN=6 VERSION_GT_6="false" VERSION_GT_7="false" if [ -r /etc/os-release ]; then source /etc/os-release ID_LIKE=`echo "$ID_LIKE" | cut -d' ' -f1` if [ "$VERSION_ID" != "" ]; then VERSION_MAIN=`echo "$VERSION_ID" | cut -d'.' -f1` if [ "$VERSION_MAIN" != "" ]; then if [ "$ID" = "ol" -o "$ID" = "rhel" ]; then if [ $VERSION_MAIN -gt 6 ]; then VERSION_GT_6="true" if [ $VERSION_MAIN -gt 7 ]; then VERSION_GT_7="true" fi fi fi fi fi fi # Determine tools case "$ID_LIKE" in fedora) instcmd=`which "yum" 2>/dev/null | sed 's|^no yum in .*||'` if [ "$instcmd" = "" ]; then instcmd=`which "dnf" 2>/dev/null | sed 's|^no dnf in .*||'` fi firewallcmd=`which "firewall-cmd" 2>/dev/null | sed 's|^no firewall-cmd in .*||'` ;; debian) instcmd=`which "apt" 2>/dev/null | sed 's|^no apt in .*||' if [ "$instcmd" = "" ]; then instcmd=`which "apt-get" 2>/dev/null | sed 's|^no apt-get in .*||'` fi if [ "$instcmd" != "" ]; then $instcmd update >/dev/null 2>&1 fi firewallcmd=`which "ufw" 2>/dev/null | sed 's|^no ufw in .*||'` ;; suse | opensuse) instcmd=`which "zypper" 2>/dev/null | sed 's|^no zypper in .*||' firewallcmd=`which "firewall-cmd" 2>/dev/null | sed 's|^no firewall-cmd in .*||'` ;; esac systemctl=`which "systemctl" 2>/dev/null | sed 's|^no systemctl in .*||'` } # Set locale # Current locale should also be stored in /etc/locale.conf function SetLocale { local locale=${1} local localectl=`which "localectl" 2>/dev/null | sed 's|^no localectl in .*||'` local found="" local grepres="" local currlocale="" local stat=1 if [ "$localectl" != "" -a "$locale" != "" ]; then # Check if parameter is in available languages locale=`echo "$locale" | tr '[:upper:]' '[:lower:]' | sed 's|^lang=||'` found=`$localectl list-locales | grep -i "^${locale}$"` if [ "$found" = "" ]; then grepres=`echo "$locale" | cut -d'.' -f1` found=`echo "$locale" | cut -d'.' -f2` if [ "$found" = "utf-8" ]; then grepres="${grepres}.utf8" found=`$localectl list-locales | grep -i "^${grepres}$"` fi fi if [ "$found" = "" ]; then echo "Locale '$locale' not available." else grepres=`$localectl status | tr -s ' '` currlocale=`echo "$grepres" | grep "^ System Locale: " | cut -d' ' -f4` if [ "$currlocale" = "" ]; then currlocale=`echo "$grepres" | grep "^System Locale: " | cut -d' ' -f3` fi if [ "$currlocale" != "LANG=$found" ]; then $localectl set-locale "LANG=$found" stat=$? if [ $stat -eq 0 ]; then echo "Locale set to 'LANG=$found'." fi # else # echo "Locale already set to 'LANG=$found'." fi fi fi return $stat } # Set time zone for VM # timedatectl status: Show settings in human readyble format function SetTimeZone { local timezone=${1} local timedatectl=`which "timedatectl" 2>/dev/null | sed 's|^no timedatectl in .*||'` local found="" local currtimezone="" local stat=1 if [ "$timedatectl" != "" -a "$timezone" != "" ]; then found=`$timedatectl list-timezones | grep -i "^${timezone}$"` if [ "$found" = "" ]; then echo "Time zone '$timezone' not available." else currtimezone=`$timedatectl show 2>/dev/null | grep "^Timezone=" | cut -d'=' -f2` # Does not work for OL7 if [ "$currtimezone" = "" ]; then currtimezone=`$timedatectl status | tr -s ' ' | grep "^ Time zone: " | cut -d' ' -f4` fi if [ "$currtimezone" != "$found" ]; then $timedatectl set-timezone "$found" stat=$? if [ $stat -eq 0 ]; then echo "Timezone set to '$found'." fi if [ "$systemctl" != "" ]; then $systemctl restart crond.service fi # else # echo "Timezone already set to '$found'." fi fi fi return $stat } # Activate or deactivate cockpit function EnableCockpit { local enableit=${1} local grepres="" local stat=1 if [ "$systemctl" != "" ]; then enableit=`echo "$enableit" | tr '[:upper:]' '[:lower:]'` grepres=`$systemctl status cockpit.socket 2>/dev/null | grep "Active:" | tr -s ' ' | cut -d' ' -f3` if [ "$enableit" = "enabled" -o "$enableit" = "enable" ]; then if [ "$grepres" != "active" ]; then # Open the port in firewall if [ "$firewallcmd" != "" ]; then $firewallcmd --add-service=cockpit --permanent 2>/dev/null $firewallcmd --reload else if [ "$ufw" != "" ]; then $ufw allow 9090/tcp $ufw reload fi fi $systemctl enable --now cockpit.socket 2>/dev/null stat=$? if [ $stat -eq 0 ]; then echo "Cockpit enabled." else echo "Cockpit not enabled." fi # else # echo "Cockpit already enabled." fi else if [ "$enableit" = "disabled" -o "$enableit" = "disable" ]; then if [ "$grepres" = "active" ]; then $systemctl disable --now cockpit.socket 2>/dev/null stat=$? if [ $stat -eq 0 ]; then echo "Cockpit disabled." else echo "Cockpit not disabled." fi # Close the port in firewall if [ "$firewallcmd" != "" ]; then $firewallcmd --remove-service=cockpit --permanent 2>/dev/null $firewallcmd --reload else if [ "$ufw" != "" ]; then $ufw deny 9090 $ufw reload fi fi # else # echo "Cockpit already disabled." fi else echo "Unknown parameter '$enableit' along with cockpit." fi fi fi return $stat } # Rotate one logfile if lines are greater then maxlines function RotateLog { local logfile=${1} local maxlines=10000 local lines=0 local stat=0 if [ "$logfile" != "" ]; then if [ ! -d "$logfile" -a ! -L "$logfile" -a -s "$logfile" ]; then lines=`cat "$logfile" | wc -l` if [ $lines -gt $maxlines ]; then # Display message echo "Maintaining log '$logfile'." # Move logfile mv -f "$logfile" "${logfile}.old" stat=$? if [ $stat -gt 0 ]; then echo "Unable to rotate logfile '$logfile'." else # Create new logfile printf "" > "$logfile" stat=$? if [ $stat -eq 0 ]; then chmod 644 "$logfile" else echo "Unable to create new logfile '$logfile'." fi fi fi fi fi } # Cleanup logs function MaintainLogs { local logdir=${1} local wd=`pwd` local logfile="" local stat=0 if [ "$logdir" != "" ]; then # Display message echo "Checking logs in directory '$logdir'." # cd to logdir cd "$logdir" stat=$? if [ $stat -gt 0 ]; then echo "Unable to access '$logdir'." else for logfile in `ls`; do case "$logfile" in bootstrap.log | disk-management.log | install-tools.log | setup-tools.log) RotateLog "$logfile" ;; esac done fi # cd back to working directory and display message cd "$wd" echo "Maintainig logs in directory '$logdir' done." fi } # Main if [ "$OS" != "Linux" ]; then echo "Unsupported platform '$OS'. Exiting." exitcode=1 else # Check if we have root privileges if [ "$ME" != "root" ]; then echo "Need to be 'root'. Exiting." exitcode=2 else ### GetOSVersion case "$PARAM1" in status) echo "Tool '$progstr' reday to use." ;; check) if [ "$transfer" != "" ]; then for item in Locale TimeZone Cockpit; do PARAM2=`$transfer --quiet --auth "${imdsurl}/${insttags}/$item" 2>/dev/null | head -n 1 | grep -v "^"` if [ "$PARAM2" != "" ]; then case "$item" in Locale) SetLocale "$PARAM2" ;; TimeZone) SetTimeZone "$PARAM2" ;; Cockpit) EnableCockpit "$PARAM2" ;; esac fi done else echo "Tool 'transfer' not found. Exiting." exitcode=3 fi ;; locale | timezone | cockpit | rotatelogs) if [ "$PARAM2" != "" ]; then case "$PARAM1" in locale) SetLocale "$PARAM2" stat=$? ;; timezone) SetTimeZone "$PARAM2" stat=$? ;; cockpit) EnableCockpit "$PARAM2" stat=$? ;; rotatelogs) MaintainLogs "$PARAM2" stat=$? ;; esac if [ $stat -ne 0 ]; then exitcode=5 fi else # Get infos from metadata echo "Missing second parameter. Exiting." exitcode=4 fi ;; *) echo "Unknown parameter '$PARAM1'. Exiting." exitcode=4 esac fi fi # Cleanup and exit with exitcode Cleanup exit $exitcode