Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/lint-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Lint Check

on:
pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.24
- name: Install shfmt and Lint
run: |
make shfmt-install
make shfmt
- name: Check Lint Result
run: |
changes=$(git status --porcelain)
if [ -n "$changes" ]; then
echo "Lint check failed, unformatted files:"
echo "--------------------------------------------------"
echo "$changes"
echo "--------------------------------------------------"
exit 1
else
echo "Lint check passed"
fi
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 - 2022 Matrix Origin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
ROOT_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

.PHONY: shfmt
shfmt:
@cd $(ROOT_DIR) && shfmt -ci -kp -sr -bn -i 4 -w -l ./

.PHONY: shfmt-install
shfmt-install:
@if which go >/dev/null 2>&1; then \
echo "Installing shfmt using go..."; \
go install mvdan.cc/sh/v3/cmd/shfmt@v3.11.0; \
elif which apt-get >/dev/null 2>&1; then \
echo "Installing shfmt using apt..."; \
sudo apt-get update && sudo apt-get install -y shfmt; \
else \
echo "Error: No supported package manager found. Please install shfmt manually."; \
exit 1; \
fi
221 changes: 95 additions & 126 deletions bin/auto_backup.sh

Large diffs are not rendered by default.

49 changes: 20 additions & 29 deletions bin/auto_clean_logs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ CLEAN_LOGS_CRON_USER=""
CLEAN_LOGS_CRON_SCRIPT="/usr/local/bin/mo_ctl clean_logs"
OS=""

function clean_logs()
{
clean_sysdb_logs_date=`date -d "@$(($(date +%s) - CLEAN_LOGS_DAYS_BEFORE * 86400))" +%Y%m%d`
function clean_logs() {
clean_sysdb_logs_date=$(date -d "@$(($(date +%s) - CLEAN_LOGS_DAYS_BEFORE * 86400))" +%Y%m%d)
add_log "I" "CLEAN_LOGS_DAYS_BEFORE: ${CLEAN_LOGS_DAYS_BEFORE}, clean date: ${clean_sysdb_logs_date}"
for table in $(echo "${CLEAN_LOGS_TABLE_LIST}" | sed "s/,/ /g"); do
sql="select PURGE_LOG('${table}', '${clean_sysdb_logs_date}');"
Expand All @@ -23,8 +22,7 @@ function clean_logs()
done
}

function auto_clean_logs_status()
{
function auto_clean_logs_status() {
rc=0

if [[ "${OS}" == "Mac" ]]; then
Expand All @@ -35,10 +33,10 @@ function auto_clean_logs_status()
# 2. Linux
if [[ -f ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME} ]]; then
add_log "D" "Cron file ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME} for ${acl_name} already exists, trying to get content: "
acl_content=`cat ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME}`
acl_content=$(cat ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME})
add_log "D" "${acl_content}"
if [[ "${acl_content}" == "" ]];then
add_log "E" "Content seems to be empty, something might be wrong when enabling ${acl_name}."
if [[ "${acl_content}" == "" ]]; then
add_log "E" "Content seems to be empty, something might be wrong when enabling ${acl_name}."
add_log "D" "The correct content should be: ${CLEAN_LOGS_CRON_CONTENT}"
add_log "I" "${acl_name} status:disabled"
rc=1
Expand All @@ -49,41 +47,39 @@ function auto_clean_logs_status()
add_log "I" "${acl_name} status:disabled"
rc=1
fi

fi

return ${rc}
}

function auto_clean_logs_enable()
{
function auto_clean_logs_enable() {
if [[ "${os}" == "Mac" ]]; then
add_log "E" "Currently ${acl_name} is not supported on MacOS system"
fi


if ! check_cron_service; then
return 1
fi

if ! auto_clean_logs_status; then
if ! auto_clean_logs_status; then
add_log "I" "Enabling ${acl_name}"

add_log "D" "Creating log folder: mkdir -p ${TOOL_LOG_PATH}/${acl_name}/"
mkdir -p ${TOOL_LOG_PATH}/${acl_name}/

if [[ "${OS}" == "Mac" ]]; then
# 1. Mac
# to do
:
else
else
# 2. Linux
add_log "I" "Creating cron file ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME} for ${acl_name}"
add_log "D" "Content: ${CLEAN_LOGS_CRON_CONTENT}"

if sudo touch ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME} && sudo chown ${CLEAN_LOGS_CRON_USER} ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME} && sudo echo "${CLEAN_LOGS_CRON_CONTENT}"> ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME} ; then
if sudo touch ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME} && sudo chown ${CLEAN_LOGS_CRON_USER} ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME} && sudo echo "${CLEAN_LOGS_CRON_CONTENT}" > ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME}; then
add_log "I" "Succeeded"
sudo chown root:root ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME}
sudo chown root:root ${CLEAN_LOGS_CRON_PATH}/${CLEAN_LOGS_CRON_FILE_NAME}
else
add_log "E" "Failed"
return 1
Expand All @@ -97,10 +93,9 @@ function auto_clean_logs_enable()

}

function auto_clean_logs_disable()
{
function auto_clean_logs_disable() {
if auto_clean_logs_status; then
if [[ "${OS}" == "Mac" ]]; then
if [[ "${OS}" == "Mac" ]]; then
# 1. Mac
# to do
:
Expand All @@ -122,19 +117,16 @@ function auto_clean_logs_disable()
fi
}


function auto_clean_logs()
{
function auto_clean_logs() {
option="$1"
OS=`what_os`
CLEAN_LOGS_CRON_USER=`whoami`
# backup cron file and its content
OS=$(what_os)
CLEAN_LOGS_CRON_USER=$(whoami)

# backup cron file and its content
#yearmonth_expr="\$(date '+\\%Y\\%m\\%d_\\%H\\%M\\%S')"
datetime_expr="\$(date '+\\%Y\\%m\\%d_\\%H\\%M\\%S')"
CLEAN_LOGS_CRON_CONTENT="${CLEAN_LOGS_CRON_SCHEDULE} ${CLEAN_LOGS_CRON_USER} ${CLEAN_LOGS_CRON_SCRIPT} > ${TOOL_LOG_PATH}/${acl_name}/${datetime_expr}.log 2>&1"


case "${option}" in
"" | "status")
auto_clean_logs_status
Expand All @@ -152,5 +144,4 @@ function auto_clean_logs()
;;
esac


}
44 changes: 15 additions & 29 deletions bin/auto_logrotate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,34 @@ MO_LOG_RESERVE_MAX_NUM="100000"
OS=""
LOG_SPLIT_STRATEGY=""

function auto_log_rotate_precheck()
{
function auto_log_rotate_precheck() {
# 1. Mac
if [[ "${OS}" == "Mac" ]]; then

add_log "E" "Auto log rotate on Mac is not yet supported"
return 1
# 2. Linux
else
# 1. check logrotate cmd
add_log "I" "Check if logrotate command exists"
flag=1
if which logrotate > /dev/null 2>&1 ; then
if which logrotate > /dev/null 2>&1; then
add_log "I" "Command 'which logrotate' found logrotate"
elif [[ -f /usr/sbin/logrotate ]] ; then
elif [[ -f /usr/sbin/logrotate ]]; then
add_log "I" "Command 'which logrotate' did not found logrotate, but found file '/usr/sbin/logrotate'"
else
add_log "E" "Either command 'which logrotate' nor file '/usr/sbin/logrotate' is found, exiting"
return 1
fi

# 2. Check sudo privilege
current_user=`whoami`
current_user=$(whoami)
add_log "I" "Check if current user has sudo command, you may need to enter password for current user"
if ! sudo ls /root >/dev/null 2>&1 ; then
if ! sudo ls /root > /dev/null 2>&1; then
add_log "E" "Failed to execute cmd 'sudo ls /root' with current user '${current_user}', please make sure current user has sudo privilege"
return 1
fi


# 3. Check MO_LOG_PATH conf
add_log "I" "Check related confs as below:"
get_conf | grep -E "MO_LOG_AUTO_SPLIT|MO_LOG_MAX_SIZE|MO_LOG_RESERVE_NUM"
Expand Down Expand Up @@ -73,13 +71,12 @@ function auto_log_rotate_precheck()
fi
}

function auto_log_rotate_status()
{
function auto_log_rotate_status() {
rc=0

if [[ -s ${LOG_ROTATE_CRON_PATH}/${LOG_ROTATE_CRON_FILE_NAME} ]]; then
add_log "D" "Cron file ${LOG_ROTATE_CRON_PATH}/${LOG_ROTATE_CRON_FILE_NAME} for ${alr_name} already exists, trying to get content: "
acl_content=`cat ${LOG_ROTATE_CRON_PATH}/${LOG_ROTATE_CRON_FILE_NAME}`
acl_content=$(cat ${LOG_ROTATE_CRON_PATH}/${LOG_ROTATE_CRON_FILE_NAME})
add_log "D" "${acl_content}"
add_log "I" "${alr_name} status:enabled"
else
Expand All @@ -91,9 +88,7 @@ function auto_log_rotate_status()
return ${rc}
}


function auto_log_rotate_enable()
{
function auto_log_rotate_enable() {
if ! auto_log_rotate_precheck; then
return 1
fi
Expand Down Expand Up @@ -153,17 +148,14 @@ EOF
add_log "D" "sudo chmod 644 ${LOG_ROTATE_CRON_PATH}/${LOG_ROTATE_CRON_FILE_NAME}"
sudo chmod 644 ${LOG_ROTATE_CRON_PATH}/${LOG_ROTATE_CRON_FILE_NAME}

if ! auto_log_rotate_status; then
if ! auto_log_rotate_status; then
add_log "Failed to enable auto_log_rotate"
return 1
fi

}



function auto_log_rotate_disable()
{
function auto_log_rotate_disable() {
if ! auto_log_rotate_precheck; then
return 1
fi
Expand All @@ -173,7 +165,6 @@ function auto_log_rotate_disable()
return 0
fi


add_log "I" "Disabling ${alr_name} by removing cron file ${LOG_ROTATE_CRON_PATH}/${LOG_ROTATE_CRON_FILE_NAME}"
if cd ${LOG_ROTATE_CRON_PATH} && sudo rm -f ./${LOG_ROTATE_CRON_FILE_NAME}; then
add_log "I" "Succeeded"
Expand All @@ -182,22 +173,18 @@ function auto_log_rotate_disable()
return 1
fi

if auto_log_rotate_status; then
if auto_log_rotate_status; then
add_log "E" "Failed to disable auto_log_rotate"
return 1
fi

}

function auto_log_rotate() {



function auto_log_rotate()
{

option="$1"
OS=`what_os`
CLEAN_LOGS_CRON_USER=`whoami`
OS=$(what_os)
CLEAN_LOGS_CRON_USER=$(whoami)

add_log "D" "Current OS: ${OS}"

Expand All @@ -218,5 +205,4 @@ function auto_log_rotate()
;;
esac


}
Loading
Loading