From 3d01211937079bb93a0eb2fec99d6d2a522c8ef5 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Sun, 23 Mar 2014 11:06:43 -0700 Subject: [PATCH] feat(coreos.oem): Write coreos.oem fields to /etc/oem-release --- Documentation/cloud-config.md | 39 +++++++++++++++++++++++++ initialize/config.go | 8 ++++++ initialize/config_test.go | 10 +++++++ initialize/oem.go | 39 +++++++++++++++++++++++++ initialize/oem_test.go | 54 +++++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 initialize/oem.go create mode 100644 initialize/oem_test.go diff --git a/Documentation/cloud-config.md b/Documentation/cloud-config.md index 227d097..80577b7 100644 --- a/Documentation/cloud-config.md +++ b/Documentation/cloud-config.md @@ -39,6 +39,45 @@ Note that hyphens in the coreos.etcd.* keys are mapped to underscores. [etcd-config]: https://github.com/coreos/etcd/blob/master/Documentation/configuration.md +### coreos.oem + +These fields are borrowed from the [os-release spec][os-release] and repurposed +as a way for coreos-cloudinit to know about the OEM partition on this machine: + +- **id**: Lowercase string identifying the OEM +- **name**: Human-friendly string representing the OEM +- **version-id**: Lowercase string identifying the version of the OEM +- **home-url**: Link to the homepage of the provider or OEM +- **bug-report-url**: Link to a place to file bug reports about this OEM + +coreos-cloudinit renders these fields to `/etc/oem-release`. +If no **id** field is provided, coreos-cloudinit will ignore this section. + +For example, the following cloud-config document... + +``` +#cloud-config +coreos: + oem: + id: rackspace + name: Rackspace Cloud Servers + version-id: 168.0.0 + home-url: https://www.rackspace.com/cloud/servers/ + bug-report-url: https://github.com/coreos/coreos-overlay +``` + +...would be rendered to the following `/etc/oem-release`: + +``` +ID="rackspace" +NAME="Rackspace Cloud Servers" +VERSION_ID="168.0.0" +HOME_URL="https://www.rackspace.com/cloud/servers/" +BUG_REPORT_URL="https://github.com/coreos/coreos-overlay" +``` + +[os-release]: http://www.freedesktop.org/software/systemd/man/os-release.html + ### coreos.units Arbitrary systemd units may be provided in the `coreos.units` attribute. diff --git a/initialize/config.go b/initialize/config.go index 4b05782..2563c16 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -15,6 +15,7 @@ type CloudConfig struct { Coreos struct { Etcd EtcdEnvironment Units []system.Unit + OEM OEMRelease } WriteFiles []system.File `yaml:"write_files"` Hostname string @@ -47,6 +48,13 @@ func Apply(cfg CloudConfig, env *Environment) error { log.Printf("Set hostname to %s", cfg.Hostname) } + if cfg.Coreos.OEM.ID != "" { + if err := WriteOEMRelease(&cfg.Coreos.OEM, env.Root()); err != nil { + return err + } + log.Printf("Wrote /etc/oem-release to filesystem") + } + if len(cfg.Users) > 0 { for _, user := range cfg.Users { if user.Name == "" { diff --git a/initialize/config_test.go b/initialize/config_test.go index 7bae05d..bc36de5 100644 --- a/initialize/config_test.go +++ b/initialize/config_test.go @@ -45,6 +45,12 @@ coreos: Address=10.209.171.177/19 ' + oem: + id: rackspace + name: Rackspace Cloud Servers + version-id: 168.0.0 + home-url: https://www.rackspace.com/cloud/servers/ + bug-report-url: https://github.com/coreos/coreos-overlay ssh_authorized_keys: - foobar - foobaz @@ -116,6 +122,10 @@ Address=10.209.171.177/19 } } + if cfg.Coreos.OEM.ID != "rackspace" { + t.Errorf("Failed parsing coreos.oem. Expected ID 'rackspace', got %q.", cfg.Coreos.OEM.ID) + } + if cfg.Hostname != "trontastic" { t.Errorf("Failed to parse hostname") } diff --git a/initialize/oem.go b/initialize/oem.go new file mode 100644 index 0000000..b7f3d6c --- /dev/null +++ b/initialize/oem.go @@ -0,0 +1,39 @@ +package initialize + +import ( + "fmt" + "path" + "strings" + + "github.com/coreos/coreos-cloudinit/system" +) + +type OEMRelease struct { + ID string `yaml:"id"` + Name string `yaml:"name"` + VersionID string `yaml:"version-id"` + HomeURL string `yaml:"home-url"` + BugReportURL string `yaml:"bug-report-url"` +} + +func (oem *OEMRelease) String() string { + fields := []string{ + fmt.Sprintf("ID=%q", oem.ID), + fmt.Sprintf("NAME=%q", oem.Name), + fmt.Sprintf("VERSION_ID=%q", oem.VersionID), + fmt.Sprintf("HOME_URL=%q", oem.HomeURL), + fmt.Sprintf("BUG_REPORT_URL=%q", oem.BugReportURL), + } + + return strings.Join(fields, "\n") + "\n" +} + +func WriteOEMRelease(oem *OEMRelease, root string) error { + file := system.File{ + Path: path.Join(root, "etc", "oem-release"), + RawFilePermissions: "0644", + Content: oem.String(), + } + + return system.WriteFile(&file) +} diff --git a/initialize/oem_test.go b/initialize/oem_test.go new file mode 100644 index 0000000..50ebaee --- /dev/null +++ b/initialize/oem_test.go @@ -0,0 +1,54 @@ +package initialize + +import ( + "io/ioutil" + "os" + "path" + "syscall" + "testing" +) + +func TestOEMReleaseWrittenToDisk(t *testing.T) { + oem := OEMRelease{ + ID: "rackspace", + Name: "Rackspace Cloud Servers", + VersionID: "168.0.0", + HomeURL: "https://www.rackspace.com/cloud/servers/", + BugReportURL: "https://github.com/coreos/coreos-overlay", + } + 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 := WriteOEMRelease(&oem, dir); err != nil { + t.Fatalf("Processing of EtcdEnvironment failed: %v", err) + } + + fullPath := path.Join(dir, "etc", "oem-release") + + fi, err := os.Stat(fullPath) + if err != nil { + t.Fatalf("Unable to stat file: %v", err) + } + + if fi.Mode() != os.FileMode(0644) { + t.Errorf("File has incorrect mode: %v", fi.Mode()) + } + + contents, err := ioutil.ReadFile(fullPath) + if err != nil { + t.Fatalf("Unable to read expected file: %v", err) + } + + expect := `ID="rackspace" +NAME="Rackspace Cloud Servers" +VERSION_ID="168.0.0" +HOME_URL="https://www.rackspace.com/cloud/servers/" +BUG_REPORT_URL="https://github.com/coreos/coreos-overlay" +` + if string(contents) != expect { + t.Fatalf("File has incorrect contents") + } +}