From d5256cb214995abdf49d30e1a648446f9a18e30f Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 12 Dec 2016 15:42:10 -0500 Subject: [PATCH] server: check for expected files When the configuration is loaded, check that expected files are present early. This can prevent getting into a partial state later. Signed-off-by: Vincent Batts --- cmd/server/main.go | 4 ++++ server/config.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cmd/server/main.go b/cmd/server/main.go index e9dcf7be..320feb4c 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -163,6 +163,10 @@ func main() { return err } + if err := config.Check(); err != nil { + return err + } + cf := &logrus.TextFormatter{ TimestampFormat: "2006-01-02 15:04:05.000000000Z07:00", FullTimestamp: true, diff --git a/server/config.go b/server/config.go index 75e93aa3..9eaa68b2 100644 --- a/server/config.go +++ b/server/config.go @@ -2,7 +2,9 @@ package server import ( "bytes" + "fmt" "io/ioutil" + "os" "github.com/BurntSushi/toml" ) @@ -152,3 +154,29 @@ func (c *Config) ToFile(path string) error { return ioutil.WriteFile(path, w.Bytes(), 0644) } + +// Check resolves that all paths and files are available as expected +func (c *Config) Check() error { + for _, file := range []string{ + c.RootConfig.SandboxDir, + c.RootConfig.ContainerDir, + c.RuntimeConfig.Runtime, + c.RuntimeConfig.Conmon, + c.RuntimeConfig.SeccompProfile, + c.ImageConfig.Pause, + } { + if _, err := os.Stat(file); err != nil && os.IsNotExist(err) { + return checkError{Path: file} + } + } + + return nil +} + +type checkError struct { + Path string +} + +func (ce checkError) Error() string { + return fmt.Sprintf("%q does not exist", ce.Path) +}