Skip to content

Commit

Permalink
add exec command
Browse files Browse the repository at this point in the history
this command can run user commands inside container. This feature will be used
in kubevirtci to copy file within container into speacial folder which is mounted at
the same time in container and host. With this we can copy files from container to host,
without any hacks.

Signed-off-by: Karel Šimon <ksimon@redhat.com>
  • Loading branch information
ksimon1 committed Nov 19, 2019
1 parent 28a3d2e commit ddc200c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"context"
"os"

"github.com/fromanirh/pack8s/internal/pkg/podman"
"github.com/spf13/cobra"
)

type execOptions struct {
commands []string
}

var execOpt execOptions

// NewExecCommand runs given command inside container
func NewExecCommand() *cobra.Command {
exec := &cobra.Command{
Use: "exec",
Short: "exec runs given command in container",
RunE: execCommand,
}

exec.Flags().StringArrayVar(&execOpt.commands, "commands", []string{}, "commands which will be executed in container")

return exec
}

func execCommand(cmd *cobra.Command, args []string) error {
containerID := args[0]

podmanSocket, err := cmd.Flags().GetString("podman-socket")
if err != nil {
return err
}

ctx := context.Background()

hnd, err := podman.NewHandle(ctx, podmanSocket)
if err != nil {
return err
}

return hnd.Exec(containerID, execOpt.commands, os.Stdout)
}
3 changes: 3 additions & 0 deletions cmd/okd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type okdRunOptions struct {
sshWorkerPort uint
background bool
randomPorts bool
volume string
}

var okdRunOpts okdRunOptions
Expand Down Expand Up @@ -64,6 +65,7 @@ func NewRunCommand() *cobra.Command {
run.Flags().UintVar(&okdRunOpts.sshWorkerPort, "ssh-worker-port", 0, "port on localhost to ssh to worker node")
run.Flags().BoolVar(&okdRunOpts.background, "background", false, "go to background after nodes are up")
run.Flags().BoolVar(&okdRunOpts.randomPorts, "random-ports", true, "expose all ports on random localhost ports")
run.Flags().StringVar(&okdRunOpts.volume, "volume", "", "Bind mount a volume into the container")
return run
}

Expand Down Expand Up @@ -157,6 +159,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
Privileged: &okdRunOpts.privileged,
Publish: &clusterPorts,
PublishAll: &okdRunOpts.randomPorts,
Volume: &[]string{okdRunOpts.volume},
})
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewRootCommand() *cobra.Command {
NewSSHCommand(),
NewShowCommand(),
NewPruneVolumesCommand(),
NewExecCommand(),
)

return root
Expand Down

0 comments on commit ddc200c

Please sign in to comment.