Skip to content

Commit 2b1b635

Browse files
authored
Image Customizer: Add Initial MIC release file (#6594)
1 parent 631a180 commit 2b1b635

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

toolkit/tools/pkg/imagecustomizerlib/customizeutils.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"strconv"
12+
"time"
1213

1314
"github.com/microsoft/CBL-Mariner/toolkit/tools/imagecustomizerapi"
1415
"github.com/microsoft/CBL-Mariner/toolkit/tools/imagegen/installutils"
@@ -34,6 +35,8 @@ func doCustomizations(buildDir string, baseConfigPath string, config *imagecusto
3435
// Note: The ordering of the customization steps here should try to mirror the order of the equivalent steps in imager
3536
// tool as closely as possible.
3637

38+
buildTime := time.Now().Format("2006-01-02T15:04:05Z")
39+
3740
err = overrideResolvConf(imageChroot)
3841
if err != nil {
3942
return err
@@ -69,6 +72,11 @@ func doCustomizations(buildDir string, baseConfigPath string, config *imagecusto
6972
return err
7073
}
7174

75+
err = addCustomizerRelease(imageChroot, ToolVersion, buildTime)
76+
if err != nil {
77+
return err
78+
}
79+
7280
err = runScripts(baseConfigPath, config.SystemConfig.PostInstallScripts, imageChroot)
7381
if err != nil {
7482
return err
@@ -347,3 +355,23 @@ func loadOrDisableModules(modules imagecustomizerapi.Modules, imageChroot *safec
347355

348356
return nil
349357
}
358+
359+
func addCustomizerRelease(imageChroot *safechroot.Chroot, toolVersion string, buildTime string) error {
360+
var err error
361+
362+
logger.Log.Infof("Creating image customizer release file")
363+
364+
customizerReleaseFilePath := filepath.Join(imageChroot.RootDir(), "/etc/mariner-customizer-release")
365+
lines := []string{
366+
fmt.Sprintf("%s=\"%s\"", "TOOL_VERSION", toolVersion),
367+
fmt.Sprintf("%s=\"%s\"", "BUILD_DATE", buildTime),
368+
"",
369+
}
370+
371+
err = file.WriteLines(lines, customizerReleaseFilePath)
372+
if err != nil {
373+
return fmt.Errorf("error writing customizer release file (%s): %w", customizerReleaseFilePath, err)
374+
}
375+
376+
return nil
377+
}

toolkit/tools/pkg/imagecustomizerlib/customizeutils_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
package imagecustomizerlib
55

66
import (
7+
"bufio"
78
"os"
89
"path/filepath"
10+
"strings"
911
"testing"
12+
"time"
1013

1114
"github.com/microsoft/CBL-Mariner/toolkit/tools/imagecustomizerapi"
1215
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/ptrutils"
@@ -98,3 +101,47 @@ func TestCopyAdditionalFiles(t *testing.T) {
98101
assert.Equal(t, orig_contents, copy_1_contents)
99102
assert.Equal(t, orig_contents, copy_2_contents)
100103
}
104+
105+
func TestAddCustomizerRelease(t *testing.T) {
106+
if os.Geteuid() != 0 {
107+
t.Skip("Test must be run as root because it uses a chroot")
108+
}
109+
110+
proposedDir := filepath.Join(tmpDir, "TestAddCustomizerRelease")
111+
chroot := safechroot.NewChroot(proposedDir, false)
112+
err := chroot.Initialize("", []string{}, []*safechroot.MountPoint{})
113+
assert.NoError(t, err)
114+
defer chroot.Close(false)
115+
116+
err = os.MkdirAll(filepath.Join(chroot.RootDir(), "etc"), os.ModePerm)
117+
assert.NoError(t, err)
118+
119+
expectedVersion := "0.1.0"
120+
expectedDate := time.Now().Format("2006-01-02T15:04:05Z")
121+
err = addCustomizerRelease(chroot, expectedVersion, expectedDate)
122+
assert.NoError(t, err)
123+
124+
releaseFilePath := filepath.Join(chroot.RootDir(), "etc/mariner-customizer-release")
125+
126+
file, err := os.Open(releaseFilePath)
127+
if err != nil {
128+
t.Fatalf("Failed to open file: %v", err)
129+
}
130+
defer file.Close()
131+
132+
scanner := bufio.NewScanner(file)
133+
config := make(map[string]string)
134+
for scanner.Scan() {
135+
line := scanner.Text()
136+
if line == "" {
137+
continue
138+
}
139+
parts := strings.Split(line, "=")
140+
key := parts[0]
141+
value := strings.Trim(parts[1], "\"")
142+
config[key] = value
143+
}
144+
145+
assert.Equal(t, expectedVersion, config["TOOL_VERSION"])
146+
assert.Equal(t, expectedDate, config["BUILD_DATE"])
147+
}

0 commit comments

Comments
 (0)