Add exec to example

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-09-28 15:27:24 -07:00
parent b3ab74fee3
commit df79231bdf
4 changed files with 50 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
*.exe
main
/containerd/containerd
/containerd-shim/containerd-shim
/bin/

View file

@ -100,9 +100,10 @@ func (c *Container) NewProcess(spec *specs.Process) (*Process, error) {
c.mu.Lock()
defer c.mu.Unlock()
process := &Process{
s: spec,
c: c,
exec: true,
s: spec,
c: c,
exec: true,
driver: c.driver,
}
c.processes = append(c.processes, process)
return process, nil
@ -123,8 +124,9 @@ func (c *Container) Pid() int {
// Wait will perform a blocking wait on the init process of the container
func (c *Container) Wait() (uint32, error) {
c.mu.Lock()
defer c.mu.Unlock()
return c.init.Wait()
proc := c.init
c.mu.Unlock()
return proc.Wait()
}
// Signal will send the provided signal to the init process of the container

View file

@ -1,6 +1,7 @@
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
@ -65,6 +66,43 @@ func runTest() error {
return err
}
for i := 0; i < 10; i++ {
process, err := container.NewProcess(&specs.Process{
Args: []string{
"echo", fmt.Sprintf("sup from itteration %d", i),
},
Env: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"},
Terminal: false,
Cwd: "/",
NoNewPrivileges: true,
Capabilities: []string{
"CAP_AUDIT_WRITE",
"CAP_KILL",
"CAP_FOWNER",
"CAP_CHOWN",
"CAP_MKNOD",
"CAP_FSETID",
"CAP_DAC_OVERRIDE",
"CAP_SETFCAP",
"CAP_SETPCAP",
"CAP_SETGID",
"CAP_SETUID",
"CAP_NET_BIND_SERVICE",
},
})
process.Stdin = os.Stdin
process.Stdout = os.Stdout
process.Stderr = os.Stderr
if err := process.Start(); err != nil {
return err
}
procStatus, err := process.Wait()
if err != nil {
return err
}
logrus.Infof("process %d returned with %d", i, procStatus)
}
// wait for it to exit and get the exit status
logrus.Info("wait container")
status, err := container.Wait()

View file

@ -66,8 +66,11 @@ func (r *Runc) Exec(c *containerkit.Container, p *containerkit.Process) (contain
if err != nil {
return nil, err
}
cmd := r.command("exec", "--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
if err := cmd.Run(); err != nil {
return nil, err
}
data, err := ioutil.ReadFile(pidFile)
if err != nil {
return nil, err