@@ -6,9 +6,14 @@ package servicedeployer
66
77import (
88 "fmt"
9+ "io/ioutil"
10+ "os"
11+ "path/filepath"
12+ "time"
913
1014 "github.com/pkg/errors"
1115
16+ "github.com/elastic/elastic-package/internal/builder"
1217 "github.com/elastic/elastic-package/internal/compose"
1318 "github.com/elastic/elastic-package/internal/docker"
1419 "github.com/elastic/elastic-package/internal/files"
@@ -151,15 +156,19 @@ func (s *dockerComposeDeployedService) TearDown() error {
151156 return errors .Wrap (err , "could not create Docker Compose project for service" )
152157 }
153158
154- downOptions := compose.CommandOptions {
159+ containerLogs , err := p . Logs ( compose.CommandOptions {
155160 Env : append (
156161 []string {fmt .Sprintf ("%s=%s" , serviceLogsDirEnv , s .ctxt .Logs .Folder .Local )},
157162 s .sv .Env ... ),
163+ })
164+ s .processServiceContainerLogs (containerLogs , err )
158165
159- // Remove associated volumes.
160- ExtraArgs : []string {"--volumes" },
161- }
162- if err := p .Down (downOptions ); err != nil {
166+ if err := p .Down (compose.CommandOptions {
167+ Env : append (
168+ []string {fmt .Sprintf ("%s=%s" , serviceLogsDirEnv , s .ctxt .Logs .Folder .Local )},
169+ s .sv .Env ... ),
170+ ExtraArgs : []string {"--volumes" }, // Remove associated volumes.
171+ }); err != nil {
163172 return errors .Wrap (err , "could not shut down service using Docker Compose" )
164173 }
165174 return nil
@@ -175,3 +184,41 @@ func (s *dockerComposeDeployedService) SetContext(ctxt ServiceContext) error {
175184 s .ctxt = ctxt
176185 return nil
177186}
187+
188+ func (s * dockerComposeDeployedService ) processServiceContainerLogs (content []byte , err error ) {
189+ if err != nil {
190+ logger .Errorf ("can't export service container logs: %v" , err )
191+ return
192+ }
193+
194+ if len (content ) == 0 {
195+ logger .Info ("service container hasn't written anything logs." )
196+ return
197+ }
198+
199+ err = s .writeServiceContainerLogs (content )
200+ if err != nil {
201+ logger .Errorf ("can't write service container logs: %v" , err )
202+ }
203+ }
204+
205+ func (s * dockerComposeDeployedService ) writeServiceContainerLogs (content []byte ) error {
206+ buildDir , err := builder .BuildDirectory ()
207+ if err != nil {
208+ return errors .Wrap (err , "locating build directory failed" )
209+ }
210+
211+ containerLogsDir := filepath .Join (buildDir , "container-logs" )
212+ err = os .MkdirAll (containerLogsDir , 0755 )
213+ if err != nil {
214+ return errors .Wrapf (err , "can't create directory for service container logs (path: %s)" , containerLogsDir )
215+ }
216+
217+ containerLogsFilepath := filepath .Join (containerLogsDir , fmt .Sprintf ("%s-%d.log" , s .ctxt .Name , time .Now ().UnixNano ()))
218+ logger .Infof ("Write container logs to file: %s" , containerLogsFilepath )
219+ err = ioutil .WriteFile (containerLogsFilepath , content , 0644 )
220+ if err != nil {
221+ return errors .Wrapf (err , "can't write container logs to file (path: %s)" , containerLogsFilepath )
222+ }
223+ return nil
224+ }
0 commit comments