Skip to content
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

feat(discovery-client): Add CLI commands to find and SSH into a robot #2072

Merged
merged 5 commits into from
Aug 20, 2018
Merged
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: 1 addition & 2 deletions app-shell/src/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import uniqBy from 'lodash/uniqBy'
import Store from 'electron-store'

import DiscoveryClient, {
DEFAULT_PORT,
SERVICE_EVENT,
SERVICE_REMOVED_EVENT
} from '@opentrons/discovery-client'
Expand Down Expand Up @@ -109,7 +108,7 @@ function serviceToConnection (service: Service): ?Connection {
return {
ip: service.ip,
ok: service.ok,
port: service.port || DEFAULT_PORT,
port: service.port,
local: isLocal(service.ip)
}
}
Expand Down
5 changes: 5 additions & 0 deletions discovery-client/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src
dist
__mocks__
.babelrc
*.tgz
10 changes: 8 additions & 2 deletions discovery-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ env := cross-env NODE_ENV
#####################################################################

.PHONY: all
all: clean lib
all: dist

.PHONY: install
install:
yarn

.PHONY: clean
clean:
shx rm -rf lib
shx rm -rf lib dist

# artifacts
#####################################################################
Expand All @@ -36,3 +36,9 @@ clean:
lib:
$(env)=production $(babel)
$(flow_copy)

.PHONY: dist
dist: clean lib
@mkdir -p dist
npm pack
shx mv '*.tgz' dist
102 changes: 81 additions & 21 deletions discovery-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ Creates a new `DiscoveryClient`.

```js
const options = {
nameFilter: /^opentrons/i,
allowedPorts: [31950],
nameFilter: ['opentrons'],
portFilter: [31950],
pollInterval: 5000,
candidates: [{ip: '[fd00:0:cafe:fefe::1]', port: 31950}, 'localhost']
candidates: [{ ip: '[fd00:0:cafe:fefe::1]', port: 31950 }, 'localhost']
}

const client = DiscoveryClientFactory(options)
Expand Down Expand Up @@ -127,21 +127,28 @@ type Option = {

/**
* list of extra IP addresses to add to the search list
* default: {} + ip/port from all Robots in options.discovered
* default: []
*/
candidates?: Array<string | Candidate>,

/**
* regexp or string (passed to `new RegExp`) to filter mDNS service names
* RegExps or strings to filter services by name
* default: ''
*/
nameFilter?: string | RegExp,
nameFilter?: Array<string | RegExp>,

/**
* regexp or string (passed to `new RegExp`) to filter mDNS service names
* RegExps or strings to filter services by IP address
* default: ''
*/
allowedPorts?: Array<number>,
ipFilter?: Array<string | RegExp>,

/**
* array of numbers to filter services by port
* the default port of 31950 is always included
* default: []
*/
portFilter?: Array<number>,

/** optional logger */
logger?: {
Expand All @@ -153,7 +160,7 @@ type Option = {
If you need access to the `DiscoveryClient` class itself for some reason:

```js
import {DiscoveryClient} from '@opentrons/discovery-client'
import { DiscoveryClient } from '@opentrons/discovery-client'

const client = new DiscoveryClient({})
```
Expand Down Expand Up @@ -185,19 +192,72 @@ client.on('error', (error) => console.error(error)
make -C discovery-client

# run via yarn run
yarn run discovery [options]
# or run from node_modules directly
node_modules/.bin/discovery [options]
yarn run discovery [command] [options]

# run via npx
npx discovery [command] [options]

# run from node_modules directly
node_modules/.bin/discovery [command] [options]
```

### global options

The CLI's global options are almost completely the same as the API's options, with the addition of `logLevel`:

| flag | description | default | example |
| -------------------- | ------------------------- | -------- | ---------------- |
| `-p, --pollInterval` | see `pollInterval` option | `1000` | `-p 500` |
| `-c, --candidates` | see `candidates` option | `[]` | `-c localhost` |
| `-n, --nameFilter` | see `nameFilter` option | `[]` | `-n opentrons` |
| `-i, --ipFilter` | see `ipFilter` option | `[]` | `-i 169.254` |
| `-a, --portFilter` | see `portFilter` option | `[]` | `-a 31951 31952` |
| `-l, --logLevel` | log level for printout | `'info'` | `-l debug` |

### `discovery (browse) [options]`

Print out robots as it discovers them.

```shell
# example: browse for robots, including at localhost
discovery browse -c localhost

# browse is the default command, so you can leave the "browse" out
discovery --nameFilter moon
```

It will print out robots as it discovers them. It has the same options the API:
### `discovery find [name] [options]`

| api option | cli flag | example |
| -------------- | -------------------- | ------------------------------------ |
| `pollInterval` | `-p, --pollInterval` | `--pollInterval 1000` |
| `services` | `-s, --services` | `-s.0.name=foo -s.0.ip=192.168.1.42` |
| `candidates` | `-c, --candidates` | `-c localhost 192.168.1.42` |
| `nameFilter` | `-n, --nameFilter` | `-n opentrons` |
| `allowedPorts` | `-a, --allowedPorts` | `-a 31951 31952 31953` |
Find the first robot you can, optionally specifying name or any other global options, and print out the IP address to `stdout` (bypassing any log level settings).

`--services` and `--candidates` may be passed multiple times to add more than one service or candidate.
```shell
# example: find a specific robot
discovery find opentrons-moon-moon

# example: find the IP address of a link-local wired robot
discovery find --ipFilter 169.254

# example: find the IP address of a wired robot that may be IPv4 or IPv6
# (IPv6 means legacy non-mDNS wired configuration)
discovery find -i "169.254" "fd00" -c "[fd00:0:cafe:fefe::1]"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Universal SSH into wired bot command 🎉

```

#### command specific options

| flag | description | default | example |
| --------------- | ---------------------------- | ------- | ---------- |
| `-t, --timeout` | How long to wait for a robot | `5000` | `-t 10000` |

### `discovery-ssh [name] [options]`

Calls `discovery find` and using the output to SSH into the robot it finds. **Takes all the same arguments and options as `discovery find`.**

`discovery-ssh` is a Bash script, so it must be called from a command line with Bash available.

```shell
# example: SSH into a link-local wired robot
discovery-ssh --ipFilter 169.254

# example: SSH into any wired robot, including legacy wired configuration
discovery-ssh -i "169.254" "fd00" -c "[fd00:0:cafe:fefe::1]"
```
18 changes: 18 additions & 0 deletions discovery-client/bin/discovery-ssh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# usage:
# discovery-ssh # ssh into first robot we can find
# discovery-ssh name # ssh into the named robot
# discovery-ssh --nameFilter moon # ssh into first robot that matches filter
#
# all arguments are passed directly to `discovery find`

dir=`dirname "$0"`
host=`$dir/discovery find $*`

if [[ -z "$host" ]]; then
echo "No robot found"
exit 1
fi;

echo "SSH'ing into $host"
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "root@$host"
8 changes: 6 additions & 2 deletions discovery-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Node.js client for discovering Opentrons robots on the network",
"main": "lib/index.js",
"bin": {
"discovery": "bin/index.js"
"discovery": "bin/index.js",
"discovery-ssh": "bin/discovery-ssh"
},
"repository": {
"type": "git",
Expand All @@ -21,7 +22,10 @@
"flow-typed": "^2.5.1"
},
"dependencies": {
"escape-string-regexp": "^1.0.5",
"mdns-js": "^1.0.1",
"node-fetch": "^2.1.2"
"node-fetch": "^2.1.2",
"to-regex": "^3.0.2",
"yargs": "^12.0.1"
}
}
Loading