Skip to content

Commit

Permalink
Split out ssh package
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis <[email protected]>
  • Loading branch information
alexellis committed Aug 15, 2019
1 parent 0b7c634 commit 2735efd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 77 deletions.
80 changes: 3 additions & 77 deletions pkg/cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cmd

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

kssh "github.com/alexellis/k3sup/pkg/ssh"

homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -57,7 +56,7 @@ func MakeInstall() *cobra.Command {
}

address := fmt.Sprintf("%s:%d", ip.String(), port)
operator, err := NewSSHOperator(address, config)
operator, err := kssh.NewSSHOperator(address, config)

if err != nil {
return errors.Wrapf(err, "unable to connect to %s over ssh", address)
Expand Down Expand Up @@ -92,8 +91,6 @@ func MakeInstall() *cobra.Command {

kubeconfig := strings.Replace(string(res.StdOut), "localhost", ip.String(), -1)

fmt.Println(res)

writeErr := ioutil.WriteFile(absPath, []byte(kubeconfig), 0600)
if writeErr != nil {
return writeErr
Expand Down Expand Up @@ -134,74 +131,3 @@ func loadPublickey(path string) ssh.AuthMethod {
}
return ssh.PublicKeys(signer)
}

type commandRes struct {
StdOut []byte
StdErr []byte
}

func executeCommand(cmd string) (commandRes, error) {

return commandRes{}, nil
}

type SSHOperator struct {
conn *ssh.Client
}

func (s *SSHOperator) Close() error {

return s.conn.Close()
}

func NewSSHOperator(address string, config *ssh.ClientConfig) (*SSHOperator, error) {
conn, err := ssh.Dial("tcp", address, config)
if err != nil {
return nil, err
}

operator := SSHOperator{
conn: conn,
}

return &operator, nil
}

func (s *SSHOperator) Execute(command string) (commandRes, error) {

sess, err := s.conn.NewSession()
if err != nil {
return commandRes{}, err
}

defer sess.Close()

sessStdOut, err := sess.StdoutPipe()
if err != nil {
return commandRes{}, err
}

output := bytes.Buffer{}

stdOutWriter := io.MultiWriter(os.Stdout, &output)
go io.Copy(stdOutWriter, sessStdOut)

sessStderr, err := sess.StderrPipe()
if err != nil {
return commandRes{}, err
}

errorOutput := bytes.Buffer{}
stdErrWriter := io.MultiWriter(os.Stderr, &errorOutput)
go io.Copy(stdErrWriter, sessStderr)

err = sess.Run(command)
if err != nil {
return commandRes{}, err
}

return commandRes{
StdErr: errorOutput.Bytes(),
StdOut: output.Bytes(),
}, nil
}
93 changes: 93 additions & 0 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ssh

import (
"bytes"
"io"
"os"
"sync"

"golang.org/x/crypto/ssh"
)

type SSHOperator struct {
conn *ssh.Client
}

func (s *SSHOperator) Close() error {

return s.conn.Close()
}

func NewSSHOperator(address string, config *ssh.ClientConfig) (*SSHOperator, error) {
conn, err := ssh.Dial("tcp", address, config)
if err != nil {
return nil, err
}

operator := SSHOperator{
conn: conn,
}

return &operator, nil
}

func (s *SSHOperator) Execute(command string) (commandRes, error) {

sess, err := s.conn.NewSession()
if err != nil {
return commandRes{}, err
}

defer sess.Close()

sessStdOut, err := sess.StdoutPipe()
if err != nil {
return commandRes{}, err
}

output := bytes.Buffer{}

wg := sync.WaitGroup{}

stdOutWriter := io.MultiWriter(os.Stdout, &output)
wg.Add(1)
go func() {
io.Copy(stdOutWriter, sessStdOut)
wg.Done()
}()
sessStderr, err := sess.StderrPipe()
if err != nil {
return commandRes{}, err
}

errorOutput := bytes.Buffer{}
stdErrWriter := io.MultiWriter(os.Stderr, &errorOutput)
wg.Add(1)
go func() {
io.Copy(stdErrWriter, sessStderr)
wg.Done()
}()

err = sess.Run(command)

wg.Wait()

if err != nil {
return commandRes{}, err
}

return commandRes{
StdErr: errorOutput.Bytes(),
StdOut: output.Bytes(),
}, nil
}

type commandRes struct {
StdOut []byte
StdErr []byte
}

func executeCommand(cmd string) (commandRes, error) {

return commandRes{}, nil
}

0 comments on commit 2735efd

Please sign in to comment.