#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)cloud-agent 3.2.0 25.09.2025 (c)2025 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # #@ Show infos about installed oracle cloud agent and restart and renew it if sprecified # # Exit codes: # 01: Unsupported platform. # 02: No root privileges (needed to proceed). # 03: Needed tool missing in in PATH. # 04: Unknown parameter. # # Update history: # # V 3.2.0 23.09.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 package="oracle-cloud-agent" # RPM package name 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 # Get current user and os ME=`whoami` # Current user OS=`uname -s` # Infos about the host os (e.g. Darwin, SunOS, Linux) PARAM=`echo "$1" | tr '[:upper:]' '[:lower:]'` # First papameter # Set PATH PATH="/bin:/.local/bin:/usr/local/bin:$PATH" # Check for some tools yum=`which "yum" 2>/dev/null | sed 's|^no yum in .*||'` rpm=`which "rpm" 2>/dev/null | sed 's|^no rpm in .*||'` systemctl=`which "systemctl" 2>/dev/null | sed 's|^no systemctl 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 } # Kill the old agent the hard way ('systemctl stop oracle-cloud-agent' may not work here) function KillAgent { sleep 10 pid=`ps -ef | grep ${package}/agent | grep -v grep | head -n 1 | awk '{print $2}'` if [ "$pid" != "" ]; then kill -9 $pid 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 # Check if we have curl installed if [ "$yum" = "" ]; then echo "No 'yum' in PATH. Exiting." exitcode=3 else if [ "$PARAM" = "" ]; then $yum info "$package" > $scratchfile 2>/dev/null exitcode=$? if [ $exitcode -eq 0 -a -s $scratchfile ]; then cat $scratchfile | grep ' : ' else echo "Not installed: '$package'. Exiting." fi else case "$PARAM" in status) if [ "$systemctl" != "" ]; then $systemctl "$PARAM" "$package" | head -n 8 else exitcode=3 fi ;; restart) if [ "$systemctl" != "" ]; then $script kill_restart & exitcode=$? if [ $exitcode -eq 0 ]; then disown pid=`ps -ef | grep "$progstr kill_$PARAM" | grep -v grep | awk '{print $2}'` echo "Process ID: $pid" fi else exitcode=3 fi ;; do_restart | kill_restart) if [ "$PARAM" = "kill_restart" ]; then KillAgent fi $systemctl restart $package >/dev/null 2>&1 # systemctl status oracle-cloud-agent exitcode=$? ;; renew) $script kill_renew & exitcode=$? if [ $exitcode -eq 0 ]; then disown pid=`ps -ef | grep "$progstr kill_$PARAM" | grep -v grep | awk '{print $2}'` echo "Process ID: $pid" # echo "Parent Prozess ID: $$" # echo "Sudo Prozess ID: $PPID" fi ;; do_renew | kill_renew) if [ "$PARAM" = "kill_renew" ]; then KillAgent fi $yum remove -y "$package" >/dev/null 2>&1 $yum install -y "$package" >/dev/null 2>&1 exitcode=$? ;; *) echo "Unknown parameter '$PARAM'. Exiting." exitcode=4 esac fi fi fi fi # Cleanup and exit with exitcode Cleanup exit $exitcode