From 3babbf0de1091597d379637074e5f308362ae7ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20J=2E=20=C5=81akis?= Date: Wed, 8 Feb 2017 14:20:38 +0100 Subject: [PATCH] ocic: Add container exec command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jacek J. Łakis --- cmd/ocic/container.go | 68 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/cmd/ocic/container.go b/cmd/ocic/container.go index bdb27489..7b76a828 100644 --- a/cmd/ocic/container.go +++ b/cmd/ocic/container.go @@ -22,6 +22,7 @@ var containerCommand = cli.Command{ containerStatusCommand, listContainersCommand, execSyncCommand, + execCommand, }, } @@ -236,6 +237,45 @@ var execSyncCommand = cli.Command{ }, } +var execCommand = cli.Command{ + Name: "exec", + Usage: "prepare a streaming endpoint to execute a command in the container", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "id", + Value: "", + Usage: "id of the container", + }, + cli.BoolFlag{ + Name: "tty", + Usage: "whether to use tty", + }, + cli.BoolFlag{ + Name: "stdin", + Usage: "whether to stream to stdin", + }, + cli.BoolFlag{ + Name: "url", + Usage: "do not exec command, just prepare streaming endpoint", + }, + }, + Action: func(context *cli.Context) error { + // Set up a connection to the server. + conn, err := getClientConnection(context) + if err != nil { + return fmt.Errorf("failed to connect: %v", err) + } + defer conn.Close() + client := pb.NewRuntimeServiceClient(conn) + + err = Exec(client, context.String("id"), context.Bool("tty"), context.Bool("stdin"), context.Bool("url"), context.Args()) + if err != nil { + return fmt.Errorf("execing command in container failed: %v", err) + } + return nil + }, +} + type listOptions struct { // id of the container id string @@ -441,6 +481,34 @@ func ExecSync(client pb.RuntimeServiceClient, ID string, cmd []string, timeout i return nil } +// Exec sends an ExecRequest to the server, and parses +// the returned ExecResponse. +func Exec(client pb.RuntimeServiceClient, ID string, tty bool, stdin bool, urlOnly bool, cmd []string) error { + if ID == "" { + return fmt.Errorf("ID cannot be empty") + } + r, err := client.Exec(context.Background(), &pb.ExecRequest{ + ContainerId: ID, + Cmd: cmd, + Tty: tty, + Stdin: stdin, + }) + if err != nil { + return err + } + + url := r.Url + + if urlOnly { + fmt.Println("URL:") + fmt.Println(url) + return nil + } + + // Do exec here + return nil +} + // ListContainers sends a ListContainerRequest to the server, and parses // the returned ListContainerResponse. func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {