From 12fba57e6e9d0632d98e0ce609246ace4073134c Mon Sep 17 00:00:00 2001 From: linxiaozhi Date: Sat, 23 Dec 2023 00:28:28 +0800 Subject: [PATCH] fix file config update --- Dockerfile | 9 +++- cli/config.go | 104 ++++++++++++++----------------------- cli/daemon.go | 2 +- cli/file_config_handler.go | 64 ++++++++++++----------- core/common.go | 1 + go.mod | 41 +++++++++++++-- go.sum | 23 +++----- middlewares/save.go | 20 ++++--- 8 files changed, 139 insertions(+), 125 deletions(-) diff --git a/Dockerfile b/Dockerfile index d2dc826..098b808 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,10 @@ -FROM golang:1.17.1-alpine AS builder +FROM golang:1.17-alpine AS builder LABEL org.opencontainers.image.description Cron alternative for Docker Swarm enviornments. +RUN if [ "mirrors.ustc.edu.cn" != "" ] ; then sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories ; fi +RUN go env -w GOPROXY=https://proxy.golang.com.cn,https://goproxy.cn,direct + RUN apk --no-cache add gcc musl-dev WORKDIR ${GOPATH}/src/github.com/PremoWeb/Chadburn @@ -9,7 +12,9 @@ COPY . ${GOPATH}/src/github.com/PremoWeb/Chadburn RUN go build -o /go/bin/chadburn . -FROM alpine:3.15.0 +FROM alpine + +RUN if [ "mirrors.ustc.edu.cn" != "" ] ; then sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories ; fi RUN apk --update --no-cache add ca-certificates tzdata diff --git a/cli/config.go b/cli/config.go index db52b45..1434d63 100644 --- a/cli/config.go +++ b/cli/config.go @@ -3,8 +3,8 @@ package cli import ( "github.com/PremoWeb/Chadburn/core" "github.com/PremoWeb/Chadburn/middlewares" - defaults "github.com/mcuadros/go-defaults" + "github.com/mitchellh/hashstructure/v2" gcfg "gopkg.in/gcfg.v1" ) @@ -62,17 +62,17 @@ func BuildFromString(config string, logger core.Logger) (*Config, error) { } // Call this only once at app init -func (c *Config) InitializeApp(configFile string, dd bool) error { +func (c *Config) InitializeApp(daemon *DaemonCommand) error { c.sh = core.NewScheduler(c.logger) c.buildSchedulerMiddlewares(c.sh) var err error - c.configHandler, err = NewFileConfigHandler(configFile, c, c.logger) + c.configHandler, err = NewFileConfigHandler(daemon.ConfigFile, c, c.logger) if err != nil { return err } - if !dd { + if !daemon.DisableDocker { c.dockerHandler, err = NewDockerHandler(c, c.logger) if err != nil { return err @@ -120,13 +120,35 @@ func (c *Config) buildSchedulerMiddlewares(sh *core.Scheduler) { sh.Use(middlewares.NewGotify(&c.Global.GotifyConfig)) } -func (c *Config) updateExecJobs(newConfig *Config) { - c.Global = newConfig.Global - c.buildSchedulerMiddlewares(c.sh) +func (c *Config) updateExecJobs(newConfig *Config, isDockerLabels bool) { + isConfigClobalUpdate := false + if !isDockerLabels { + oldHash, err := hashstructure.Hash(c.Global, hashstructure.FormatV2, nil) + if err != nil { + c.logger.Errorf("config global old hash err:%s", err.Error()) + } + + newhash, newerr := hashstructure.Hash(newConfig.Global, hashstructure.FormatV2, nil) + if newerr != nil { + c.logger.Errorf("config global new hash err:%s", newerr.Error()) + } + + if oldHash != newhash { + isConfigClobalUpdate = true + } + + c.logger.Debugf("config global hash old:%d new:%d", oldHash, newhash) + + if isConfigClobalUpdate { + c.Global = newConfig.Global + c.buildSchedulerMiddlewares(c.sh) + } + } + // Calculate the delta for name, j := range c.ExecJobs { // this prevents deletion of jobs that were added by reading a configuration file - if j.FromDockerLabel { + if (isDockerLabels && !j.FromDockerLabel) || (!isDockerLabels && j.FromDockerLabel) { continue } @@ -141,7 +163,7 @@ func (c *Config) updateExecJobs(newConfig *Config) { defaults.SetDefaults(newJob) newJob.Client = c.dockerHandler.GetInternalDockerClient() newJob.Name = newJobsName - if newJob.Hash() != j.Hash() { + if newJob.Hash() != j.Hash() || isConfigClobalUpdate { // Remove from the scheduler c.sh.RemoveJob(j) // Add the job back to the scheduler @@ -185,66 +207,18 @@ func (c *Config) dockerLabelsUpdate(labels map[string]map[string]string) { var parsedLabelConfig Config parsedLabelConfig.buildFromDockerLabels(labels) - // Calculate the delta - for name, j := range c.ExecJobs { - // this prevents deletion of jobs that were added by reading a configuration file - if !j.FromDockerLabel { - continue - } - - found := false - for newJobsName, newJob := range parsedLabelConfig.ExecJobs { - // Check if the schedule has changed - if name == newJobsName { - found = true - // There is a slight race condition were a job can be canceled / restarted with different params - // so, lets take care of it by simply restarting - // For the hash to work properly, we must fill the fields before calling it - defaults.SetDefaults(newJob) - newJob.Client = c.dockerHandler.GetInternalDockerClient() - newJob.Name = newJobsName - if newJob.Hash() != j.Hash() { - // Remove from the scheduler - c.sh.RemoveJob(j) - // Add the job back to the scheduler - newJob.buildMiddlewares() - c.sh.AddJob(newJob) - // Update the job config - c.ExecJobs[name] = newJob - } - break - } - } - if !found { - // Remove the job - c.sh.RemoveJob(j) - delete(c.ExecJobs, name) - } - } - - // Check for aditions - for newJobsName, newJob := range parsedLabelConfig.ExecJobs { - found := false - for name := range c.ExecJobs { - if name == newJobsName { - found = true - break - } - } - if !found { - defaults.SetDefaults(newJob) - newJob.Client = c.dockerHandler.GetInternalDockerClient() - newJob.Name = newJobsName - newJob.buildMiddlewares() - c.sh.AddJob(newJob) - c.ExecJobs[newJobsName] = newJob - } - } + newConfig := NewConfig(c.logger) + newConfig.Global = parsedLabelConfig.Global + newConfig.ExecJobs = parsedLabelConfig.ExecJobs + newConfig.RunJobs = parsedLabelConfig.RunJobs + newConfig.LocalJobs = parsedLabelConfig.LocalJobs + newConfig.ServiceJobs = parsedLabelConfig.ServiceJobs + c.updateExecJobs(newConfig, true) } func (c *Config) fileConfigUpdate(newConfig *Config) { - c.updateExecJobs(newConfig) + c.updateExecJobs(newConfig, false) } // ExecJobConfig contains all configuration params needed to build a ExecJob diff --git a/cli/daemon.go b/cli/daemon.go index 506ad92..91379f9 100644 --- a/cli/daemon.go +++ b/cli/daemon.go @@ -48,7 +48,7 @@ func (c *DaemonCommand) boot() (err error) { c.Logger.Debugf("Cannot read config file: %q", err) } - err = config.InitializeApp(c.ConfigFile, c.DisableDocker) + err = config.InitializeApp(c) if err != nil { c.Logger.Criticalf("Can't start the app: %v", err) } diff --git a/cli/file_config_handler.go b/cli/file_config_handler.go index d41ab25..1cb8b1b 100644 --- a/cli/file_config_handler.go +++ b/cli/file_config_handler.go @@ -1,12 +1,8 @@ package cli import ( - "crypto/sha256" - "fmt" "github.com/PremoWeb/Chadburn/core" - "io" - "os" - "time" + "github.com/fsnotify/fsnotify" ) type FileConfigHandler struct { @@ -29,37 +25,43 @@ func NewFileConfigHandler(configFile string, notifier fileConfigUpdate, logger c } func (c *FileConfigHandler) watch() { - cfgHash := c.getCfgHash(c.ConfigFile) - tick := time.Tick(10000 * time.Millisecond) - for { - select { - case <-tick: - newCfgHash := c.getCfgHash(c.ConfigFile) - if cfgHash != newCfgHash { - c.logger.Debugf("config file has changed,old hash:%s,new hash:%s", cfgHash, newCfgHash) - config, err := BuildFromFile(c.ConfigFile, c.logger) - if err != nil { - c.logger.Debugf("Cannot read config file: %q", err) + watcher, err := fsnotify.NewWatcher() + if err != nil { + c.logger.Errorf("create watcher err:%s", err.Error()) + } + defer watcher.Close() + + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + + if (event.Has(fsnotify.Write) || event.Has(fsnotify.Create)) && event.Name == c.ConfigFile { + + c.logger.Debugf("config file has changed %s", event.Name) + + config, err := BuildFromFile(c.ConfigFile, c.logger) + if err != nil { + c.logger.Debugf("Cannot read config file: %q", err) + } + c.notifier.fileConfigUpdate(config) + } + case err, ok := <-watcher.Errors: + if !ok { + return } - c.notifier.fileConfigUpdate(config) - cfgHash = newCfgHash + c.logger.Errorf("watcher err:%s", err.Error()) } } - } -} + }() -func (c *FileConfigHandler) getCfgHash(filename string) string { - file, err := os.Open(filename) - defer file.Close() + err = watcher.Add(c.ConfigFile) if err != nil { - c.logger.Errorf("filename:%s,err:%s", filename, err.Error()) - } - - hash := sha256.New() - if _, err := io.Copy(hash, file); err != nil { - c.logger.Errorf("filename:%s,err:%s", filename, err.Error()) + c.logger.Errorf("watcher err:%s", err.Error()) } - sum := fmt.Sprintf("%x", hash.Sum(nil)) - return sum + select {} } diff --git a/core/common.go b/core/common.go index 61060c3..7feb276 100644 --- a/core/common.go +++ b/core/common.go @@ -201,6 +201,7 @@ func (c *middlewareContainer) Use(ms ...Middleware) { } t := reflect.TypeOf(m).String() + // 注释允许替换中间件 //if _, ok := c.m[t]; ok { // continue //} diff --git a/go.mod b/go.mod index 765c7a4..737f0a8 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/PremoWeb/Chadburn -go 1.15 +go 1.17 require ( github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2 @@ -9,6 +9,7 @@ require ( github.com/gobs/args v0.0.0-20180315064131-86002b4df18c github.com/jessevdk/go-flags v1.4.0 github.com/mcuadros/go-defaults v1.2.0 + github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/mitchellh/mapstructure v1.3.3 github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 github.com/robfig/cron/v3 v3.0.1 @@ -18,9 +19,43 @@ require ( ) require ( - github.com/docker/docker v1.4.2-0.20191101170500-ac7306503d23 + github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect + github.com/Microsoft/go-winio v0.4.15-0.20200113171025-3fe6c5262873 // indirect + github.com/Microsoft/hcsshim v0.8.7 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/containerd/containerd v1.3.0 // indirect + github.com/containerd/continuity v0.0.0-20200228182428-0f16d7a0959c // indirect + github.com/docker/distribution v2.7.1+incompatible // indirect + github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-units v0.4.0 // indirect + github.com/gogo/protobuf v1.3.1 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/gorilla/mux v1.7.4 // indirect + github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect + github.com/kr/text v0.1.0 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/morikuni/aec v1.0.0 // indirect + github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/opencontainers/go-digest v1.0.0-rc1 // indirect + github.com/opencontainers/image-spec v1.0.1 // indirect + github.com/opencontainers/runc v0.1.1 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/prometheus/client_golang v1.13.0 // indirect + github.com/prometheus/client_model v0.2.0 // indirect + github.com/prometheus/common v0.37.0 // indirect + github.com/prometheus/procfs v0.8.0 // indirect + github.com/sirupsen/logrus v1.6.0 // indirect + golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect + golang.org/x/sys v0.4.0 // indirect + google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect + google.golang.org/grpc v1.31.0 // indirect + google.golang.org/protobuf v1.28.1 // indirect +) + +require ( + github.com/docker/docker v1.4.2-0.20191101170500-ac7306503d23 + github.com/fsnotify/fsnotify v1.7.0 + github.com/prometheus/client_golang v1.13.0 gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/warnings.v0 v0.1.2 // indirect ) diff --git a/go.sum b/go.sum index 15f283c..57e97a7 100644 --- a/go.sum +++ b/go.sum @@ -94,6 +94,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/fsouza/go-dockerclient v1.6.5 h1:vuFDnPcds3LvTWGYb9h0Rty14FLgkjHZdwLDROCdgsw= github.com/fsouza/go-dockerclient v1.6.5/go.mod h1:GOdftxWLWIbIWKbIMDroKFJzPdg6Iw7r+jX1DDZdVsA= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -115,7 +117,6 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -129,7 +130,6 @@ github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= @@ -150,7 +150,6 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -195,7 +194,6 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8 github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -208,6 +206,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0j github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mcuadros/go-defaults v1.2.0 h1:FODb8WSf0uGaY8elWJAkoLL0Ri6AlZ1bFlenk56oZtc= github.com/mcuadros/go-defaults v1.2.0/go.mod h1:WEZtHEVIGYVDqkKSWBdWKUVdRyKlMfulPaGDWIVeCWY= +github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4= +github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE= github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8= github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -274,7 +274,6 @@ github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzG github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= @@ -306,8 +305,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975 h1:/Tl7pH94bvbAAHBdZJT947M/+gp0+CqQXDtMRC0fseo= golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -347,7 +346,6 @@ golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09 h1:KaQtG+aDELoNmXYas3TVkGNYRuq8JQ1aa7LJt8EXVyo= golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= @@ -384,7 +382,6 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -431,17 +428,16 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -494,7 +490,6 @@ golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= @@ -525,7 +520,6 @@ google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRn google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= @@ -557,7 +551,6 @@ google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= @@ -599,11 +592,11 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= diff --git a/middlewares/save.go b/middlewares/save.go index 174eec2..2f693cf 100644 --- a/middlewares/save.go +++ b/middlewares/save.go @@ -4,11 +4,11 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/PremoWeb/Chadburn/core" "io" "os" "path/filepath" - - "github.com/PremoWeb/Chadburn/core" + "time" ) // SaveConfig configuration for the Save middleware @@ -56,25 +56,29 @@ func (m *Save) Run(ctx *core.Context) error { func (m *Save) saveToDisk(ctx *core.Context) error { root := filepath.Join(m.SaveFolder, fmt.Sprintf( "%s_%s", - ctx.Execution.Date.Format("20060102_150405"), ctx.Job.GetName(), + ctx.Job.GetName(), ctx.Execution.Date.Format("20060102"), )) e := ctx.Execution - err := m.saveReaderToDisk(bytes.NewReader(e.ErrorStream.Bytes()), fmt.Sprintf("%s.stderr.log", root)) + err := m.saveReaderToDisk(bytes.NewReader(e.ErrorStream.Bytes()), fmt.Sprintf("%s.log", root)) if err != nil { return err } - err = m.saveReaderToDisk(bytes.NewReader(e.OutputStream.Bytes()), fmt.Sprintf("%s.stdout.log", root)) + err = m.saveReaderToDisk(bytes.NewReader([]byte(fmt.Sprintf("%s [%s] [%s]\n", time.Now().Format("2006-01-02 15:04:05.000"), ctx.Job.GetName(), ctx.Job.GetCommand()))), fmt.Sprintf("%s.log", root)) if err != nil { return err } - err = m.saveContextToDisk(ctx, fmt.Sprintf("%s.json", root)) + err = m.saveReaderToDisk(bytes.NewReader(e.OutputStream.Bytes()), fmt.Sprintf("%s.log", root)) if err != nil { return err } + err = m.saveContextToDisk(ctx, fmt.Sprintf("%s.json", root)) + if err != nil { + return err + } return nil } @@ -88,12 +92,12 @@ func (m *Save) saveContextToDisk(ctx *core.Context, filename string) error { } func (m *Save) saveReaderToDisk(r io.Reader, filename string) error { - f, err := os.Create(filename) + f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { return err } - defer f.Close() + if _, err := io.Copy(f, r); err != nil { return err }