Skip to content

Commit

Permalink
Simplify config and port check (#60)
Browse files Browse the repository at this point in the history
* remove path and port

* fix error

* remove light

* fix number error

* fix api error

* check karst folder

* exit when error

* update readme
  • Loading branch information
LowEntropyBody authored Sep 15, 2020
1 parent 03d780d commit bf571ac
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 81 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ sudo ./install.sh
sudo vim /opt/crust/crust-node/config.yaml
```

### Run crust service
### Run crust service and karst service
Please make sure the following ports are not occupied before starting:
- 30333 9933 9944 (for crust chain)
- 56666 (for crust API)
- 12222 (for crust sWorker)
- 17000 (for karst)

```shell
sudo systemctl start crust
sudo systemctl start karst
```

## License
Expand Down
23 changes: 1 addition & 22 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## node configurations
node:
## the type of crust, for example: authority, full, light
## the type of crust, for example: authority, full
chain: "authority"
## enable or disable sworker
sworker: "enable"
Expand All @@ -16,40 +16,19 @@ identity:

## chain configurations
chain:
## chain data will be saved in this path, need absolute path
base_path: "/opt/crust/data/chain"
## the name of chain node
name: "vnode"
## the port
port: 30333
## the rpc port
rpc_port: 9933
## the ws port
ws_port: 9944

## api configurations
api:
## the port
port: 56666

## sworker configurations
sworker:
## sworker base data will be saved in this path, need absolute path
base_path: "/opt/crust/data/sworker"
## the srd data will be saved in those paths, need absolute path
srd_paths:
- "/opt/crust/data/sworker/srd1"
- "/opt/crust/data/sworker/srd2"
## the number of srd in GB, input must be an integer greater than 0
srd_init_capacity: 1
## the port
port: 12222

## karst configurations
karst:
## karst data will be saved in this path, need absolute path
base_path: "/opt/crust/data/karst"
## the thacker address
tracker_addrs: "127.0.0.1:22122"
## the port
port: 17000
6 changes: 3 additions & 3 deletions generator/config-gen/api-config.gen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

async function genApiConfig(config, outputCfg) {
const apiConfig = {
...config.api,
port: 56666,
chain_ws_url: `ws://127.0.0.1:${config.chain.ws_port}`,
}
return {
Expand All @@ -12,8 +12,8 @@ async function genApiConfig(config, outputCfg) {

async function genApiComposeConfig(config) {
const args = [
`${config.api.port}`,
`ws://127.0.0.1:${config.chain.ws_port}`,
'56666',
`ws://127.0.0.1:9944`,
].join(' ')
return {
image: 'crustio/crust-api:latest',
Expand Down
16 changes: 7 additions & 9 deletions generator/config-gen/chain-config.gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,28 @@ async function genChainComposeConfig(config) {
let args = [
'./crust',
'--base-path',
`${config.chain.base_path}`,
'/opt/crust/data/chain',
'--chain',
'maxwell',
'--port',
`${config.chain.port}`,
'30333',
'--name',
`${config.chain.name}`,
'--rpc-port',
`${config.chain.rpc_port}`,
'9933',
'--ws-port',
`${config.chain.ws_port}`,
'9944',
]

if (config.node.chain == "authority") {
args.push('--validator', '--pruning', 'archive')
} else if (config.node.chain == "light") {
args.push('--light')
}

return {
image: 'crustio/crust:latest',
network_mode: 'host',
volumes: [
`${config.chain.base_path}:${config.chain.base_path}`
'/opt/crust/data/chain:/opt/crust/data/chain'
],
command: args,
}
Expand All @@ -46,7 +44,7 @@ async function genChainComposeConfig(config) {
function getSharedChainConfig(config) {
return {
...config.identity,
base_url: `http://127.0.0.1:${config.api.port}/api/v1`,
base_url: `http://127.0.0.1:56666/api/v1`,
address: config.identity.backup.address,
account_id: config.identity.account_id,
backup: JSON.stringify(config.identity.backup),
Expand All @@ -56,7 +54,7 @@ function getSharedChainConfig(config) {
function getSharedChainConfigForKarst(config) {
return {
...config.identity,
base_url: `127.0.0.1:${config.api.port}`,
base_url: `127.0.0.1:56666`,
address: config.identity.backup.address,
backup: JSON.stringify(config.identity.backup),
}
Expand Down
14 changes: 7 additions & 7 deletions generator/config-gen/karst-config.gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const { getSharedChainConfigForKarst } = require('./chain-config.gen')

async function genKarstConfig(config, outputCfg) {
const karstConfig = {
base_path: config.karst.base_path,
port: config.karst.port,
base_path: '/opt/crust/data/karst',
port: 17000,
debug: true,
crust: getSharedChainConfigForKarst(config),
sworker: {
base_url: `127.0.0.1:${config.sworker.port}`
base_url: `127.0.0.1:12222`
},
file_system: {
fastdfs: {
Expand All @@ -19,9 +19,9 @@ async function genKarstConfig(config, outputCfg) {
}
}
}
const basePaths = _.isEmpty(config.karst.base_path) ? [] : [{
const basePaths = [{
required: true,
path: config.karst.base_path,
path: '/opt/crust/data/karst',
}]
return {
config: karstConfig,
Expand All @@ -30,8 +30,8 @@ async function genKarstConfig(config, outputCfg) {
}

async function genKarstComposeConfig(config) {
const basePath = _.isEmpty(config.karst.base_path) ? '/home/crust/crust/karst' : config.karst.base_path
const baseVolume = _.isEmpty(config.karst.base_path) ? [] : [ `${basePath}:${basePath}` ]
const basePath = '/opt/crust/data/karst'
const baseVolume =[ `${basePath}:${basePath}` ]

return {
image: 'crustio/karst:latest',
Expand Down
21 changes: 6 additions & 15 deletions generator/config-gen/sworker-config.gen.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
const _ = require('lodash')
const { createDir, writeConfig, } = require('../utils')
const { getSharedChainConfig } = require('./chain-config.gen')

async function genSworkerConfig(config, outputCfg) {
karstPort = 17000
if (config.karst) {
karstPort = config.karst.port
}

const sworkerConfig = {
..._.omit(config.sworker, ['port']),
base_url: `http://0.0.0.0:${config.sworker.port}/api/v0`,
karst_url: `ws://127.0.0.1:${karstPort}/api/v0/node/data`,
base_url: `http://0.0.0.0:12222/api/v0`,
karst_url: `ws://127.0.0.1:17000/api/v0/node/data`,
chain: getSharedChainConfig(config),
}
const srdPaths = _.map(config.sworker.srdPaths, (p) => ({
Expand All @@ -22,7 +16,7 @@ async function genSworkerConfig(config, outputCfg) {
config: sworkerConfig,
paths: [{
required: true,
path: config.sworker.base_path,
path: '/opt/crust/data/sworker',
}, ...srdPaths],
}
}
Expand All @@ -32,15 +26,12 @@ async function genSworkerComposeConfig(config) {
.map((p) => `${p}:${p}`)

let tempVolumes = [
`${config.sworker.base_path}:${config.sworker.base_path}`,
'/opt/crust/data/sworker:/opt/crust/data/sworker',
...srdVolumes,
'./sworker:/config'
'./sworker:/config',
'/opt/crust/data/karst:/opt/crust/data/karst'
]

if (config.karst) {
tempVolumes.push(`${config.karst.base_path}:${config.karst.base_path}`)
}

return {
image: 'crustio/crust-sworker:latest',
network_mode: 'host',
Expand Down
9 changes: 0 additions & 9 deletions generator/schema/api.schema.js

This file was deleted.

6 changes: 1 addition & 5 deletions generator/schema/chain.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ const Joi = require('joi')
const bluebird = require('bluebird')

const chainSchema = Joi.object({
base_path: Joi.string().required(),
name: Joi.string().required(),
port: Joi.number().port().default(30333),
rpc_port: Joi.number().port().default(9933),
ws_port: Joi.number().port().default(9944),
name: Joi.string().required()
})

module.exports = {
Expand Down
5 changes: 2 additions & 3 deletions generator/schema/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@

const Joi = require('joi')
const { apiSchema } = require('./api.schema')
const { chainSchema } = require('./chain.schema')
const { karstSchema } = require('./karst.schema')
const { identitySchema } = require('./identity.schema')
const { sworkerSchema } = require('./sworker.schema')
const { nodeSchema } = require('./node.schema')

function getConfigSchema(config) {
sMap = {
let sMap = {
node: nodeSchema.required(),
chain: chainSchema.required(),
}

if (config.node.sworker != "enable") {
return Joi.object(sMap)
}
sMap["api"] = apiSchema.required()
sMap["api"] = Joi.object().default()
sMap["identity"] = identitySchema.required()
sMap["sworker"] = sworkerSchema.required()

Expand Down
2 changes: 0 additions & 2 deletions generator/schema/karst.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
const Joi = require('joi')

const karstSchema = Joi.object({
base_path: Joi.string().required(),
tracker_addrs: Joi.string().required(),
port: Joi.number().port().default(17000),
})

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion generator/schema/node.schema.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Joi = require('joi')

const nodeSchema = Joi.object({
chain: Joi.string().valid('authority', 'full', 'light').required(),
chain: Joi.string().valid('authority', 'full').required(),
sworker: Joi.string().valid('enable', 'disable').required(),
karst: Joi.string().valid('enable', 'disable').required(),
})
Expand Down
2 changes: 0 additions & 2 deletions generator/schema/sworker.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
const Joi = require('joi')

const sworkerSchema = Joi.object({
base_path: Joi.string().required(),
srd_paths: Joi.array().items(Joi.string()).required(),
srd_init_capacity: Joi.number().positive().integer().default(1),
port: Joi.number().port().default(12222),
})

module.exports = {
Expand Down
14 changes: 14 additions & 0 deletions scripts/crust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ builddir=$basedir/build
start()
{
echo "Start"
check_port 30333 && check_port 9933 && check_port 9944 && check_port 56666 && check_port 12222 && check_port 17000

$scriptdir/gen_config.sh
if [ $? -ne 0 ]; then
echo "Generate configuration files failed"
Expand All @@ -30,13 +32,16 @@ start()
if [ -d "$builddir/sworker" ]; then
docker-compose -f $builddir/docker-compose.yaml up -d crust-api
if [ $? -ne 0 ]; then
docker-compose -f $builddir/docker-compose.yaml rm -fsv crust
echo "Start crust-api failed"
exit 1
fi

a_or_b=`cat $basedir/etc/sWorker.ab`
docker-compose -f $builddir/docker-compose.yaml up -d crust-sworker-$a_or_b
if [ $? -ne 0 ]; then
docker-compose -f $builddir/docker-compose.yaml rm -fsv crust
docker-compose -f $builddir/docker-compose.yaml rm -fsv crust-api
echo "Start crust-sworker-$a_or_b failed"
exit 1
fi
Expand All @@ -62,6 +67,15 @@ reload() {
stop
start
}

check_port() {
port=$1
grep_port=`netstat -tlpn | grep "\b$port\b"`
if [ -n "$grep_port" ]; then
echo "Please make sure port $port is not occupied"
exit 1
fi
}

case "$1" in
start)
Expand Down
7 changes: 5 additions & 2 deletions scripts/karst.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ builddir=$basedir/build
start()
{
echo "Start"
$scriptdir/gen_config.sh
$scriptdir/gen_config.sh
if [ $? -ne 0 ]; then
echo "Generate configuration files failed"
exit 1
fi

if [ ! -d "$builddir/sworker" ]; then
if [ ! -d "$builddir/sworker" ]; then
echo "No sworker"
exit 1
fi
Expand All @@ -24,6 +24,9 @@ start()
echo "Start karst failed"
exit 1
fi
else
echo "Please enable karst in /opt/crust/crust-node/config.yaml"
exit 1
fi
}

Expand Down

0 comments on commit bf571ac

Please sign in to comment.