Add --pid-file to ctr
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
129167132c
commit
d3a8055e2d
@ -20,6 +20,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
@ -99,3 +100,22 @@ func PrintAsJSON(x interface{}) {
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
|
||||
// WritePidFile writes the pid atomically to a file
|
||||
func WritePidFile(path string, pid int) error {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
|
||||
f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Fprintf(f, "%d", pid)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tempPath, path)
|
||||
}
|
||||
|
@ -85,6 +85,10 @@ var ContainerFlags = []cli.Flag{
|
||||
Name: "with-ns",
|
||||
Usage: "specify existing Linux namespaces to join at container runtime (format '<nstype>:<path>')",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "pid-file",
|
||||
Usage: "file path to write the task's pid",
|
||||
},
|
||||
}
|
||||
|
||||
func loadSpec(path string, s *specs.Spec) error {
|
||||
@ -211,6 +215,11 @@ var Command = cli.Command{
|
||||
return err
|
||||
}
|
||||
}
|
||||
if context.IsSet("pid-file") {
|
||||
if err := commands.WritePidFile(context.String("pid-file"), int(task.Pid())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var con console.Console
|
||||
if tty {
|
||||
con = console.Current()
|
||||
|
@ -38,6 +38,10 @@ var startCommand = cli.Command{
|
||||
Name: "fifo-dir",
|
||||
Usage: "directory used for storing IO FIFOs",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "pid-file",
|
||||
Usage: "file path to write the task's pid",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
var (
|
||||
@ -72,7 +76,11 @@ var startCommand = cli.Command{
|
||||
return err
|
||||
}
|
||||
defer task.Delete(ctx)
|
||||
|
||||
if context.IsSet("pid-file") {
|
||||
if err := commands.WritePidFile(context.String("pid-file"), int(task.Pid())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
statusC, err := task.Wait(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user