Skip to content

Commit

Permalink
Merge pull request #4550 from smarterclayton/rsh_like_ssh
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot committed Sep 13, 2015
2 parents 9b11c48 + 0de8255 commit 2695cdc
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 14 deletions.
3 changes: 3 additions & 0 deletions docs/generated/oc_by_example_content.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,9 @@ Start a shell session in a pod
// Open a shell session on the first container in pod 'foo'
$ openshift cli rsh foo
// Run the command 'cat /etc/resolv.conf' inside pod 'foo'
$ openshift cli rsh foo cat /etc/resolv.conf
----
====

Expand Down
1 change: 0 additions & 1 deletion hack/test-cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ set -o pipefail

OS_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${OS_ROOT}/hack/util.sh"

os::log::install_errexit

function cleanup()
Expand Down
5 changes: 4 additions & 1 deletion hack/test-end-to-end-scenario.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set -o pipefail

OS_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${OS_ROOT}/hack/util.sh"
os::log::install_errexit

ROUTER_TESTS_ENABLED="${ROUTER_TESTS_ENABLED:-true}"
TEST_ASSETS="${TEST_ASSETS:-false}"
Expand Down Expand Up @@ -149,7 +150,9 @@ echo "[INFO] Validating exec"
frontend_pod=$(oc get pod -l deploymentconfig=frontend -t '{{(index .items 0).metadata.name}}')
# when running as a restricted pod the registry will run with a pre-allocated
# user in the neighborhood of 1000000+. Look for a substring of the pre-allocated uid range
oc exec -p ${frontend_pod} id | grep 10
[ "$(oc exec -p ${frontend_pod} id | grep 1000)" ]
[ "$(oc rsh ${frontend_pod} id -u | grep 1000)" ]
[ "$(oc rsh -T ${frontend_pod} id -u | grep 1000)" ]

# Port forwarding
echo "[INFO] Validating port-forward"
Expand Down
3 changes: 1 addition & 2 deletions hack/test-end-to-end.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set -o pipefail

OS_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${OS_ROOT}/hack/util.sh"
os::log::install_errexit

ensure_iptables_or_die

Expand Down Expand Up @@ -45,6 +46,4 @@ start_os_server
# set our default KUBECONFIG location
export KUBECONFIG="${ADMIN_KUBECONFIG}"


${OS_ROOT}/hack/test-end-to-end-scenario.sh

1 change: 1 addition & 0 deletions hack/test-extended.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set -o pipefail
OS_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${OS_ROOT}/hack/common.sh"
source "${OS_ROOT}/hack/util.sh"
os::log::install_errexit

# Go to the top of the tree.
cd "${OS_ROOT}"
Expand Down
1 change: 1 addition & 0 deletions hack/test-integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set -o pipefail
OS_ROOT=$(dirname "${BASH_SOURCE}")/..
source "${OS_ROOT}/hack/common.sh"
source "${OS_ROOT}/hack/util.sh"
os::log::install_errexit

# Go to the top of the tree.
cd "${OS_ROOT}"
Expand Down
47 changes: 37 additions & 10 deletions pkg/cmd/cli/cmd/rsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"github.com/spf13/cobra"

kubecmd "k8s.io/kubernetes/pkg/kubectl/cmd"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"

cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)

Expand All @@ -18,12 +19,19 @@ Open a remote shell session to a container
This command will attempt to start a shell session in the specified pod. It will default to the
first container if none is specified, and will attempt to use '/bin/bash' as the default shell.
You may pass an optional command after the pod name, which will be executed instead of a login
shell. A TTY will be automatically allocated if standard input is interactive - use -t and -T
to override.
Note, some containers may not include a shell - use '%[1]s exec' if you need to run commands
directly.`

rshExample = `
// Open a shell session on the first container in pod 'foo'
$ %[1]s rsh foo`
$ %[1]s rsh foo
// Run the command 'cat /etc/resolv.conf' inside pod 'foo'
$ %[1]s rsh foo cat /etc/resolv.conf`
)

// NewCmdRsh attempts to open a shell session to the server.
Expand All @@ -39,29 +47,48 @@ func NewCmdRsh(fullName string, f *clientcmd.Factory, in io.Reader, out, err io.
Executor: &kubecmd.DefaultRemoteExecutor{},
}
executable := "/bin/bash"
forceTTY, disableTTY := false, false

cmd := &cobra.Command{
Use: "rsh POD",
Use: "rsh POD [command]",
Short: "Start a shell session in a pod",
Long: fmt.Sprintf(rshLong, fullName),
Example: fmt.Sprintf(rshExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
options.Command = []string{executable}
cmdutil.CheckErr(RunRsh(options, f, cmd, args))
switch {
case forceTTY && disableTTY:
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, "you may not specify -t and -T together"))
case forceTTY:
options.TTY = true
case disableTTY:
options.TTY = false
default:
options.TTY = cmdutil.IsTerminal(in)
}
if len(args) < 1 {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, "rsh requires a single Pod to connect to"))
}
options.PodName = args[0]
args = args[1:]
if len(args) > 0 {
options.Command = args
} else {
options.Command = []string{executable}
}

kcmdutil.CheckErr(RunRsh(options, f, cmd, args))
},
}
cmd.Flags().BoolVarP(&forceTTY, "tty", "t", false, "Force a pseudo-terminal to be allocated")
cmd.Flags().BoolVarP(&disableTTY, "no-tty", "T", false, "Disable pseudo-terminal allocation")
cmd.Flags().StringVar(&executable, "shell", executable, "Path to shell command")
cmd.Flags().StringVarP(&options.ContainerName, "container", "c", "", "Container name; defaults to first container")
cmd.Flags().SetInterspersed(false)
return cmd
}

// RunRsh starts a remote shell session on the server
func RunRsh(options *kubecmd.ExecOptions, f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmdutil.UsageError(cmd, "rsh requires a single POD to connect to")
}
options.PodName = args[0]

_, client, err := f.Clients()
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions rel-eng/completions/bash/oc
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,11 @@ _oc_rsh()
two_word_flags+=("-c")
flags+=("--help")
flags+=("-h")
flags+=("--no-tty")
flags+=("-T")
flags+=("--shell=")
flags+=("--tty")
flags+=("-t")

must_have_one_flag=()
must_have_one_noun=()
Expand Down
4 changes: 4 additions & 0 deletions rel-eng/completions/bash/openshift
Original file line number Diff line number Diff line change
Expand Up @@ -2583,7 +2583,11 @@ _openshift_cli_rsh()
two_word_flags+=("-c")
flags+=("--help")
flags+=("-h")
flags+=("--no-tty")
flags+=("-T")
flags+=("--shell=")
flags+=("--tty")
flags+=("-t")

must_have_one_flag=()
must_have_one_noun=()
Expand Down

0 comments on commit 2695cdc

Please sign in to comment.