file: refactor config
- Seperate the config from Permissions() - Add YAML tags for the fields
This commit is contained in:
parent
1fbbaaec19
commit
85b8d804c8
9
config/file.go
Normal file
9
config/file.go
Normal 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"`
|
||||
}
|
@ -37,7 +37,7 @@ type CloudConfig struct {
|
||||
Update config.Update
|
||||
Units []config.Unit
|
||||
}
|
||||
WriteFiles []system.File `yaml:"write_files"`
|
||||
WriteFiles []config.File `yaml:"write_files"`
|
||||
Hostname string
|
||||
Users []system.User
|
||||
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{
|
||||
system.OEM{cfg.Coreos.OEM},
|
||||
system.Update{cfg.Coreos.Update, system.DefaultReadConfig},
|
||||
@ -227,7 +232,7 @@ func Apply(cfg CloudConfig, env *Environment) error {
|
||||
return err
|
||||
}
|
||||
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
|
||||
for _, file := range cfg.WriteFiles {
|
||||
for _, file := range writeFiles {
|
||||
fullPath, err := system.WriteFile(&file, env.Root())
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -178,7 +178,7 @@ hostname: trontastic
|
||||
if len(cfg.WriteFiles) != 1 {
|
||||
t.Error("Failed to parse correct number of write_files")
|
||||
} else {
|
||||
wf := cfg.WriteFiles[0]
|
||||
wf := system.File{cfg.WriteFiles[0]}
|
||||
if wf.Content != "penny\nelroy\n" {
|
||||
t.Errorf("WriteFile has incorrect contents '%s'", wf.Content)
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
"github.com/coreos/coreos-cloudinit/system"
|
||||
)
|
||||
|
||||
@ -81,9 +82,9 @@ func (e *Environment) Apply(data string) string {
|
||||
|
||||
func (e *Environment) DefaultEnvironmentFile() *system.EnvFile {
|
||||
ef := system.EnvFile{
|
||||
File: &system.File{
|
||||
File: &system.File{config.File{
|
||||
Path: "/etc/environment",
|
||||
},
|
||||
}},
|
||||
Vars: map[string]string{},
|
||||
}
|
||||
if ip, ok := e.substitutions["$public_ipv4"]; ok && len(ip) > 0 {
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
"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)
|
||||
|
||||
file := system.File{
|
||||
file := system.File{config.File{
|
||||
Path: relpath,
|
||||
RawFilePermissions: "0744",
|
||||
Content: string(script),
|
||||
}
|
||||
}}
|
||||
|
||||
return system.WriteFile(&file, workspace)
|
||||
}
|
||||
|
||||
func PersistUnitNameInWorkspace(name string, workspace string) error {
|
||||
file := system.File{
|
||||
file := system.File{config.File{
|
||||
Path: path.Join("scripts", "unit-name"),
|
||||
RawFilePermissions: "0644",
|
||||
Content: name,
|
||||
}
|
||||
}}
|
||||
_, err := system.WriteFile(&file, workspace)
|
||||
return err
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -48,9 +50,9 @@ func TestWriteEnvFileUpdate(t *testing.T) {
|
||||
}
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueUpdate,
|
||||
}
|
||||
|
||||
@ -95,9 +97,9 @@ func TestWriteEnvFileUpdateNoNewline(t *testing.T) {
|
||||
}
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueUpdate,
|
||||
}
|
||||
|
||||
@ -136,9 +138,9 @@ func TestWriteEnvFileCreate(t *testing.T) {
|
||||
fullPath := path.Join(dir, name)
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueUpdate,
|
||||
}
|
||||
|
||||
@ -174,9 +176,9 @@ func TestWriteEnvFileNoop(t *testing.T) {
|
||||
}
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueNoop,
|
||||
}
|
||||
|
||||
@ -221,9 +223,9 @@ func TestWriteEnvFileUpdateDos(t *testing.T) {
|
||||
}
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueUpdate,
|
||||
}
|
||||
|
||||
@ -270,9 +272,9 @@ func TestWriteEnvFileDos2Unix(t *testing.T) {
|
||||
}
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueNoop,
|
||||
}
|
||||
|
||||
@ -318,9 +320,9 @@ func TestWriteEnvFileEmpty(t *testing.T) {
|
||||
}
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueEmpty,
|
||||
}
|
||||
|
||||
@ -360,9 +362,9 @@ func TestWriteEnvFileEmptyNoCreate(t *testing.T) {
|
||||
fullPath := path.Join(dir, name)
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueEmpty,
|
||||
}
|
||||
|
||||
@ -391,9 +393,9 @@ func TestWriteEnvFilePermFailure(t *testing.T) {
|
||||
ioutil.WriteFile(fullPath, []byte(base), 0000)
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueUpdate,
|
||||
}
|
||||
|
||||
@ -413,9 +415,9 @@ func TestWriteEnvFileNameFailure(t *testing.T) {
|
||||
name := "foo.conf"
|
||||
|
||||
ef := EnvFile{
|
||||
File: &File{
|
||||
File: &File{config.File{
|
||||
Path: name,
|
||||
},
|
||||
}},
|
||||
Vars: valueInvalid,
|
||||
}
|
||||
|
||||
|
@ -40,9 +40,9 @@ func (eh EtcHosts) File() (*File, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &File{
|
||||
return &File{config.File{
|
||||
Path: path.Join("etc", "hosts"),
|
||||
RawFilePermissions: "0644",
|
||||
Content: etcHosts,
|
||||
}, nil
|
||||
}}, nil
|
||||
}
|
||||
|
@ -27,11 +27,11 @@ func TestEtcdHostsFile(t *testing.T) {
|
||||
},
|
||||
{
|
||||
"localhost",
|
||||
&File{
|
||||
&File{config.File{
|
||||
Content: fmt.Sprintf("127.0.0.1 %s\n", hostname),
|
||||
Path: "etc/hosts",
|
||||
RawFilePermissions: "0644",
|
||||
},
|
||||
}},
|
||||
nil,
|
||||
},
|
||||
} {
|
||||
|
@ -8,14 +8,14 @@ import (
|
||||
"os/exec"
|
||||
"path"
|
||||
"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 {
|
||||
Encoding string
|
||||
Content string
|
||||
Owner string
|
||||
Path string
|
||||
RawFilePermissions string `yaml:"permissions"`
|
||||
config.File
|
||||
}
|
||||
|
||||
func (f *File) Permissions() (os.FileMode, error) {
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
)
|
||||
|
||||
func TestWriteFileUnencodedContent(t *testing.T) {
|
||||
@ -17,11 +19,11 @@ func TestWriteFileUnencodedContent(t *testing.T) {
|
||||
fn := "foo"
|
||||
fullPath := path.Join(dir, fn)
|
||||
|
||||
wf := File{
|
||||
wf := File{config.File{
|
||||
Path: fn,
|
||||
Content: "bar",
|
||||
RawFilePermissions: "0644",
|
||||
}
|
||||
}}
|
||||
|
||||
path, err := WriteFile(&wf, dir)
|
||||
if err != nil {
|
||||
@ -56,11 +58,11 @@ func TestWriteFileInvalidPermission(t *testing.T) {
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
wf := File{
|
||||
wf := File{config.File{
|
||||
Path: path.Join(dir, "tmp", "foo"),
|
||||
Content: "bar",
|
||||
RawFilePermissions: "pants",
|
||||
}
|
||||
}}
|
||||
|
||||
if _, err := WriteFile(&wf, dir); err == nil {
|
||||
t.Fatalf("Expected error to be raised when writing file with invalid permission")
|
||||
@ -77,10 +79,10 @@ func TestWriteFilePermissions(t *testing.T) {
|
||||
fn := "foo"
|
||||
fullPath := path.Join(dir, fn)
|
||||
|
||||
wf := File{
|
||||
wf := File{config.File{
|
||||
Path: fn,
|
||||
RawFilePermissions: "0755",
|
||||
}
|
||||
}}
|
||||
|
||||
path, err := WriteFile(&wf, dir)
|
||||
if err != nil {
|
||||
@ -106,11 +108,11 @@ func TestWriteFileEncodedContent(t *testing.T) {
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
wf := File{
|
||||
wf := File{config.File{
|
||||
Path: path.Join(dir, "tmp", "foo"),
|
||||
Content: "",
|
||||
Encoding: "base64",
|
||||
}
|
||||
}}
|
||||
|
||||
if _, err := WriteFile(&wf, dir); err == nil {
|
||||
t.Fatalf("Expected error to be raised when writing file with encoding")
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
"github.com/coreos/coreos-cloudinit/network"
|
||||
"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
|
||||
}
|
||||
|
||||
func writeConfig(filename string, config string) error {
|
||||
if config == "" {
|
||||
func writeConfig(filename string, content string) error {
|
||||
if content == "" {
|
||||
return nil
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -24,9 +24,9 @@ func (oem OEM) File() (*File, error) {
|
||||
content += fmt.Sprintf("HOME_URL=%q\n", oem.HomeURL)
|
||||
content += fmt.Sprintf("BUG_REPORT_URL=%q\n", oem.BugReportURL)
|
||||
|
||||
return &File{
|
||||
return &File{config.File{
|
||||
Path: path.Join("etc", "oem-release"),
|
||||
RawFilePermissions: "0644",
|
||||
Content: content,
|
||||
}, nil
|
||||
}}, nil
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ func TestOEMFile(t *testing.T) {
|
||||
HomeURL: "https://www.rackspace.com/cloud/servers/",
|
||||
BugReportURL: "https://github.com/coreos/coreos-overlay",
|
||||
},
|
||||
&File{
|
||||
&File{config.File{
|
||||
Path: "etc/oem-release",
|
||||
RawFilePermissions: "0644",
|
||||
Content: `ID=rackspace
|
||||
@ -33,7 +33,7 @@ NAME="Rackspace Cloud Servers"
|
||||
HOME_URL="https://www.rackspace.com/cloud/servers/"
|
||||
BUG_REPORT_URL="https://github.com/coreos/coreos-overlay"
|
||||
`,
|
||||
},
|
||||
}},
|
||||
},
|
||||
} {
|
||||
file, err := OEM{tt.config}.File()
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/coreos-cloudinit/config"
|
||||
"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),
|
||||
Content: u.Content,
|
||||
RawFilePermissions: "0644",
|
||||
}
|
||||
}}
|
||||
|
||||
_, err := WriteFile(&file, dir)
|
||||
if err != nil {
|
||||
|
@ -90,11 +90,11 @@ func (uc Update) File() (*File, error) {
|
||||
out += "\n"
|
||||
}
|
||||
|
||||
return &File{
|
||||
return &File{config.File{
|
||||
Path: path.Join("etc", "coreos", "update.conf"),
|
||||
RawFilePermissions: "0644",
|
||||
Content: out,
|
||||
}, nil
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// Units generates units for the cloud-init initializer to act on:
|
||||
|
@ -92,52 +92,52 @@ func TestUpdateFile(t *testing.T) {
|
||||
},
|
||||
{
|
||||
config: config.Update{Group: "master", Server: "http://foo.com"},
|
||||
file: &File{
|
||||
file: &File{config.File{
|
||||
Content: "GROUP=master\nSERVER=http://foo.com\n",
|
||||
Path: "etc/coreos/update.conf",
|
||||
RawFilePermissions: "0644",
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
config: config.Update{RebootStrategy: "best-effort"},
|
||||
file: &File{
|
||||
file: &File{config.File{
|
||||
Content: "REBOOT_STRATEGY=best-effort\n",
|
||||
Path: "etc/coreos/update.conf",
|
||||
RawFilePermissions: "0644",
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
config: config.Update{RebootStrategy: "etcd-lock"},
|
||||
file: &File{
|
||||
file: &File{config.File{
|
||||
Content: "REBOOT_STRATEGY=etcd-lock\n",
|
||||
Path: "etc/coreos/update.conf",
|
||||
RawFilePermissions: "0644",
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
config: config.Update{RebootStrategy: "reboot"},
|
||||
file: &File{
|
||||
file: &File{config.File{
|
||||
Content: "REBOOT_STRATEGY=reboot\n",
|
||||
Path: "etc/coreos/update.conf",
|
||||
RawFilePermissions: "0644",
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
config: config.Update{RebootStrategy: "off"},
|
||||
file: &File{
|
||||
file: &File{config.File{
|
||||
Content: "REBOOT_STRATEGY=off\n",
|
||||
Path: "etc/coreos/update.conf",
|
||||
RawFilePermissions: "0644",
|
||||
},
|
||||
}},
|
||||
},
|
||||
{
|
||||
config: config.Update{RebootStrategy: "etcd-lock"},
|
||||
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",
|
||||
Path: "etc/coreos/update.conf",
|
||||
RawFilePermissions: "0644",
|
||||
},
|
||||
}},
|
||||
},
|
||||
} {
|
||||
file, err := Update{tt.config, testReadConfig(tt.orig)}.File()
|
||||
|
Loading…
Reference in New Issue
Block a user