-
Notifications
You must be signed in to change notification settings - Fork 71
/
deploy.sh
172 lines (152 loc) · 4.79 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
UPDATE=0
DEST_DIR=""
FED_KEY=""
MAIN_BLOCK_NUMBER=""
SIDE_BLOCK_NUMBER=""
MAIN_BLOCK=""
SIDE_BLOCK=""
ETH_HOST=""
RSK_HOST=""
PROGRAMS="docker npm nodejs jq"
quit() {
if [ $1 -eq 1 ]; then
echo "ERROR: "$2
else
echo $2
fi
exit $1
}
config() {
cp $DEST_DIR/federator/config/config.sample.js $DEST_DIR/federator/config/config.js &&
sed -i "s|rsktestnet-kovan|rskmainnet|g" $DEST_DIR/federator/config/config.js &&
sed -i "s|kovan|ethmainnet|g" $DEST_DIR/federator/config/config.js &&
return 0
quit 1 "There was a problem setting the configuration, please verify."
}
check_required_programs() {
echo "Checking for required programs..."
rc=0
for program in $PROGRAMS; do
if ! command -v "$program" >/dev/null 2>&1; then
rc=1
echo "$program: command not found"
fi
done
if [ $rc -ne 0 ]; then
quit 1 "Requirements not acomplished"
fi
}
dependencies() {
echo "Installing required packages ..."
npm install &&
echo "Packages installed" &&
return 0
quit 1 "There was an error installing the needed packages, please check"
}
key() {
while [ ! -f $DEST_DIR/federator/config/federator.key ]; do
read -p "Please move your private key to $DEST_DIR/federator/config/federator.key, then press any key to continue " FED_KEY
continue
done
echo ""
echo "Private key placed on $DEST_DIR/federator/config/federator.key"
return 0
}
block_exists() {
grep -q '^[0-9]*$' "$DEST_DIR/federator/db/lastBlock.txt" 2>/dev/null &&
grep -q '^[0-9]*$' "$DEST_DIR/federator/db/side-fed/lastBlock.txt" 2>/dev/null &&
return 0
return 1
}
eth_host(){
read -p "Please enter your ETH host address along with the RPC port, (https://127.0.0.1:8545) " ETH_HOST
[ ! -z "${ETH_HOST}" ] &&
sed -i "s|<YOUR HOST URL AND PORT>|$ETH_HOST|g" $DEST_DIR/federator/config/ethmainnet.json &&
return 0
quit 1 "There was an error setting the ETH host and port"
}
rsk_host(){
read -p "Please enter your RSK host address along with the RPC port, (https://127.0.0.1:4444) " RSK_HOST
[ ! -z "${RSK_HOST}" ] &&
sed -i "s|<YOUR HOST URL AND PORT>|$RSK_HOST|g" $DEST_DIR/federator/config/rskmainnet.json &&
return 0
quit 1 "There was an error setting the RSK host and port"
}
last_block_rsk() {
MAIN_BLOCK_NUMBER=$(curl -fSs 'https://backend.explorer.rsk.co/api?module=blocks&action=getBlocks&limit=1' |
jq -r '.data[0].number')
[ "$MAIN_BLOCK_NUMBER" != "null" ]
}
last_block_eth() {
SIDE_BLOCK_NUMBER=$(curl -fSs https://api.blockcypher.com/v1/eth/main | jq -r '.height')
[ "$SIDE_BLOCK_NUMBER" != "null" ]
}
block() {
last_block_eth &&
last_block_rsk &&
read -p "Enter the block number of RSK mainchain to start syncing [$MAIN_BLOCK_NUMBER] " MAIN_BLOCK &&
read -p "Enter the block number from Eth chain to start syncing [$SIDE_BLOCK_NUMBER] " SIDE_BLOCK
MAIN_BLOCK_NUMBER=${MAIN_BLOCK:-$MAIN_BLOCK_NUMBER}
SIDE_BLOCK_NUMBER=${SIDE_BLOCK:-$SIDE_BLOCK_NUMBER}
[ ! -z "${MAIN_BLOCK_NUMBER}" ] &&
echo $MAIN_BLOCK_NUMBER > $DEST_DIR/federator/db/lastBlock.txt
[ ! -z "${SIDE_BLOCK_NUMBER}" ] &&
echo $SIDE_BLOCK_NUMBER > $DEST_DIR/federator/db/side-fed/lastBlock.txt
block_exists &&
echo "Blocks number configured suscessfully" &&
return 0
quit 1 "There was a problem configuring the block numbers"
}
build() {
cd $DEST_DIR
docker build -t fed-tokenbridge . &&
echo "Docker image created suscessfully" &&
return 0
quit 1 "There was a problem creating the docker image"
}
run_message() {
RUN_MESSAGE="docker run -d --rm \
--network host \
-v $DEST_DIR/federator/config:/app/federator/config \
-v $DEST_DIR/federator/db:/app/federator/db \
--name=fed-tokenbridge \
fed-tokenbridge:latest"
}
setup() {
dependencies &&
build &&
run_message &&
echo "To run the federate node container execute: " &&
quit 0 "$RUN_MESSAGE"
}
if [ ! -z $1 ]; then
UPDATE=$1
else
quit 1 "Undefinied parameter. 0 for install, 1 to update."
fi
if [ ! -z $2 ]; then
DEST_DIR=$2
else
quit 1 "Undefined DEST_DIR"
fi
check_required_programs
cd $DEST_DIR/federator
if [ $UPDATE -eq 1 ]; then
echo "Installing the token bridge federate node"
rsk_host &&
eth_host &&
config &&
key &&
block &&
setup &&
quit 0
quit 1 "Error installing the token bridge federate node"
elif [ $UPDATE -eq 0 ]; then
echo "Updating the token bridge federate node"
setup &&
quit 0
quit 1 "Error updating the token bridge federate node"
else
quit 1 "The valid values for the script are 0 to install and 1 to update"
fi