]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Sending mail on update minecraft server
authorFrank Brehm <frank@brehm-online.com>
Thu, 22 Apr 2021 17:10:46 +0000 (19:10 +0200)
committerFrank Brehm <frank@brehm-online.com>
Thu, 22 Apr 2021 17:10:46 +0000 (19:10 +0200)
bin/update-minecraft-server-jar

index 062aeab8f2e96e26c798af42ebcaa150db474dd4..3d7ec032e0b7b694873f250c6102a0fe78926647 100755 (executable)
@@ -36,12 +36,20 @@ SHA1_SUM=
 TARGET_JAR=
 NEED_RESTART='n'
 
+MAIL_FROM_ADDRESS="minecraft+$( fqdn -f )@uhu-banane.de"
+MAIL_TO_ADDRESS="frank@brehm-online.com"
+MAIL_RETURN_ADDRESS="postmaster@brehm-online.com"
+
+MAIL_BODY=""
+
 DESCRIPTION=$( cat <<-EOF
        Update Micraft server .jar file.
 
        EOF
 )
 
+START_DATE=$( my_date )
+
 detect_color
 
 #------------------------------------------------------------------------------
@@ -235,6 +243,11 @@ do_download() {
     CHOWN "${MC_USER}:${MC_GROUP}" "${TARGET_JAR}"
     NEED_RESTART='y'
 
+    if [[ -n "${MAIL_BODY}" ]] ; then
+        MAIL_BODY+="\n"
+    fi
+    MAIL_BODY+="$( my_date ): Downloaded latest server.jar file as '${TARGET_JAR}'.\n"
+
 }
 
 #------------------------------------------------------------------------------
@@ -258,6 +271,7 @@ check_sha1() {
 fix_symlinks() {
 
     info "Checking symlink in '${CYAN}${MC_BACKUP_DIR}${NORMAL}' ..."
+    local symlink_was_set="n"
 
     if [[ -e "${MC_SERVER_CUR_LINK_BASE}" ]] ; then
         if [[ ! -h "${MC_SERVER_CUR_LINK_BASE}" ]] ; then
@@ -278,6 +292,7 @@ fix_symlinks() {
             info "Symlink '${CYAN}${MC_SERVER_CUR_LINK_BASE}${NORMAL}' must be corrected."
             LN -sf "${TARGET_JAR}" "${MC_SERVER_CUR_LINK_BASE}"
             NEED_RESTART='y'
+            symlink_was_set="y"
         fi
 
     else
@@ -285,9 +300,17 @@ fix_symlinks() {
         info "Symlink '${CYAN}${MC_SERVER_CUR_LINK_BASE}${NORMAL}' does not exists, will be created."
         LN -sf "${TARGET_JAR}" "${MC_SERVER_CUR_LINK_BASE}"
         NEED_RESTART='y'
+        symlink_was_set="y"
 
     fi
 
+    if [[ "${symlink_was_set}" == "y" ]] ; then
+        if [[ -n "${MAIL_BODY}" ]] ; then
+            MAIL_BODY+="\n"
+        fi
+        MAIL_BODY+="$( my_date ): Symlink '${MC_SERVER_CUR_LINK_BASE}' was set pointing to '${TARGET_JAR}'.\n"
+    fi
+
 }
 
 #------------------------------------------------------------------------------
@@ -295,6 +318,7 @@ restart_service() {
 
     local -a services=()
     local service=
+    local svc_status=
 
     for service in $( systemctl --plain --type=service --state=running --no-legend --no-pager | \
             grep '^minecraft' | awk '{print $1}' ) ; do
@@ -306,12 +330,17 @@ restart_service() {
         return
     fi
 
+    if [[ -n "${MAIL_BODY}" ]] ; then
+        MAIL_BODY+="\n"
+    fi
+
     empty_line
     for service in "${ervices[@]}" ; do
         info "Restarting '${CYAN}${service}${NORMAL}' ..."
         if [[ "${SIMULATE}" != "y" ]] ; then
             systemctl restart "${service}"
         fi
+        MAIL_BODY+="$( my_date ): Restarted service '${service}'.\n"
     done
 
     sleep 15
@@ -319,11 +348,46 @@ restart_service() {
     for service in "${ervices[@]}" ; do
         empty_line
         info "Status of '${CYAN}${service}${NORMAL}':"
-        systemctl status "${service}"
+        svc_status=$( systemctl status "${service}" 2>&1 )
+        if [[ "${QUIET}" != "y" ]] ; then
+            echo "${svc_status}"
+        fi
+        MAIL_BODY+="\n$( my_date ): Status of service '${service}':\n${svc_status}\n"
     done
 
 }
 
+#------------------------------------------------------------------------------
+check_and_send_mail() {
+
+    if [[ -z "${MAIL_BODY}" ]] ; then
+        debug "No mail message body - don't sending mail."
+        return
+    fi
+
+    local subject="Updating Minecraft server on $( fqdn -f ) to version '${UPSTREAM_VERSION}'."
+
+    MAIL_BODY="Update was starting: ${START_DATE}.\n\n${MAIL_BODY}\n\n"
+    MAIL_BODY+="With best regards\n\n"
+    MAIL_BODY+="Your ${BASE_NAME}"
+
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        local msg="Following mail should be sent:\n"
+        msg+="From: <${MAIL_FROM_ADDRESS}>\n"
+        msg+="To: <${MAIL_TO_ADDRESS}>\n"
+        msg+="Return-path: <${MAIL_RETURN_ADDRESS}>\n"
+        msg+="Subject: ${subject}\n\n"
+        msg+="${MAIL_BODY}"
+        info "${msg}"
+        return
+    fi
+
+    info "Sending mail to <${MAIL_TO_ADDRESS}>"
+    echo "${MAIL_BODY}" | mailx --return-address="${MAIL_RETURN_ADDRESS}" \
+        --append="From: ${MAIL_FROM_ADDRESS}" --subject="${subject}" "${MAIL_TO_ADDRESS}"
+
+}
+
 #------------------------------------------------------------------------------
 main() {
 
@@ -347,6 +411,8 @@ main() {
         restart_service
     fi
 
+    check_and_send_mail
+
 }
 
 main "$@"