Skip to content

Commit 3b87dbb

Browse files
Ipfs config and launch cmd (#86)
* add ipfs enable * add timeout (#85) * generate ipfs config * pull ipfs docker when install * ipfs cmd * upgrade reload ipfs * change port * fix typo
1 parent 170b42d commit 3b87dbb

11 files changed

+173
-17
lines changed

config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ node:
44
chain: "authority"
55
## enable or disable sworker
66
sworker: "enable"
7+
## enable or disable ipfs
8+
ipfs: "disable"
79

810
## identity configurations
911
identity:

generator/config-gen/api-config.gen.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
async function genApiConfig(config, outputCfg) {
32
const apiConfig = {
43
port: 56666,

generator/config-gen/chain-config.gen.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
async function genChainConfig(config, outputCfg) {
32
const chainConfig = config.chain
43
return {

generator/config-gen/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const path = require('path')
55
const { createDir, writeConfig, } = require('../utils')
66
const { genApiConfig, genApiComposeConfig } = require('./api-config.gen')
7+
const { genIpfsConfig, genIpfsComposeConfig } = require('./ipfs-config.gen')
78
const { genChainConfig, genChainComposeConfig } = require('./chain-config.gen')
89
const { genSworkerConfig, genSworkerComposeConfig } = require('./sworker-config.gen')
910
const { logger } = require('../logger')
@@ -46,6 +47,12 @@ const configGenerators = [{
4647
to: path.join('sworker', 'sworker_config.json'),
4748
composeName: 'crust-sworker-b',
4849
composeFunc: genSworkerComposeConfig,
50+
}, {
51+
name: 'ipfs',
52+
configFunc: genIpfsConfig,
53+
to: path.join('ipfs', 'ipfs_config.json'),
54+
composeName: 'ipfs',
55+
composeFunc: genIpfsComposeConfig,
4956
}]
5057

5158
async function genConfig(config, outputOpts) {
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
async function genIpfsConfig(config, outputCfg) {
2+
const ipfsConfig = {
3+
swarm_port: 4001,
4+
api_port: 5001,
5+
gateway_port: 37773,
6+
}
7+
return {
8+
config: ipfsConfig,
9+
paths: [],
10+
}
11+
}
12+
13+
async function genIpfsComposeConfig(config) {
14+
return {
15+
image: 'ipfs/go-ipfs:latest',
16+
ports: [
17+
'4001:4001',
18+
'5001:5001',
19+
'37773:8080',
20+
],
21+
volumes: [
22+
'/opt/crust/data/ipfs:/data/ipfs'
23+
]
24+
}
25+
}
26+
27+
module.exports = {
28+
genIpfsConfig,
29+
genIpfsComposeConfig,
30+
}

generator/schema/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ function getConfigSchema(config) {
1111
chain: chainSchema.required(),
1212
}
1313

14-
if (config.node.sworker != "enable") {
15-
return Joi.object(sMap)
14+
if (config.node.sworker == "enable") {
15+
sMap["api"] = Joi.object().default()
16+
sMap["identity"] = identitySchema.required()
17+
sMap["sworker"] = sworkerSchema.required()
18+
}
19+
20+
if (config.node.ipfs == "enable") {
21+
sMap["ipfs"] = Joi.object().default()
1622
}
17-
sMap["api"] = Joi.object().default()
18-
sMap["identity"] = identitySchema.required()
19-
sMap["sworker"] = sworkerSchema.required()
2023

2124
return Joi.object(sMap)
2225
}

generator/schema/node.schema.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Joi = require('joi')
33
const nodeSchema = Joi.object({
44
chain: Joi.string().valid('authority', 'full').required(),
55
sworker: Joi.string().valid('enable', 'disable').required(),
6+
ipfs: Joi.string().valid('enable', 'disable').required(),
67
})
78

89
module.exports = {

install.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,18 @@ download_docker_images()
7171
docker pull $aliyun_address/crustio/crust-sworker
7272
res=$(($?|$res))
7373
docker tag $aliyun_address/crustio/crust-sworker crustio/crust-sworker
74+
75+
docker pull $aliyun_address/crustio/go-ipfs
76+
res=$(($?|$res))
77+
docker tag $aliyun_address/crustio/go-ipfs ipfs/go-ipfs
7478
else
7579
docker pull crustio/config-generator
7680
res=$(($?|$res))
7781
docker pull crustio/crust
7882
res=$(($?|$res))
7983
docker pull crustio/crust-api
8084
res=$(($?|$res))
81-
docker pull crustio/crust-sworker
85+
docker pull ipfs/go-ipfs
8286
res=$(($?|$res))
8387
fi
8488

scripts/crust.sh

+111-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ start()
3939
exit 1
4040
fi
4141

42+
start_ipfs
43+
if [ $? -ne 0 ]; then
44+
docker-compose -f $builddir/docker-compose.yaml down
45+
exit 1
46+
fi
47+
4248
log_success "Start crust success"
4349
}
4450

@@ -47,6 +53,7 @@ stop()
4753
stop_chain
4854
stop_api
4955
stop_sworker
56+
stop_ipfs
5057

5158
log_success "Stop crust success"
5259
}
@@ -75,6 +82,13 @@ logs()
7582
return 0
7683
fi
7784
docker logs -f crust-sworker-$a_or_b
85+
elif [ x"$1" == x"ipfs" ]; then
86+
check_docker_status ipfs
87+
if [ $? -eq 1 ]; then
88+
log_info "Service ipfs is not started now"
89+
return 0
90+
fi
91+
docker logs -f ipfs
7892
elif [ x"$1" == x"sworker-a" ]; then
7993
check_docker_status crust-sworker-a
8094
if [ $? -eq 1 ]; then
@@ -235,6 +249,45 @@ stop_api()
235249
fi
236250
return 0
237251
}
252+
253+
start_ipfs()
254+
{
255+
if [ -d "$builddir/ipfs" ]; then
256+
check_docker_status ipfs
257+
if [ $? -ne 1 ]; then
258+
return 0
259+
fi
260+
261+
local res=0
262+
check_port 4001
263+
res=$(($?|$res))
264+
check_port 5001
265+
res=$(($?|$res))
266+
check_port 37773
267+
res=$(($?|$res))
268+
if [ $res -ne 0 ]; then
269+
return 1
270+
fi
271+
272+
docker-compose -f $builddir/docker-compose.yaml up -d ipfs
273+
if [ $? -ne 0 ]; then
274+
log_err "[ERROR] Start ipfs failed"
275+
return 1
276+
fi
277+
fi
278+
return 0
279+
}
280+
281+
stop_ipfs()
282+
{
283+
check_docker_status ipfs
284+
if [ $? -ne 1 ]; then
285+
log_info "Stopping ipfs service"
286+
docker stop ipfs &>/dev/null
287+
docker rm ipfs &>/dev/null
288+
fi
289+
return 0
290+
}
238291

239292
reload() {
240293
if [ x"$1" = x"" ]; then
@@ -290,6 +343,21 @@ reload() {
290343
return 0
291344
fi
292345

346+
if [ x"$1" = x"ipfs" ]; then
347+
log_info "Reload ipfs service"
348+
349+
stop_ipfs
350+
$scriptdir/gen_config.sh
351+
if [ $? -ne 0 ]; then
352+
log_err "[ERROR] Generate configuration files failed"
353+
exit 1
354+
fi
355+
start_ipfs
356+
357+
log_success "Reload ipfs service success"
358+
return 0
359+
fi
360+
293361
help
294362
return 0
295363
}
@@ -302,6 +370,8 @@ status()
302370
api_status
303371
elif [ x"$1" == x"sworker" ]; then
304372
sworker_status
373+
elif [ x"$1" == x"ipfs" ]; then
374+
ipfs_status
305375
elif [ x"$1" == x"" ]; then
306376
all_status
307377
else
@@ -314,6 +384,7 @@ all_status()
314384
local chain_status="stop"
315385
local api_status="stop"
316386
local sworker_status="stop"
387+
local ipfs_status="stop"
317388

318389
check_docker_status crust
319390
local res=$?
@@ -340,13 +411,22 @@ all_status()
340411
sworker_status="exited"
341412
fi
342413

414+
check_docker_status ipfs
415+
res=$?
416+
if [ $res -eq 0 ]; then
417+
ipfs_status="running"
418+
elif [ $res -eq 2 ]; then
419+
ipfs_status="exited"
420+
fi
421+
343422
cat << EOF
344423
-----------------------------------------
345424
Service Status
346425
-----------------------------------------
347426
chain ${chain_status}
348427
api ${api_status}
349428
sworker ${sworker_status}
429+
ipfs ${ipfs_status}
350430
-----------------------------------------
351431
EOF
352432
}
@@ -433,6 +513,27 @@ cat << EOF
433513
EOF
434514
}
435515

516+
ipfs_status()
517+
{
518+
local ipfs_status="stop"
519+
520+
check_docker_status ipfs
521+
res=$?
522+
if [ $res -eq 0 ]; then
523+
ipfs_status="running"
524+
elif [ $res -eq 2 ]; then
525+
ipfs_status="exited"
526+
fi
527+
528+
cat << EOF
529+
-----------------------------------------
530+
Service Status
531+
-----------------------------------------
532+
ipfs ${ipfs_status}
533+
-----------------------------------------
534+
EOF
535+
}
536+
436537
help()
437538
{
438539
cat << EOF
@@ -441,9 +542,9 @@ Usage:
441542
start start all crust service
442543
stop stop all crust service
443544
444-
status {chain|api|sworker} check status or reload one service status
445-
reload {chain|api|sworker} reload all service or reload one service
446-
logs {chain|api|sworker} track service logs, ctrl-c to exit
545+
status {chain|api|sworker|ipfs} check status or reload one service status
546+
reload {chain|api|sworker|ipfs} reload all service or reload one service
547+
logs {chain|api|sworker|ipfs} track service logs, ctrl-c to exit
447548
tools {...} use 'crust tools help' for more details
448549
EOF
449550
}
@@ -455,7 +556,7 @@ crust tools usage:
455556
help show help information
456557
rotate-keys generate session key of chain node
457558
workload show workload information
458-
upgrade-reload {chain|api|c-gen} upgrade one docker image and reload the service
559+
upgrade-reload {chain|api|ipfs|c-gen} upgrade one docker image and reload the service
459560
change-srd {number} change sworker's srd capacity(GB), for example: 'crust tools change-srd 100', 'crust tools change-srd -50'
460561
EOF
461562
}
@@ -543,6 +644,12 @@ upgrade_reload()
543644
return 1
544645
fi
545646
reload api
647+
elif [ x"$1" == x"ipfs" ]; then
648+
upgrade_docker_image ipfs/go-ipfs crustio/go-ipfs
649+
if [ $? -ne 0 ]; then
650+
return 1
651+
fi
652+
reload api
546653
elif [ x"$1" == x"c-gen" ]; then
547654
upgrade_docker_image crustio/config-generator
548655
if [ $? -ne 0 ]; then

scripts/upgrade.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ sleep 100s
7171
while :
7272
do
7373

74-
system_health=`curl $api_base_url/system/health 2>/dev/null`
74+
system_health=`curl --max-time 30 $api_base_url/system/health 2>/dev/null`
7575
if [ x"$system_health" = x"" ]; then
7676
echo "please run crust chain and api"
7777
sleep 60
@@ -91,7 +91,7 @@ if [ x"$is_syncing" = x"true" ]; then
9191
continue
9292
fi
9393

94-
code=`curl $api_base_url/swork/code 2>/dev/null`
94+
code=`curl --max-time 30 $api_base_url/swork/code 2>/dev/null`
9595
if [ x"$code" = x"" ]; then
9696
echo "please run chain and api"
9797
sleep 60
@@ -107,7 +107,7 @@ fi
107107
code=`echo ${code: 3: 64}`
108108
echo "sWorker code on chain: $code"
109109

110-
id_info=`curl $sworker_base_url/enclave/id_info 2>/dev/null`
110+
id_info=`curl --max-time 30 $sworker_base_url/enclave/id_info 2>/dev/null`
111111
if [ x"$id_info" = x"" ]; then
112112
echo "please run sworker"
113113
sleep 60

scripts/utils.sh

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function log_err()
2323
function upgrade_docker_image()
2424
{
2525
local basedir=/opt/crust/crust-node
26+
local crustio_name=$2
27+
if [ x"$crustio_name" == x"" ]; then
28+
crustio_name=$1
29+
fi
2630

2731
local old_image=(`docker images | grep '^\b'$1'\b ' | grep 'latest'`)
2832
old_image=${old_image[2]}
@@ -31,9 +35,9 @@ function upgrade_docker_image()
3135
local res=0
3236
if [ x"$region" == x"cn" ]; then
3337
local aliyun_address=registry.cn-hangzhou.aliyuncs.com
34-
docker pull $aliyun_address/$1
38+
docker pull $aliyun_address/$crustio_name
3539
res=$(($?|$res))
36-
docker tag $aliyun_address/$1 $1
40+
docker tag $aliyun_address/$crustio_name $1
3741
else
3842
docker pull $1
3943
res=$(($?|$res))

0 commit comments

Comments
 (0)