upgdate log rotation

This commit is contained in:
2026-03-25 09:29:17 +02:00
parent fe2376b615
commit 8e7a0fffa7
9 changed files with 88 additions and 68 deletions
+6 -4
View File
@@ -1,7 +1,7 @@
FROM alpine:3.20 AS builder
FROM alpine:3.23 AS builder
RUN apk --no-cache add make binutils gcc libc-dev automake autoconf curl
RUN curl -o /usr/local/lib/go.tar.gz https://dl.google.com/go/go1.25.5.linux-amd64.tar.gz
RUN curl -o /usr/local/lib/go.tar.gz https://dl.google.com/go/go1.25.6.linux-amd64.tar.gz
RUN cd /usr/local/lib && tar xzf go.tar.gz
RUN cd /usr/local/bin && ln -sf ../lib/go/bin/* .
@@ -14,11 +14,13 @@ RUN make all install
RUN make clean
RUN rm -rf /app/src
FROM alpine:3.20 AS runner
FROM alpine:3.23 AS runner
COPY --from=builder /app /app
RUN chmod 1777 /var
RUN mkdir -p /app/etc/mstore
RUN touch /app/etc/mstore/mstored.yaml
WORKDIR /app
#USER daemon:daemon
ENTRYPOINT ["/app/sbin/mstored"]
ENTRYPOINT ["/app/sbin/mstored", "--asDaemon=false", "--port=1025"]
-3
View File
@@ -282,9 +282,6 @@ EXTRA_DIST = vendor/* \
docs/mstore.png \
docs/podman-manifest.json.txt
format:
@dirs=$$($(FIND) $(CWD)/app $(CWD)/cmd $(CWD)/pkg $(CWD)/test \
-name '*.go' | $(XARGS) -n1 dirname | $(SORT) | $(UNIQ)); \
+3 -3
View File
@@ -1,10 +1,10 @@
package config
const (
confdir = "/usr/local/etc/mstore"
confdir = "/etc/mstore"
rundir = "/var/run/mstore"
logdir = "/var/log/mstore"
datadir = "/var/lib/mstore"
version = "0.2.0"
srvname = "mstored"
version = "0.2.3"
srvname = "mstored"
)
+62 -30
View File
@@ -12,12 +12,14 @@ package server
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/signal"
"os/user"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"
@@ -38,16 +40,20 @@ import (
)
type Server struct {
conf *config.Config
fiop *fileoper.Operator
acop *accoper.Operator
imop *imageoper.Operator
svc *service.Service
mdb *maindb.Database
hand *handler.Handler
logg *logger.Logger
stor *storage.Storage
stat descr.Server
conf *config.Config
fiop *fileoper.Operator
acop *accoper.Operator
imop *imageoper.Operator
svc *service.Service
mdb *maindb.Database
hand *handler.Handler
logg *logger.Logger
stor *storage.Storage
stat descr.Server
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
logf *os.File
}
func NewServer() (*Server, error) {
@@ -97,7 +103,7 @@ func (srv *Server) Configure() error {
err = srv.conf.ReadConfigfile()
if err != nil {
srv.logg.Warningf("Error loading config file: %v", err)
//return err
err = nil
}
err = srv.conf.ReadX509Cert()
if err != nil {
@@ -327,6 +333,7 @@ func (srv *Server) Run() error {
}
srv.logg.Infof("Server run as user %s", currUser.Username)
srv.ctx, srv.cancel = context.WithCancel(context.Background())
svcDone := make(chan error, 1)
// Service run
@@ -341,17 +348,20 @@ func (srv *Server) Run() error {
go startService(srv.svc, svcDone)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
var signal os.Signal
select {
case signal = <-sigs:
srv.logg.Infof("Services stopped by signal: %v", signal)
srv.cancel()
srv.svc.Stop()
srv.wg.Wait()
case err = <-svcDone:
srv.logg.Infof("Service stopped by service error: %v", err)
srv.cancel()
srv.svc.Stop()
srv.wg.Wait()
}
return err
}
@@ -431,24 +441,7 @@ func (srv *Server) Daemonize() error {
if err != nil {
return err
}
// Log file rotator
logFunc := func() {
for {
stat, err := os.Stat(srv.conf.Logpath)
if err == nil && stat.Size() > srv.conf.LogLimit {
os.Rename(srv.conf.Logpath+".2", srv.conf.Logpath+".3")
os.Rename(srv.conf.Logpath+".1", srv.conf.Logpath+".2")
os.Rename(srv.conf.Logpath, srv.conf.Logpath+".1")
logFile.Close()
logFile, err = os.OpenFile(srv.conf.Logpath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
time.Sleep(10 * time.Second)
}
}
}
go logFunc()
srv.logf = logFile
// Write process ID
rundir := filepath.Dir(srv.conf.Runpath)
err = os.MkdirAll(rundir, 0750)
@@ -468,3 +461,42 @@ func (srv *Server) Daemonize() error {
}
return err
}
func (srv *Server) Rotator() {
srv.wg.Add(1)
var counter uint64
logFunc := func() {
for {
counter += 1
select {
case <-srv.ctx.Done():
srv.wg.Done()
srv.logg.Infof("Log file rotator done")
return
default:
}
if (counter % 60) == 1 {
stat, err := srv.logf.Stat()
if err == nil && stat.Size() > srv.conf.LogLimit {
srv.logg.Infof("Rotate log file")
countFiles := 3
for i := 1; i < countFiles; i++ {
nextName := fmt.Sprintf("%s.%d", srv.conf.Logpath, i+1)
prevName := fmt.Sprintf("%s.%d", srv.conf.Logpath, i)
os.Rename(prevName, nextName)
}
os.Rename(srv.conf.Logpath, srv.conf.Logpath+".1")
logFile, err := os.OpenFile(srv.conf.Logpath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err == nil {
syscall.Dup2(int(logFile.Fd()), int(os.Stdout.Fd()))
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
srv.logf.Close()
srv.logf = logFile
}
}
}
time.Sleep(1 * time.Second)
}
}
go logFunc()
}
+5 -5
View File
@@ -26,12 +26,12 @@ spec:
volumeMounts:
- name: config-volume
mountPath: /app/etc/mstore
- name: db-volume
mountPath: /var/lib
# - name: db-volume
# mountPath: /var/lib
volumes:
- name: config-volume
configMap:
name: mstored-config
- name: db-volume
persistentVolumeClaim:
claimName: mstore-data
# - name: db-volume
# persistentVolumeClaim:
# claimName: mstore-data
-12
View File
@@ -1,12 +0,0 @@
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mstore-data
spec:
storageClassName: {{ include "mstore.storageClass" . }}
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: {{ include "mstore.storageSize" . }}
+1
View File
@@ -67,6 +67,7 @@ func (sta *Starter) run(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
srv.Rotator()
err = srv.Run()
if err != nil {
return err
Vendored
+10 -10
View File
@@ -1,6 +1,6 @@
#! /bin/sh
# Guess values for system-dependent variables and create Makefiles.
# Generated by GNU Autoconf 2.72 for mstore 0.2.0.
# Generated by GNU Autoconf 2.72 for mstore 0.2.3.
#
#
# Copyright (C) 1992-1996, 1998-2017, 2020-2023 Free Software Foundation,
@@ -600,8 +600,8 @@ MAKEFLAGS=
# Identity of this package.
PACKAGE_NAME='mstore'
PACKAGE_TARNAME='mstore'
PACKAGE_VERSION='0.2.0'
PACKAGE_STRING='mstore 0.2.0'
PACKAGE_VERSION='0.2.3'
PACKAGE_STRING='mstore 0.2.3'
PACKAGE_BUGREPORT=''
PACKAGE_URL=''
@@ -1278,7 +1278,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing.
# This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF
'configure' configures mstore 0.2.0 to adapt to many kinds of systems.
'configure' configures mstore 0.2.3 to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]...
@@ -1349,7 +1349,7 @@ fi
if test -n "$ac_init_help"; then
case $ac_init_help in
short | recursive ) echo "Configuration of mstore 0.2.0:";;
short | recursive ) echo "Configuration of mstore 0.2.3:";;
esac
cat <<\_ACEOF
@@ -1436,7 +1436,7 @@ fi
test -n "$ac_init_help" && exit $ac_status
if $ac_init_version; then
cat <<\_ACEOF
mstore configure 0.2.0
mstore configure 0.2.3
generated by GNU Autoconf 2.72
Copyright (C) 2023 Free Software Foundation, Inc.
@@ -1473,7 +1473,7 @@ cat >config.log <<_ACEOF
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.
It was created by mstore $as_me 0.2.0, which was
It was created by mstore $as_me 0.2.3, which was
generated by GNU Autoconf 2.72. Invocation command line was
$ $0$ac_configure_args_raw
@@ -2612,7 +2612,7 @@ fi
# Define the identity of the package.
PACKAGE='mstore'
VERSION='0.2.0'
VERSION='0.2.3'
printf "%s\n" "#define PACKAGE \"$PACKAGE\"" >>confdefs.h
@@ -4448,7 +4448,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
# report actual input values of CONFIG_FILES etc. instead of their
# values after options handling.
ac_log="
This file was extended by mstore $as_me 0.2.0, which was
This file was extended by mstore $as_me 0.2.3, which was
generated by GNU Autoconf 2.72. Invocation command line was
CONFIG_FILES = $CONFIG_FILES
@@ -4503,7 +4503,7 @@ ac_cs_config_escaped=`printf "%s\n" "$ac_cs_config" | sed "s/^ //; s/'/'\\\\\\\\
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
ac_cs_config='$ac_cs_config_escaped'
ac_cs_version="\\
mstore config.status 0.2.0
mstore config.status 0.2.3
configured by $0, generated by GNU Autoconf 2.72,
with options \\"\$ac_cs_config\\"
+1 -1
View File
@@ -1,4 +1,4 @@
AC_INIT([mstore],[0.2.0])
AC_INIT([mstore],[0.2.4])
AM_INIT_AUTOMAKE([foreign subdir-objects tar-pax])
AC_PREFIX_DEFAULT(/usr/local)