ocic: Add container exec command
Signed-off-by: Jacek J. Łakis <jacek.lakis@intel.com>
This commit is contained in:
parent
bf51655a7b
commit
3babbf0de1
1 changed files with 68 additions and 0 deletions
|
@ -22,6 +22,7 @@ var containerCommand = cli.Command{
|
||||||
containerStatusCommand,
|
containerStatusCommand,
|
||||||
listContainersCommand,
|
listContainersCommand,
|
||||||
execSyncCommand,
|
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 {
|
type listOptions struct {
|
||||||
// id of the container
|
// id of the container
|
||||||
id string
|
id string
|
||||||
|
@ -441,6 +481,34 @@ func ExecSync(client pb.RuntimeServiceClient, ID string, cmd []string, timeout i
|
||||||
return nil
|
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
|
// ListContainers sends a ListContainerRequest to the server, and parses
|
||||||
// the returned ListContainerResponse.
|
// the returned ListContainerResponse.
|
||||||
func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
|
func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
|
||||||
|
|
Loading…
Reference in a new issue