file: refactor config

- Seperate the config from Permissions()
- Add YAML tags for the fields
This commit is contained in:
Alex Crawford 2014-09-21 19:22:27 -07:00
parent 1fbbaaec19
commit 85b8d804c8
16 changed files with 92 additions and 70 deletions

9
config/file.go Normal file
View File

@ -0,0 +1,9 @@
package config
type File struct {
Encoding string `yaml:"-"`
Content string `yaml:"content"`
Owner string `yaml:"owner"`
Path string `yaml:"path"`
RawFilePermissions string `yaml:"permissions"`
}

View File

@ -37,7 +37,7 @@ type CloudConfig struct {
Update config.Update Update config.Update
Units []config.Unit Units []config.Unit
} }
WriteFiles []system.File `yaml:"write_files"` WriteFiles []config.File `yaml:"write_files"`
Hostname string Hostname string
Users []system.User Users []system.User
ManageEtcHosts config.EtcHosts `yaml:"manage_etc_hosts"` ManageEtcHosts config.EtcHosts `yaml:"manage_etc_hosts"`
@ -217,6 +217,11 @@ func Apply(cfg CloudConfig, env *Environment) error {
} }
} }
var writeFiles []system.File
for _, file := range cfg.WriteFiles {
writeFiles = append(writeFiles, system.File{file})
}
for _, ccf := range []CloudConfigFile{ for _, ccf := range []CloudConfigFile{
system.OEM{cfg.Coreos.OEM}, system.OEM{cfg.Coreos.OEM},
system.Update{cfg.Coreos.Update, system.DefaultReadConfig}, system.Update{cfg.Coreos.Update, system.DefaultReadConfig},
@ -227,7 +232,7 @@ func Apply(cfg CloudConfig, env *Environment) error {
return err return err
} }
if f != nil { if f != nil {
cfg.WriteFiles = append(cfg.WriteFiles, *f) writeFiles = append(writeFiles, *f)
} }
} }
@ -249,7 +254,7 @@ func Apply(cfg CloudConfig, env *Environment) error {
} }
wroteEnvironment := false wroteEnvironment := false
for _, file := range cfg.WriteFiles { for _, file := range writeFiles {
fullPath, err := system.WriteFile(&file, env.Root()) fullPath, err := system.WriteFile(&file, env.Root())
if err != nil { if err != nil {
return err return err

View File

@ -178,7 +178,7 @@ hostname: trontastic
if len(cfg.WriteFiles) != 1 { if len(cfg.WriteFiles) != 1 {
t.Error("Failed to parse correct number of write_files") t.Error("Failed to parse correct number of write_files")
} else { } else {
wf := cfg.WriteFiles[0] wf := system.File{cfg.WriteFiles[0]}
if wf.Content != "penny\nelroy\n" { if wf.Content != "penny\nelroy\n" {
t.Errorf("WriteFile has incorrect contents '%s'", wf.Content) t.Errorf("WriteFile has incorrect contents '%s'", wf.Content)
} }

View File

@ -6,6 +6,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/coreos/coreos-cloudinit/config"
"github.com/coreos/coreos-cloudinit/system" "github.com/coreos/coreos-cloudinit/system"
) )
@ -81,9 +82,9 @@ func (e *Environment) Apply(data string) string {
func (e *Environment) DefaultEnvironmentFile() *system.EnvFile { func (e *Environment) DefaultEnvironmentFile() *system.EnvFile {
ef := system.EnvFile{ ef := system.EnvFile{
File: &system.File{ File: &system.File{config.File{
Path: "/etc/environment", Path: "/etc/environment",
}, }},
Vars: map[string]string{}, Vars: map[string]string{},
} }
if ip, ok := e.substitutions["$public_ipv4"]; ok && len(ip) > 0 { if ip, ok := e.substitutions["$public_ipv4"]; ok && len(ip) > 0 {

View File

@ -5,6 +5,7 @@ import (
"path" "path"
"strings" "strings"
"github.com/coreos/coreos-cloudinit/config"
"github.com/coreos/coreos-cloudinit/system" "github.com/coreos/coreos-cloudinit/system"
) )
@ -31,21 +32,21 @@ func PersistScriptInWorkspace(script system.Script, workspace string) (string, e
relpath := strings.TrimPrefix(tmp.Name(), workspace) relpath := strings.TrimPrefix(tmp.Name(), workspace)
file := system.File{ file := system.File{config.File{
Path: relpath, Path: relpath,
RawFilePermissions: "0744", RawFilePermissions: "0744",
Content: string(script), Content: string(script),
} }}
return system.WriteFile(&file, workspace) return system.WriteFile(&file, workspace)
} }
func PersistUnitNameInWorkspace(name string, workspace string) error { func PersistUnitNameInWorkspace(name string, workspace string) error {
file := system.File{ file := system.File{config.File{
Path: path.Join("scripts", "unit-name"), Path: path.Join("scripts", "unit-name"),
RawFilePermissions: "0644", RawFilePermissions: "0644",
Content: name, Content: name,
} }}
_, err := system.WriteFile(&file, workspace) _, err := system.WriteFile(&file, workspace)
return err return err
} }

View File

@ -7,6 +7,8 @@ import (
"strings" "strings"
"syscall" "syscall"
"testing" "testing"
"github.com/coreos/coreos-cloudinit/config"
) )
const ( const (
@ -48,9 +50,9 @@ func TestWriteEnvFileUpdate(t *testing.T) {
} }
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueUpdate, Vars: valueUpdate,
} }
@ -95,9 +97,9 @@ func TestWriteEnvFileUpdateNoNewline(t *testing.T) {
} }
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueUpdate, Vars: valueUpdate,
} }
@ -136,9 +138,9 @@ func TestWriteEnvFileCreate(t *testing.T) {
fullPath := path.Join(dir, name) fullPath := path.Join(dir, name)
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueUpdate, Vars: valueUpdate,
} }
@ -174,9 +176,9 @@ func TestWriteEnvFileNoop(t *testing.T) {
} }
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueNoop, Vars: valueNoop,
} }
@ -221,9 +223,9 @@ func TestWriteEnvFileUpdateDos(t *testing.T) {
} }
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueUpdate, Vars: valueUpdate,
} }
@ -270,9 +272,9 @@ func TestWriteEnvFileDos2Unix(t *testing.T) {
} }
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueNoop, Vars: valueNoop,
} }
@ -318,9 +320,9 @@ func TestWriteEnvFileEmpty(t *testing.T) {
} }
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueEmpty, Vars: valueEmpty,
} }
@ -360,9 +362,9 @@ func TestWriteEnvFileEmptyNoCreate(t *testing.T) {
fullPath := path.Join(dir, name) fullPath := path.Join(dir, name)
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueEmpty, Vars: valueEmpty,
} }
@ -391,9 +393,9 @@ func TestWriteEnvFilePermFailure(t *testing.T) {
ioutil.WriteFile(fullPath, []byte(base), 0000) ioutil.WriteFile(fullPath, []byte(base), 0000)
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueUpdate, Vars: valueUpdate,
} }
@ -413,9 +415,9 @@ func TestWriteEnvFileNameFailure(t *testing.T) {
name := "foo.conf" name := "foo.conf"
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{config.File{
Path: name, Path: name,
}, }},
Vars: valueInvalid, Vars: valueInvalid,
} }

View File

@ -40,9 +40,9 @@ func (eh EtcHosts) File() (*File, error) {
return nil, err return nil, err
} }
return &File{ return &File{config.File{
Path: path.Join("etc", "hosts"), Path: path.Join("etc", "hosts"),
RawFilePermissions: "0644", RawFilePermissions: "0644",
Content: etcHosts, Content: etcHosts,
}, nil }}, nil
} }

View File

@ -27,11 +27,11 @@ func TestEtcdHostsFile(t *testing.T) {
}, },
{ {
"localhost", "localhost",
&File{ &File{config.File{
Content: fmt.Sprintf("127.0.0.1 %s\n", hostname), Content: fmt.Sprintf("127.0.0.1 %s\n", hostname),
Path: "etc/hosts", Path: "etc/hosts",
RawFilePermissions: "0644", RawFilePermissions: "0644",
}, }},
nil, nil,
}, },
} { } {

View File

@ -8,14 +8,14 @@ import (
"os/exec" "os/exec"
"path" "path"
"strconv" "strconv"
"github.com/coreos/coreos-cloudinit/config"
) )
// File is a top-level structure which embeds its underlying configuration,
// config.File, and provides the system-specific Permissions().
type File struct { type File struct {
Encoding string config.File
Content string
Owner string
Path string
RawFilePermissions string `yaml:"permissions"`
} }
func (f *File) Permissions() (os.FileMode, error) { func (f *File) Permissions() (os.FileMode, error) {

View File

@ -5,6 +5,8 @@ import (
"os" "os"
"path" "path"
"testing" "testing"
"github.com/coreos/coreos-cloudinit/config"
) )
func TestWriteFileUnencodedContent(t *testing.T) { func TestWriteFileUnencodedContent(t *testing.T) {
@ -17,11 +19,11 @@ func TestWriteFileUnencodedContent(t *testing.T) {
fn := "foo" fn := "foo"
fullPath := path.Join(dir, fn) fullPath := path.Join(dir, fn)
wf := File{ wf := File{config.File{
Path: fn, Path: fn,
Content: "bar", Content: "bar",
RawFilePermissions: "0644", RawFilePermissions: "0644",
} }}
path, err := WriteFile(&wf, dir) path, err := WriteFile(&wf, dir)
if err != nil { if err != nil {
@ -56,11 +58,11 @@ func TestWriteFileInvalidPermission(t *testing.T) {
} }
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
wf := File{ wf := File{config.File{
Path: path.Join(dir, "tmp", "foo"), Path: path.Join(dir, "tmp", "foo"),
Content: "bar", Content: "bar",
RawFilePermissions: "pants", RawFilePermissions: "pants",
} }}
if _, err := WriteFile(&wf, dir); err == nil { if _, err := WriteFile(&wf, dir); err == nil {
t.Fatalf("Expected error to be raised when writing file with invalid permission") t.Fatalf("Expected error to be raised when writing file with invalid permission")
@ -77,10 +79,10 @@ func TestWriteFilePermissions(t *testing.T) {
fn := "foo" fn := "foo"
fullPath := path.Join(dir, fn) fullPath := path.Join(dir, fn)
wf := File{ wf := File{config.File{
Path: fn, Path: fn,
RawFilePermissions: "0755", RawFilePermissions: "0755",
} }}
path, err := WriteFile(&wf, dir) path, err := WriteFile(&wf, dir)
if err != nil { if err != nil {
@ -106,11 +108,11 @@ func TestWriteFileEncodedContent(t *testing.T) {
} }
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
wf := File{ wf := File{config.File{
Path: path.Join(dir, "tmp", "foo"), Path: path.Join(dir, "tmp", "foo"),
Content: "", Content: "",
Encoding: "base64", Encoding: "base64",
} }}
if _, err := WriteFile(&wf, dir); err == nil { if _, err := WriteFile(&wf, dir); err == nil {
t.Fatalf("Expected error to be raised when writing file with encoding") t.Fatalf("Expected error to be raised when writing file with encoding")

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/coreos/coreos-cloudinit/config"
"github.com/coreos/coreos-cloudinit/network" "github.com/coreos/coreos-cloudinit/network"
"github.com/coreos/coreos-cloudinit/third_party/github.com/dotcloud/docker/pkg/netlink" "github.com/coreos/coreos-cloudinit/third_party/github.com/dotcloud/docker/pkg/netlink"
) )
@ -108,11 +109,11 @@ func WriteNetworkdConfigs(interfaces []network.InterfaceGenerator) error {
return nil return nil
} }
func writeConfig(filename string, config string) error { func writeConfig(filename string, content string) error {
if config == "" { if content == "" {
return nil return nil
} }
log.Printf("Writing networkd unit %q\n", filename) log.Printf("Writing networkd unit %q\n", filename)
_, err := WriteFile(&File{Content: config, Path: filename}, runtimeNetworkPath) _, err := WriteFile(&File{config.File{Content: content, Path: filename}}, runtimeNetworkPath)
return err return err
} }

View File

@ -24,9 +24,9 @@ func (oem OEM) File() (*File, error) {
content += fmt.Sprintf("HOME_URL=%q\n", oem.HomeURL) content += fmt.Sprintf("HOME_URL=%q\n", oem.HomeURL)
content += fmt.Sprintf("BUG_REPORT_URL=%q\n", oem.BugReportURL) content += fmt.Sprintf("BUG_REPORT_URL=%q\n", oem.BugReportURL)
return &File{ return &File{config.File{
Path: path.Join("etc", "oem-release"), Path: path.Join("etc", "oem-release"),
RawFilePermissions: "0644", RawFilePermissions: "0644",
Content: content, Content: content,
}, nil }}, nil
} }

View File

@ -24,7 +24,7 @@ func TestOEMFile(t *testing.T) {
HomeURL: "https://www.rackspace.com/cloud/servers/", HomeURL: "https://www.rackspace.com/cloud/servers/",
BugReportURL: "https://github.com/coreos/coreos-overlay", BugReportURL: "https://github.com/coreos/coreos-overlay",
}, },
&File{ &File{config.File{
Path: "etc/oem-release", Path: "etc/oem-release",
RawFilePermissions: "0644", RawFilePermissions: "0644",
Content: `ID=rackspace Content: `ID=rackspace
@ -33,7 +33,7 @@ NAME="Rackspace Cloud Servers"
HOME_URL="https://www.rackspace.com/cloud/servers/" HOME_URL="https://www.rackspace.com/cloud/servers/"
BUG_REPORT_URL="https://github.com/coreos/coreos-overlay" BUG_REPORT_URL="https://github.com/coreos/coreos-overlay"
`, `,
}, }},
}, },
} { } {
file, err := OEM{tt.config}.File() file, err := OEM{tt.config}.File()

View File

@ -10,6 +10,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/coreos/coreos-cloudinit/config"
"github.com/coreos/coreos-cloudinit/third_party/github.com/coreos/go-systemd/dbus" "github.com/coreos/coreos-cloudinit/third_party/github.com/coreos/go-systemd/dbus"
) )
@ -35,11 +36,11 @@ func (s *systemd) PlaceUnit(u *Unit, dst string) error {
} }
} }
file := File{ file := File{config.File{
Path: filepath.Base(dst), Path: filepath.Base(dst),
Content: u.Content, Content: u.Content,
RawFilePermissions: "0644", RawFilePermissions: "0644",
} }}
_, err := WriteFile(&file, dir) _, err := WriteFile(&file, dir)
if err != nil { if err != nil {

View File

@ -90,11 +90,11 @@ func (uc Update) File() (*File, error) {
out += "\n" out += "\n"
} }
return &File{ return &File{config.File{
Path: path.Join("etc", "coreos", "update.conf"), Path: path.Join("etc", "coreos", "update.conf"),
RawFilePermissions: "0644", RawFilePermissions: "0644",
Content: out, Content: out,
}, nil }}, nil
} }
// Units generates units for the cloud-init initializer to act on: // Units generates units for the cloud-init initializer to act on:

View File

@ -92,52 +92,52 @@ func TestUpdateFile(t *testing.T) {
}, },
{ {
config: config.Update{Group: "master", Server: "http://foo.com"}, config: config.Update{Group: "master", Server: "http://foo.com"},
file: &File{ file: &File{config.File{
Content: "GROUP=master\nSERVER=http://foo.com\n", Content: "GROUP=master\nSERVER=http://foo.com\n",
Path: "etc/coreos/update.conf", Path: "etc/coreos/update.conf",
RawFilePermissions: "0644", RawFilePermissions: "0644",
}, }},
}, },
{ {
config: config.Update{RebootStrategy: "best-effort"}, config: config.Update{RebootStrategy: "best-effort"},
file: &File{ file: &File{config.File{
Content: "REBOOT_STRATEGY=best-effort\n", Content: "REBOOT_STRATEGY=best-effort\n",
Path: "etc/coreos/update.conf", Path: "etc/coreos/update.conf",
RawFilePermissions: "0644", RawFilePermissions: "0644",
}, }},
}, },
{ {
config: config.Update{RebootStrategy: "etcd-lock"}, config: config.Update{RebootStrategy: "etcd-lock"},
file: &File{ file: &File{config.File{
Content: "REBOOT_STRATEGY=etcd-lock\n", Content: "REBOOT_STRATEGY=etcd-lock\n",
Path: "etc/coreos/update.conf", Path: "etc/coreos/update.conf",
RawFilePermissions: "0644", RawFilePermissions: "0644",
}, }},
}, },
{ {
config: config.Update{RebootStrategy: "reboot"}, config: config.Update{RebootStrategy: "reboot"},
file: &File{ file: &File{config.File{
Content: "REBOOT_STRATEGY=reboot\n", Content: "REBOOT_STRATEGY=reboot\n",
Path: "etc/coreos/update.conf", Path: "etc/coreos/update.conf",
RawFilePermissions: "0644", RawFilePermissions: "0644",
}, }},
}, },
{ {
config: config.Update{RebootStrategy: "off"}, config: config.Update{RebootStrategy: "off"},
file: &File{ file: &File{config.File{
Content: "REBOOT_STRATEGY=off\n", Content: "REBOOT_STRATEGY=off\n",
Path: "etc/coreos/update.conf", Path: "etc/coreos/update.conf",
RawFilePermissions: "0644", RawFilePermissions: "0644",
}, }},
}, },
{ {
config: config.Update{RebootStrategy: "etcd-lock"}, config: config.Update{RebootStrategy: "etcd-lock"},
orig: "SERVER=https://example.com\nGROUP=thegroupc\nREBOOT_STRATEGY=awesome", orig: "SERVER=https://example.com\nGROUP=thegroupc\nREBOOT_STRATEGY=awesome",
file: &File{ file: &File{config.File{
Content: "SERVER=https://example.com\nGROUP=thegroupc\nREBOOT_STRATEGY=etcd-lock\n", Content: "SERVER=https://example.com\nGROUP=thegroupc\nREBOOT_STRATEGY=etcd-lock\n",
Path: "etc/coreos/update.conf", Path: "etc/coreos/update.conf",
RawFilePermissions: "0644", RawFilePermissions: "0644",
}, }},
}, },
} { } {
file, err := Update{tt.config, testReadConfig(tt.orig)}.File() file, err := Update{tt.config, testReadConfig(tt.orig)}.File()