Skip to content

Ssh plain commands #153

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 3 commits into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix SSH command execution
ATTENTION: the commandline signature is not checked at the moment!

example payload:
```
{"board":"arduino:avr:yun","port":"10.130.22.105","commandline":"wget {upload.verbose} -O /tmp/arduino_connector && wget https://raw.githubusercontent.com/xively/mosquitto/master/test/ssl/mosquitto.org.crt -O ~/mosquitto.org.crt && mv /tmp/arduino* ~ && chmod +x ~/arduino_connector && ~/arduino_connector","signature":null,"hex":"","filename":"Blink.hex","extra":{"auth":{"password":"user", "username":"user"},"wait_for_upload_port":false,"use_1200bps_touch":false,"network":true,"params_verbose":"http://10.130.22.222/arduino_connector","params_quiet":null,"verbose":true, "SSH":true}}
```
  • Loading branch information
facchinm committed May 5, 2017
commit e1218d220eacf0059b48162441a90fc3825d61c8
9 changes: 7 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/arduino/arduino-create-agent/upload"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
socketio "github.com/googollee/go-socket.io"
)

type connection struct {
Expand Down Expand Up @@ -72,6 +72,7 @@ type Upload struct {
var uploadStatusStr string = "ProgrammerStatus"

func uploadHandler(c *gin.Context) {

data := new(Upload)
c.BindJSON(data)

Expand Down Expand Up @@ -115,8 +116,12 @@ func uploadHandler(c *gin.Context) {
return
}

var filePaths []string
filePaths = append(filePaths, filePath)

for _, extraFile := range data.ExtraFiles {
path := filepath.Join(filepath.Dir(filePath), extraFile.Filename)
filePaths = append(filePaths, path)
log.Printf("Saving %s on %s", extraFile.Filename, path)
err := ioutil.WriteFile(path, extraFile.Hex, 0644)
if err != nil {
Expand All @@ -141,7 +146,7 @@ func uploadHandler(c *gin.Context) {
// Upload
if data.Extra.Network {
send(map[string]string{uploadStatusStr: "Starting", "Cmd": "Network"})
err = upload.Network(data.Port, data.Board, filePath, commandline, data.Extra.Auth, l, data.Extra.SSH)
err = upload.Network(data.Port, data.Board, filePaths, commandline, data.Extra.Auth, l, data.Extra.SSH)
} else {
send(map[string]string{uploadStatusStr: "Starting", "Cmd": "Serial"})
err = upload.Serial(data.Port, commandline, data.Extra, l)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/arduino/arduino-create-agent/tools"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/gin-gonic/gin"
"github.com/itsjamie/gin-cors"
cors "github.com/itsjamie/gin-cors"
"github.com/kardianos/osext"
"github.com/vharitonsky/iniflags"
//"github.com/sanbornm/go-selfupdate/selfupdate" #included in update.go to change heavily
Expand Down
37 changes: 21 additions & 16 deletions upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
shellwords "github.com/mattn/go-shellwords"
"github.com/pkg/errors"
"github.com/sfreiberg/simplessh"
"go.bug.st/serial.v1"
serial "go.bug.st/serial.v1"
)

// Busy tells wether the programmer is doing something
Expand Down Expand Up @@ -81,7 +81,7 @@ func fixupPort(port, commandline string) string {
}

// Network performs a network upload
func Network(port, board, file, commandline string, auth Auth, l Logger, SSH bool) error {
func Network(port, board string, files []string, commandline string, auth Auth, l Logger, SSH bool) error {
Busy = true

// Defaults
Expand All @@ -94,11 +94,11 @@ func Network(port, board, file, commandline string, auth Auth, l Logger, SSH boo

commandline = fixupPort(port, commandline)

// try with a form
err := form(port, board, file, auth, l)
// try with ssh
err := ssh(port, files, commandline, auth, l, SSH)
if err != nil {
// try with ssh
err = ssh(port, file, commandline, auth, l, SSH)
// fallback on form
err = form(port, board, files[0], auth, l)
}

Busy = false
Expand Down Expand Up @@ -365,7 +365,7 @@ func form(port, board, file string, auth Auth, l Logger) error {
return nil
}

func ssh(port, file, commandline string, auth Auth, l Logger, SSH bool) error {
func ssh(port string, files []string, commandline string, auth Auth, l Logger, SSH bool) error {
// Connect via ssh
client, err := simplessh.ConnectWithPassword(port+":22", auth.Username, auth.Password)
debug(l, "Connect via ssh ", client, err)
Expand All @@ -374,24 +374,29 @@ func ssh(port, file, commandline string, auth Auth, l Logger, SSH bool) error {
}
defer client.Close()

if !SSH {
// Copy the sketch
err = client.Upload(file, "/tmp/sketch"+filepath.Ext(file))
debug(l, "Copy the sketch ", err)
// Copy the sketch
for _, file := range files {
fileName := "/tmp/sketch" + filepath.Ext(file)
if SSH {
// don't rename files
fileName = "/tmp/" + filepath.Base(file)
}
err = client.Upload(file, fileName)
debug(l, "Copy "+filepath.Ext(file), err)
if err != nil {
return errors.Wrapf(err, "Copy sketch")
}
}

// very special case for Yun (remove once AVR boards.txt is fixed)
if commandline == "" {
commandline = "merge-sketch-with-bootloader.lua /tmp/sketch.hex && /usr/bin/run-avrdude /tmp/sketch.hex"
}
// very special case for Yun (remove once AVR boards.txt is fixed)
if commandline == "" {
commandline = "merge-sketch-with-bootloader.lua /tmp/sketch.hex && /usr/bin/run-avrdude /tmp/sketch.hex"
}

// Execute commandline
output, err := client.Exec(commandline)
info(l, output)
debug(l, "Execute commandline ", commandline, output, err)
debug(l, "Execute commandline ", commandline, string(output), err)
if err != nil {
return errors.Wrapf(err, "Execute commandline")
}
Expand Down