From 70472907cc5bc4792b1ee4859f3c94289a04b754 Mon Sep 17 00:00:00 2001 From: Evan Tschuy Date: Mon, 6 Feb 2017 14:05:50 -0800 Subject: [PATCH 1/2] don't quit if services can't be found the only time we should quit is when we can't understand user options. --- mayday.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mayday.go b/mayday.go index 443cf60..c2068ec 100644 --- a/mayday.go +++ b/mayday.go @@ -126,7 +126,8 @@ func main() { journals, err := journal.List() if err != nil { - log.Fatal(err) + log.Println("Could not retreive system services. Skipping...") + log.Printf("Service error: %s", err) } pods, rktLogs, err := rkt.GetPods() From 575f2a3b1a36c8b082906de10146d8d3698a05a6 Mon Sep 17 00:00:00 2001 From: Evan Tschuy Date: Tue, 7 Feb 2017 11:17:26 -0800 Subject: [PATCH 2/2] compile in a default config --- config/config.go | 237 ++++++++++++++++++++++++++++ default.json => config/default.json | 0 mayday.go | 27 +++- 3 files changed, 257 insertions(+), 7 deletions(-) create mode 100644 config/config.go rename default.json => config/default.json (100%) diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..84850f0 --- /dev/null +++ b/config/config.go @@ -0,0 +1,237 @@ +// Code generated by go-bindata. +// sources: +// config/default.json +// DO NOT EDIT! + +package config + +import ( + "bytes" + "compress/gzip" + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" + "strings" + "time" +) + +func bindataRead(data []byte, name string) ([]byte, error) { + gz, err := gzip.NewReader(bytes.NewBuffer(data)) + if err != nil { + return nil, fmt.Errorf("Read %q: %v", name, err) + } + + var buf bytes.Buffer + _, err = io.Copy(&buf, gz) + clErr := gz.Close() + + if err != nil { + return nil, fmt.Errorf("Read %q: %v", name, err) + } + if clErr != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +type asset struct { + bytes []byte + info os.FileInfo +} + +type bindataFileInfo struct { + name string + size int64 + mode os.FileMode + modTime time.Time +} + +func (fi bindataFileInfo) Name() string { + return fi.name +} +func (fi bindataFileInfo) Size() int64 { + return fi.size +} +func (fi bindataFileInfo) Mode() os.FileMode { + return fi.mode +} +func (fi bindataFileInfo) ModTime() time.Time { + return fi.modTime +} +func (fi bindataFileInfo) IsDir() bool { + return false +} +func (fi bindataFileInfo) Sys() interface{} { + return nil +} + +var _configDefaultJson = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xa4\x55\xcd\x8a\xdb\x30\x10\xbe\xfb\x29\x84\xce\x71\x43\xf7\x90\xc3\x42\xdf\xa0\x7d\x82\x12\x82\x6c\x8d\xb3\x62\xf5\x63\x34\xe3\xa4\x65\xd9\x77\x2f\x1a\xd9\x6d\x21\x8a\xe5\xdd\x5c\x86\x30\xdf\xdf\x78\x14\xcb\x6f\x8d\x10\x72\x30\x16\x50\x3e\x8b\x9f\x8d\x10\x42\xbc\x71\x15\x42\x7a\xe5\x40\x3e\x0b\xb9\x1f\x63\xe8\xf7\x17\x87\xa4\x48\x32\xf8\xbe\xbb\xc7\x72\xe0\x8c\x1f\x82\xdc\x2d\xb0\x35\xfe\x35\xc1\x0b\x50\xd3\x87\xc9\x13\x16\xe4\xb9\x7f\x5f\x0d\xd4\xef\x03\xb6\x11\x2c\x28\x84\x5b\x83\xff\xb0\x6c\xd2\x08\x71\x4c\x2c\xd9\x07\xe7\x94\xd7\x85\x05\xa8\x78\xe6\xae\x7c\x09\x48\x9c\x74\xbc\xf1\xfd\x0b\xcd\xa3\xdd\x71\xd0\x8a\x4a\x6a\x6e\xaf\x2b\xf1\x37\x12\x38\xdd\xf6\x67\x8b\xf2\xb8\x95\x4b\x61\x94\x3b\x21\x5b\xff\xb5\xa6\x19\x31\x11\x07\x35\xfd\xba\x5e\xaf\x85\x11\x47\xac\x0c\x68\xd1\x05\x5d\x10\xe6\x7e\x4d\x3b\xf6\xa6\xa8\x4d\xfd\x9a\x36\x0c\xfc\x8c\x1d\xd7\x1f\xf9\x79\xb9\xda\xa2\x65\x18\x2a\x8e\x9d\x7d\x35\xba\xb6\xaf\x8e\xe2\x90\x57\x66\x52\xc5\x97\x70\xad\x49\x74\x9e\x53\x95\xc6\xd2\xb5\xa1\x4c\x3e\xc8\x90\xaa\xd2\x3a\xfe\x0b\xbd\xf1\x32\xe3\x29\x31\x4e\x0c\x6f\x77\x65\xf9\xaa\x6b\xfa\xf9\x51\xd7\x18\x26\x82\x75\x5b\xa6\x6c\xf1\xf5\x40\x7c\xf9\xf0\x09\x43\x18\x55\xc1\x6f\xe1\x6c\x3c\x0a\x53\x3b\xb4\x21\x02\x8f\xdf\xba\x42\x18\x83\x5b\xde\xc5\x9e\x6c\x5e\x31\x52\x3b\x79\x43\x98\xe3\x0b\x96\xca\xda\x53\x66\x7c\xde\xb7\x4d\x1b\x80\x6f\x71\xf2\xde\xf8\x73\x21\x64\x46\x3e\x1e\x94\x8c\x27\x0e\x01\xea\xf5\x17\x84\x78\x31\x7d\xe9\x46\x4b\xf0\x69\x66\x7f\xda\xff\xa9\x12\xf0\xf4\x50\xc2\x60\x01\x68\x25\x81\xf1\x07\x13\x94\xf7\x60\xd7\xf6\xb4\x50\x36\xe6\x58\xd5\x2d\x57\x7a\xa8\xfd\x73\xcd\x48\xaa\x4b\x5f\xf4\xc4\xbe\xf8\xef\xc5\x97\x6f\xa6\xd4\x9c\x0e\x1b\xac\x0e\x1b\xbd\x32\xad\x45\x75\x81\xfa\x23\x1c\x0a\xec\xf4\xc9\x6e\xde\x9b\x3f\x01\x00\x00\xff\xff\x3e\x27\x90\xac\xb4\x08\x00\x00") + +func configDefaultJsonBytes() ([]byte, error) { + return bindataRead( + _configDefaultJson, + "config/default.json", + ) +} + +func configDefaultJson() (*asset, error) { + bytes, err := configDefaultJsonBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "config/default.json", size: 2228, mode: os.FileMode(436), modTime: time.Unix(1486494741, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + +// Asset loads and returns the asset for the given name. +// It returns an error if the asset could not be found or +// could not be loaded. +func Asset(name string) ([]byte, error) { + cannonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[cannonicalName]; ok { + a, err := f() + if err != nil { + return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err) + } + return a.bytes, nil + } + return nil, fmt.Errorf("Asset %s not found", name) +} + +// MustAsset is like Asset but panics when Asset would return an error. +// It simplifies safe initialization of global variables. +func MustAsset(name string) []byte { + a, err := Asset(name) + if err != nil { + panic("asset: Asset(" + name + "): " + err.Error()) + } + + return a +} + +// AssetInfo loads and returns the asset info for the given name. +// It returns an error if the asset could not be found or +// could not be loaded. +func AssetInfo(name string) (os.FileInfo, error) { + cannonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[cannonicalName]; ok { + a, err := f() + if err != nil { + return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err) + } + return a.info, nil + } + return nil, fmt.Errorf("AssetInfo %s not found", name) +} + +// AssetNames returns the names of the assets. +func AssetNames() []string { + names := make([]string, 0, len(_bindata)) + for name := range _bindata { + names = append(names, name) + } + return names +} + +// _bindata is a table, holding each asset generator, mapped to its name. +var _bindata = map[string]func() (*asset, error){ + "config/default.json": configDefaultJson, +} + +// AssetDir returns the file names below a certain +// directory embedded in the file by go-bindata. +// For example if you run go-bindata on data/... and data contains the +// following hierarchy: +// data/ +// foo.txt +// img/ +// a.png +// b.png +// then AssetDir("data") would return []string{"foo.txt", "img"} +// AssetDir("data/img") would return []string{"a.png", "b.png"} +// AssetDir("foo.txt") and AssetDir("notexist") would return an error +// AssetDir("") will return []string{"data"}. +func AssetDir(name string) ([]string, error) { + node := _bintree + if len(name) != 0 { + cannonicalName := strings.Replace(name, "\\", "/", -1) + pathList := strings.Split(cannonicalName, "/") + for _, p := range pathList { + node = node.Children[p] + if node == nil { + return nil, fmt.Errorf("Asset %s not found", name) + } + } + } + if node.Func != nil { + return nil, fmt.Errorf("Asset %s not found", name) + } + rv := make([]string, 0, len(node.Children)) + for childName := range node.Children { + rv = append(rv, childName) + } + return rv, nil +} + +type bintree struct { + Func func() (*asset, error) + Children map[string]*bintree +} +var _bintree = &bintree{nil, map[string]*bintree{ + "config": &bintree{nil, map[string]*bintree{ + "default.json": &bintree{configDefaultJson, map[string]*bintree{}}, + }}, +}} + +// RestoreAsset restores an asset under the given directory +func RestoreAsset(dir, name string) error { + data, err := Asset(name) + if err != nil { + return err + } + info, err := AssetInfo(name) + if err != nil { + return err + } + err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755)) + if err != nil { + return err + } + err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + if err != nil { + return err + } + err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) + if err != nil { + return err + } + return nil +} + +// RestoreAssets restores an asset under the given directory recursively +func RestoreAssets(dir, name string) error { + children, err := AssetDir(name) + // File + if err != nil { + return RestoreAsset(dir, name) + } + // Dir + for _, child := range children { + err = RestoreAssets(dir, filepath.Join(name, child)) + if err != nil { + return err + } + } + return nil +} + +func _filePath(dir, name string) string { + cannonicalName := strings.Replace(name, "\\", "/", -1) + return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) +} + diff --git a/default.json b/config/default.json similarity index 100% rename from default.json rename to config/default.json diff --git a/mayday.go b/mayday.go index c2068ec..883bb6e 100644 --- a/mayday.go +++ b/mayday.go @@ -2,10 +2,12 @@ package main import ( "archive/tar" + "bytes" "log" "os" "time" + "github.com/coreos/mayday/config" "github.com/coreos/mayday/mayday" "github.com/coreos/mayday/mayday/plugins/command" "github.com/coreos/mayday/mayday/plugins/docker" @@ -108,21 +110,32 @@ func main() { // viper returns an `unsupported config type ""` error if it can't find a file // https://github.com/spf13/viper/issues/210 if strings.HasSuffix(err.Error(), `Type ""`) { - log.Fatalf("Could not find configuration file in %s", - viper.GetString("config")) + // load the default config asset + defaultConfig, err := config.Asset("config/default.json") + log.Print("Using built-in configuration defaults") + if err != nil { + log.Fatal("Error loading default config: %s", err) + } + + viper.SetConfigType("json") + err = viper.MergeConfig(bytes.NewBuffer(defaultConfig)) + } else { + log.Printf("Error reading configuration file.") + log.Fatalf("Fatal error reading config: %s\n", err) } - log.Printf("Error reading configuration file.") - log.Fatalf("Fatal error reading config: %s\n", err) + } else { + log.Printf("loading config from %s", viper.ConfigFileUsed()) } - log.Printf("loading config from %s", viper.ConfigFileUsed()) - var tarables []tarable.Tarable var C Config // fill C with configuration data - viper.Unmarshal(&C) + err = viper.Unmarshal(&C) + if err != nil { + log.Fatalf("Could not unmarshal configuration: %s", err) + } journals, err := journal.List() if err != nil {