Astroport.ONE/tools/ipfs_setup.sh

131 lines
4.3 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}')
isLAN=$(echo $myIP | grep -E "/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/")
2022-11-12 15:12:27 +01:00
YOU=$(ps auxf --sort=+utime | grep -w ipfs | grep -v -E 'color=auto|grep' | tail -n 1 | 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-12 15:15:12 +01:00
[[ $YOU == $USER]] && echo "Stopping ipfs daemon" && killall ipfs \
|| (echo "ERROR $YOU is running ipfs, must be $USER" && exit 1)
2022-09-10 15:48:23 +02:00
else
2022-11-12 15:12:27 +01:00
# INIT ipfs
if [[ ! -d ~/.ipfs ]]; then
[[ $isLAN ]] && ipfs init -p lowpower \
|| ipfs init -p server
fi
fi
echo -e "Astroport activate IPFS Layer installation..."
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
2022-10-16 17:09:03 +02:00
ExecStart=/usr/local/bin/ipfs daemon --enable-pubsub-experiment --enable-namesys-pubsub
2022-09-10 15:48:23 +02:00
CPUAccounting=true
CPUQuota=60%
[Install]
WantedBy=multi-user.target
EOF
2022-11-12 15:12:27 +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
###########################################
### IMPORTANT !!!!!!! IMPORTANT !!!!!!
###########################################
# DHT PUBSUB mode
ipfs config Pubsub.Router gossipsub
2022-11-12 15:12:27 +01:00
2022-09-10 15:48:23 +02:00
# MAXSTORAGE = 1/2 available
availableDiskSize=$(df -P ~/ | awk 'NR>1{sum+=$4}END{print sum}')
diskSize="$((availableDiskSize / 2))"
ipfs config Datastore.StorageMax $diskSize
2022-11-12 15:12:27 +01:00
2022-09-10 15:48:23 +02:00
## Activate Rapid "ipfs p2p"
ipfs config --json Experimental.Libp2pStreamMounting true
ipfs config --json Experimental.P2pHttpProxy true
2022-11-13 04:08:06 +01:00
ipfs config --json Swarm.ConnMgr.LowWater 20
ipfs config --json Swarm.ConnMgr.HighWater 40
2022-11-12 15:12:27 +01:00
[[ ! $isLAN ]] && ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://'$myIP':8080", "http://127.0.0.1:8080", "http://127.0.1.1:8080" ]' \
|| ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://127.0.0.1:8080", "http://127.0.1.1:8080" ]'
2022-11-12 15:12:27 +01:00
2022-09-10 15:48:23 +02:00
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'
2022-11-12 15:19:42 +01:00
## For ipfs.js = https://github.com/ipfs/js-ipfs/blob/master/docs/DELEGATE_ROUTERS.md
ipfs config --json Addresses.Swarm | jq '. += ["/ip4/0.0.0.0/tcp/30215/ws"]' > /tmp/30215.ws
ipfs config --json Addresses.Swarm "$(cat /tmp/30215.ws)"
2022-09-10 15:48:23 +02:00
ipfs config Addresses.API "/ip4/0.0.0.0/tcp/5001"
ipfs config Addresses.Gateway "/ip4/0.0.0.0/tcp/8080"
2022-11-12 15:12:27 +01:00
######### CLEAN DEFAULT BOOTSTRAP ADD Astroport.ONE Officials ###########
2022-09-10 15:48:23 +02:00
ipfs bootstrap rm --all
2022-11-12 15:12:27 +01:00
for bootnode in $(cat ~/.zen/Astroport.ONE/A_boostrap_nodes.txt | grep -Ev "#") # remove comments
do
ipfsnodeid=${bootnode##*/}
ipfs bootstrap add $bootnode
done
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)'