Skip to content

Commit

Permalink
Implement cluster status command
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Minter committed Sep 30, 2016
1 parent b6ba8ec commit 8ce8f13
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 37 deletions.
45 changes: 45 additions & 0 deletions contrib/completions/bash/oc
Original file line number Diff line number Diff line change
Expand Up @@ -5323,6 +5323,50 @@ _oc_cluster_down()
noun_aliases=()
}

_oc_cluster_status()
{
last_command="oc_cluster_status"
commands=()

flags=()
two_word_flags=()
local_nonpersistent_flags=()
flags_with_completion=()
flags_completion=()

flags+=("--docker-machine=")
local_nonpersistent_flags+=("--docker-machine=")
flags+=("--as=")
flags+=("--certificate-authority=")
flags_with_completion+=("--certificate-authority")
flags_completion+=("_filedir")
flags+=("--client-certificate=")
flags_with_completion+=("--client-certificate")
flags_completion+=("_filedir")
flags+=("--client-key=")
flags_with_completion+=("--client-key")
flags_completion+=("_filedir")
flags+=("--cluster=")
flags+=("--config=")
flags_with_completion+=("--config")
flags_completion+=("_filedir")
flags+=("--context=")
flags+=("--insecure-skip-tls-verify")
flags+=("--log-flush-frequency=")
flags+=("--loglevel=")
flags+=("--logspec=")
flags+=("--match-server-version")
flags+=("--namespace=")
two_word_flags+=("-n")
flags+=("--server=")
flags+=("--token=")
flags+=("--user=")

must_have_one_flag=()
must_have_one_noun=()
noun_aliases=()
}

_oc_cluster_up()
{
last_command="oc_cluster_up"
Expand Down Expand Up @@ -5401,6 +5445,7 @@ _oc_cluster()
last_command="oc_cluster"
commands=()
commands+=("down")
commands+=("status")
commands+=("up")

flags=()
Expand Down
46 changes: 46 additions & 0 deletions contrib/completions/bash/openshift
Original file line number Diff line number Diff line change
Expand Up @@ -9796,6 +9796,51 @@ _openshift_cli_cluster_down()
noun_aliases=()
}

_openshift_cli_cluster_status()
{
last_command="openshift_cli_cluster_status"
commands=()

flags=()
two_word_flags=()
local_nonpersistent_flags=()
flags_with_completion=()
flags_completion=()

flags+=("--docker-machine=")
local_nonpersistent_flags+=("--docker-machine=")
flags+=("--as=")
flags+=("--certificate-authority=")
flags_with_completion+=("--certificate-authority")
flags_completion+=("_filedir")
flags+=("--client-certificate=")
flags_with_completion+=("--client-certificate")
flags_completion+=("_filedir")
flags+=("--client-key=")
flags_with_completion+=("--client-key")
flags_completion+=("_filedir")
flags+=("--cluster=")
flags+=("--config=")
flags_with_completion+=("--config")
flags_completion+=("_filedir")
flags+=("--context=")
flags+=("--google-json-key=")
flags+=("--insecure-skip-tls-verify")
flags+=("--log-flush-frequency=")
flags+=("--loglevel=")
flags+=("--logspec=")
flags+=("--match-server-version")
flags+=("--namespace=")
two_word_flags+=("-n")
flags+=("--server=")
flags+=("--token=")
flags+=("--user=")

must_have_one_flag=()
must_have_one_noun=()
noun_aliases=()
}

_openshift_cli_cluster_up()
{
last_command="openshift_cli_cluster_up"
Expand Down Expand Up @@ -9875,6 +9920,7 @@ _openshift_cli_cluster()
last_command="openshift_cli_cluster"
commands=()
commands+=("down")
commands+=("status")
commands+=("up")

flags=()
Expand Down
45 changes: 45 additions & 0 deletions contrib/completions/zsh/oc
Original file line number Diff line number Diff line change
Expand Up @@ -5484,6 +5484,50 @@ _oc_cluster_down()
noun_aliases=()
}

_oc_cluster_status()
{
last_command="oc_cluster_status"
commands=()

flags=()
two_word_flags=()
local_nonpersistent_flags=()
flags_with_completion=()
flags_completion=()

flags+=("--docker-machine=")
local_nonpersistent_flags+=("--docker-machine=")
flags+=("--as=")
flags+=("--certificate-authority=")
flags_with_completion+=("--certificate-authority")
flags_completion+=("_filedir")
flags+=("--client-certificate=")
flags_with_completion+=("--client-certificate")
flags_completion+=("_filedir")
flags+=("--client-key=")
flags_with_completion+=("--client-key")
flags_completion+=("_filedir")
flags+=("--cluster=")
flags+=("--config=")
flags_with_completion+=("--config")
flags_completion+=("_filedir")
flags+=("--context=")
flags+=("--insecure-skip-tls-verify")
flags+=("--log-flush-frequency=")
flags+=("--loglevel=")
flags+=("--logspec=")
flags+=("--match-server-version")
flags+=("--namespace=")
two_word_flags+=("-n")
flags+=("--server=")
flags+=("--token=")
flags+=("--user=")

must_have_one_flag=()
must_have_one_noun=()
noun_aliases=()
}

_oc_cluster_up()
{
last_command="oc_cluster_up"
Expand Down Expand Up @@ -5562,6 +5606,7 @@ _oc_cluster()
last_command="oc_cluster"
commands=()
commands+=("down")
commands+=("status")
commands+=("up")

flags=()
Expand Down
46 changes: 46 additions & 0 deletions contrib/completions/zsh/openshift
Original file line number Diff line number Diff line change
Expand Up @@ -9957,6 +9957,51 @@ _openshift_cli_cluster_down()
noun_aliases=()
}

_openshift_cli_cluster_status()
{
last_command="openshift_cli_cluster_status"
commands=()

flags=()
two_word_flags=()
local_nonpersistent_flags=()
flags_with_completion=()
flags_completion=()

flags+=("--docker-machine=")
local_nonpersistent_flags+=("--docker-machine=")
flags+=("--as=")
flags+=("--certificate-authority=")
flags_with_completion+=("--certificate-authority")
flags_completion+=("_filedir")
flags+=("--client-certificate=")
flags_with_completion+=("--client-certificate")
flags_completion+=("_filedir")
flags+=("--client-key=")
flags_with_completion+=("--client-key")
flags_completion+=("_filedir")
flags+=("--cluster=")
flags+=("--config=")
flags_with_completion+=("--config")
flags_completion+=("_filedir")
flags+=("--context=")
flags+=("--google-json-key=")
flags+=("--insecure-skip-tls-verify")
flags+=("--log-flush-frequency=")
flags+=("--loglevel=")
flags+=("--logspec=")
flags+=("--match-server-version")
flags+=("--namespace=")
two_word_flags+=("-n")
flags+=("--server=")
flags+=("--token=")
flags+=("--user=")

must_have_one_flag=()
must_have_one_noun=()
noun_aliases=()
}

_openshift_cli_cluster_up()
{
last_command="openshift_cli_cluster_up"
Expand Down Expand Up @@ -10036,6 +10081,7 @@ _openshift_cli_cluster()
last_command="openshift_cli_cluster"
commands=()
commands+=("down")
commands+=("status")
commands+=("up")

flags=()
Expand Down
24 changes: 4 additions & 20 deletions pkg/bootstrap/docker/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,29 @@ import (
"io"
"strings"

docker "github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
"github.com/spf13/cobra"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubelet/dockertools"

"github.com/openshift/origin/pkg/bootstrap/docker/dockerhelper"
"github.com/openshift/origin/pkg/bootstrap/docker/errors"
"github.com/openshift/origin/pkg/bootstrap/docker/openshift"
osclientcmd "github.com/openshift/origin/pkg/cmd/util/clientcmd"
dockerutil "github.com/openshift/origin/pkg/cmd/util/docker"
)

const (
cmdDownLong = `
Stops the container running OpenShift on Docker and associated containers.
If you started your OpenShift with a specific docker-machine, you need to specify the
If you started your OpenShift with a specific docker-machine, you need to specify the
same machine using the --docker-machine argument.
`

cmdDownExample = `
# Stop local Docker cluster
# Stop local OpenShift cluster
%[1]s
# Stop cluster running on Docker machine 'mymachine'
# Stop OpenShift cluster running on Docker machine 'mymachine'
%[1]s --docker-machine=mymachine
`

Expand Down Expand Up @@ -60,7 +57,7 @@ func NewCmdDown(name, fullName string, f *osclientcmd.Factory, out io.Writer) *c
// Stop stops the currently running origin container and any
// containers started by the node.
func (c *ClientStopConfig) Stop(out io.Writer) error {
client, err := c.getDockerClient(out)
client, _, err := getDockerClient(out, c.DockerMachine, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -94,16 +91,3 @@ func (c *ClientStopConfig) Stop(out io.Writer) error {
}
return nil
}

func (c *ClientStopConfig) getDockerClient(out io.Writer) (*docker.Client, error) {
// Get Docker client
if len(c.DockerMachine) > 0 {
client, _, err := getDockerMachineClient(c.DockerMachine, out)
return client, err
}
client, _, err := dockerutil.NewHelper().GetClient()
if err != nil {
return nil, errors.ErrNoDockerClient(err)
}
return client, nil
}
86 changes: 86 additions & 0 deletions pkg/bootstrap/docker/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package docker

import (
"fmt"
"io"
"time"

"github.com/spf13/cobra"

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

"github.com/openshift/origin/pkg/bootstrap/docker/dockerhelper"
"github.com/openshift/origin/pkg/bootstrap/docker/errors"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)

const (
// CmdStatusRecommendedName is the recommended command name
CmdStatusRecommendedName = "status"

cmdStatusLong = `
Show the status of the local OpenShift cluster.
If you started your OpenShift with a specific docker-machine, you need to specify the
same machine using the --docker-machine argument.
`
cmdStatusExample = `
# See status of local OpenShift cluster
%[1]s
# See status of OpenShift cluster running on Docker machine 'mymachine'
%[1]s --docker-machine=mymachine
`
)

// NewCmdStatus implements the OpenShift cluster status command.
func NewCmdStatus(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
config := &ClientStatusConfig{}
cmd := &cobra.Command{
Use: name,
Short: "Show OpenShift on Docker status",
Long: cmdStatusLong,
Example: fmt.Sprintf(cmdStatusExample, fullName),
Run: func(c *cobra.Command, args []string) {
kcmdutil.CheckErr(config.Status(f, out))
},
}
cmd.Flags().StringVar(&config.DockerMachine, "docker-machine", "", "Specify the Docker machine to use")
return cmd
}

// ClientStatusConfig is the configuration for the client status command
type ClientStatusConfig struct {
DockerMachine string
}

// Status prints the developer cluster status
func (c *ClientStatusConfig) Status(f *clientcmd.Factory, out io.Writer) error {
dockerClient, _, err := getDockerClient(out, c.DockerMachine, false)
if err != nil {
return errors.ErrNoDockerClient(err)
}
helper := dockerhelper.NewHelper(dockerClient, nil)

_, running, err := helper.GetContainerState(openShiftContainer)
if err != nil {
return errors.NewError("cannot get state of OpenShift container %s", openShiftContainer).WithCause(err)
}

if running {
client, _, err := f.Clients()
if err != nil {
return err
}

var statusCode int
client.Client.Timeout = 10 * time.Second
client.Get().AbsPath("/healthz").Do().StatusCode(&statusCode)
if statusCode == 200 {
fmt.Fprintf(out, "success: OpenShift cluster is running\n")
return nil
}
}

return errors.NewError("OpenShift cluster is not running")
}
Loading

0 comments on commit 8ce8f13

Please sign in to comment.