-
Notifications
You must be signed in to change notification settings - Fork 4
Adding capability to use SSH for remote prep-node. #19
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
Merged
Changes from all commits
Commits
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
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
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
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
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 |
|---|---|---|
| @@ -1,20 +1,61 @@ | ||
| package clients | ||
|
|
||
| import "os/exec" | ||
| import ( | ||
| "os/exec" | ||
| "github.com/platform9/pf9ctl/pkg/ssh" | ||
| "github.com/platform9/pf9ctl/pkg/log" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // Executor interace abstracts us from local or remote execution | ||
| type Executor interface { | ||
| Run(name string, args ...string) error | ||
| RunWithStdout(name string, args ...string) (string, error) | ||
| } | ||
|
|
||
| type ExecutorImpl struct{} | ||
| // LocalExecutor as the name implies executes commands locally | ||
| type LocalExecutor struct{} | ||
|
|
||
| func (c ExecutorImpl) Run(name string, args ...string) error { | ||
| // Run runs a command locally returning just success or failure | ||
| func (c LocalExecutor) Run(name string, args ...string) error { | ||
| cmd := exec.Command(name, args...) | ||
| return cmd.Run() | ||
| } | ||
|
|
||
| func (c ExecutorImpl) RunWithStdout(name string, args ...string) (string, error) { | ||
| // RunWithStdout runs a command locally returning stdout and err | ||
| func (c LocalExecutor) RunWithStdout(name string, args ...string) (string, error) { | ||
| byt, err := exec.Command(name, args...).Output() | ||
| return string(byt), err | ||
| } | ||
|
|
||
| // RemoteExecutor as the name implies runs commands usign SSH on remote host | ||
| type RemoteExecutor struct{ | ||
| Client ssh.Client | ||
| } | ||
|
|
||
| // Run runs a command locally returning just success or failure | ||
| func (r *RemoteExecutor) Run(name string, args ...string) error { | ||
| _,err := r.RunWithStdout(name, args...) | ||
| return err | ||
| } | ||
|
|
||
| // RunWithStdout runs a command locally returning stdout and err | ||
| func (r *RemoteExecutor) RunWithStdout(name string, args ...string) (string, error) { | ||
| cmd := name | ||
| for _, arg := range args { | ||
| cmd = fmt.Sprintf("%s \"%s\"", cmd, arg) | ||
| } | ||
| stdout, stderr, err := r.Client.RunCommand(cmd) | ||
| log.Debug("Running command ",cmd, "stdout:", string(stdout), "stderr:", string(stderr)) | ||
| return string(stdout), err | ||
| } | ||
|
|
||
| // NewRemoteExecutor create an Executor interface to execute commands remotely | ||
| func NewRemoteExecutor(host string, port int, username string, privateKey []byte, password string) (Executor, error) { | ||
| client, err := ssh.NewClient(host, port, username, privateKey, password) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| re := &RemoteExecutor{Client: client} | ||
| return re, nil | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would return an error here and not Fatalf ( FatalingF at top layer ). Wdyt ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I debated that, didn't see a point in doing that, but if you feel it is inconsistent, I can change it. Here is the reason why I did it this way.
if this check was in another file, I would have absolutely agreed with you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok @abhimanyu-babbar I have refactored it little bit PTAL