Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dietb committed Jan 14, 2021
0 parents commit 4d0fa39
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
release
vc
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Digital Dream Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY: build release

COMMIT := $(shell git rev-parse --short HEAD)

build:
CGO_ENABLED=0 go build \
-ldflags "-w -s -extldflags "-static"" \
-trimpath \
-o vc cmd/main.go

release:
mkdir -p release
GOOS=windows GOARCH=amd64 make build
mv vc release/vc-win-amd64-$(COMMIT).exe
GOOS=darwin GOARCH=amd64 make build
mv vc release/vc-darwin-amd64-$(COMMIT)
GOOS=linux GOARCH=amd64 make build
mv vc release/vc-linux-amd64-$(COMMIT)
GOOS=linux GOARCH=arm64 make build
mv vc release/vc-linux-arm64-$(COMMIT)
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Vector configurator



Vector configurator is a command-line utility for doing some common operatioins on an [OSKR](https://oskr.ddl.io/) enabled bot.



# Usage/Features


## set-environment
This allows you to easily change the environment your Vector is pointed to.

An example command would be...
```
$ vc set-environment -e escapepod -h 10.0.2.42 -k ~/.ssh/vector.key
```

### Arguments
| flag | description| notes |
|--|--|--|
| -e | environment | `escapepod `and `production` are the supported environments|
| -h | hostname or IP of your robot | |
| -k | The location of the SSH key for your robot | |

## upload-cloud-binaries
This allows you to easily upload the cloud binaries built from the [vector-cloud](https://github.com/digital-dream-labs/vector-cloud) repository

An example command would be...
```
$ vc upload-cloud-binaries -b ~/vector-cloud/build/ -h 10.0.2.42 -k ~/.ssh/vector.key
```

### Arguments
| flag | description| notes |
|--|--|--|
| -b | binary directory | The directory containing the vic-cloud and vic-gateway files |
| -h | hostname or IP of your robot | |
| -k | The location of the SSH key for your robot | |

# Building

```
$ make build
```
55 changes: 55 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"flag"
"fmt"
"os"
"vector-configurator/pkg/configurator"
)

func main() {

// set environment arguments
setenv := flag.NewFlagSet("set-environment", flag.ExitOnError)
shost := setenv.String("h", "", "the ip of the vector robot")
skey := setenv.String("k", "", "the location of the ssh key")
senv := setenv.String("e", "", "environment")

cloudbin := flag.NewFlagSet("upload-cloud-binaries", flag.ExitOnError)
chost := cloudbin.String("h", "", "the ip of the vector robot")
ckey := cloudbin.String("k", "", "the location of the ssh key")
cbindir := cloudbin.String("b", "", "path to vic-cloud and vic-gateway binaries")

flag.Parse()

if len(os.Args) < 2 {
fmt.Println(`vector configurator
This tool will allow you to do the following things:
set-environment - change the environment that
your bot is pointed at
upload-cloud-binaries - upload and set permissions
for the vector-cloud binaries
`)
os.Exit(0)
}

switch os.Args[1] {
case "set-environment":
_ = setenv.Parse(os.Args[2:])
if *senv == "" || *shost == "" || *skey == "" {
setenv.Usage()
os.Exit(1)
}
configurator.SetEnvironment(*shost, *skey, *senv)
case "upload-cloud-binaries":
_ = cloudbin.Parse(os.Args[2:])
if *cbindir == "" || *chost == "" || *ckey == "" {
cloudbin.Usage()
os.Exit(1)
}
configurator.UploadCloud(*chost, *ckey, *cbindir)

}

}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module vector-configurator

go 1.15

require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef h1:7D6Nm4D6f0ci9yttWaKjM1TMAXrH5Su72dojqYGntFY=
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef/go.mod h1:WLFStEdnJXpjK8kd4qKLwQKX/1vrDzp5BcDyiZJBHJM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
32 changes: 32 additions & 0 deletions pkg/configurator/rebootprompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package configurator

import (
"bufio"
"fmt"
"os"
"strings"

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

func rebootPrompt(c *ssh.Client) {
reader := bufio.NewReader(os.Stdin)
fmt.Println("configuration complete. Would you like to reboot your robot? (y/n)")

for {
fmt.Print("-> ")
r, _ := reader.ReadString('\n')
r = strings.ReplaceAll(r, "\n", "")
fmt.Println(r)
if r == "y" {
if err := reboot(c); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
} else {
os.Exit(0)
}
}

}
72 changes: 72 additions & 0 deletions pkg/configurator/set-environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package configurator

import (
"fmt"
"os"
)

const (
escapepod = `
{
"jdocs": "escapepod.local:8084",
"tms": "escapepod.local:8084",
"chipper": "escapepod.local:8084",
"check": "escapepod.local/ok",
"logfiles": "s3://anki-device-logs-prod/victor",
"appkey": "oDoa0quieSeir6goowai7f"
}`
prod = `
{
"jdocs": "jdocs.api.anki.com:443",
"tms": "token.api.anki.com:443",
"chipper": "chipper.api.anki.com:443",
"check": "conncheck.global.anki-services.com/ok",
"logfiles": "s3://anki-device-logs-prod/victor",
"appkey": "oDoa0quieSeir6goowai7f"
}`

cfpath = "/anki/data/assets/cozmo_resources/config/server_config.json"
)

// SetEnvironment updates a vectors environment
func SetEnvironment(host, key, env string) {

var cf string

switch env {
case "escapepod":
cf = escapepod
case "production":
cf = prod
default:
fmt.Println(`valid environments are:
escapepod
production`)
os.Exit(1)
}

c, err := getSSHConn(key, host)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := runCmds(
c,
[]string{
"mount -o remount rw /",
fmt.Sprintf("cp %s %s.bak", cfpath, cfpath),
fmt.Sprintf(`cat > %s << EOF
%s
EOF`,
cfpath,
cf,
),
},
); err != nil {
fmt.Println(err)
os.Exit(1)
}

rebootPrompt(c)
}
83 changes: 83 additions & 0 deletions pkg/configurator/ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package configurator

import (
"fmt"
"io/ioutil"
"path/filepath"

"github.com/tmc/scp"
"golang.org/x/crypto/ssh"
)

// getSSHConn connects to the robot and returns an ssh client
func getSSHConn(key, host string) (*ssh.Client, error) {
b, err := ioutil.ReadFile(filepath.Clean(key))
if err != nil {
return nil, err
}

k, err := ssh.ParsePrivateKey(b)
if err != nil {
return nil, err
}

return ssh.Dial(
"tcp",
fmt.Sprintf("%s:22", host),
&ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
ssh.PublicKeys(k),
},
// nolint: gosec -- I have no idea how this would be handled on windows, so I expect an issue
// or PR about this at some point -bd
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
},
)
}

func runCmds(c *ssh.Client, cmds []string) error {
for i := range cmds {
s, err := c.NewSession()
if err != nil {
return err
}
defer func() {
_ = s.Close()
}()

if err := s.Run(cmds[i]); err != nil {
return fmt.Errorf(
"error running command %s: %v",
cmds[i],
err,
)
}

}
return nil
}

func reboot(c *ssh.Client) error {
s, err := c.NewSession()
if err != nil {
return err
}
defer func() {
_ = s.Close()
}()

return s.Run("reboot")
}

func scpFile(c *ssh.Client, file, dest string) error {
s, err := c.NewSession()
if err != nil {
return err
}
defer func() {
_ = s.Close()
}()

return scp.CopyPath(file, dest, s)
}
Loading

0 comments on commit 4d0fa39

Please sign in to comment.