Skip to content

Commit

Permalink
Merge pull request #30 from unumotors/cli-config-file
Browse files Browse the repository at this point in the history
cli integration of replicaset and moveto config file
  • Loading branch information
pelzerim authored Jun 28, 2020
2 parents 2890560 + 4c014b3 commit fcfac9e
Show file tree
Hide file tree
Showing 9 changed files with 579 additions and 400 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ At unu we run large mongo clusters. We use homegrown tools to manage these clust

Mongo-hydra is an attempt to open source all that learning in a proper reuseable maintainable tool that we will use internally too.

## Installation

### CLI

```sh
$ npm install -g mongo-hydra
+ mongo-hydra@latest

hydra replication -f examples/replication/hydra.yaml
```

### Docker Image

```sh
docker pull unumotors/mongo-hydra
```

## Release Schedule

| Version | Component / Feature | Github Milestone |
Expand All @@ -35,13 +52,13 @@ Similar tools like [KubeDB](https://kubedb.com/docs/0.9.0/concepts/databases/mon
| | Raw Machines ||||
| | Docker Standalone ||||
| | k8s operator ||||
| MongoDB support | Replica sets | |||
| MongoDB support | Replica sets | |||
| | Delayed members ||||
| | Arbiter members ||||
| | Sharded cluster ||||
| | User management ||||
| | Admin user setup || ✅ (k8s) | ✅(k8s) |
| | Mongo version support | ✅ =<4.4 | 🟠 3.6 | ✅ 4.2 |
| | Mongo version support | ✅ =<4.2 | 🟠 3.6 | ✅ 4.2 |
| Security | Certificates ||||
| | Keyfiles ||||
| | Encryption at rest ||||
Expand Down
33 changes: 25 additions & 8 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
#!/usr/bin/env node
const yargs = require('yargs')
const fs = require('fs')
const yaml = require('js-yaml')
const replication = require('./lib/cli/replication')

// This seems required to parse
// eslint-disable-next-line no-unused-vars
const { argv } = yargs
.command(replication)
.demandCommand()
.help()
// Disable wrapping
.wrap(null)
.epilog('Hail Hydra\ncopyright 2020 unu GmbH')
async function main() {
// eslint-disable-next-line no-unused-vars
const { argv } = await yargs
.usage('Usage: $0 -c <configfile>')
// Loads the configuration
.config('f', 'Configuration file', (filename) => {
const file = fs.existsSync(filename) ? filename : 'hydra.yaml'
return yaml.safeLoad(fs.readFileSync(file, 'utf8'))
})
.command(replication)
.demandCommand()
.help()
// Disable wrapping
.wrap(null)
.epilog('Hail Hydra\ncopyright 2020 unu GmbH')
}

try {
main()
} catch (e) {
console.log(e)
throw e
}
17 changes: 17 additions & 0 deletions examples/replication/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Replication Example

This example shows off how to use mongo-hydra to configure replication with a basic 3 node replica set

## Requirements

* Docker
* Docker compose
* node >= 10
* npm

### Steps

1. Start instances `docker-compose up`
2. Run hydra `hydra replication -f hydra.yaml`

You now have a 3 node mongo replica set!
29 changes: 29 additions & 0 deletions examples/replication/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.8'
services:
mongo-rs-0:
image: "mongo:${MONGO_VERSION:-4.2}"
command:
- '--replSet'
- 'mongo-rs'
- '--port'
- '27000'
ports:
- "27000:27000"
mongo-rs-1:
image: "mongo:${MONGO_VERSION:-4.2}"
command:
- '--replSet'
- 'mongo-rs'
- '--port'
- '27001'
ports:
- "27001:27001"
mongo-rs-2:
image: "mongo:${MONGO_VERSION:-4.2}"
command:
- '--replSet'
- 'mongo-rs'
- '--port'
- '27002'
ports:
- "27002:27002"
15 changes: 15 additions & 0 deletions examples/replication/hydra.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Define a replica set
# to support multiple replica sets use multiple config files
replicaSet:
# Replica set name
replicaSetName: mongo-rs
# The servers that should be part of this replica set
# host: The host mongo internally uses to name and connect to these
# uri: the URI that hydra uses to connect and configure (can be a mapping/port-forwarding/public URI)
servers:
- host: mongo-rs-0:27000
uri: mongodb://localhost:27000
- host: mongo-rs-1:27001
uri: mongodb://localhost:27001
- host: mongo-rs-2:27002
uri: mongodb://localhost:27002
24 changes: 18 additions & 6 deletions lib/cli/replication.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
exports.command = 'replication [-r=mongo-1:27017]'
const { ReplicaSetState, REPLICA_SET_STATUS } = require('../core/state/replication')
const logger = require('../helpers/logger')

exports.command = 'replication'

exports.describe = 'Sets up replication against a basic replica set'

exports.builder = {
replica: {
alias: 'r'
}
}

exports.handler = (argv) => {
console.log(argv)
exports.handler = async (argv) => {
logger.debug('Setting up replication')
const { replicaSet } = argv
const state = new ReplicaSetState(replicaSet)
await state.apply()

await state.disconnect()
if (state.replicaSetStatus === REPLICA_SET_STATUS.HEALTHY) {
logger.info('+ ReplicaSet status is healthy')
} else {
logger.warn('- ReplicaSet status not healthy')
// this will cause an exit code
throw new Error(`ReplicaSet not healthy. Has state ${state.replicaSetStatus}`)
}
}
Loading

0 comments on commit fcfac9e

Please sign in to comment.