-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from lpabon/sshscp
ssh: SCP PUT support
- Loading branch information
Showing
5 changed files
with
237 additions
and
32 deletions.
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 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,59 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/heketi/utils/ssh" | ||
) | ||
|
||
func runDemo(s ssh.SshExecutor) { | ||
// scp scpdemo | ||
fmt.Print("Copying scpdemo to server...") | ||
host := "127.0.0.1:22" | ||
err := s.CopyPath("scpdemo.go", host, "/tmp/scpdemo-copy.go") | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(3) | ||
} | ||
fmt.Println("Done") | ||
|
||
// run a few commands | ||
fmt.Println("Running commands...") | ||
commands := []string{ | ||
"date", | ||
"echo \"HELLO\" > /tmp/file", | ||
"cat /tmp/file", | ||
"ls -al", | ||
"rm /tmp/file", | ||
"rm /tmp/scpdemo-copy.go", | ||
} | ||
|
||
out, err := s.Exec(host, commands, 10, false) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(2) | ||
} | ||
|
||
fmt.Printf("%+v\n", out) | ||
} | ||
|
||
func main() { | ||
fmt.Println("- Real Demo -") | ||
s, err := ssh.NewSshExecWithAuth(os.Getenv("USER")) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
runDemo(s) | ||
|
||
fmt.Println("- Mock Demo -") | ||
// Now run with a mock demo | ||
m := ssh.NewMockSshExecutor() | ||
m.MockExec = func(host string, commands []string, timeoutMinutes int, useSudo bool) ([]string, error) { | ||
return []string{ | ||
"In Mock function", | ||
}, nil | ||
} | ||
runDemo(m) | ||
} |
This file contains 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,74 @@ | ||
// | ||
// Copyright (c) 2017 The heketi Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
// Package to mock ssh interface for unit tests | ||
// Create a mock object and override the functions to test | ||
// functions which return expected values | ||
package ssh | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
type MockSshExec struct { | ||
MockCopy func(size int64, mode os.FileMode, fileName string, contents io.Reader, host, destinationPath string) error | ||
MockCopyPath func(sourcePath, host, destinationPath string) error | ||
MockExec func(host string, commands []string, timeoutMinutes int, useSudo bool) ([]string, error) | ||
} | ||
|
||
func NewMockSshExecutor() *MockSshExec { | ||
m := &MockSshExec{} | ||
m.MockCopy = func(size int64, | ||
mode os.FileMode, | ||
fileName string, | ||
contents io.Reader, | ||
host, destinationPath string) error { | ||
return nil | ||
} | ||
|
||
m.MockCopyPath = func(sourcePath, host, destinationPath string) error { | ||
return nil | ||
} | ||
|
||
m.MockExec = func(host string, | ||
commands []string, | ||
timeoutMinutes int, | ||
useSudo bool) ([]string, error) { | ||
return []string{""}, nil | ||
} | ||
|
||
return m | ||
} | ||
|
||
func (m *MockSshExec) Copy(size int64, | ||
mode os.FileMode, | ||
fileName string, | ||
contents io.Reader, | ||
host, destinationPath string) error { | ||
return m.MockCopy(size, mode, fileName, contents, host, destinationPath) | ||
} | ||
|
||
func (m *MockSshExec) CopyPath(sourcePath, host, destinationPath string) error { | ||
return m.MockCopyPath(sourcePath, host, destinationPath) | ||
} | ||
|
||
func (m *MockSshExec) Exec(host string, | ||
commands []string, | ||
timeoutMinutes int, | ||
useSudo bool) ([]string, error) { | ||
return m.MockExec(host, commands, timeoutMinutes, useSudo) | ||
} |
This file contains 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 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,28 @@ | ||
// | ||
// Copyright (c) 2017 The heketi Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package ssh | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
type SshExecutor interface { | ||
Copy(size int64, mode os.FileMode, fileName string, contents io.Reader, host, destinationPath string) error | ||
CopyPath(sourcePath, host, destinationPath string) error | ||
Exec(host string, commands []string, timeoutMinutes int, useSudo bool) ([]string, error) | ||
} |