From 4c829ea2adb7a6cf13dc09e0829cc2292f298273 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 29 Jun 2015 11:34:22 -0700 Subject: [PATCH] Add version check for spec We need this version check during our initial work so that runc does not fail silently while make changes to runc to match the spec work going on. Signed-off-by: Michael Crosby --- README.md | 2 +- main.go | 8 ++++---- spec_linux.go | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 47a190dba..1dfd136a2 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ user named `daemon` defined within that file-system. ```json { - "version": "0.1", + "version": "0.1.1", "platform": { "os": "linux", "arch": "amd64" diff --git a/main.go b/main.go index ecc4e5dd5..f4793d392 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( ) const ( - version = "0.1" + version = "0.1.1" usage = `Open Container Project runtime runc is a command line client for running applications packaged according to the Open Container Format (OCF) and is @@ -79,13 +79,13 @@ func main() { } // default action is to execute a container app.Action = func(context *cli.Context) { - if os.Geteuid() != 0 { - logrus.Fatal("runc should be run as root") - } spec, err := loadSpec(context.Args().First()) if err != nil { fatal(err) } + if os.Geteuid() != 0 { + logrus.Fatal("runc should be run as root") + } status, err := execContainer(context, spec) if err != nil { logrus.Fatalf("Container start failed: %v", err) diff --git a/spec_linux.go b/spec_linux.go index 917d68094..6c0974ae3 100644 --- a/spec_linux.go +++ b/spec_linux.go @@ -107,10 +107,10 @@ var namespaceMapping = map[string]configs.NamespaceType{ } // loadSpec loads the specification from the provided path. -// If the path is empty then the default path will be "container.json" +// If the path is empty then the default path will be "config.json" func loadSpec(path string) (*Spec, error) { if path == "" { - path = "container.json" + path = "config.json" } f, err := os.Open(path) if err != nil { @@ -124,7 +124,16 @@ func loadSpec(path string) (*Spec, error) { if err := json.NewDecoder(f).Decode(&s); err != nil { return nil, err } - return s, nil + return s, checkSpecVersion(s) +} + +// checkSpecVersion makes sure that the spec version matches runc's while we are in the initial +// development period. It is better to hard fail than have missing fields or options in the spec. +func checkSpecVersion(s *Spec) error { + if s.Version != version { + return fmt.Errorf("spec version is not compatible with runc version %q: spec %q", version, s.Version) + } + return nil } func createLibcontainerConfig(spec *Spec) (*configs.Config, error) {