@@ -2,7 +2,6 @@ package util
22
33import (
44 "encoding/json"
5- "errors"
65 "fmt"
76 "io"
87 "io/ioutil"
@@ -11,6 +10,7 @@ import (
1110 "path/filepath"
1211
1312 rspecs "github.com/opencontainers/runtime-spec/specs-go"
13+ "github.com/opencontainers/runtime-tools/clierror"
1414 "github.com/opencontainers/runtime-tools/generate"
1515 "github.com/satori/go.uuid"
1616)
@@ -30,19 +30,26 @@ func NewRuntime(runtimeCommand string, bundleDir string) (Runtime, error) {
3030 var err error
3131 r .RuntimeCommand , err = exec .LookPath (runtimeCommand )
3232 if err != nil {
33- return Runtime {}, err
33+ return Runtime {}, clierror . NewError ( clierror . Executable , err , "1.0.0" )
3434 }
3535
3636 r .BundleDir = bundleDir
3737 return r , err
3838}
3939
40- // SetConfig creates a 'config.json' by the generator
41- func (r * Runtime ) SetConfig (g * generate.Generator ) error {
40+ // bundleDir returns the bundle directory. Generally this is
41+ // BundleDir, but when BundleDir is the empty string, it falls back to
42+ // ., as specified in the CLI spec.
43+ func (r * Runtime ) bundleDir () (bundleDir string ) {
4244 if r .BundleDir == "" {
43- return errors . New ( "Please set the bundle directory first" )
45+ return "."
4446 }
45- return g .SaveToFile (filepath .Join (r .BundleDir , "config.json" ), generate.ExportOptions {})
47+ return r .BundleDir
48+ }
49+
50+ // SetConfig creates a 'config.json' by the generator
51+ func (r * Runtime ) SetConfig (g * generate.Generator ) error {
52+ return g .SaveToFile (filepath .Join (r .bundleDir (), "config.json" ), generate.ExportOptions {})
4653}
4754
4855// SetID sets the container ID
@@ -58,19 +65,17 @@ func (r *Runtime) Create() (stderr []byte, err error) {
5865 args = append (args , r .ID )
5966 }
6067
61- // TODO: following the spec, we need define the bundle, but 'runc' does not..
62- // if r.BundleDir != "" {
63- // args = append(args, r.BundleDir)
64- // }
68+ if r .BundleDir != "" {
69+ args = append (args , "--bundle" , r .BundleDir )
70+ }
6571 cmd := exec .Command (r .RuntimeCommand , args ... )
66- cmd .Dir = r .BundleDir
6772 id := uuid .NewV4 ().String ()
68- r .stdout , err = os .OpenFile (filepath .Join (r .BundleDir , fmt .Sprintf ("stdout-%s" , id )), os .O_CREATE | os .O_EXCL | os .O_RDWR , 0600 )
73+ r .stdout , err = os .OpenFile (filepath .Join (r .bundleDir () , fmt .Sprintf ("stdout-%s" , id )), os .O_CREATE | os .O_EXCL | os .O_RDWR , 0600 )
6974 if err != nil {
7075 return []byte ("" ), err
7176 }
7277 cmd .Stdout = r .stdout
73- r .stderr , err = os .OpenFile (filepath .Join (r .BundleDir , fmt .Sprintf ("stderr-%s" , id )), os .O_CREATE | os .O_EXCL | os .O_RDWR , 0600 )
78+ r .stderr , err = os .OpenFile (filepath .Join (r .bundleDir () , fmt .Sprintf ("stderr-%s" , id )), os .O_CREATE | os .O_EXCL | os .O_RDWR , 0600 )
7479 if err != nil {
7580 return []byte ("" ), err
7681 }
@@ -161,7 +166,7 @@ func (r *Runtime) Clean(removeBundle bool, forceRemoveBundle bool) error {
161166 err := r .Delete ()
162167
163168 if removeBundle && (err == nil || forceRemoveBundle ) {
164- err2 := os .RemoveAll (r .BundleDir )
169+ err2 := os .RemoveAll (r .bundleDir () )
165170 if err2 != nil && err == nil {
166171 err = err2
167172 }
0 commit comments