Astroport.ONE/tools/ipfs_setup.sh

119 lines
3.7 KiB
Bash
Raw Normal View History

2022-09-10 15:48:23 +02:00
#!/bin/bash
########################################################################
{ # this ensures the entire script is downloaded #
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
ME="${0##*/}"
# CHECK not root user !!
if [ "$EUID" -eq 0 ]
then echo -e "DO NOT EXECUTE AS root. Choose a user for your Astroport Station (we like pi)"
exit 1
else echo -e "OK $USER, let's go!";
fi
# Ask user password on start
sudo true
## Error funciton
err() {
echo -e "ERREUR: $1"
exit 1
}
# CHECK node IP isLAN?
myIP=$(hostname -I | awk '{print $1}')
2022-12-18 20:45:43 +01:00
isLAN=$(route -n |awk '$1 == "0.0.0.0" {print $2}' | grep -E "/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/")
2024-04-14 17:37:53 +02:00
YOU=$(ipfs swarm peers >/dev/null 2>&1 && echo "$USER" || ps auxf --sort=+utime | grep -w ipfs | grep -v -E 'color=auto|grep' | tail -n 1 | xargs | cut -d " " -f 1);
2022-09-10 15:48:23 +02:00
MACHINE_TYPE=`uname -m`
2022-11-12 15:12:27 +01:00
# CHECK if daemon is already running
if [[ $YOU ]]; then
echo "ipfs daemon already running...! Run by $YOU $MACHINE_TYPE"
2022-11-25 20:58:43 +01:00
[[ $YOU == $USER ]] && echo "Stopping ipfs daemon" && killall ipfs \
2022-11-12 15:15:12 +01:00
|| (echo "ERROR $YOU is running ipfs, must be $USER" && exit 1)
2022-09-10 15:48:23 +02:00
else
2023-01-17 15:23:45 +01:00
# REINIT ipfs
[[ -s ~/.ipfs/config ]] && echo ">>> WARNING BACKUP OLD IPFS CONFIG ~/.ipfs/config.old"
rm -f ~/.ipfs/config.old 2>/dev/null
mv ~/.ipfs/config ~/.ipfs/config.old 2>/dev/null
2022-11-12 15:12:27 +01:00
[[ $isLAN ]] && ipfs init -p lowpower \
|| ipfs init -p server
# RESET NODE SECRET
rm -f ~/.zen/game/secret.* 2>/dev/null
2022-11-12 15:12:27 +01:00
fi
echo -e "Astroport activate IPFS Layer installation..."
if [[ "$USER" == "xbian" ]]
then
echo "enabling ipfs initV service autostart"
cd /etc/rc2.d && sudo ln -s ../init.d/ipfs S02ipfs
cd /etc/rc3.d && sudo ln -s ../init.d/ipfs S02ipfs
cd /etc/rc4.d && sudo ln -s ../init.d/ipfs S02ipfs
cd /etc/rc5.d && sudo ln -s ../init.d/ipfs S02ipfs
cd /etc/rc0.d && sudo ln -s ../init.d/ipfs K01ipfs
cd /etc/rc1.d && sudo ln -s ../init.d/ipfs K01ipfs
cd /etc/rc6.d && sudo ln -s ../init.d/ipfs K01ipfs
# Disable xbian-config auto launch
echo 0 > ~/.xbian-config-start
fi
2022-09-10 15:48:23 +02:00
## DEBIAN
echo "CREATE SYSTEMD ipfs SERVICE >>>>>>>>>>>>>>>>>>"
cat > /tmp/ipfs.service <<EOF
[Unit]
Description=IPFS daemon
After=network.target
2022-11-13 02:53:18 +01:00
Requires=network.target
2022-09-10 15:48:23 +02:00
[Service]
2022-11-13 02:53:18 +01:00
Type=simple
2022-09-10 15:48:23 +02:00
User=_USER_
2022-11-13 02:53:18 +01:00
RestartSec=1
Restart=always
Environment=IPFS_FD_MAX=8192
ExecStart=/usr/local/bin/ipfs daemon --migrate --enable-pubsub-experiment --enable-namesys-pubsub --routing=dhtclient
2022-09-10 15:48:23 +02:00
CPUAccounting=true
CPUQuota=60%
[Install]
WantedBy=multi-user.target
EOF
## LAN is dhtclient only
[[ ! $isLAN ]] \
&& sed -i "s/--routing=dhtclient//g" /tmp/ipfs.service
2023-01-17 15:23:45 +01:00
sudo cp -f /tmp/ipfs.service /etc/systemd/system/
sudo sed -i "s/_USER_/$USER/g" /etc/systemd/system/ipfs.service
2022-09-10 15:48:23 +02:00
2022-11-12 15:12:27 +01:00
sudo systemctl daemon-reload
sudo systemctl enable ipfs
2022-09-10 15:48:23 +02:00
###########################################
# ACTIVATE IPFS OPTIONS: #swarm0 INIT
###########################################
2023-01-17 15:23:45 +01:00
$MY_PATH/ipfs_config.sh
2022-11-12 15:12:27 +01:00
sudo systemctl restart ipfs
2022-09-10 15:48:23 +02:00
2022-11-13 02:53:18 +01:00
## Add ulimit "open files" (avoid ipfs hang)
ulimit -n 2048
2022-09-10 15:48:23 +02:00
} # this ensures the entire script is downloaded #
# IPFS CONFIG documentation: https://github.com/ipfs/go-ipfs/blob/master/docs/config.md#addressesswarm
2022-11-13 04:08:06 +01:00
# https://github.com/ipfs/kubo/blob/master/docs/config.md
# VISUALISER DHT
# ipfs stats dht wan
# CONSUMING RESSOURCES
# export DPID=26024; watch -n0 'printf "sockets: %s\nleveldb: %s\nflatfs: %s\n" $(ls /proc/${DPID}/fd/ -l | grep "socket:" | wc -l) $(ls /proc/${DPID}/fd/ -l | grep "\\/datastore\\/" | wc -l) $(ls /proc/${DPID}/fd/ -l | grep "\\/blocks\\/" | wc -l)'