-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Liang Chenye <liangchenye@huawei.com>
- Loading branch information
1 parent
3db644c
commit 5a90e8a
Showing
3 changed files
with
215 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package validation | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
rspecs "github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/opencontainers/runtime-tools/generate" | ||
) | ||
|
||
type Runtime struct { | ||
RuntimeCommand string | ||
BundleDir string | ||
ID string | ||
} | ||
|
||
func NewRuntime(runtimeCommand string, bundleDir string) (Runtime, error) { | ||
var r Runtime | ||
var err error | ||
r.RuntimeCommand, err = exec.LookPath(runtimeCommand) | ||
if err != nil { | ||
return Runtime{}, err | ||
} | ||
|
||
r.BundleDir = bundleDir | ||
return r, err | ||
} | ||
|
||
func (r *Runtime) SetConfig(g *generate.Generator) error { | ||
if r.BundleDir == "" { | ||
return errors.New("Please set the bundle directory first") | ||
} | ||
return g.SaveToFile(filepath.Join(r.BundleDir, "config.json"), generate.ExportOptions{}) | ||
} | ||
|
||
func (r *Runtime) SetID(id string) { | ||
r.ID = id | ||
} | ||
|
||
func (r *Runtime) Create() error { | ||
var args []string | ||
args = append(args, "create") | ||
if r.ID != "" { | ||
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) | ||
// } | ||
cmd := exec.Command(r.RuntimeCommand, args...) | ||
cmd.Dir = r.BundleDir | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
func (r *Runtime) Start() error { | ||
var args []string | ||
args = append(args, "start") | ||
if r.ID != "" { | ||
args = append(args, r.ID) | ||
} | ||
|
||
cmd := exec.Command(r.RuntimeCommand, args...) | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
return cmd.Run() | ||
} | ||
|
||
func (r *Runtime) State() (rspecs.State, error) { | ||
var args []string | ||
args = append(args, "state") | ||
if r.ID != "" { | ||
args = append(args, r.ID) | ||
} | ||
|
||
out, err := exec.Command(r.RuntimeCommand, args...).Output() | ||
if err != nil { | ||
return rspecs.State{}, err | ||
} | ||
|
||
var state rspecs.State | ||
err = json.Unmarshal(out, &state) | ||
return state, err | ||
} | ||
|
||
func (r *Runtime) Delete() error { | ||
var args []string | ||
args = append(args, "delete") | ||
if r.ID != "" { | ||
args = append(args, r.ID) | ||
} | ||
|
||
cmd := exec.Command(r.RuntimeCommand, args...) | ||
return cmd.Run() | ||
} | ||
|
||
func (r *Runtime) Clean(removeBundle bool) error { | ||
err := r.Delete() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if removeBundle { | ||
os.RemoveAll(r.BundleDir) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters