From d57d1c9ba644f7431f69cdffbcf7a8c9de0a5b44 Mon Sep 17 00:00:00 2001 From: Nicolas Roy Date: Wed, 25 Nov 2015 14:47:33 -0800 Subject: [PATCH] Add "disable" and "enable" omsagent service commands to /opt/microsoft/omsagent/bin/service_control --- installer/bundle/bundle_skel.sh | 64 ++++++++++++++++- installer/datafiles/linux.data | 110 ++--------------------------- installer/scripts/omsagent.systemd | 4 +- installer/scripts/service_control | 97 +++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 106 deletions(-) diff --git a/installer/bundle/bundle_skel.sh b/installer/bundle/bundle_skel.sh index 3286891c0..152dcad39 100644 --- a/installer/bundle/bundle_skel.sh +++ b/installer/bundle/bundle_skel.sh @@ -200,6 +200,67 @@ pkg_upd() { fi } +# This code is here to allow upgrade from version 47-48 +# It was moved to the OMS service_control script but is also needed here +# because version 47-48 do not have the 'disable' option in that script +# and it is called in preinstall +resolve_systemd_paths(){ + # Various distributions have different paths for systemd unit files ... + SYSTEMD_UNIT_DIR="" + if pidof systemd 1> /dev/null 2> /dev/null; then + # Be sure systemctl lives where we expect it to + if [ ! -f /bin/systemctl ]; then + echo "FATAL: Unable to locate systemctl program" 1>&2 + exit 1 + fi + + # Find systemd unit directory + UNIT_DIR_LIST="/usr/lib/systemd/system /lib/systemd/system" + for i in ${UNIT_DIR_LIST}; do + if [ -d $i ]; then + SYSTEMD_UNIT_DIR=${i} + return + fi + done + + echo "FATAL: Unable to resolve systemd unit directory!" 1>&2 + exit 1 + fi +} + +disable_omsagent_service() { + OMS_SERVICE='/opt/microsoft/omsagent/bin/service_control' + # Stop the server if it's running + if [ -f ${OMS_SERVICE} ]; then + ${OMS_SERVICE} stop + fi + + resolve_systemd_paths + + # Registered as a systemd service? + if [ -f ${SYSTEMD_UNIT_DIR}/omsagent.service ]; then + echo "Unconfiguring OMS agent (systemd) service ..." + /bin/systemctl disable omsagent + rm -f ${SYSTEMD_UNIT_DIR}/omsagent.service + /bin/systemctl daemon-reload + elif [ -f /etc/init.d/omsagent ]; then + echo "Unconfiguring OMS agent service ..." + if [ -f /usr/sbin/update-rc.d ]; then + /usr/sbin/update-rc.d -f omsagent remove + elif [ -x /usr/lib/lsb/remove_initd ]; then + /usr/lib/lsb/remove_initd /etc/init.d/omsagent + elif [ -x /sbin/chkconfig ]; then + chkconfig --del omsagent > /dev/null + else + echo "Unrecognized Service Controller to unregister OMS Agent Service." + exit 1 + fi + + rm /etc/init.d/omsagent + fi +} +# End of temporary upgradecode + # # Main script follows @@ -467,7 +528,7 @@ case "$installMode" in pkg_add $SCX_PKG scx SCX_EXIT_STATUS=$? - + pkg_add $OMS_PKG omsagent OMS_EXIT_STATUS=$? @@ -558,6 +619,7 @@ case "$installMode" in echo "${CIPHER}" >> $OMISERV_CONF fi + disable_omsagent_service pkg_upd $OMS_PKG omsagent OMS_EXIT_STATUS=$? diff --git a/installer/datafiles/linux.data b/installer/datafiles/linux.data index f4dbf9adf..d75d27ecd 100644 --- a/installer/datafiles/linux.data +++ b/installer/datafiles/linux.data @@ -17,99 +17,6 @@ OMS_SERVICE: '/opt/microsoft/omsagent/bin/service_control' /etc/opt/microsoft/omsagent/sysconf/sudoers; installer/conf/sudoers; 664; root; root - -%Systemd_resolver -# Various distributions have different paths for systemd unit files ... - -SYSTEMD_UNIT_DIR="" -ResolveSystemdPaths() -{ - local UNIT_DIR_LIST="/usr/lib/systemd/system /lib/systemd/system" - - if pidof systemd 1> /dev/null 2> /dev/null; then - # Be sure systemctl lives where we expect it to - if [ ! -f /bin/systemctl ]; then - echo "FATAL: Unable to locate systemctl program" 1>&2 - exit 1 - fi - - # Find systemd unit directory - for i in ${UNIT_DIR_LIST}; do - if [ -d $i ]; then - SYSTEMD_UNIT_DIR=${i} - return 0 - fi - done - - # Didn't fine unit directory, that's fatal - echo "FATAL: Unable to resolve systemd unit directory!" 1>&2 - exit 1 - else - return 1 - fi -} - -ResolveSystemdPaths - -%OMSService_funcs -StopOMSService() { - if [ -f ${{OMS_SERVICE}} ]; then - ${{OMS_SERVICE}} stop - fi -} - -RemoveOMSService() { - # Stop the server if it's running - StopOMSService - - # Registered as a systemd service? - if [ -f ${SYSTEMD_UNIT_DIR}/omsagent.service ]; then - echo "Unconfiguring OMS agent (systemd) service ..." - /bin/systemctl disable omsagent - rm -f ${SYSTEMD_UNIT_DIR}/omsagent.service - /bin/systemctl daemon-reload - elif [ -f /etc/init.d/omsagent ]; then - echo "Unconfiguring OMS agent service ..." - if [ -f /usr/sbin/update-rc.d ]; then - /usr/sbin/update-rc.d -f omsagent remove - elif [ -x /usr/lib/lsb/remove_initd ]; then - /usr/lib/lsb/remove_initd /etc/init.d/omsagent - elif [ -x /sbin/chkconfig ]; then - chkconfig --del omsagent > /dev/null - else - echo "Unrecognized Service Controller to unregister OMS Agent Service." - exit 1 - fi - - rm /etc/init.d/omsagent - fi -} - -ConfigureOMSService() { - echo "Configuring OMS agent service ..." - if pidof systemd 1> /dev/null 2> /dev/null; then - # systemd - cp /etc/opt/microsoft/omsagent/sysconf/omsagent.systemd ${SYSTEMD_UNIT_DIR}/omsagent.service - /bin/systemctl daemon-reload - /bin/systemctl enable omsagent - else - cp /etc/opt/microsoft/omsagent/sysconf/omsagent.ulinux /etc/init.d/omsagent - - if [ -x /usr/sbin/update-rc.d ]; then - update-rc.d omsagent defaults > /dev/null - elif [ -x /usr/lib/lsb/install_initd ]; then - /usr/lib/lsb/install_initd /etc/init.d/omsagent - elif [ -x /sbin/chkconfig ]; then - chkconfig --add omsagent > /dev/null - else - echo "Unrecognized Service Controller to configure OMS Agent Service." - exit 1 - fi - fi - - ${{OMS_SERVICE}} start -} - %Syslog_Services RestartService() { if [ -z "$1" ]; then @@ -120,8 +27,9 @@ RestartService() { echo "Restarting service: $1" # Does the service exist under systemd? + local systemd_dir=$(${{OMS_SERVICE}} find-systemd-dir) pidof systemd 1> /dev/null 2> /dev/null - if [ $? -eq 0 -a -f ${SYSTEMD_UNIT_DIR}/${1}.service ]; then + if [ $? -eq 0 -a -f ${systemd_dir}/${1}.service ]; then /bin/systemctl restart $1 else if [ -x /usr/sbin/invoke-rc.d ]; then @@ -265,9 +173,7 @@ SudoSupportsIncludeDirective() { %Preinstall_1000 -#include Systemd_resolver #include Syslog_Services -#include OMSService_funcs # If our service is already running, stop it if [ -f /opt/microsoft/omsagent/bin/service_control ]; then @@ -275,7 +181,9 @@ if [ -f /opt/microsoft/omsagent/bin/service_control ]; then fi UnconfigureSyslog -RemoveOMSService + +# The OMS_SERVICE script will not be present on a fresh install +[ -f ${{OMS_SERVICE}} ] && ${{OMS_SERVICE}} disable # Add the 'omsagent' group if it does not already exist # (Can't use useradd with -U since that doesn't exist on older systems) @@ -293,13 +201,11 @@ if [ $? -ne 0 ]; then fi %Postinstall_200 -#include Systemd_resolver -#include OMSService_funcs #include Syslog_Services #include Sudoer_Functions ${{OMI_SERVICE}} reload -ConfigureOMSService +${{OMS_SERVICE}} enable ConfigureSyslog # Deal with sudo (either place in sudoers.d or append to sudoers configuration) @@ -328,14 +234,12 @@ fi %Postuninstall_1000 -#include Systemd_resolver #include Syslog_Services -#include OMSService_funcs # If we're called for upgrade, don't do anything if ${{PERFORMING_UPGRADE_NOT}}; then UnconfigureSyslog - RemoveOMSService + ${{OMS_SERVICE}} disable # Restart the OMI server in case an agent is running under service account ${{OMI_SERVICE}} restart diff --git a/installer/scripts/omsagent.systemd b/installer/scripts/omsagent.systemd index 6e0656544..80bdb11af 100644 --- a/installer/scripts/omsagent.systemd +++ b/installer/scripts/omsagent.systemd @@ -1,7 +1,7 @@ [Unit] Description=Operations Management Suite agent After=network.target -Wants=omiserverd.service +Wants=omid.service [Service] Type=simple @@ -12,7 +12,7 @@ ExecStart=/opt/microsoft/omsagent/bin/omsagent \ -d /var/opt/microsoft/omsagent/run/omsagent.pid \ -o /var/opt/microsoft/omsagent/log/omsagent.log \ --no-supervisor -ExecStop=rm -f /var/opt/microsoft/omsagent/run/omsagent.pid +ExecStop=/bin/rm -f /var/opt/microsoft/omsagent/run/omsagent.pid KillMode=process KillSignal=SIGKILL TimeoutStartSec=10 diff --git a/installer/scripts/service_control b/installer/scripts/service_control index 922f19792..b279f0502 100644 --- a/installer/scripts/service_control +++ b/installer/scripts/service_control @@ -160,6 +160,91 @@ restart_omsagent() fi } + +find_systemd_dir() +{ + # Various distributions have different paths for systemd unit files ... + local UNIT_DIR_LIST="/usr/lib/systemd/system /lib/systemd/system" + + if pidof systemd 1> /dev/null 2> /dev/null; then + # Be sure systemctl lives where we expect it to + if [ ! -f /bin/systemctl ]; then + echo "FATAL: Unable to locate systemctl program" 1>&2 + exit 1 + fi + + # Find systemd unit directory + for i in ${UNIT_DIR_LIST}; do + if [ -d $i ]; then + echo ${i} + return 0 + fi + done + + # Didn't find unit directory, that's fatal + echo "FATAL: Unable to resolve systemd unit directory!" 1>&2 + exit 1 + else + return 1 + fi +} + +enable_omsagent_service() +{ + echo "Configuring OMS agent service ..." + if pidof systemd 1> /dev/null 2> /dev/null; then + # systemd + local systemd_dir=$(find_systemd_dir) + cp /etc/opt/microsoft/omsagent/sysconf/omsagent.systemd ${systemd_dir}/omsagent.service + /bin/systemctl daemon-reload + /bin/systemctl enable omsagent + else + cp /etc/opt/microsoft/omsagent/sysconf/omsagent.ulinux /etc/init.d/omsagent + + if [ -x /usr/sbin/update-rc.d ]; then + update-rc.d omsagent defaults > /dev/null + elif [ -x /usr/lib/lsb/install_initd ]; then + /usr/lib/lsb/install_initd /etc/init.d/omsagent + elif [ -x /sbin/chkconfig ]; then + chkconfig --add omsagent > /dev/null + else + echo "Could not find a service controller to configure the OMS Agent Service." + exit 1 + fi + fi + + start_omsagent +} + +disable_omsagent_service() +{ + # Stop the server if it's running + stop_omsagent + + # Registered as a systemd service? + local systemd_dir=$(find_systemd_dir) + if [ -f ${systemd_dir}/omsagent.service ]; then + echo "Unconfiguring OMS agent (systemd) service ..." + /bin/systemctl disable omsagent + rm -f ${systemd_dir}/omsagent.service + /bin/systemctl daemon-reload + elif [ -f /etc/init.d/omsagent ]; then + echo "Unconfiguring OMS agent service ..." + if [ -f /usr/sbin/update-rc.d ]; then + /usr/sbin/update-rc.d -f omsagent remove + elif [ -x /usr/lib/lsb/remove_initd ]; then + /usr/lib/lsb/remove_initd /etc/init.d/omsagent + elif [ -x /sbin/chkconfig ]; then + chkconfig --del omsagent > /dev/null + else + echo "Unrecognized Service Controller to unregister OMS Agent Service." + exit 1 + fi + + rm /etc/init.d/omsagent + fi +} + case "$1" in functions) ;; @@ -186,6 +271,18 @@ case "$1" in restart_omsagent ;; + find-systemd-dir) + find_systemd_dir + ;; + + enable) + enable_omsagent_service + ;; + + disable) + disable_omsagent_service + ;; + *) echo "Unknown parameter : $1" 1>&2 exit 1