44package image
55
66import (
7+ "context"
78 "errors"
89 "fmt"
910 "path"
11+ "path/filepath"
1012 "strings"
13+ "time"
1114
1215 "github.com/microsoft/azldev/internal/app/azldev"
16+ "github.com/microsoft/azldev/internal/projectgen"
1317 "github.com/microsoft/azldev/internal/utils/docker"
1418)
1519
1620const (
21+ logFileTimeFormat = "20060102-150405"
1722 imageCustomizerCustomize = "customize"
1823 imageCustomizerInject = "inject-files"
24+ // The Image Customizer log always goes to a debug-level log file - regardless
25+ // of the log level set for azldev.
26+ // The log level set for azldev controls what is shown on the console.
27+ defaultLogLevel = "debug"
1928)
2029
2130type imageCustomizerOptions struct {
@@ -28,17 +37,6 @@ type imageCustomizerOptions struct {
2837 packageSnapshotTime string
2938}
3039
31- func getLogLevel (env * azldev.Env ) string {
32- switch {
33- case env .Verbose ():
34- return "debug"
35- case env .Quiet ():
36- return "error"
37- default :
38- return "info"
39- }
40- }
41-
4240func getImageCustomizerImageFormats () []string {
4341 // This is temporarily hardcoded until there is a dynamic way of getting the supported formats.
4442 // Tracking Item: https://dev.azure.com/mariner-org/polar/_workitems/edit/15309
@@ -54,7 +52,7 @@ func getImageCustomizerImageFormatsString() string {
5452}
5553
5654func buildDockerArgs (
57- buildDir , configDir , inputImageDir , outputPathDir string , rpmSources []rpmSourceInfo ,
55+ buildDir , configDir , inputImageDir , logsDir , outputPathDir string , rpmSources []rpmSourceInfo ,
5856) []string {
5957 args := []string {
6058 "run" , "--rm" ,
@@ -63,6 +61,7 @@ func buildDockerArgs(
6361 "-v" , buildDir + ":" + buildDir + docker .MountRWOption ,
6462 "-v" , configDir + ":" + configDir + docker .MountROOption ,
6563 "-v" , inputImageDir + ":" + inputImageDir + docker .MountROOption ,
64+ "-v" , logsDir + ":" + logsDir + docker .MountRWOption ,
6665 "-v" , outputPathDir + ":" + outputPathDir + docker .MountRWOption ,
6766 "-v" , "/dev:/dev" ,
6867 }
@@ -106,7 +105,7 @@ func buildRpmSourcesInfo(rpmSources []string) []rpmSourceInfo {
106105}
107106
108107func buildImageCustomizerArgs (
109- options * imageCustomizerOptions , buildDir , logLevel , logColor string , rpmSources []rpmSourceInfo ,
108+ options * imageCustomizerOptions , buildDir , logFile , logLevel , logColor string , rpmSources []rpmSourceInfo ,
110109) []string {
111110 args := []string {
112111 "--build-dir" , buildDir ,
@@ -116,6 +115,7 @@ func buildImageCustomizerArgs(
116115 "--output-path" , options .outputPath ,
117116 "--log-level" , logLevel ,
118117 "--log-color" , logColor ,
118+ "--log-file" , logFile ,
119119 }
120120
121121 for _ , rpmSource := range rpmSources {
@@ -145,27 +145,44 @@ func runImageCustomizerContainer(
145145 return errors .New ("the Image Customizer container tag is not set in the project configuration" )
146146 }
147147
148+ // Because buildDir and logsDir will be mapped to the container, they
149+ // cannot be empty.
148150 buildDir := env .WorkDir ()
151+ if buildDir == "" {
152+ buildDir = filepath .Join (env .ProjectDir (), projectgen .DefaultWorkDir )
153+ }
154+
155+ logsDir := env .LogsDir ()
156+ if logsDir == "" {
157+ logsDir = filepath .Join (env .ProjectDir (), projectgen .DefaultLogDir )
158+ }
159+
149160 configDir := path .Dir (options .configFile )
150161 inputImageDir := path .Dir (options .imageFile )
151162 outputPathDir := path .Dir (options .outputPath )
152163
164+ timestamp := time .Now ().Format (logFileTimeFormat )
165+ logFile := path .Join (logsDir , fmt .Sprintf ("image-customizer-%s.log" , timestamp ))
166+ logMarkers := getImageCustomizerInfoMarkers ()
167+
168+ env .Event ("Saving detailed logs to: " + logFile )
169+
153170 rpmSourcesInfo := buildRpmSourcesInfo (options .rpmSources )
154171
155- dockerArgs := buildDockerArgs (buildDir , configDir , inputImageDir , outputPathDir , rpmSourcesInfo )
172+ dockerArgs := buildDockerArgs (buildDir , configDir , inputImageDir , logsDir , outputPathDir , rpmSourcesInfo )
156173
157174 imageCustomizerArgs := buildImageCustomizerArgs (
158- options , buildDir , getLogLevel ( env ) , string (env .ColorMode ()), rpmSourcesInfo ,
175+ options , buildDir , logFile , defaultLogLevel , string (env .ColorMode ()), rpmSourcesInfo ,
159176 )
160177
161- output , err := docker .RunDocker (env .Context (), env , dockerArgs , containerTag , command , imageCustomizerArgs )
178+ _ , err := docker .RunDocker (env .Context (), env , dockerArgs , containerTag , command , imageCustomizerArgs , logFile ,
179+ func (_ context.Context , line string ) {
180+ filterImageCustomizerOutput (env , line , logMarkers )
181+ },
182+ )
162183 if err != nil {
163- fmt .Printf ("%s\n " , output )
164-
165- return fmt .Errorf ("failed to customize image:\n %w" , err )
184+ return fmt .Errorf ("failed to customize image: %w" , err )
166185 }
167186
168- fmt .Printf ("%s\n " , output )
169-
170187 return nil
171188}
0 commit comments