Skip to content

Commit

Permalink
validation/util/container: Use --bundle (and stop requiring BundleDir)
Browse files Browse the repository at this point in the history
Address a previous TODO.  And now that we are using --bundle, we no
longer need to set cmd.Dir.  The TODO mentions a lack of runc support,
but runc supports --bundle since opencontainers/runc@3fe7d7f3 (Add
create and start command for container lifecycle, 2016-05-13,
opencontainers/runc#827).
  • Loading branch information
wking committed Jan 11, 2018
1 parent e6a60ee commit cf5cd7f
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions validation/util/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package util

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -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"
)
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit cf5cd7f

Please sign in to comment.