-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sh
executable file
·186 lines (152 loc) · 6.26 KB
/
init.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
set -u
set -e
MESSAGE='Usage: init <mode> <node-type> <node-name>
mode: CURRENT_HOST_IP | auto | backup
node-type: validator | general
node-name: NODE_NAME (example: Alastria)'
if ( [ $# -ne 3 ] ); then
echo "$MESSAGE"
exit
fi
CURRENT_HOST_IP="$1"
NODE_TYPE="$2"
NODE_NAME="$3"
if ( [ "auto" == "$1" -o "backup" == "$1" ]); then
echo "Autodiscovering public host IP ..."
CURRENT_HOST_IP="$(dig +short myip.opendns.com @resolver1.opendns.com 2>/dev/null || curl -s --retry 2 icanhazip.com)"
echo "Public host IP found: $CURRENT_HOST_IP"
fi
if ( [ "backup" == "$1" ]); then
echo "Backing up current node keys ..."
#Backup directory tree
mkdir ~/alastria-keysBackup
mkdir ~/alastria-keysBackup/data
mkdir ~/alastria-keysBackup/data/geth
mkdir ~/alastria-keysBackup/data/constellation
echo "Saving constellation keys ..."
cp -r ~/alastria/data/constellation/keystore ~/alastria-keysBackup/data/constellation/
echo "Saving node keys ..."
cp -r ~/alastria/data/keystore ~/alastria-keysBackup/data
echo "Saving enode ID ..."
cp ~/alastria/data/geth/nodekey ~/alastria-keysBackup/data/geth/nodekey
fi
PWD="$(pwd)"
CONSTELLATION_NODES=$(cat ../data/constellation-nodes.json)
STATIC_NODES=$(cat ../data/static-nodes.json)
PERMISSIONED_NODES_VALIDATOR=$(cat ../data/permissioned-nodes_validator.json)
PERMISSIONED_NODES_GENERAL=$(cat ../data/permissioned-nodes_general.json)
update_constellation_nodes() {
NODE_IP="$1"
CONSTELLATION_PORT="$2"
URL=",
\"http://$NODE_IP:$CONSTELLATION_PORT/\"
]"
CONSTELLATION_NODES=${CONSTELLATION_NODES::-2}
CONSTELLATION_NODES="$CONSTELLATION_NODES$URL"
echo "$CONSTELLATION_NODES" > ~/alastria-node/data/constellation-nodes.json
}
update_nodes_list() {
echo "Selected $NODE_TYPE node..."
echo "Updating permissioned nodes..."
ENODE=",
\"$1\"
]"
PERMISSIONED_NODES_VALIDATOR=${PERMISSIONED_NODES_VALIDATOR::-2}
PERMISSIONED_NODES_VALIDATOR="$PERMISSIONED_NODES_VALIDATOR$ENODE"
echo "$PERMISSIONED_NODES_VALIDATOR" > ~/alastria-node/data/permissioned-nodes_validator.json
if ( [ "validator" == "$NODE_TYPE" ]); then
PERMISSIONED_NODES_GENERAL=${PERMISSIONED_NODES_GENERAL::-2}
PERMISSIONED_NODES_GENERAL="$PERMISSIONED_NODES_GENERAL$ENODE"
echo "$PERMISSIONED_NODES_GENERAL" > ~/alastria-node/data/permissioned-nodes_general.json
fi
echo "Updating static-nodes..."
cp ~/alastria-node/data/permissioned-nodes_general.json ~/alastria-node/data/static-nodes.json
}
generate_conf() {
#define parameters which are passed in.
NODE_IP="$1"
CONSTELLATION_PORT="$2"
OTHER_NODES="$3"
PWD="$4"
#define the template.
cat << EOF
# Externally accessible URL for this node (this is what's advertised)
url = "http://$NODE_IP:$CONSTELLATION_PORT/"
# Port to listen on for the public API
port = $CONSTELLATION_PORT
# Socket file to use for the private API / IPC
socket = "$PWD/alastria/data/constellation/constellation.ipc"
# Initial (not necessarily complete) list of other nodes in the network.
# Constellation will automatically connect to other nodes not in this list
# that are advertised by the nodes below, thus these can be considered the
# "boot nodes."
othernodes = $OTHER_NODES
# The set of public keys this node will host
publickeys = ["$PWD/alastria/data/constellation/keystore/node.pub"]
# The corresponding set of private keys
privatekeys = ["$PWD/alastria/data/constellation/keystore/node.key"]
# Optional file containing the passwords to unlock the given privatekeys
# (one password per line -- add an empty line if one key isn't locked.)
passwords = "$PWD/alastria/data/passwords.txt"
# Where to store payloads and related information
storage = "$PWD/alastria/data/constellation/data"
# Verbosity level (each level includes all prior levels)
# - 0: Only fatal errors
# - 1: Warnings
# - 2: Informational messages
# - 3: Debug messages
verbosity = 2
EOF
}
echo "[*] Cleaning up temporary data directories."
rm -rf ~/alastria
mkdir -p ~/alastria/data/{keystore,geth,constellation}
mkdir -p ~/alastria/data/constellation/{data,keystore}
mkdir -p ~/alastria/logs
echo "$NODE_NAME" > ~/alastria/data/IDENTITY
echo "$NODE_TYPE" > ~/alastria/data/NODE_TYPE
# Creamos el fichero de passwords con la contraseña de las cuentas
echo "Passw0rd" > ~/alastria/data/passwords.txt
echo "[*] Initializing quorum"
geth --datadir ~/alastria/data init ~/alastria-node/data/genesis.json
cd ~/alastria/data/geth
bootnode -genkey nodekey
ENODE_KEY=$(bootnode -nodekey nodekey -writeaddress)
echo "ENODE -> 'enode://${ENODE_KEY}@${CURRENT_HOST_IP}:21000?raftport=41000'"
update_nodes_list "enode://${ENODE_KEY}@${CURRENT_HOST_IP}:21000?raftport=41000"
cd ~
if [[ "$CURRENT_HOST_IP" == "52.56.69.220" ]]; then
cp ~/alastria-node/data/static-nodes.json ~/alastria/data/static-nodes.json
cp ~/alastria-node/data/static-nodes.json ~/alastria/data/permissioned-nodes.json
fi
echo " Por favor, introduzca como contraseña 'Passw0rd'."
geth --datadir ~/alastria/data account new
echo "[*] Initializing Constellation node."
update_constellation_nodes "${CURRENT_HOST_IP}" "9000"
generate_conf "${CURRENT_HOST_IP}" "9000" "$CONSTELLATION_NODES" "${PWD}" > ~/alastria/data/constellation/constellation.conf
cd ~/alastria/data/constellation/keystore
cat ~/alastria/data/passwords.txt | constellation-node --generatekeys=node
echo "______"
cd ~
if ( [ "backup" == "$1" ]); then
echo "Recovering keys from backup ..."
rm -rf ~/alastria/data/constellation/keystore
rm -rf ~/alastria/data/keystore
rm ~/alastria/data/geth/nodekey
echo "Recovering constellation keys ..."
cp -rf ~/alastria-keysBackup/data/constellation/keystore ~/alastria/data/constellation/
echo "Recovering node keys ..."
cp -rf ~/alastria-keysBackup/data/keystore ~/alastria/data/
echo "Recovering enode ID ..."
cp ~/alastria-keysBackup/data/geth/nodekey ~/alastria/data/geth/nodekey
echo "Cleaning backup files ..."
rm -rf ~/alastria-keysBackup
fi
echo "[*] Initialization was completed successfully."
echo " "
echo " Update DIRECTORY.md from alastria-node repository and send a Pull Request."
echo " The network administrator will send a RAFT_ID file. It will be stored in '~/alastria/data/' directory."
echo " "
set +u
set +e