Update oci executor to use Opts
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
23adfe42f9
commit
be20dd0484
4 changed files with 54 additions and 48 deletions
|
@ -120,6 +120,9 @@ func (c *Container) Create() error {
|
||||||
c.init = &Process{
|
c.init = &Process{
|
||||||
d: d,
|
d: d,
|
||||||
driver: c.driver,
|
driver: c.driver,
|
||||||
|
Stdin: c.Stdin,
|
||||||
|
Stdout: c.Stdout,
|
||||||
|
Stderr: c.Stderr,
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,6 @@ func runContainer() error {
|
||||||
driver, err := oci.New(oci.Opts{
|
driver, err := oci.New(oci.Opts{
|
||||||
Root: "/run/runc",
|
Root: "/run/runc",
|
||||||
Name: "runc",
|
Name: "runc",
|
||||||
LogFile: "/tmp/runc",
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
52
oci/oci.go
52
oci/oci.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/containerkit"
|
"github.com/docker/containerkit"
|
||||||
|
@ -18,7 +17,6 @@ type Opts struct {
|
||||||
Name string
|
Name string
|
||||||
Root string
|
Root string
|
||||||
Args []string
|
Args []string
|
||||||
LogFile string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(opts Opts) (*OCIRuntime, error) {
|
func New(opts Opts) (*OCIRuntime, error) {
|
||||||
|
@ -27,7 +25,6 @@ func New(opts Opts) (*OCIRuntime, error) {
|
||||||
}
|
}
|
||||||
return &OCIRuntime{
|
return &OCIRuntime{
|
||||||
root: opts.Root,
|
root: opts.Root,
|
||||||
log: opts.LogFile,
|
|
||||||
name: opts.Name,
|
name: opts.Name,
|
||||||
args: opts.Args,
|
args: opts.Args,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -39,15 +36,17 @@ type OCIRuntime struct {
|
||||||
root string
|
root string
|
||||||
// name is the name of the runtime, i.e. runc
|
// name is the name of the runtime, i.e. runc
|
||||||
name string
|
name string
|
||||||
// log is the path to the log files for the containers
|
|
||||||
log string
|
|
||||||
// args specifies additional arguments to the OCI runtime
|
// args specifies additional arguments to the OCI runtime
|
||||||
args []string
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *OCIRuntime) Name() string {
|
||||||
|
return r.name
|
||||||
|
}
|
||||||
|
|
||||||
func (r *OCIRuntime) Create(c *containerkit.Container) (containerkit.ProcessDelegate, error) {
|
func (r *OCIRuntime) Create(c *containerkit.Container) (containerkit.ProcessDelegate, error) {
|
||||||
pidFile := fmt.Sprintf("%s/%s.pid", filepath.Join(r.root, c.ID()), "init")
|
pidFile := fmt.Sprintf("%s/%s.pid", filepath.Join(r.root, c.ID()), "init")
|
||||||
cmd := r.command("create", "--pid-file", pidFile, "--bundle", c.Path(), c.ID())
|
cmd := r.Command("create", "--pid-file", pidFile, "--bundle", c.Path(), c.ID())
|
||||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = c.Stdin, c.Stdout, c.Stderr
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = c.Stdin, c.Stdout, c.Stderr
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -64,11 +63,11 @@ func (r *OCIRuntime) Create(c *containerkit.Container) (containerkit.ProcessDele
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *OCIRuntime) Start(c *containerkit.Container) error {
|
func (r *OCIRuntime) Start(c *containerkit.Container) error {
|
||||||
return r.command("start", c.ID()).Run()
|
return r.Command("start", c.ID()).Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *OCIRuntime) Delete(c *containerkit.Container) error {
|
func (r *OCIRuntime) Delete(c *containerkit.Container) error {
|
||||||
return r.command("delete", c.ID()).Run()
|
return r.Command("delete", c.ID()).Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *OCIRuntime) Exec(c *containerkit.Container, p *containerkit.Process) (containerkit.ProcessDelegate, error) {
|
func (r *OCIRuntime) Exec(c *containerkit.Container, p *containerkit.Process) (containerkit.ProcessDelegate, error) {
|
||||||
|
@ -83,7 +82,7 @@ func (r *OCIRuntime) Exec(c *containerkit.Container, p *containerkit.Process) (c
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
cmd := r.command("exec", "--detach", "--process", path, "--pid-file", pidFile, c.ID())
|
cmd := r.Command("exec", "--detach", "--process", path, "--pid-file", pidFile, c.ID())
|
||||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = p.Stdin, p.Stdout, p.Stderr
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = p.Stdin, p.Stdout, p.Stderr
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -110,7 +109,7 @@ type state struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *OCIRuntime) Load(id string) (containerkit.ProcessDelegate, error) {
|
func (r *OCIRuntime) Load(id string) (containerkit.ProcessDelegate, error) {
|
||||||
data, err := r.command("state", id).Output()
|
data, err := r.Command("state", id).Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -121,40 +120,9 @@ func (r *OCIRuntime) Load(id string) (containerkit.ProcessDelegate, error) {
|
||||||
return newProcess(s.Pid)
|
return newProcess(s.Pid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *OCIRuntime) command(args ...string) *exec.Cmd {
|
func (r *OCIRuntime) Command(args ...string) *exec.Cmd {
|
||||||
baseArgs := append([]string{
|
baseArgs := append([]string{
|
||||||
"--root", r.root,
|
"--root", r.root,
|
||||||
"--log", r.log,
|
|
||||||
}, r.args...)
|
}, r.args...)
|
||||||
return exec.Command(r.name, append(baseArgs, args...)...)
|
return exec.Command(r.name, append(baseArgs, args...)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newProcess(pid int) (*process, error) {
|
|
||||||
proc, err := os.FindProcess(pid)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &process{
|
|
||||||
proc: proc,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type process struct {
|
|
||||||
proc *os.Process
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *process) Pid() int {
|
|
||||||
return p.proc.Pid
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *process) Wait() (uint32, error) {
|
|
||||||
state, err := p.proc.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return uint32(state.Sys().(syscall.WaitStatus).ExitStatus()), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *process) Signal(s os.Signal) error {
|
|
||||||
return p.proc.Signal(s)
|
|
||||||
}
|
|
||||||
|
|
36
oci/process.go
Normal file
36
oci/process.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package oci
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newProcess(pid int) (*process, error) {
|
||||||
|
proc, err := os.FindProcess(pid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &process{
|
||||||
|
proc: proc,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type process struct {
|
||||||
|
proc *os.Process
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *process) Pid() int {
|
||||||
|
return p.proc.Pid
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *process) Wait() (uint32, error) {
|
||||||
|
state, err := p.proc.Wait()
|
||||||
|
if err != nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
return uint32(state.Sys().(syscall.WaitStatus).ExitStatus()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *process) Signal(s os.Signal) error {
|
||||||
|
return p.proc.Signal(s)
|
||||||
|
}
|
Loading…
Reference in a new issue