-
Notifications
You must be signed in to change notification settings - Fork 1.5k
cmd: gather the logs from bootstrap instead of printing commands #1822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-robot
merged 4 commits into
openshift:master
from
abhinavdahiya:gather_agent
Jun 10, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9a237b5
vendor: add github.com/pkg/sftp and golang.org/x/crypto/ssh/agent
abhinavdahiya 6c082a8
pkg: add gather/ssh package for utilities for gathering using ssh
abhinavdahiya cad7f02
cmd: gather the logs from bootstrap instead of printing commands
abhinavdahiya 4326be6
cmd/openshift-install: allow users to specify the SSH key to used for…
abhinavdahiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package ssh | ||
|
|
||
| import ( | ||
| "github.com/pkg/errors" | ||
| "golang.org/x/crypto/ssh/agent" | ||
|
|
||
| utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
| ) | ||
|
|
||
| // newAgent initializes an SSH Agent with the keys. | ||
| // If no keys are provided, it loads all the keys from the user's environment. | ||
| func newAgent(keyPaths []string) (agent.Agent, error) { | ||
| keys, err := loadKeys(keyPaths) | ||
wking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(keys) == 0 { | ||
| return nil, errors.New("no keys found for SSH agent") | ||
| } | ||
|
|
||
| ag := agent.NewKeyring() | ||
| var errs []error | ||
| for idx := range keys { | ||
| if err := ag.Add(agent.AddedKey{PrivateKey: keys[idx]}); err != nil { | ||
wking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| errs = append(errs, errors.Wrap(err, "failed to add key to agent")) | ||
| } | ||
| } | ||
| if agg := utilerrors.NewAggregate(errs); agg != nil { | ||
| return nil, agg | ||
wking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return ag, nil | ||
| } | ||
|
|
||
| func loadKeys(paths []string) ([]interface{}, error) { | ||
| if len(paths) > 0 { | ||
| return LoadPrivateSSHKeys(paths) | ||
| } | ||
| return defaultPrivateSSHKeys() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Package ssh contains utilities that help gather logs, etc. on failures using ssh. | ||
| package ssh | ||
|
|
||
| import ( | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/openshift/installer/pkg/lineprinter" | ||
| "github.com/pkg/errors" | ||
| "github.com/pkg/sftp" | ||
| "github.com/sirupsen/logrus" | ||
| "golang.org/x/crypto/ssh" | ||
| "golang.org/x/crypto/ssh/agent" | ||
|
|
||
| utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
| ) | ||
|
|
||
| // NewClient creates a new SSH client which can be used to SSH to address using user and the keys. | ||
| // | ||
| // if keys list is empty, it tries to load the keys from the user's environment. | ||
| func NewClient(user, address string, keys []string) (*ssh.Client, error) { | ||
| ag, err := newAgent(keys) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to initialize the SSH agent") | ||
| } | ||
|
|
||
| client, err := ssh.Dial("tcp", address, &ssh.ClientConfig{ | ||
| User: user, | ||
| Auth: []ssh.AuthMethod{ | ||
| // Use a callback rather than PublicKeys | ||
| // so we only consult the agent once the remote server | ||
| // wants it. | ||
| ssh.PublicKeysCallback(ag.Signers), | ||
| }, | ||
| HostKeyCallback: ssh.InsecureIgnoreHostKey(), | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := agent.ForwardToAgent(client, ag); err != nil { | ||
| return nil, errors.Wrap(err, "failed to forward agent") | ||
| } | ||
| return client, nil | ||
| } | ||
|
|
||
| // Run uses an SSH client to execute commands. | ||
| func Run(client *ssh.Client, command string) error { | ||
| sess, err := client.NewSession() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer sess.Close() | ||
| if err := agent.RequestAgentForwarding(sess); err != nil { | ||
| return errors.Wrap(err, "failed to setup request agent forwarding") | ||
| } | ||
|
|
||
| debugW := &lineprinter.LinePrinter{Print: (&lineprinter.Trimmer{WrappedPrint: logrus.Debug}).Print} | ||
| defer debugW.Close() | ||
| sess.Stdout = debugW | ||
| sess.Stderr = debugW | ||
| return sess.Run(command) | ||
| } | ||
|
|
||
| // PullFileTo downloads the file from remote server using SSH connection and writes to localPath. | ||
| func PullFileTo(client *ssh.Client, remotePath, localPath string) error { | ||
| sc, err := sftp.NewClient(client) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to initialize the sftp client") | ||
| } | ||
| defer sc.Close() | ||
|
|
||
| // Open the source file | ||
| rFile, err := sc.Open(remotePath) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to open remote file") | ||
| } | ||
| defer rFile.Close() | ||
|
|
||
| lFile, err := os.Create(localPath) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to create file") | ||
| } | ||
| defer lFile.Close() | ||
|
|
||
| if _, err := rFile.WriteTo(lFile); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // defaultPrivateSSHKeys returns a list of all the PRIVATE SSH keys from user's home directory. | ||
| // It does not return any intermediate errors if at least one private key was loaded. | ||
| func defaultPrivateSSHKeys() ([]interface{}, error) { | ||
| d := filepath.Join(os.Getenv("HOME"), ".ssh") | ||
wking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| paths, err := ioutil.ReadDir(d) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "failed to read directory %q", d) | ||
| } | ||
|
|
||
| var files []string | ||
| for _, path := range paths { | ||
| if path.IsDir() { | ||
| continue | ||
| } | ||
| files = append(files, filepath.Join(d, path.Name())) | ||
| } | ||
| keys, err := LoadPrivateSSHKeys(files) | ||
| if keys != nil && len(keys) > 0 { | ||
| return keys, nil | ||
| } | ||
| return nil, err | ||
| } | ||
|
|
||
| // LoadPrivateSSHKeys try to optimistically load PRIVATE SSH keys from the all paths. | ||
| func LoadPrivateSSHKeys(paths []string) ([]interface{}, error) { | ||
| var errs []error | ||
| var keys []interface{} | ||
| for _, path := range paths { | ||
| data, err := ioutil.ReadFile(path) | ||
| if err != nil { | ||
| errs = append(errs, errors.Wrapf(err, "failed to read %q", path)) | ||
| continue | ||
| } | ||
| key, err := ssh.ParseRawPrivateKey(data) | ||
| if err != nil { | ||
| errs = append(errs, errors.Wrapf(err, "failed to parse SSH private key from %q", path)) | ||
| continue | ||
| } | ||
| keys = append(keys, key) | ||
| } | ||
| if err := utilerrors.NewAggregate(errs); err != nil { | ||
| return keys, err | ||
| } | ||
| return keys, nil | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.