@@ -6,7 +6,10 @@ import (
66 "fmt"
77 "io"
88 "os"
9+ "os/signal"
10+ "path/filepath"
911 "strings"
12+ "syscall"
1013 "time"
1114
1215 "github.com/docker/docker/api/types"
@@ -21,17 +24,23 @@ const (
2124)
2225
2326func SyncSystemLogs () {
24- ctx , cancel := context . WithTimeout (context .Background (), 30 * time . Second )
25- defer cancel ()
27+ rootCtx , stop := signal . NotifyContext (context .Background (), os . Interrupt , syscall . SIGTERM )
28+ defer stop ()
2629
2730 for {
31+ select {
32+ case <- rootCtx .Done ():
33+ return
34+ default :
35+ }
36+
2837 active , err := isLogSenderEnabled ()
2938 if err != nil {
3039 config .Logger ().ErrorF ("Error getting log sender config: %v" , err )
3140 }
3241
33- if active {
34- err := CollectAndShipSwarmLogs (ctx )
42+ if ! config . Updating && active {
43+ err := CollectAndShipSwarmLogs ()
3544 if err != nil {
3645 config .Logger ().ErrorF ("Error collecting and shipping logs: %v" , err )
3746 } else {
@@ -52,7 +61,10 @@ func isLogSenderEnabled() (bool, error) {
5261 return backConf [0 ].ConfParamValue == "true" , nil
5362}
5463
55- func CollectAndShipSwarmLogs (ctx context.Context ) error {
64+ func CollectAndShipSwarmLogs () error {
65+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
66+ defer cancel ()
67+
5668 cli , err := client .NewClientWithOpts (client .FromEnv , client .WithAPIVersionNegotiation ())
5769 if err != nil {
5870 return fmt .Errorf ("unable to create Docker client: %v" , err )
@@ -122,7 +134,39 @@ func createZip(
122134 }
123135 rc .Close ()
124136 }
137+ if info , err := os .Stat (config .EventProcessorLogsPath ); err == nil && info .IsDir () {
138+ if err := addDirToZip (zipWriter , config .EventProcessorLogsPath , "events-engine" ); err != nil {
139+ return fmt .Errorf ("error adding events-engine logs to zip: %v" , err )
140+ }
141+ }
142+
125143 return nil
126144}
127145
146+ func addDirToZip (zipWriter * zip.Writer , dirPath , baseInZip string ) error {
147+ return filepath .Walk (dirPath , func (path string , info os.FileInfo , err error ) error {
148+ if err != nil {
149+ return err
150+ }
151+ if info .IsDir () {
152+ return nil
153+ }
154+ relPath , err := filepath .Rel (dirPath , path )
155+ if err != nil {
156+ return err
157+ }
158+ zipEntry , err := zipWriter .Create (filepath .Join (baseInZip , relPath ))
159+ if err != nil {
160+ return err
161+ }
162+ file , err := os .Open (path )
163+ if err != nil {
164+ return err
165+ }
166+ defer file .Close ()
167+ _ , err = io .Copy (zipEntry , file )
168+ return err
169+ })
170+ }
171+
128172func sanitize (name string ) string { return strings .TrimPrefix (name , "/" ) }
0 commit comments