decentralized-sources/scripts/hostIPFS.sh

128 lines
3.6 KiB
Bash
Raw Normal View History

#!/bin/bash
# Script must be running from root
if [ "$EUID" -ne 0 ];
then echo "Please run as root";
exit 1;
fi;
this_repo_ipfs_cid="bafybeienn6huru6cxs3ovl5ogeik2wnvviqtoainqj4zn2q3q7zzezvu6u";
user_home_dir=$(eval echo ~$USER);
tornado_folder="$user_home_dir/tornado-ipfs";
script_log_file="/tmp/tornado-ipfs-installation.log";
cron_script_path="$tornado_folder/cronfile.cron";
function echo_log_err(){
echo $1 1>&2;
echo -e "$1\n" &>> $script_log_file;
}
function echo_log_err_and_exit(){
echo_log_err "$1";
exit 1;
}
function delete_if_exists(){
if test -d $1; then rm -rf $1; fi;
if test -f $1; then rm $1; fi;
}
function is_package_installed(){
if [ $(dpkg-query -W -f='${Status}' $1 2>/dev/null | grep -c "ok installed") -eq 0 ]; then return 1; else return 0; fi;
}
function install_requred_packages(){
apt update &>> $script_log_file;
requred_packages=("curl" "git" "ufw" "cron");
local package;
for package in ${requred_packages[@]}; do
if ! is_package_installed $package; then
# Kill apache process, because Debian configuring nginx package right during installation
if [ $package = "nginx" ]; then systemctl stop apache2; fi;
apt install --yes --force-yes -o DPkg::Options::="--force-confold" $package &>> $script_log_file;
if ! is_package_installed $package; then
echo_log_err_and_exit "Error: cannot install \"$package\" package";
fi;
fi;
done;
echo -e "\nAll required packages installed successfully";
}
function install_node(){
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash &>> $script_log_file;
. ~/.nvm/nvm.sh;
. ~/.profile;
. ~/.bashrc;
nvm install 16.20.2 &>> $script_log_file;
}
function download_repo(){
ipfs_gateways=("https://ipfs.io/ipfs" "https://dweb.link/ipfs" "https://cloudflare-ipfs.com/ipfs" "https://gateway.pinata.cloud/ipfs" "https://hardbin.com/ipfs");
for gateway in ${ipfs_gateways[@]}; do
delete_if_exists $tornado_folder;
if git clone $gateway/$this_repo_ipfs_cid $tornado_folder; then break; fi;
done;
if [ ! -d $tornado_folder ]; then echo_log_err_and_exit "Cannot download repository from IPFS"; fi;
}
function install_node_dependencies(){
npm i -g yarn &>> $script_log_file;
cd $tornado_folder;
yarn &>> $script_log_file;
}
function run_ipfs_daemon(){
cd $tornado_folder;
wget https://dist.ipfs.tech/kubo/v0.22.0/kubo_v0.22.0_linux-amd64.tar.gz;
tar -xvzf kubo_v0.22.0_linux-amd64.tar.gz;
cd kubo;
sudo bash install.sh;
ipfs init --profile server;
sudo -b ipfs daemon;
}
function configure_firewall(){
ufw allow 8080;
ufw allow 4001;
ufw deny 5000;
ufw deny 5001;
ufw insert 1 allow OpenSSH;
echo "y" | ufw enable;
ufw reload;
}
function add_to_cron(){
delete_if_exists $cron_script_path;
# Add startup script to cron (job sheduler) to up IPFS daemon after restart
echo "@reboot ipfs daemon &" > $cron_script_path;
# Add existing cron rules (not related to this ipfs) to cron script, so that they are not removed
# https://unix.stackexchange.com/questions/21297/how-do-i-add-an-entry-to-my-crontab
crontab -l | grep -v "ipfs daemon" >> $cron_script_path;
crontab $cron_script_path;
systemctl restart cron;
if crontab -l | grep -q "ipfs daemon"; then
echo "IPFS daemon added to cron autorun successfully";
else
echo_log_err "Warning: adding script to cron autorun failed.";
fi;
}
function run_pinner(){
cd $tornado_folder;
yarn pin;
}
install_requred_packages;
install_node;
download_repo;
install_node_dependencies;
run_ipfs_daemon;
configure_firewall;
add_to_cron;
run_pinner;