Skip to content

Commit

Permalink
cmd: gather the logs from bootstrap instead of printing commands
Browse files Browse the repository at this point in the history
This changes the gather bootstrap to establish an SSH connection to bootstrap host to gather the logs, rather than printing the commands for
the user to execute.
  • Loading branch information
abhinavdahiya committed Jun 6, 2019
1 parent 6c082a8 commit cad7f02
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions cmd/openshift-install/gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/openshift/installer/pkg/asset/installconfig"
assetstore "github.com/openshift/installer/pkg/asset/store"
"github.com/openshift/installer/pkg/gather/ssh"
"github.com/openshift/installer/pkg/terraform"
gatheraws "github.com/openshift/installer/pkg/terraform/gather/aws"
gatherazure "github.com/openshift/installer/pkg/terraform/gather/azure"
Expand Down Expand Up @@ -71,7 +73,7 @@ func runGatherBootstrapCmd(directory string) error {
tfStateFilePath := filepath.Join(directory, terraform.StateFileName)
_, err := os.Stat(tfStateFilePath)
if os.IsNotExist(err) {
return unSupportedPlatformGather()
return unSupportedPlatformGather(directory)
}
if err != nil {
return err
Expand All @@ -95,30 +97,30 @@ func runGatherBootstrapCmd(directory string) error {
if err != nil {
if err2, ok := err.(errUnSupportedGatherPlatform); ok {
logrus.Error(err2)
return unSupportedPlatformGather()
return unSupportedPlatformGather(directory)
}
return errors.Wrapf(err, "failed to get bootstrap and control plane host addresses from %q", tfStateFilePath)
}

logGatherBootstrap(bootstrap, port, masters)
return nil
return logGatherBootstrap(bootstrap, port, masters, directory)
}

func logGatherBootstrap(bootstrap string, port int, masters []string) {
var (
sshport string
scpport string
)
if port != 22 {
sshport = fmt.Sprintf(" -p %d", port)
scpport = fmt.Sprintf(" -P %d", port)
func logGatherBootstrap(bootstrap string, port int, masters []string, directory string) {
logrus.Info("Pulling logs from bootstrap for debugging")

client, err := ssh.NewClient("core", fmt.Sprintf("%s:%d", bootstrap, port), nil)
if err != nil {
return errors.Wrap(err, "failed to create SSH client")
}
if s, ok := os.LookupEnv("SSH_AUTH_SOCK"); !ok || s == "" {
logrus.Info("Make sure ssh-agent is running, env SSH_AUTH_SOCK is set to the ssh-agent's UNIX socket and your private key is added to the agent.")
if err := ssh.Run(client, fmt.Sprintf("/usr/local/bin/installer-gather.sh %s", strings.Join(masters, " "))); err != nil {
return errors.Wrap(err, "failed to run remote command")
}
logrus.Info("Use the following commands to gather logs from the cluster")
logrus.Infof("ssh -A%s core@%s '/usr/local/bin/installer-gather.sh %s'", sshport, bootstrap, strings.Join(masters, " "))
logrus.Infof("scp%s core@%s:~/log-bundle.tar.gz .", scpport, bootstrap)
file := filepath.Join(directory, fmt.Sprintf("log-bundle-%s.tar.gz", time.Now().Format("20060102150405")))
if err := ssh.PullFileTo(client, "/home/core/log-bundle.tar.gz", file); err != nil {
return errors.Wrap(err, "failed to pull log file from remote")
}
logrus.Infof("Bootstrap gather logs captured here %q", file)
return nil
}

func extractHostAddresses(config *types.InstallConfig, tfstate *terraform.State) (bootstrap string, port int, masters []string, err error) {
Expand Down Expand Up @@ -175,11 +177,10 @@ func (e errUnSupportedGatherPlatform) Error() string {
return e.Message
}

func unSupportedPlatformGather() error {
func unSupportedPlatformGather(directory string) error {
if gatherBootstrapOpts.bootstrap == "" || len(gatherBootstrapOpts.masters) == 0 {
return errors.New("boostrap host address and at least one control plane host address must be provided")
}

logGatherBootstrap(gatherBootstrapOpts.bootstrap, 22, gatherBootstrapOpts.masters)
return nil
return logGatherBootstrap(gatherBootstrapOpts.bootstrap, 22, gatherBootstrapOpts.masters, directory)
}

0 comments on commit cad7f02

Please sign in to comment.