Skip to content

Rewrite of download #79

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"

log "github.com/Sirupsen/logrus"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
)
Expand Down Expand Up @@ -95,7 +96,7 @@ func uploadHandler(c *gin.Context) {

buffer := bytes.NewBuffer(data.Hex)

path, err := saveFileonTempDir(data.Filename, buffer)
path, err := utilities.SaveFileonTempDir(data.Filename, buffer)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
}
Expand Down
16 changes: 14 additions & 2 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
package main

import (
log "github.com/Sirupsen/logrus"
"github.com/oleksandr/bonjour"
"net"
"strings"
"time"

log "github.com/Sirupsen/logrus"
"github.com/oleksandr/bonjour"
)

const timeoutConst = 2
Expand Down Expand Up @@ -143,3 +144,14 @@ func getPorts() ([]OsSerialPort, error) {
return arrPorts, nil
}
}

// Filter returns a new slice containing all OsSerialPort in the slice that satisfy the predicate f.
func Filter(vs []OsSerialPort, f func(OsSerialPort) bool) []OsSerialPort {
var vsf []OsSerialPort
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}
64 changes: 64 additions & 0 deletions docs/tools.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# tools
--
import "github.com/arduino/arduino-create-agent/tools"


## Usage

#### type Tools

```go
type Tools struct {
Directory string
IndexURL string
Logger log.StdLogger
}
```

Tools handle the tools necessary for an upload on a board. It provides a means
to download a tool from the arduino servers.

- *Directory* contains the location where the tools are downloaded.
- *IndexURL* contains the url where the tools description is contained.
- *Logger* is a StdLogger used for reporting debug and info messages
- *installed* contains a map of the tools and their exact location

Usage: You have to instantiate the struct by passing it the required parameters:

_tools := tools.Tools{
Directory: "/home/user/.arduino-create",
IndexURL: "http://downloads.arduino.cc/packages/package_index.json"
Logger: log.Logger
}

#### func (*Tools) Download

```go
func (t *Tools) Download(name, version, behaviour string) error
```
Download will parse the index at the indexURL for the tool to download. It will
extract it in a folder in .arduino-create, and it will update the Installed map.

name contains the name of the tool. version contains the version of the tool.
behaviour contains the strategy to use when there is already a tool installed

If version is "latest" it will always download the latest version (regardless of
the value of behaviour)

If version is not "latest" and behaviour is "replace", it will download the
version again. If instead behaviour is "keep" it will not download the version
if it already exists.

#### func (*Tools) GetLocation

```go
func (t *Tools) GetLocation(command string) (string, error)
```
GetLocation extracts the toolname from a command like

#### func (*Tools) Init

```go
func (t *Tools) Init()
```
Init creates the Installed map and populates it from a file in .arduino-create
124 changes: 0 additions & 124 deletions download.go

This file was deleted.

16 changes: 14 additions & 2 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

log "github.com/Sirupsen/logrus"
"github.com/kardianos/osext"
//"os"
Expand Down Expand Up @@ -190,8 +191,19 @@ func checkCmd(m []byte) {
go spList(true)
} else if strings.HasPrefix(sl, "downloadtool") {
args := strings.Split(s, " ")
if len(args) > 2 {
go spDownloadTool(args[1], args[2])
if len(args) > 1 {
go func() {
err := Tools.Download(args[1], "latest", "replace")
if err != nil {
mapD := map[string]string{"DownloadStatus": "Error", "Msg": err.Error()}
mapB, _ := json.Marshal(mapD)
h.broadcastSys <- mapB
} else {
mapD := map[string]string{"DownloadStatus": "Success", "Msg": "Map Updated"}
mapB, _ := json.Marshal(mapD)
h.broadcastSys <- mapB
}
}()
}
} else if strings.HasPrefix(sl, "bufferalgorithm") {
go spBufferAlgorithms()
Expand Down
4 changes: 3 additions & 1 deletion killbrowser_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package main
import (
"os/exec"
"strings"

"github.com/arduino/arduino-create-agent/utilities"
)

func findBrowser(process string) ([]byte, error) {
ps := exec.Command("ps", "-A", "-o", "command")
grep := exec.Command("grep", process)
head := exec.Command("head", "-n", "1")

return pipe_commands(ps, grep, head)
return utilities.PipeCommands(ps, grep, head)
}

func killBrowser(process string) ([]byte, error) {
Expand Down
42 changes: 23 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"time"

log "github.com/Sirupsen/logrus"
"github.com/arduino/arduino-create-agent/tools"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/gin-gonic/gin"
"github.com/itsjamie/gin-cors"
"github.com/kardianos/osext"
Expand All @@ -34,17 +36,17 @@ var (
gcType = flag.String("gc", "std", "Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)")
logDump = flag.String("log", "off", "off = (default)")
// hostname. allow user to override, otherwise we look it up
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
updateUrl = flag.String("updateUrl", "", "")
appName = flag.String("appName", "", "")
genCert = flag.Bool("generateCert", false, "")
globalToolsMap = make(map[string]string)
tempToolsPath = createToolsDir()
port string
portSSL string
origins = flag.String("origins", "", "Allowed origin list for CORS")
signatureKey = flag.String("signatureKey", "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvc0yZr1yUSen7qmE3cxF\nIE12rCksDnqR+Hp7o0nGi9123eCSFcJ7CkIRC8F+8JMhgI3zNqn4cUEn47I3RKD1\nZChPUCMiJCvbLbloxfdJrUi7gcSgUXrlKQStOKF5Iz7xv1M4XOP3JtjXLGo3EnJ1\npFgdWTOyoSrA8/w1rck4c/ISXZSinVAggPxmLwVEAAln6Itj6giIZHKvA2fL2o8z\nCeK057Lu8X6u2CG8tRWSQzVoKIQw/PKK6CNXCAy8vo4EkXudRutnEYHEJlPkVgPn\n2qP06GI+I+9zKE37iqj0k1/wFaCVXHXIvn06YrmjQw6I0dDj/60Wvi500FuRVpn9\ntwIDAQAB\n-----END PUBLIC KEY-----", "Pem-encoded public key to verify signed commandlines")
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
hostname = flag.String("hostname", "unknown-hostname", "Override the hostname we get from the OS")
updateUrl = flag.String("updateUrl", "", "")
appName = flag.String("appName", "", "")
genCert = flag.Bool("generateCert", false, "")
port string
portSSL string
origins = flag.String("origins", "", "Allowed origin list for CORS")
address = flag.String("address", "127.0.0.1", "The address where to listen. Defaults to localhost")
signatureKey = flag.String("signatureKey", "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvc0yZr1yUSen7qmE3cxF\nIE12rCksDnqR+Hp7o0nGi9123eCSFcJ7CkIRC8F+8JMhgI3zNqn4cUEn47I3RKD1\nZChPUCMiJCvbLbloxfdJrUi7gcSgUXrlKQStOKF5Iz7xv1M4XOP3JtjXLGo3EnJ1\npFgdWTOyoSrA8/w1rck4c/ISXZSinVAggPxmLwVEAAln6Itj6giIZHKvA2fL2o8z\nCeK057Lu8X6u2CG8tRWSQzVoKIQw/PKK6CNXCAy8vo4EkXudRutnEYHEJlPkVgPn\n2qP06GI+I+9zKE37iqj0k1/wFaCVXHXIvn06YrmjQw6I0dDj/60Wvi500FuRVpn9\ntwIDAQAB\n-----END PUBLIC KEY-----", "Pem-encoded public key to verify signed commandlines")
Tools tools.Tools
indexURL = flag.String("indexURL", "http://downloads.arduino.cc/packages/package_index.json", "The address from where to download the index json containing the location of upload tools")
)

type NullWriter int
Expand All @@ -60,11 +62,6 @@ func (u *logWriter) Write(p []byte) (n int, err error) {

var logger_ws logWriter

func createToolsDir() string {
usr, _ := user.Current()
return usr.HomeDir + "/.arduino-create"
}

func homeHandler(c *gin.Context) {
homeTemplate.Execute(c.Writer, c.Request.Host)
}
Expand Down Expand Up @@ -92,14 +89,21 @@ func main() {
src, _ := osext.Executable()
dest := filepath.Dir(src)

os.Mkdir(tempToolsPath, 0777)
hideFile(tempToolsPath)
// Instantiate Tools
usr, _ := user.Current()
directory := usr.HomeDir + "/.arduino-create"
Tools = tools.Tools{
Directory: directory,
IndexURL: *indexURL,
Logger: log.New(),
}
Tools.Init()

if embedded_autoextract {
// save the config.ini (if it exists)
if _, err := os.Stat(dest + "/" + *configIni); os.IsNotExist(err) {
log.Println("First run, unzipping self")
err := Unzip(src, dest)
err := utilities.Unzip(src, dest)
log.Println("Self extraction, err:", err)
}

Expand Down
Loading