forked from maticnetwork/node-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-gcp.sh
190 lines (169 loc) · 5.42 KB
/
install-gcp.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
187
188
189
190
#!/usr/bin/env bash
INSTALL_DIR="/opt/polygon"
POLYGON_ANSIBLE_REPO="https://github.com/maticnetwork/node-ansible.git"
INVENTORY_PATH="${INSTALL_DIR}/inventory.yml"
function check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root or sudo"
exit 1
fi
}
function check_os() {
if ! lsb_release -si | egrep -q "Ubuntu|Debian"; then
echo "Please run in Ubuntu or Debian"
exit 1
fi
}
function mount_disk() {
EXT_DISK="${1}"
MOUNT_POINT="${2}"
FS_LABEL="${3}"
MOUNT_OPTS="discard,defaults"
# https://cloud.google.com/compute/docs/disks/add-persistent-disk#format_and_mount_linux
MKFS_OPTS="-m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard -L ${FS_LABEL}"
# backup existing dir if any
mv "${MOUNT_POINT}" "${MOUNT_POINT}.old"
mkdir -p "${MOUNT_POINT}"
# mount disk or create a file system on it and then mount it again using label
mount -o "${MOUNT_OPTS}" "${EXT_DISK}" "${MOUNT_POINT}" || mkfs.ext4 ${MKFS_OPTS} "${EXT_DISK}" && mount -o "${MOUNT_OPTS}" LABEL="${FS_LABEL}" "${MOUNT_POINT}"
# generate fstab entry
echo -e "LABEL=${FS_LABEL}\t${MOUNT_POINT}\text4\t${MOUNT_OPTS}\t 0 1" >> /etc/fstab
umount "${MOUNT_POINT}"
# label the disk
tune2fs -L "${FS_LABEL}" "${EXT_DISK}"
# ensure fstab entry is working fine
mount "${MOUNT_POINT}"
# restore content if any
cp -a "${MOUNT_POINT}.old/." "${MOUNT_POINT}/"
}
function apt_upd() {
apt update
}
function install_dependencies() {
apt_upd
apt install -f git
}
function install_ansible() {
apt_upd
# apt install -y software-properties-common
# add-apt-repository --yes --update ppa:ansible/ansible
apt install -y ansible
}
function clone_repo() {
git clone ${POLYGON_ANSIBLE_REPO} ${INSTALL_DIR}
}
function configure_inventory() {
echo "all:
hosts:
children:
sentry:
hosts:
localhost:
validator:
hosts:
localhost:" >${INVENTORY_PATH}
}
function configure_node() {
# https://docs.polygon.technology/docs/validate/mainnet/validator-guide/#configuring-your-sentry-node
http_port="${1}"
HEIMDALLD_CONFIG="/root/.heimdalld/config/config.toml"
BOR_START_SH="/root/node/bor/start.sh"
sed -i 's/^seeds =.*/seeds ="[email protected]:26656,[email protected]:26656"/g' "${HEIMDALLD_CONFIG}"
sed -i 's/^prometheus =.*/prometheus = true/g' "${HEIMDALLD_CONFIG}"
sed -i 's/^max_open_connections =.*/max_open_connections = 100/g' "${HEIMDALLD_CONFIG}"
if ! grep -q bootnodes "${BOR_START_SH}"; then
echo ' --bootnodes "enode://0cb82b395094ee4a2915e9714894627de9ed8498fb881cec6db7c65e8b9a5bd7f2f25cc84e71e89d0947e51c76e85d0847de848c7782b13c0255247a6758178c@44.232.55.71:30303,enode://88116f4295f5a31538ae409e4d44ad40d22e44ee9342869e7d68bdec55b0f83c1530355ce8b41fbec0928a7d75a5745d528450d30aec92066ab6ba1ee351d710@159.203.9.164:30303"' >> "${BOR_START_SH}"
fi
sed -i 's/--http.port.*/--http.port '${http_port}' \\/g' "${BOR_START_SH}"
}
function sentry_node_setup() {
cd ${INSTALL_DIR}
ansible-playbook --connection=local -l sentry playbooks/network.yml --extra-var="bor_branch=v0.2.12-beta3 heimdall_branch=v0.2.4 network_version=mainnet-v1 node_type=sentry/sentry heimdall_network=mainnet"
}
function get_snapshot_url() {
network="${1}"
mode="${2}"
# always use pruned snapshot instead of fullnode snapshot
if [ "$mode" == "fullnode" ];then
mode="pruned"
fi
node_type="${3}"
curl -s https://snapshots.matic.today/ | grep "${network}/${node_type}-${mode}" | cut -f 3 -d '>' | cut -f 1 -d '<'
}
function load_snapshots() {
network="${1}"
mode="${2}"
heimdall_snapshot_url=`get_snapshot_url ${network} "" heimdall`
bor_snapshot_url=`get_snapshot_url ${network} ${mode} bor`
echo "$heimdall_snapshot_url"
echo "$bor_snapshot_url"
wget "${heimdall_snapshot_url}" -O - | tar -xz -C ~/.heimdalld/data/
wget "${bor_snapshot_url}" -O - | tar -xz -C ~/.bor/data/bor/chaindata
chown -R root:root ~/.heimdalld/data/ ~/.bor/data/bor/chaindata
}
function polygonctl() {
service heimdalld $1
service bor $1
}
function usage() {
echo "Usage: $0 {-n <mainnet|mumbai> -m <archive|fullnode> -s <snapshot|from_scratch> -p <rpc-port-number>}" 1>&2
exit 1
}
# Getopts
while getopts ":s:m:n:p:" o; do
case "${o}" in
s)
s=${OPTARG}
;;
m)
m=${OPTARG}
;;
n)
n=${OPTARG}
;;
p)
p=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# Validation
if [ -z "${s}" ] || [ -z "${m}" ] || [ -z "${n}" ]|| [ -z "${p}" ]; then
usage
fi
if [ "${s}" != "snapshot" ] && [ "${s}" != "from_scratch" ]; then
usage
fi
if [ "${m}" != "archive" ] && [ "${m}" != "fullnode" ]; then
usage
fi
if [ "${n}" != "mainnet" ] && [ "${n}" != "mumbai" ]; then
usage
fi
# p is not a natural decimal number
if [[ ! $p =~ ^[1-9][0-9]*$ ]];then
usage
fi
# p is not in port range ( 2'nd check isn't needed for natural number)
if ((p > 65535 || p < 1));then
usage
fi
# Main
check_root
check_os
install_dependencies
install_ansible
clone_repo
configure_inventory
sentry_node_setup
configure_node "${p}"
# we need to mount disks on late phase to prevent "existing datadir" complains from ansible
mount_disk /dev/sdb /root/.bor/data/bor/chaindata bor
mount_disk /dev/sdc /root/.heimdalld/data heimdalld
if [ "${s}" == "snapshot" ]; then
load_snapshots ${n} ${m}
fi
polygonctl start