Skip to content

Commit

Permalink
chore: add docker setup docks
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Dec 28, 2020
1 parent e1cd224 commit 264ce2a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 23 deletions.
65 changes: 59 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,75 @@ libp2p-rendezvous-server --disableMetrics

### Docker Setup

TODO: Finish docker setup
When running the rendezvous server in Docker, you can configure the same parameters via environment variables, as follows:

```sh
PEER_ID='/etc/opt/rendezvous/id.json'
LISTEN_MULTIADDRS='/ip4/127.0.0.1/tcp/15002/ws,/ip4/127.0.0.1/tcp/8001'
ANNOUNCE_MULTIADDRS='/dns4/test.io/tcp/443/wss,/dns6/test.io/tcp/443/wss'
DATASTORE_HOST='localhost'
DATASTORE_USER='root'
DATASTORE_PASSWORD='your-secret-pw'
DATASTORE_DATABASE='libp2p_rendezvous_db'
```

Please note that you should expose the listening ports with the docker run command. The default ports used are `8003` for the metrics, `8000` for the tcp listener and `150003` for the websockets listener.

Example:

```sh
peer-id --type=ed25519 > id.json
docker build . -t libp2p-rendezvous
docker run -p 8003:8003 -p 15002:15002 -p 8000:8000 \
-e LISTEN_MULTIADDRS='/ip4/127.0.0.1/tcp/8000,/ip4/127.0.0.1/tcp/15002/ws' \
-e ANNOUNCE_MULTIADDRS='/dns4/localhost/tcp/8000,/dns4/localhost/tcp/15002/ws' \
-e DATASTORE_USER='root' \
-e DATASTORE_PASSWORD='your-secret-pw' \
-e DATASTORE_DATABASE='libp2p_rendezvous_db' \
-e PEER_ID='/etc/opt/rendezvous/id.json' \
-v $PWD/id.json:/etc/opt/rendezvous/id.json \
-d libp2p-rendezvous
```

### Docker compose setup with mysql

Here follows an example on how you can setup a rendezvous server with a mysql database.

```yml
version: '3.1'
version: '3.2'
services:
db:
image: mysql
image: mysql:8
volumes:
- mysql-db:/var/lib/mysql
- mysql-db:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: your-secret-pw
MYSQL_DATABASE: libp2p_rendezvous_db
- MYSQL_ROOT_PASSWORD=my-secret-pw
- MYSQL_DATABASE=libp2p_rendezvous_db
ports:
- "3306:3306"
healthcheck:
test: ["CMD-SHELL", 'mysqladmin ping']
interval: 10s
timeout: 2s
retries: 10
server:
image: libp2p/js-libp2p-rendezvous
volumes:
- ./id.json:/etc/opt/rendezvous/id.json
ports:
- "8000:8000"
- "8003:8003"
- "15003:15003"
restart: always
environment:
- DATASTORE_PASSWORD=my-secret-pw
- DATASTORE_DATABASE=libp2p_rendezvous_db
- DATASTORE_HOST=db
depends_on:
db:
condition: service_healthy
volumes:
mysql-db:
```
Expand Down
34 changes: 27 additions & 7 deletions mysql-local/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
version: '3.1'
version: '3.2'
services:
db:
image: mysql
image: mysql:8
volumes:
- mysql-db:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
MYSQL_USER: libp2p
MYSQL_PASSWORD: my-secret-pw
MYSQL_DATABASE: libp2p_rendezvous_db
- MYSQL_ROOT_PASSWORD=my-secret-pw
- MYSQL_DATABASE=libp2p_rendezvous_db
ports:
- "3306:3306"
healthcheck:
test: ["CMD-SHELL", 'mysqladmin ping']
interval: 10s
timeout: 2s
retries: 10
server:
image: libp2p-rendezvous
volumes:
- ./id.json:/etc/opt/rendezvous/id.json
ports:
- "8000:8000"
- "8003:8003"
- "15003:15003"
restart: always
environment:
- DATASTORE_PASSWORD=my-secret-pw
- DATASTORE_DATABASE=libp2p_rendezvous_db
- DATASTORE_HOST=db
- PEER_ID=/etc/opt/rendezvous/id.json
depends_on:
db:
condition: service_healthy
volumes:
mysql-db:
mysql-db:
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"minimist": "^1.2.5",
"multiaddr": "^8.0.0",
"mysql": "^2.18.1",
"p-retry": "^4.2.0",
"peer-id": "^0.14.1",
"protons": "^2.0.0",
"set-delayed-interval": "^1.0.0",
Expand All @@ -85,7 +86,6 @@
"ipfs-utils": "^5.0.1",
"is-ci": "^2.0.0",
"p-defer": "^3.0.0",
"p-retry": "^4.2.0",
"p-times": "^3.0.0",
"p-wait-for": "^3.1.0",
"sinon": "^9.0.3"
Expand Down
4 changes: 2 additions & 2 deletions src/server/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ async function main () {

// PeerId
let peerId
if (argv.peerId) {
const peerData = fs.readFileSync(argv.peerId)
if (argv.peerId || process.env.PEER_ID) {
const peerData = fs.readFileSync(argv.peerId || process.env.PEER_ID)
peerId = await PeerId.createFromJSON(JSON.parse(peerData.toString()))
} else {
peerId = await PeerId.create()
Expand Down
10 changes: 7 additions & 3 deletions src/server/datastores/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const errCode = require('err-code')
const { codes: errCodes } = require('../errors')

const mysql = require('mysql')
const pRetry = require('p-retry')

/**
* @typedef {import('peer-id')} PeerId
Expand Down Expand Up @@ -58,9 +59,8 @@ class Mysql {
* @returns {Promise<void>}
*/
async start () {
this.conn = mysql.createConnection(this.options)

await this._initDB()
// Retry starting the Database in case it is still booting
await pRetry(() => this._initDB())
}

/**
Expand Down Expand Up @@ -311,6 +311,8 @@ class Mysql {
* @returns {Promise<void>}
*/
_initDB () {
this.conn = mysql.createConnection(this.options)

return new Promise((resolve, reject) => {
this.conn.query(`
CREATE TABLE IF NOT EXISTS registration (
Expand All @@ -332,8 +334,10 @@ class Mysql {
);
`, (err) => {
if (err) {
log.error(err)
return reject(err)
}
log('db is initialized')
resolve()
})
})
Expand Down
8 changes: 4 additions & 4 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ const { fallbackNullish } = require('./utils')
* @typedef {Object} RendezvousServerOptions
* @property {Datastore} datastore
* @property {number} [minTtl = MIN_TTL] minimum acceptable ttl to store a registration
* @property {number} [maxTtl = MAX_TTL] maxium acceptable ttl to store a registration
* @property {number} [maxNsLength = MAX_NS_LENGTH] maxium acceptable namespace length
* @property {number} [maxDiscoveryLimit = MAX_DISCOVER_LIMIT] maxium acceptable discover limit
* @property {number} [maxPeerRegistrations = MAX_PEER_REGISTRATIONS] maxium acceptable registrations per peer
* @property {number} [maxTtl = MAX_TTL] maximum acceptable ttl to store a registration
* @property {number} [maxNsLength = MAX_NS_LENGTH] maximum acceptable namespace length
* @property {number} [maxDiscoveryLimit = MAX_DISCOVER_LIMIT] maximum acceptable discover limit
* @property {number} [maxPeerRegistrations = MAX_PEER_REGISTRATIONS] maximum acceptable registrations per peer
* @property {number} [gcBootDelay = GC_BOOT_DELAY] delay before starting garbage collector job
* @property {number} [gcMinInterval = GC_MIN_INTERVAL] minimum interval between each garbage collector job, in case maximum threshold reached
* @property {number} [gcInterval = GC_INTERVAL] interval between each garbage collector job
Expand Down

0 comments on commit 264ce2a

Please sign in to comment.