2016-12-09 17:17:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gocontext "context"
|
|
|
|
|
|
|
|
"github.com/docker/containerd/api/execution"
|
|
|
|
"github.com/tonistiigi/fifo"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
)
|
|
|
|
|
|
|
|
var grpcConn *grpc.ClientConn
|
|
|
|
|
2017-01-12 18:24:22 +00:00
|
|
|
func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup, error) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
ctx := gocontext.Background()
|
2016-12-09 17:17:34 +00:00
|
|
|
|
2017-01-12 18:24:22 +00:00
|
|
|
f, err := fifo.OpenFifo(ctx, stdin, syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func(c io.Closer) {
|
2016-12-09 17:17:34 +00:00
|
|
|
if err != nil {
|
2017-01-12 18:24:22 +00:00
|
|
|
c.Close()
|
2016-12-09 17:17:34 +00:00
|
|
|
}
|
2017-01-12 18:24:22 +00:00
|
|
|
}(f)
|
2017-01-17 11:04:20 +00:00
|
|
|
go func(w io.WriteCloser) {
|
2017-01-12 18:24:22 +00:00
|
|
|
io.Copy(w, os.Stdin)
|
2017-01-17 11:04:20 +00:00
|
|
|
w.Close()
|
2017-01-12 18:24:22 +00:00
|
|
|
}(f)
|
2016-12-09 17:17:34 +00:00
|
|
|
|
2017-01-12 18:24:22 +00:00
|
|
|
f, err = fifo.OpenFifo(ctx, stdout, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func(c io.Closer) {
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
2016-12-09 17:17:34 +00:00
|
|
|
}
|
2017-01-12 18:24:22 +00:00
|
|
|
}(f)
|
|
|
|
wg.Add(1)
|
2017-01-17 11:04:20 +00:00
|
|
|
go func(r io.ReadCloser) {
|
2017-01-12 18:24:22 +00:00
|
|
|
io.Copy(os.Stdout, r)
|
2017-01-17 11:04:20 +00:00
|
|
|
r.Close()
|
2017-01-12 18:24:22 +00:00
|
|
|
wg.Done()
|
|
|
|
}(f)
|
2016-12-09 17:17:34 +00:00
|
|
|
|
2017-01-12 18:24:22 +00:00
|
|
|
f, err = fifo.OpenFifo(ctx, stderr, syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func(c io.Closer) {
|
|
|
|
if err != nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}(f)
|
|
|
|
if !console {
|
|
|
|
wg.Add(1)
|
2017-01-17 11:04:20 +00:00
|
|
|
go func(r io.ReadCloser) {
|
2017-01-12 18:24:22 +00:00
|
|
|
io.Copy(os.Stderr, r)
|
2017-01-17 11:04:20 +00:00
|
|
|
r.Close()
|
2017-01-12 18:24:22 +00:00
|
|
|
wg.Done()
|
|
|
|
}(f)
|
2016-12-09 17:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &wg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) {
|
|
|
|
if grpcConn != nil {
|
|
|
|
return grpcConn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bindSocket := context.GlobalString("socket")
|
|
|
|
// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
|
|
|
|
grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
|
|
|
|
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(100 * time.Second)}
|
|
|
|
dialOpts = append(dialOpts,
|
|
|
|
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
return net.DialTimeout("unix", bindSocket, timeout)
|
|
|
|
},
|
|
|
|
))
|
|
|
|
|
|
|
|
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", bindSocket), dialOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcConn = conn
|
|
|
|
return grpcConn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExecutionService(context *cli.Context) (execution.ExecutionServiceClient, error) {
|
|
|
|
conn, err := getGRPCConnection(context)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return execution.NewExecutionServiceClient(conn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTempDir(id string) (string, error) {
|
|
|
|
err := os.MkdirAll(filepath.Join(os.TempDir(), "ctr"), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpDir, err := ioutil.TempDir(filepath.Join(os.TempDir(), "ctr"), fmt.Sprintf("%s-", id))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return tmpDir, nil
|
|
|
|
}
|