diff --git a/validation/util/container.go b/validation/util/container.go index 629ac02f2..0533e31f9 100644 --- a/validation/util/container.go +++ b/validation/util/container.go @@ -2,7 +2,6 @@ package util import ( "encoding/json" - "errors" "fmt" "io" "io/ioutil" @@ -11,6 +10,7 @@ import ( "path/filepath" rspecs "github.com/opencontainers/runtime-spec/specs-go" + "github.com/opencontainers/runtime-tools/clierror" "github.com/opencontainers/runtime-tools/generate" "github.com/satori/go.uuid" ) @@ -30,19 +30,26 @@ func NewRuntime(runtimeCommand string, bundleDir string) (Runtime, error) { var err error r.RuntimeCommand, err = exec.LookPath(runtimeCommand) if err != nil { - return Runtime{}, err + return Runtime{}, clierror.NewError(clierror.Executable, err, "1.0.0") } r.BundleDir = bundleDir return r, err } -// SetConfig creates a 'config.json' by the generator -func (r *Runtime) SetConfig(g *generate.Generator) error { +// bundleDir returns the bundle directory. Generally this is +// BundleDir, but when BundleDir is the empty string, it falls back to +// ., as specified in the CLI spec. +func (r *Runtime) bundleDir() (bundleDir string) { if r.BundleDir == "" { - return errors.New("Please set the bundle directory first") + return "." } - return g.SaveToFile(filepath.Join(r.BundleDir, "config.json"), generate.ExportOptions{}) + return r.BundleDir +} + +// SetConfig creates a 'config.json' by the generator +func (r *Runtime) SetConfig(g *generate.Generator) error { + return g.SaveToFile(filepath.Join(r.bundleDir(), "config.json"), generate.ExportOptions{}) } // SetID sets the container ID @@ -58,19 +65,17 @@ func (r *Runtime) Create() (stderr []byte, err error) { args = append(args, r.ID) } - // TODO: following the spec, we need define the bundle, but 'runc' does not.. - // if r.BundleDir != "" { - // args = append(args, r.BundleDir) - // } + if r.BundleDir != "" { + args = append(args, "--bundle", r.BundleDir) + } cmd := exec.Command(r.RuntimeCommand, args...) - cmd.Dir = r.BundleDir id := uuid.NewV4().String() - r.stdout, err = os.OpenFile(filepath.Join(r.BundleDir, fmt.Sprintf("stdout-%s", id)), os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600) + r.stdout, err = os.OpenFile(filepath.Join(r.bundleDir(), fmt.Sprintf("stdout-%s", id)), os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600) if err != nil { return []byte(""), err } cmd.Stdout = r.stdout - r.stderr, err = os.OpenFile(filepath.Join(r.BundleDir, fmt.Sprintf("stderr-%s", id)), os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600) + r.stderr, err = os.OpenFile(filepath.Join(r.bundleDir(), fmt.Sprintf("stderr-%s", id)), os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600) if err != nil { return []byte(""), err } @@ -161,7 +166,7 @@ func (r *Runtime) Clean(removeBundle bool, forceRemoveBundle bool) error { err := r.Delete() if removeBundle && (err == nil || forceRemoveBundle) { - err2 := os.RemoveAll(r.BundleDir) + err2 := os.RemoveAll(r.bundleDir()) if err2 != nil && err == nil { err = err2 }