Skip to content

Commit

Permalink
Merge pull request mudler#26 from ibuildthecloud/master
Browse files Browse the repository at this point in the history
Fix write_files permissions and hostname
  • Loading branch information
mudler authored Oct 31, 2021
2 parents 3f7c102 + 5f5552b commit e80d44c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
59 changes: 34 additions & 25 deletions pkg/schema/loader_cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package schema

import (
"fmt"
"strconv"

cloudconfig "github.com/rancher-sandbox/cloud-init/config"
"github.com/twpayne/go-vfs"
)
Expand All @@ -28,7 +31,6 @@ type cloudInit struct{}
// As Yip supports multi-stages, it is encoded in the supplied one.
// fs is used to parse the user data required from /etc/passwd.
func (cloudInit) Load(s []byte, fs vfs.FS) (*YipConfig, error) {
stage := "boot"
cc, err := cloudconfig.NewCloudConfig(string(s))
if err != nil {
return nil, err
Expand Down Expand Up @@ -75,36 +77,27 @@ func (cloudInit) Load(s []byte, fs vfs.FS) (*YipConfig, error) {

// Decode writeFiles
var f []File
for _, ff := range cc.WriteFiles {
f = append(f,
File{
Path: ff.Path,
OwnerString: ff.Owner,
Content: ff.Content,
Encoding: ff.Encoding,
},
)
}

for _, ff := range cc.MilpaFiles {
f = append(f,
File{
Path: ff.Path,
OwnerString: ff.Owner,
Content: ff.Content,
Encoding: ff.Encoding,
},
)
for _, ff := range append(cc.WriteFiles, cc.MilpaFiles...) {
newFile := File{
Path: ff.Path,
OwnerString: ff.Owner,
Content: ff.Content,
Encoding: ff.Encoding,
}
newFile.Permissions, err = parseOctal(ff.RawFilePermissions)
if err != nil {
return nil, fmt.Errorf("converting permission %s for %s: %w", ff.RawFilePermissions, ff.Path, err)
}
f = append(f, newFile)
}

stages := []Stage{{
Commands: cc.RunCmd,
Files: f,
Hostname: cc.Hostname,
Users: users,
SSHKeys: sshKeys,
}}

for _, d := range cc.Partitioning.Devices {
layout := &Layout{}
layout.Expand = &Expand{Size: 0}
Expand All @@ -113,8 +106,13 @@ func (cloudInit) Load(s []byte, fs vfs.FS) (*YipConfig, error) {
}

result := &YipConfig{
Name: "Cloud init",
Stages: map[string][]Stage{stage: stages},
Name: "Cloud init",
Stages: map[string][]Stage{
"boot": stages,
"initramfs": {{
Hostname: cc.Hostname,
}},
},
}

// optimistically load data as yip yaml
Expand All @@ -127,3 +125,14 @@ func (cloudInit) Load(s []byte, fs vfs.FS) (*YipConfig, error) {

return result, nil
}

func parseOctal(srv string) (uint32, error) {
if srv == "" {
return 0, nil
}
i, err := strconv.ParseUint(srv, 8, 32)
if err != nil {
return 0, err
}
return uint32(i), nil
}
6 changes: 4 additions & 2 deletions pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ write_files:
permissions: "0644"
owner: "bar"
`)
Expect(len(yipConfig.Stages)).To(Equal(2))
Expect(len(yipConfig.Stages)).To(Equal(3))
Expect(yipConfig.Stages["boot"][0].Users["bar"].UID).To(Equal("1002"))
Expect(yipConfig.Stages["boot"][0].Users["bar"].PasswordHash).To(Equal("foo"))
Expect(yipConfig.Stages["boot"][0].SSHKeys).To(Equal(map[string][]string{"bar": {"faaapploo", "asdd"}}))
Expect(yipConfig.Stages["boot"][0].Files[0].Path).To(Equal("/foo/bar"))
Expect(yipConfig.Stages["boot"][0].Hostname).To(Equal("bar"))
Expect(yipConfig.Stages["boot"][0].Files[0].Permissions).To(Equal(uint32(0644)))
Expect(yipConfig.Stages["boot"][0].Hostname).To(Equal(""))
Expect(yipConfig.Stages["initramfs"][0].Hostname).To(Equal("bar"))
Expect(yipConfig.Stages["boot"][0].Commands).To(Equal([]string{"foo"}))
Expect(yipConfig.Stages["test"][0].Environment["foo"]).To(Equal("bar"))
Expect(yipConfig.Stages["boot"][0].Users["bar"].LockPasswd).To(Equal(true))
Expand Down

0 comments on commit e80d44c

Please sign in to comment.