Skip to content

Commit ccf0ac9

Browse files
prestistyasminvalimjmarreroc4rt0
committed
base/v0_6_exp: add parent directory sugar
Add a field called 'Parent' which is used to specify a file's parent directory. When a parent is specified, all directories from the parent to the file will be created, with the 'mode' supplied in the parent directory. resolves: coreos#380 Co-authored-by: Yasmin Valim <[email protected]> Co-authored-by: Joseph Corchado <[email protected]> Co-authored-by: Adam Piasecki <[email protected]>
1 parent d26d803 commit ccf0ac9

12 files changed

+313
-5
lines changed

base/v0_6_exp/schema.go

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ type File struct {
6767
Append []Resource `yaml:"append"`
6868
Contents Resource `yaml:"contents"`
6969
Mode *int `yaml:"mode"`
70+
Parent Parent `yaml:"parent"`
71+
}
72+
73+
type Parent struct {
74+
Path *string `yaml:"path,omitempty"`
75+
Mode *int `yaml:"mode,omitempty"`
7076
}
7177

7278
type Filesystem struct {

base/v0_6_exp/translate.go

+74-5
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ func (c Config) ToIgn3_5Unvalidated(options common.TranslateOptions) (types.Conf
8383

8484
tr := translate.NewTranslator("yaml", "json", options)
8585
tr.AddCustomTranslator(translateIgnition)
86-
tr.AddCustomTranslator(translateFile)
87-
tr.AddCustomTranslator(translateDirectory)
88-
tr.AddCustomTranslator(translateLink)
86+
tr.AddCustomTranslator(translateStorage)
8987
tr.AddCustomTranslator(translateResource)
9088
tr.AddCustomTranslator(translatePasswdUser)
9189
tr.AddCustomTranslator(translateUnit)
@@ -99,7 +97,6 @@ func (c Config) ToIgn3_5Unvalidated(options common.TranslateOptions) (types.Conf
9997
translate.MergeP(tr, tm, &r, "systemd", &c.Systemd, &ret.Systemd)
10098

10199
c.addMountUnits(&ret, &tm)
102-
103100
tm2, r2 := c.processTrees(&ret, options)
104101
tm.Merge(tm2)
105102
r.Merge(r2)
@@ -121,6 +118,62 @@ func translateIgnition(from Ignition, options common.TranslateOptions) (to types
121118
return
122119
}
123120

121+
func translateStorage(from Storage, options common.TranslateOptions) (to types.Storage, tm translate.TranslationSet, r report.Report) {
122+
tr := translate.NewTranslator("yaml", "json", options)
123+
tr.AddCustomTranslator(translateFile)
124+
tr.AddCustomTranslator(translateDirectory)
125+
tr.AddCustomTranslator(translateLink)
126+
tr.AddCustomTranslator(translateLuks)
127+
tm, r = translate.Prefixed(tr, "directories", &from.Directories, &to.Directories)
128+
translate.MergeP(tr, tm, &r, "disks", &from.Disks, &to.Disks)
129+
translate.MergeP(tr, tm, &r, "files", &from.Files, &to.Files)
130+
translate.MergeP(tr, tm, &r, "filesystems", &from.Filesystems, &to.Filesystems)
131+
translate.MergeP(tr, tm, &r, "links", &from.Links, &to.Links)
132+
translate.MergeP(tr, tm, &r, "luks", &from.Luks, &to.Luks)
133+
translate.MergeP(tr, tm, &r, "raid", &from.Raid, &to.Raid)
134+
for i, file := range from.Files {
135+
if util.NotEmpty(file.Parent.Path) {
136+
yamlPath := path.New("yaml", "files", i, "parent")
137+
138+
if !strings.Contains(file.Path, *file.Parent.Path) {
139+
r.AddOnError(yamlPath, common.ErrInvalidParent)
140+
continue
141+
}
142+
143+
dir := filepath.Dir(file.Path)
144+
// make sure to clean the path to avoid consistency issues
145+
146+
dir = filepath.Clean(dir)
147+
148+
for dir != "" {
149+
renderedDir := types.Directory{
150+
Node: types.Node{
151+
Path: dir,
152+
Group: types.NodeGroup{ID: file.Group.ID, Name: file.Group.Name},
153+
User: types.NodeUser{ID: file.User.ID, Name: file.User.Name},
154+
},
155+
DirectoryEmbedded1: types.DirectoryEmbedded1{
156+
Mode: file.Parent.Mode,
157+
},
158+
}
159+
to.Directories = append(to.Directories, renderedDir)
160+
nextDir, _ := filepath.Split(dir)
161+
// make sure to clean the path to avoid consistency issues
162+
nextDir = filepath.Clean(nextDir)
163+
nextDir = filepath.ToSlash(nextDir)
164+
if dir == *file.Parent.Path || nextDir == dir {
165+
// we have reached the parent directory or the end of the path
166+
break
167+
}
168+
dir = nextDir
169+
}
170+
tm.AddFromCommonSource(yamlPath, path.New("json", "directories"), to.Directories)
171+
}
172+
173+
}
174+
return
175+
}
176+
124177
func translateFile(from File, options common.TranslateOptions) (to types.File, tm translate.TranslationSet, r report.Report) {
125178
tr := translate.NewTranslator("yaml", "json", options)
126179
tr.AddCustomTranslator(translateResource)
@@ -134,6 +187,22 @@ func translateFile(from File, options common.TranslateOptions) (to types.File, t
134187
return
135188
}
136189

190+
func translateLuks(from Luks, options common.TranslateOptions) (to types.Luks, tm translate.TranslationSet, r report.Report) {
191+
tr := translate.NewTranslator("yaml", "json", options)
192+
tr.AddCustomTranslator(translateResource)
193+
tm, r = translate.Prefixed(tr, "clevis", &from.Clevis, &to.Clevis)
194+
translate.MergeP(tr, tm, &r, "device", &from.Device, &to.Device)
195+
translate.MergeP(tr, tm, &r, "discard", &from.Discard, &to.Discard)
196+
translate.MergeP2(tr, tm, &r, "key_file", &from.KeyFile, "keyFile", &to.KeyFile)
197+
translate.MergeP(tr, tm, &r, "label", &from.Label, &to.Label)
198+
translate.MergeP(tr, tm, &r, "name", &from.Name, &to.Name)
199+
translate.MergeP2(tr, tm, &r, "open_options", &from.OpenOptions, "openOptions", &to.OpenOptions)
200+
translate.MergeP(tr, tm, &r, "options", &from.Options, &to.Options)
201+
translate.MergeP(tr, tm, &r, "uuid", &from.UUID, &to.UUID)
202+
translate.MergeP2(tr, tm, &r, "wipe_volume", &from.WipeVolume, "wipeVolume", &to.WipeVolume)
203+
return
204+
}
205+
137206
func translateResource(from Resource, options common.TranslateOptions) (to types.Resource, tm translate.TranslationSet, r report.Report) {
138207
tr := translate.NewTranslator("yaml", "json", options)
139208
tm, r = translate.Prefixed(tr, "verification", &from.Verification, &to.Verification)
@@ -294,7 +363,7 @@ func (c Config) processTrees(ret *types.Config, options common.TranslateOptions)
294363
return ts, r
295364
}
296365
t := newNodeTracker(ret)
297-
366+
ts.AddTranslation(path.New("yaml", "storage"), path.New("json", "storage"))
298367
for i, tree := range c.Storage.Trees {
299368
yamlPath := path.New("yaml", "storage", "trees", i)
300369
if options.FilesDir == "" {

base/v0_6_exp/translate_test.go

+189
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,195 @@ func TestTranslateFile(t *testing.T) {
599599
})
600600
}
601601
}
602+
func TestTranslateStorage(t *testing.T) {
603+
tests := []struct {
604+
in Storage
605+
out types.Storage
606+
errPath path.ContextPath
607+
errors error
608+
skip func(t *testing.T)
609+
}{
610+
// Basic parent file directory
611+
{
612+
in: Storage{
613+
Files: []File{
614+
{
615+
Path: "/foo/bar/txt.txt",
616+
Contents: Resource{},
617+
Mode: util.IntToPtr(420),
618+
Parent: Parent{
619+
Path: util.StrToPtr("/foo"),
620+
Mode: util.IntToPtr(420),
621+
},
622+
},
623+
},
624+
},
625+
out: types.Storage{
626+
Files: []types.File{
627+
{
628+
Node: types.Node{
629+
Path: "/foo/bar/txt.txt",
630+
},
631+
FileEmbedded1: types.FileEmbedded1{
632+
Mode: util.IntToPtr(420),
633+
Contents: types.Resource{},
634+
},
635+
},
636+
},
637+
Directories: []types.Directory{
638+
{
639+
Node: types.Node{
640+
Path: "/foo/bar",
641+
},
642+
DirectoryEmbedded1: types.DirectoryEmbedded1{
643+
Mode: util.IntToPtr(420),
644+
},
645+
},
646+
{
647+
Node: types.Node{
648+
Path: "/foo",
649+
},
650+
DirectoryEmbedded1: types.DirectoryEmbedded1{
651+
Mode: util.IntToPtr(420),
652+
},
653+
},
654+
},
655+
},
656+
errPath: path.ContextPath{},
657+
errors: nil,
658+
skip: func(t *testing.T) {
659+
if runtime.GOOS == "windows" {
660+
t.Skip("skipping test on Windows")
661+
}
662+
},
663+
},
664+
// Empty parent file directory
665+
{
666+
in: Storage{
667+
Files: []File{
668+
{
669+
Path: "/foo/bar/txt.txt",
670+
Contents: Resource{},
671+
Mode: util.IntToPtr(420),
672+
Parent: Parent{
673+
Path: util.StrToPtr(""),
674+
Mode: util.IntToPtr(420),
675+
},
676+
},
677+
},
678+
},
679+
out: types.Storage{
680+
Files: []types.File{
681+
{
682+
Node: types.Node{
683+
Path: "/foo/bar/txt.txt",
684+
},
685+
FileEmbedded1: types.FileEmbedded1{
686+
Mode: util.IntToPtr(420),
687+
Contents: types.Resource{},
688+
},
689+
},
690+
},
691+
},
692+
errPath: path.ContextPath{},
693+
errors: nil,
694+
skip: func(t *testing.T) {
695+
if runtime.GOOS == "windows" {
696+
t.Skip("skipping test on Windows")
697+
}
698+
},
699+
},
700+
// Parent not defined
701+
{
702+
in: Storage{
703+
Files: []File{
704+
{
705+
Path: "/foo/bar/txt.txt",
706+
Contents: Resource{},
707+
Mode: util.IntToPtr(420),
708+
},
709+
},
710+
},
711+
out: types.Storage{
712+
Files: []types.File{
713+
{
714+
Node: types.Node{
715+
Path: "/foo/bar/txt.txt",
716+
},
717+
FileEmbedded1: types.FileEmbedded1{
718+
Mode: util.IntToPtr(420),
719+
Contents: types.Resource{},
720+
},
721+
},
722+
},
723+
},
724+
errPath: path.ContextPath{},
725+
errors: nil,
726+
skip: func(t *testing.T) {
727+
if runtime.GOOS == "windows" {
728+
t.Skip("skipping test on Windows")
729+
}
730+
},
731+
},
732+
// Parent path is not related to file path
733+
{
734+
in: Storage{
735+
Files: []File{
736+
{
737+
Path: "/foo/bar/txt.txt",
738+
Contents: Resource{},
739+
Mode: util.IntToPtr(420),
740+
Parent: Parent{
741+
Path: util.StrToPtr("/godzilla"),
742+
Mode: util.IntToPtr(420),
743+
},
744+
},
745+
},
746+
},
747+
out: types.Storage{
748+
Files: []types.File{
749+
{
750+
Node: types.Node{
751+
Path: "/foo/bar/txt.txt",
752+
},
753+
FileEmbedded1: types.FileEmbedded1{
754+
Mode: util.IntToPtr(420),
755+
Contents: types.Resource{},
756+
},
757+
},
758+
},
759+
},
760+
errPath: path.New("yaml", "files", 0, "parent"),
761+
errors: common.ErrInvalidParent,
762+
skip: func(t *testing.T) {
763+
if runtime.GOOS == "windows" {
764+
t.Skip("skipping test on Windows")
765+
}
766+
},
767+
},
768+
}
769+
770+
for i, test := range tests {
771+
t.Run(fmt.Sprintf("translate %d", i), func(t *testing.T) {
772+
if test.skip != nil {
773+
// give the test an opportunity to skip
774+
test.skip(t)
775+
}
776+
actual, translations, r := translateStorage(test.in, common.TranslateOptions{})
777+
r = confutil.TranslateReportPaths(r, translations)
778+
baseutil.VerifyReport(t, test.in, r)
779+
assert.Equal(t, test.out, actual, "translation mismatch")
780+
assert.NoError(t, translations.DebugVerifyCoverage(actual), "incomplete TranslationSet coverage")
781+
if test.errors != nil {
782+
expected := report.Report{}
783+
expected.AddOnError(test.errPath, test.errors)
784+
assert.Equal(t, expected, r, "bad report for test case %d", i)
785+
} else {
786+
assert.Equal(t, report.Report{}, r, "non-empty report")
787+
}
788+
})
789+
}
790+
}
602791

603792
// TestTranslateDirectory tests translating the ct storage.directories.[i] entries to ignition storage.directories.[i] entires.
604793
func TestTranslateDirectory(t *testing.T) {

config/common/errors.go

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ var (
8989
ErrLinkSupport = errors.New("links are not supported in this spec version")
9090
ErrLuksSupport = errors.New("luks is not supported in this spec version")
9191
ErrRaidSupport = errors.New("raid is not supported in this spec version")
92+
ErrInvalidParent = errors.New("parent must be included in the file path")
9293

9394
// Grub
9495
ErrGrubUserNameNotSpecified = errors.New("field \"name\" is required")

docs/config-fcos-v1_6-exp.md

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ The Fedora CoreOS configuration is a YAML document conforming to the following s
117117
* **_group_** (object): specifies the file's group.
118118
* **_id_** (integer): the group ID of the group.
119119
* **_name_** (string): the group name of the group.
120+
* **_parent_** (object): the parent directory for the specified file, by declaring a parent the directories from the parent to the file's target destination.
121+
* **_path_** (string): the path of the directory within the file's 'path'.
122+
* **_mode_** (integer): directory modes are set to 0755 as a default if not specified and directory does not exist prior to the specified file.
120123
* **_directories_** (list of objects): the list of directories to be created. Every file, directory, and link must have a unique `path`.
121124
* **path** (string): the absolute path to the directory.
122125
* **_overwrite_** (boolean): whether to delete preexisting nodes at the path. If false and a directory already exists at the path, Ignition will only set its permissions. If false and a non-directory exists at that path, Ignition will fail. Defaults to false.

docs/config-fiot-v1_1-exp.md

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ The Fedora IoT configuration is a YAML document conforming to the following spec
8888
* **_group_** (object): specifies the file's group.
8989
* **_id_** (integer): the group ID of the group.
9090
* **_name_** (string): the group name of the group.
91+
* **_parent_** (object): the parent directory for the specified file, by declaring a parent the directories from the parent to the file's target destination.
92+
* **_path_** (string): the path of the directory within the file's 'path'.
93+
* **_mode_** (integer): directory modes are set to 0755 as a default if not specified and directory does not exist prior to the specified file.
9194
* **_directories_** (list of objects): the list of directories to be created. Every file, directory, and link must have a unique `path`.
9295
* **path** (string): the absolute path to the directory.
9396
* **_overwrite_** (boolean): whether to delete preexisting nodes at the path. If false and a directory already exists at the path, Ignition will only set its permissions. If false and a non-directory exists at that path, Ignition will fail. Defaults to false.

docs/config-flatcar-v1_2-exp.md

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ The Flatcar configuration is a YAML document conforming to the following specifi
117117
* **_group_** (object): specifies the file's group.
118118
* **_id_** (integer): the group ID of the group.
119119
* **_name_** (string): the group name of the group.
120+
* **_parent_** (object): the parent directory for the specified file, by declaring a parent the directories from the parent to the file's target destination.
121+
* **_path_** (string): the path of the directory within the file's 'path'.
122+
* **_mode_** (integer): directory modes are set to 0755 as a default if not specified and directory does not exist prior to the specified file.
120123
* **_directories_** (list of objects): the list of directories to be created. Every file, directory, and link must have a unique `path`.
121124
* **path** (string): the absolute path to the directory.
122125
* **_overwrite_** (boolean): whether to delete preexisting nodes at the path. If false and a directory already exists at the path, Ignition will only set its permissions. If false and a non-directory exists at that path, Ignition will fail. Defaults to false.

docs/config-openshift-v4_17-exp.md

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ The OpenShift configuration is a YAML document conforming to the following speci
107107
* **_group_** (object): specifies the file's group.
108108
* **_id_** (integer): the group ID of the group.
109109
* **_name_** (string): the group name of the group.
110+
* **_parent_** (object): the parent directory for the specified file, by declaring a parent the directories from the parent to the file's target destination.
111+
* **_path_** (string): the path of the directory within the file's 'path'.
112+
* **_mode_** (integer): directory modes are set to 0755 as a default if not specified and directory does not exist prior to the specified file.
110113
* **_luks_** (list of objects): the list of luks devices to be created. Every device must have a unique `name`.
111114
* **name** (string): the name of the luks device.
112115
* **device** (string): the absolute path to the device. Devices are typically referenced by the `/dev/disk/by-*` symlinks.

docs/config-r4e-v1_2-exp.md

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ The RHEL for Edge configuration is a YAML document conforming to the following s
8888
* **_group_** (object): specifies the file's group.
8989
* **_id_** (integer): the group ID of the group.
9090
* **_name_** (string): the group name of the group.
91+
* **_parent_** (object): the parent directory for the specified file, by declaring a parent the directories from the parent to the file's target destination.
92+
* **_path_** (string): the path of the directory within the file's 'path'.
93+
* **_mode_** (integer): directory modes are set to 0755 as a default if not specified and directory does not exist prior to the specified file.
9194
* **_directories_** (list of objects): the list of directories to be created. Every file, directory, and link must have a unique `path`.
9295
* **path** (string): the absolute path to the directory.
9396
* **_overwrite_** (boolean): whether to delete preexisting nodes at the path. If false and a directory already exists at the path, Ignition will only set its permissions. If false and a non-directory exists at that path, Ignition will fail. Defaults to false.

0 commit comments

Comments
 (0)