@@ -18,17 +18,21 @@ import (
1818)
1919
2020const (
21- logFileTimeFormat = "20060102-150405"
22- imageCustomizerCustomize = "customize"
23- imageCustomizerInject = "inject-files"
21+ logFileTimeFormat = "20060102-150405"
22+ imageCustomizerCustomizeCmd = "customize"
23+ imageCustomizerInjectCmd = "inject-files"
2424 // The Image Customizer log always goes to a debug-level log file - regardless
2525 // of the log level set for azldev.
2626 // The log level set for azldev controls what is shown on the console.
2727 defaultLogLevel = "debug"
28+
29+ entryPointSwitch = "--entrypoint"
30+ downloadAndCustomizeScriptPath = "/usr/local/bin/run.sh"
2831)
2932
3033type imageCustomizerOptions struct {
3134 imageFile string
35+ imageTag string
3236 configFile string
3337 outputImageFormat string
3438 outputPath string
@@ -52,15 +56,17 @@ func getImageCustomizerImageFormatsString() string {
5256}
5357
5458func buildDockerArgs (
55- buildDir , configDir , inputImageDir , logsDir , outputPathDir string , rpmSources []rpmSourceInfo ,
59+ options * imageCustomizerOptions , buildDir string , logsDir string , rpmSources []rpmSourceInfo ,
5660) []string {
61+ configDir := path .Dir (options .configFile )
62+ outputPathDir := path .Dir (options .outputPath )
63+
5764 args := []string {
5865 "run" , "--rm" ,
5966 docker .InteractiveFlag , // Ensure we get a TTY (not the default when run with --privileged)
6067 docker .PrivilegedFlag ,
6168 "-v" , buildDir + ":" + buildDir + docker .MountRWOption ,
6269 "-v" , configDir + ":" + configDir + docker .MountROOption ,
63- "-v" , inputImageDir + ":" + inputImageDir + docker .MountROOption ,
6470 "-v" , logsDir + ":" + logsDir + docker .MountRWOption ,
6571 "-v" , outputPathDir + ":" + outputPathDir + docker .MountRWOption ,
6672 "-v" , "/dev:/dev" ,
@@ -72,6 +78,17 @@ func buildDockerArgs(
7278 args = append (args , "-v" , rpmSource .dir + ":" + rpmSource .dir + docker .MountRWOption )
7379 }
7480
81+ if options .imageFile != "" {
82+ inputImageDir := path .Dir (options .imageFile )
83+ args = append (args , "-v" , inputImageDir + ":" + inputImageDir + docker .MountROOption )
84+ } else if options .imageTag != "" {
85+ // To be able to use the image tag, we need to set the entry point to
86+ // the script that will download the image first and then run the
87+ // Image Customizer after that. This overrides the default entry point
88+ // which calls the Image Customizer directly.
89+ args = append (args , entryPointSwitch , downloadAndCustomizeScriptPath )
90+ }
91+
7592 return args
7693}
7794
@@ -105,18 +122,32 @@ func buildRpmSourcesInfo(rpmSources []string) []rpmSourceInfo {
105122}
106123
107124func buildImageCustomizerArgs (
108- options * imageCustomizerOptions , buildDir , logFile , logLevel , logColor string , rpmSources []rpmSourceInfo ,
125+ options * imageCustomizerOptions , imageCustomizerCommand , buildDir , logFile , logLevel , logColor string ,
126+ rpmSources []rpmSourceInfo ,
109127) []string {
110- args := []string {
128+ args := []string {}
129+
130+ if options .imageTag != "" {
131+ // /usr/local/bin/run.sh expects the first argument to be the image tag.
132+ // It then passes the rest to the image customizer binary as before.
133+ args = append (args , options .imageTag )
134+ }
135+
136+ args = append (args , imageCustomizerCommand )
137+
138+ if options .imageFile != "" {
139+ args = append (args , "--image-file" , options .imageFile )
140+ }
141+
142+ args = append (args , []string {
111143 "--build-dir" , buildDir ,
112144 "--config-file" , options .configFile ,
113- "--image-file" , options .imageFile ,
114145 "--output-image-format" , options .outputImageFormat ,
115146 "--output-path" , options .outputPath ,
116147 "--log-level" , logLevel ,
117148 "--log-color" , logColor ,
118149 "--log-file" , logFile ,
119- }
150+ }... )
120151
121152 for _ , rpmSource := range rpmSources {
122153 args = append (args , "--rpm-source" , rpmSource .source )
@@ -134,10 +165,10 @@ func buildImageCustomizerArgs(
134165}
135166
136167func runImageCustomizerContainer (
137- env * azldev.Env , command string , options * imageCustomizerOptions ,
168+ env * azldev.Env , imageCustomizerCommand string , options * imageCustomizerOptions ,
138169) error {
139170 if env .DryRun () {
140- return fmt .Errorf ("dry-run mode is not supported for the 'image %s' command" , command )
171+ return fmt .Errorf ("dry-run mode is not supported for the 'image %s' command" , imageCustomizerCommand )
141172 }
142173
143174 containerTag := env .Config ().Tools .ImageCustomizer .ContainerTag
@@ -157,10 +188,6 @@ func runImageCustomizerContainer(
157188 logsDir = filepath .Join (env .ProjectDir (), projectgen .DefaultLogDir )
158189 }
159190
160- configDir := path .Dir (options .configFile )
161- inputImageDir := path .Dir (options .imageFile )
162- outputPathDir := path .Dir (options .outputPath )
163-
164191 timestamp := time .Now ().Format (logFileTimeFormat )
165192 logFile := path .Join (logsDir , fmt .Sprintf ("image-customizer-%s.log" , timestamp ))
166193 logMarkers := getImageCustomizerInfoMarkers ()
@@ -169,13 +196,13 @@ func runImageCustomizerContainer(
169196
170197 rpmSourcesInfo := buildRpmSourcesInfo (options .rpmSources )
171198
172- dockerArgs := buildDockerArgs (buildDir , configDir , inputImageDir , logsDir , outputPathDir , rpmSourcesInfo )
199+ dockerArgs := buildDockerArgs (options , buildDir , logsDir , rpmSourcesInfo )
173200
174201 imageCustomizerArgs := buildImageCustomizerArgs (
175- options , buildDir , logFile , defaultLogLevel , string (env .ColorMode ()), rpmSourcesInfo ,
176- )
202+ options , imageCustomizerCommand , buildDir , logFile , defaultLogLevel , string (env .ColorMode ()),
203+ rpmSourcesInfo )
177204
178- _ , err := docker .RunDocker (env .Context (), env , dockerArgs , containerTag , command , imageCustomizerArgs , logFile ,
205+ _ , err := docker .RunDocker (env .Context (), env , dockerArgs , containerTag , imageCustomizerArgs , logFile ,
179206 func (_ context.Context , line string ) {
180207 filterImageCustomizerOutput (env , line , logMarkers )
181208 },
0 commit comments