@@ -2,7 +2,6 @@ package util
22
33import (
44 "encoding/json"
5- "errors"
65 "fmt"
76 "io"
87 "io/ioutil"
@@ -37,12 +36,19 @@ func NewRuntime(runtimeCommand string, bundleDir string) (Runtime, error) {
3736 return r , err
3837}
3938
40- // SetConfig creates a 'config.json' by the generator
41- func (r * Runtime ) SetConfig (g * generate.Generator ) error {
39+ // bundleDir returns the bundle directory. Generally this is
40+ // BundleDir, but when BundleDir is the empty string, it falls back to
41+ // ., as specified in the CLI spec.
42+ func (r * Runtime ) bundleDir () (bundleDir string ) {
4243 if r .BundleDir == "" {
43- return errors . New ( "Please set the bundle directory first" )
44+ return "."
4445 }
45- return g .SaveToFile (filepath .Join (r .BundleDir , "config.json" ), generate.ExportOptions {})
46+ return r .BundleDir
47+ }
48+
49+ // SetConfig creates a 'config.json' by the generator
50+ func (r * Runtime ) SetConfig (g * generate.Generator ) error {
51+ return g .SaveToFile (filepath .Join (r .bundleDir (), "config.json" ), generate.ExportOptions {})
4652}
4753
4854// SetID sets the container ID
@@ -58,19 +64,17 @@ func (r *Runtime) Create() (stderr []byte, err error) {
5864 args = append (args , r .ID )
5965 }
6066
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- // }
67+ if r .BundleDir != "" {
68+ args = append (args , "--bundle" , r .BundleDir )
69+ }
6570 cmd := exec .Command (r .RuntimeCommand , args ... )
66- cmd .Dir = r .BundleDir
6771 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 )
72+ r .stdout , err = os .OpenFile (filepath .Join (r .bundleDir () , fmt .Sprintf ("stdout-%s" , id )), os .O_CREATE | os .O_EXCL | os .O_RDWR , 0600 )
6973 if err != nil {
7074 return []byte ("" ), err
7175 }
7276 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 )
77+ r .stderr , err = os .OpenFile (filepath .Join (r .bundleDir () , fmt .Sprintf ("stderr-%s" , id )), os .O_CREATE | os .O_EXCL | os .O_RDWR , 0600 )
7478 if err != nil {
7579 return []byte ("" ), err
7680 }
@@ -161,7 +165,7 @@ func (r *Runtime) Clean(removeBundle bool, forceRemoveBundle bool) error {
161165 err := r .Delete ()
162166
163167 if removeBundle && (err == nil || forceRemoveBundle ) {
164- err2 := os .RemoveAll (r .BundleDir )
168+ err2 := os .RemoveAll (r .bundleDir () )
165169 if err2 != nil && err == nil {
166170 err = err2
167171 }
0 commit comments