-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbootstrap.sh
executable file
·204 lines (167 loc) · 6.24 KB
/
bootstrap.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
set -euo pipefail
# run this script using `sudo -E ./bootstrap.sh`, otherwise it won't work
################################################################################
# external vars the user may override
################################################################################
WITH_AUTOUPDATES=${WITH_AUTOUPDATES:-1}
NVIDIA_DRIVER_VERSION=${NVIDIA_DRIVER_VERSION:-535}
NO_LAUNCH=${NO_LAUNCH:-0}
# # internal vars
# ################################################################################
REBOOT_REQUIRED=0
DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND=noninteractive
export NEEDRESTART_MODE=a
export NEEDRESTART_SUSPEND=1
function echo_() {
echo "# $@"
echo "################################################################################"
}
# check for root and setup exit trap
################################################################################
if [[ $(id -u) -ne 0 ]]; then
echo_ "Please run this script as root."
exit 1
fi
function on_exit_ {
echo_ cleaning up...
apt-mark unhold openssh-server
systemctl enable unattended-upgrades
systemctl start unattended-upgrades
}
trap on_exit_ INT TERM EXIT
# Stop unattended upgrades
################################################################################
systemctl stop unattended-upgrades
systemctl disable unattended-upgrades
# setup base files/folders
################################################################################
echo_ setting up base files and folders
touch $HOME/.bashrc
chown $SUDO_USER:$SUDO_USER $HOME/.bashrc
chmod 644 $HOME/.bashrc
mkdir -p $HOME/.local/bin
if ! [[ $(echo $PATH | grep "$HOME/.local/bin") ]]; then
# add ~/.local/bin to the path
export PATH="$HOME/.local/bin:$PATH "
# and ensure it's there in future
echo "" >> $HOME/.bashrc
echo "export PATH=$HOME/.local/bin:$PATH" >> $HOME/.bashrc
fi
chown -R $SUDO_USER:$SUDO_USER $HOME/.local
if ! [[ $(echo $PATH | grep "$HOME/.local/bin") ]]; then
# add ~/.local/bin to the path
export PATH="$HOME/.local/bin:$PATH "
# and ensure it's there in future
echo "" >> $HOME/.bashrc
echo "export PATH=$HOME/.local/bin:$PATH" >> $HOME/.bashrc
fi
# do not upgrade openssh server whilst installing
################################################################################
apt-mark hold openssh-server
# fix anything broken, update stuff, and install base software
################################################################################
echo_ setting up base packages
apt update -qq
apt install -y vim git curl wget cron net-tools dnsutils software-properties-common
# python 3.10
################################################################################
if ! command -v python &> /dev/null; then
echo_ "Python not found, installing Python 3.10..."
apt update
apt install -y software-properties-common
add-apt-repository -y ppa:deadsnakes/ppa
apt update
apt install -y python3.10 python3.10-venv python3.10-distutils
update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
update-alternatives --set python /usr/bin/python3.10
# Install pip for Python 3.10
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
rm get-pip.py
echo_ "Python 3.10 installed successfully"
else
echo_ "Python is already installed"
fi
VENV_PATH="$HOME/.venv"
if [ ! -d "$VENV_PATH" ]; then
python -m venv $VENV_PATH
echo "source $VENV_PATH/bin/activate" >> $HOME/.bashrc
chown -R $SUDO_USER:$SUDO_USER $VENV_PATH $HOME/.bashrc
echo_ "Python venv created"
source $VENV_PATH/bin/activate
pip install bittensor==7.4.0
pip install python-dotenv==1.0.1
else
echo_ "Python venv already exists at $VENV_PATH"
fi
# docker
################################################################################
echo_ "checking for docker"
if ! [[ $(which docker) ]]; then
echo_ docker was not found, installing...
apt-get update
apt-get install -y ca-certificates curl gnupg
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io
systemctl enable --now docker
fi
echo_ "checking for docker-compose"
if ! [[ $(which docker-compose) ]]; then
echo_ "docker-compose was not found, installing..."
apt-get update
apt-get install -y docker-compose-plugin
fi
groupadd docker || true
usermod -aG docker $SUDO_USER || true
# pm2 & jq
################################################################################
echo_ "checking for pm2 & jq"
apt-get install -y -qq nodejs npm
npm i -g -q pm2
apt-get install -y -qq jq
# Nano for config
################################################################################
echo_ "checking for nano"
if ! [[ $(which nano) ]]; then
echo_ "nano was not found, installing..."
apt-get install -y -qq nano
fi
# Task for taskfile
################################################################################
echo_ "checking for task"
if ! [[ $(which task) ]]; then
echo_ "task was not found, installing..."
sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b ~/.local/bin
echo_ "task installed successfully"
fi
# configure servers to start on boot
################################################################################
if [[ NO_LAUNCH -eq 1 ]]; then
:
else
if [[ WITH_AUTOUPDATES -eq 1 ]]; then
source $HOME/.venv/bin/activate
sudo -E ./validator_autoupdater.sh
else
docker compose --env-file .vali.env -f docker-compose.yml up -d --build
fi
fi
# finally, reboot if needed
################################################################################
if [[ REBOOT_REQUIRED -eq 1 ]]; then
echo_ "bootstrap.sh modified something that requires a reboot. Please SSH back in to this machine after a short while :)"
shutdown now -r
else
echo_ "bootstrap.sh is all done :)"
fi
echo_ "Please run the following command!!"
echo_ "source ~/.bashrc"