parent
3df9c40520
commit
fdc2e68497
@ -91,8 +91,8 @@ hostname: trontastic
|
|||||||
if wf.Encoding != "" {
|
if wf.Encoding != "" {
|
||||||
t.Errorf("WriteFile has incorrect encoding %s", wf.Encoding)
|
t.Errorf("WriteFile has incorrect encoding %s", wf.Encoding)
|
||||||
}
|
}
|
||||||
if wf.Permissions != "0644" {
|
if perm, _ := wf.Permissions(); perm != 0644 {
|
||||||
t.Errorf("WriteFile has incorrect permissions %s", wf.Permissions)
|
t.Errorf("WriteFile has incorrect permissions %s", perm)
|
||||||
}
|
}
|
||||||
if wf.Path != "/etc/dogepack.conf" {
|
if wf.Path != "/etc/dogepack.conf" {
|
||||||
t.Errorf("WriteFile has incorrect path %s", wf.Path)
|
t.Errorf("WriteFile has incorrect path %s", wf.Path)
|
||||||
|
@ -2,6 +2,7 @@ package cloudinit
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@ -10,27 +11,43 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type WriteFile struct {
|
type WriteFile struct {
|
||||||
Encoding string
|
Encoding string
|
||||||
Content string
|
Content string
|
||||||
Owner string
|
Owner string
|
||||||
Path string
|
Path string
|
||||||
Permissions string
|
RawFilePermissions string `yaml:"permissions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wf *WriteFile) Permissions() (os.FileMode, error) {
|
||||||
|
if wf.RawFilePermissions == "" {
|
||||||
|
return os.FileMode(0644), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse string representation of file mode as octal
|
||||||
|
perm, err := strconv.ParseInt(wf.RawFilePermissions, 8, 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0, errors.New("Unable to parse file permissions as octal integer")
|
||||||
|
}
|
||||||
|
return os.FileMode(perm), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProcessWriteFile(base string, wf *WriteFile) error {
|
func ProcessWriteFile(base string, wf *WriteFile) error {
|
||||||
|
if wf.Encoding != "" {
|
||||||
|
return fmt.Errorf("Unable to write file with encoding %s", wf.Encoding)
|
||||||
|
}
|
||||||
|
|
||||||
fullPath := path.Join(base, wf.Path)
|
fullPath := path.Join(base, wf.Path)
|
||||||
|
|
||||||
if err := os.MkdirAll(path.Dir(fullPath), os.FileMode(0744)); err != nil {
|
if err := os.MkdirAll(path.Dir(fullPath), os.FileMode(0744)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse string representation of file mode as octal
|
perm, err := wf.Permissions()
|
||||||
perm, err := strconv.ParseInt(wf.Permissions, 8, 32)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Unable to parse file permissions as octal integer")
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ioutil.WriteFile(fullPath, []byte(wf.Content), os.FileMode(perm)); err != nil {
|
if err := ioutil.WriteFile(fullPath, []byte(wf.Content), perm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,9 +10,8 @@ import (
|
|||||||
|
|
||||||
func TestWriteFileUnencodedContent(t *testing.T) {
|
func TestWriteFileUnencodedContent(t *testing.T) {
|
||||||
wf := WriteFile{
|
wf := WriteFile{
|
||||||
Path: "/tmp/foo",
|
Path: "/tmp/foo",
|
||||||
Content: "bar",
|
Content: "bar",
|
||||||
Permissions: "0644",
|
|
||||||
}
|
}
|
||||||
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -47,9 +46,9 @@ func TestWriteFileUnencodedContent(t *testing.T) {
|
|||||||
|
|
||||||
func TestWriteFileInvalidPermission(t *testing.T) {
|
func TestWriteFileInvalidPermission(t *testing.T) {
|
||||||
wf := WriteFile{
|
wf := WriteFile{
|
||||||
Path: "/tmp/foo",
|
Path: "/tmp/foo",
|
||||||
Content: "bar",
|
Content: "bar",
|
||||||
Permissions: "pants",
|
RawFilePermissions: "pants",
|
||||||
}
|
}
|
||||||
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -62,10 +61,37 @@ func TestWriteFileInvalidPermission(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteFilePermissions(t *testing.T) {
|
||||||
|
wf := WriteFile{
|
||||||
|
Path: "/tmp/foo",
|
||||||
|
RawFilePermissions: "0755",
|
||||||
|
}
|
||||||
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to create tempdir: %v", err)
|
||||||
|
}
|
||||||
|
defer syscall.Rmdir(dir)
|
||||||
|
|
||||||
|
if err := ProcessWriteFile(dir, &wf); err != nil {
|
||||||
|
t.Fatalf("Processing of WriteFile failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fullPath := path.Join(dir, "tmp", "foo")
|
||||||
|
|
||||||
|
fi, err := os.Stat(fullPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to stat file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.Mode() != os.FileMode(0755) {
|
||||||
|
t.Errorf("File has incorrect mode: %v", fi.Mode())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteFileEncodedContent(t *testing.T) {
|
func TestWriteFileEncodedContent(t *testing.T) {
|
||||||
wf := WriteFile{
|
wf := WriteFile{
|
||||||
Path: "/tmp/foo",
|
Path: "/tmp/foo",
|
||||||
Content: "",
|
Content: "",
|
||||||
Encoding: "base64",
|
Encoding: "base64",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user