Skip to content

[WIP] Download tools refactoring #95

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 24 commits into from
May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
54b274f
Move utilities to a package to keep things clean
matteosuppo Mar 1, 2016
b6c0cbc
Move initialization of tools in package tools
matteosuppo Mar 1, 2016
3785777
Move the function to download tools in a package
matteosuppo Mar 1, 2016
92da217
Make the indexurl configurable
matteosuppo Mar 3, 2016
57fdc3e
Fix a typo for darwin
matteosuppo Mar 3, 2016
a3991cf
Fix stupid typo again
matteosuppo Mar 3, 2016
d083191
Foxed conflicts in main
matteosuppo Apr 21, 2016
22c5028
[WIP] Working download toold from json
facchinm May 5, 2016
d603c93
Add "github.com/blang/semver" to govendor
facchinm May 11, 2016
62fa42b
adding github.com/blang/semver sources
facchinm May 11, 2016
700a888
move TellCommandNotToSpawnShell to tools package
facchinm May 11, 2016
cbe8007
address filepath issue
facchinm May 11, 2016
33c4356
execute post_install.bat if contained in downloaded tool
facchinm May 11, 2016
fcb34fd
modify reference package_json and call post_install
facchinm May 11, 2016
95a9232
rework "latest" behaviour
facchinm May 11, 2016
1acfc46
remove useless log on windows
facchinm May 11, 2016
1d2a4b7
[WIN] solve some path-replated bugs
facchinm May 11, 2016
2ea2c2a
handle Verbose parameters
facchinm May 12, 2016
e2b5a2a
add exclude list for extended header "files"
facchinm May 12, 2016
2a8dfff
[STUB] add GPG check on package_index
facchinm May 12, 2016
453530a
add time handling in tools
facchinm May 13, 2016
bfd2fb4
GPG check and avoid redownloading the json every time
facchinm May 13, 2016
30a2a69
use https to download package_index.json
facchinm May 13, 2016
b663ce9
add golang.org/x/crypto/openpgp/armor to govendor
facchinm May 13, 2016
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", "keep")
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", "https://downloads.arduino.cc/packages/package_staging_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 := filepath.Join(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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

log.Println("First run, unzipping self")
err := Unzip(src, dest)
err := utilities.Unzip(src, dest)
log.Println("Self extraction, err:", err)
}

Expand Down
Loading