diff --git a/backend/image_install b/backend/image_install new file mode 100644 index 0000000..6a86fdc --- /dev/null +++ b/backend/image_install @@ -0,0 +1,234 @@ +#!/bin/bash + +# Apollo Core Services Image Installer + +#### SETUP #### +######################### +# Set colors +YELLOW='\033[1;33m' +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' + +# Check if script is being run as root +if [ "$EUID" -ne 0 ] + then echo -e "${RED}Install script must be run by root or with sudo${NC}" + exit +fi + +#### SYSTEM PACKAGES #### +######################### +echo -e "${YELLOW} ---> Installing and upgrading required system packages${NC}" + +# Install required system packages +apt-get update +apt-get -y upgrade + +DEBIAN_FRONTEND=noninteractive apt-get -y -q install tor sqlite3 htop net-tools iputils-ping zip unzip whois traceroute vim openssh-server curl git libvips-dev libssl-dev libxslt-dev libxml2-dev imagemagick libmagickwand-dev libreadline-dev zlib1g-dev libsqlite3-dev libpq-dev libxml2-dev yasm libzmq3-dev build-essential libxslt1-dev python2-dev python-dev-is-python3 vim git sudo iptables network-manager + +#### SYSTEM SETUP #### +######################### +# Define the APOLLO_DIR variable +APOLLO_DIR=/opt/apolloapi +APOLLO_UI_DIR=/opt/apolloapi/apolloui-v2 + +echo -e "${YELLOW} ---> Adding futurebit user${NC}" + +# Add the futurebit user +adduser -q --disabled-password --gecos "" futurebit + +# Add futurebit to the sudoers file +grep -qxF 'futurebit ALL=(ALL) NOPASSWD:ALL' /etc/sudoers || echo 'futurebit ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers + +#### APP SETUP #### +######################### +# Clone the Apollo app +echo -e "${YELLOW} ---> Cloning Apollo app${NC}" + +mkdir -p $APOLLO_DIR + +if [ "$1" = "dev" ]; then + echo -e "${YELLOW} ---> Using DEV branch${NC}" + git clone --single-branch --branch dev https://github.com/jstefanop/apolloapi-v2.git $APOLLO_DIR + rm -rf $APOLLO_DIR/apolloui + git clone --single-branch --branch dev https://github.com/jstefanop/apolloui-v2.git $APOLLO_UI_DIR +else + echo -e "${YELLOW} ---> Using PRODUCTION branch${NC}" + git clone --single-branch --branch main https://github.com/jstefanop/apolloapi-v2.git $APOLLO_DIR + rm -rf $APOLLO_DIR/apolloui + git clone --single-branch --branch main https://github.com/jstefanop/apolloui-v2.git $APOLLO_UI_DIR +fi + + +# Navigate to the /opt/apolloapi directory +cd $APOLLO_DIR + +#### NVM SETUP #### +######################### +# Install NVM +echo -e "${YELLOW} ---> Installing NVM${NC}" + +mkdir /usr/local/nvm +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | NVM_DIR=/usr/local/nvm bash + +. /usr/local/nvm/nvm.sh + +# Define the NVM_DIR variable +NVM_DIR_LINE='export NVM_DIR="/usr/local/nvm"' + +# Define the NVM sourcing line +NVM_SOURCE_LINE='[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm' + +# Add the lines to the user's ~/.bashrc file +echo "$NVM_DIR_LINE" >> /home/futurebit/.bashrc +echo "$NVM_SOURCE_LINE" >> /home/futurebit/.bashrc + +echo -e "${GREEN} ---> Lines added to /home/futurebit/.bashrc${NC}" + +# Enable nvm for the current shell session +eval "$NVM_DIR_LINE" +eval "$NVM_SOURCE_LINE" + +# Change ownership of /usr/local/nvm to user "futurebit" +chown -R futurebit /usr/local/nvm +echo -e "${GREEN} ---> Changed ownership of /usr/local/nvm to futurebit${NC}" + +# Install Node.js version 21 using nvm +nvm install 21.6.2 +echo -e "${GREEN} ---> Node.js version 21 installed${NC}" + +# Install yarn globally +nvm use 21.6.2 +npm i -g yarn +echo -e "${GREEN} ---> Yarn installed globally${NC}" + +#### APOLLO SETUP #### +# Install API dependencies +# Navigate to the /opt/apolloapi directory +echo -e "${YELLOW} ---> Installing API dependencies${NC}" +cd $APOLLO_DIR + +# Generate a random string for APP_SECRET +APP_SECRET=$(openssl rand -hex 16) + +# Create the .env file with the specified content +echo "APP_SECRET=$APP_SECRET" > .env +echo "DATABASE_URL=$APOLLO_DIR/futurebit.sqlite" >> .env + +echo -e "${GREEN} ---> .env file created with random APP_SECRET${NC}" + +# Install project dependencies using yarn +yarn cache clean +yarn +echo -e "${GREEN} ---> Project API dependencies installed using yarn${NC}" + +# Install UI dependencies +# Navigate to the /opt/apolloui directory +echo -e "${YELLOW} ---> Installing UI dependencies${NC}" +cd $APOLLO_UI_DIR + +# Generate a random string for NEXTAUTH_SECRET +NEXTAUTH_SECRET=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32) + +# Create the .env file with the specified content +echo "NEXT_PUBLIC_POLLING_TIME=5000" >> .env +echo "NEXT_PUBLIC_POLLING_TIME_NODE=30000" >> .env +echo "NEXTAUTH_SECRET='$NEXTAUTH_SECRET'" >> .env + +echo -e "${GREEN} ---> .env file created with random NEXTAUTH_SECRET${NC}" +#increase timeout for sd card installs +yarn install --network-timeout 600000 +# Install project dependencies using yarn +yarn cache clean +yarn +echo -e "${GREEN} ---> Project UI dependencies installed using yarn${NC}" + +# Build the project +yarn build +echo -e "${GREEN} ---> Project built using yarn${NC}" + + +### FIREWALL ### +######################### +echo -e "${YELLOW} ---> Starting firewall${NC}" + +bash $APOLLO_DIR/backend/firewall + +#### MINER SETUP #### +######################### +echo -e "${YELLOW} ---> Installing miner binaries${NC}" + +arch=$(uname -m) + +cp $APOLLO_DIR/backend/apollo-miner/bin/$arch/apollo-miner $APOLLO_DIR/backend/apollo-miner/futurebit-miner +cp $APOLLO_DIR/backend/apollo-miner/bin/$arch/apollo-miner-v2 $APOLLO_DIR/backend/apollo-miner/futurebit-miner-v2 +cp $APOLLO_DIR/backend/apollo-miner/bin/$arch/apollo-helper $APOLLO_DIR/backend/apollo-miner/ + +### Bitcoind ### +######################### +cp $APOLLO_DIR/backend/node/bin/$arch/bitcoind $APOLLO_DIR/backend/node/bitcoind +cp $APOLLO_DIR/backend/default-configs/bitcoin.conf $APOLLO_DIR/backend/node/ + +### ckpool ### +######################### +#cp $APOLLO_DIR/backend/ckpool/bin/$arch/ckpool $APOLLO_DIR/backend/ckpool/ TODO UPDATE BINARIES +cp $APOLLO_DIR/backend/default-configs/ckpool.conf $APOLLO_DIR/backend/ckpool/ + +### TOR ### +######################### +usermod -a -G debian-tor futurebit +cp $APOLLO_DIR/backend/torrc /etc/tor/torrc + +# Change ownership of /opt/apolloapi to user "futurebit" +chown -R futurebit $APOLLO_DIR + +#### SYSTEMD SETUP #### +######################### +# Setting up systemd services +echo -e "${YELLOW} ---> Setting up systemd services${NC}" + +# Copy the apollo-api.service file to /etc/systemd/system +cp $APOLLO_DIR/backend/systemd/apollo-api.service /etc/systemd/system/ + +# Copy the apollo-ui.service file to /etc/systemd/system +cp $APOLLO_DIR/backend/systemd/apollo-ui-v2.service /etc/systemd/system/ + +# Copy the node.service file to /etc/systemd/system +cp $APOLLO_DIR/backend/systemd/node.service /etc/systemd/system/ + +# Copy the ckpool.service file to /etc/systemd/system +cp $APOLLO_DIR/backend/systemd/ckpool.service /etc/systemd/system/ +cp $APOLLO_DIR/backend/default-configs/ckpool.conf $APOLLO_DIR/backend/ckpool/ + +# Copy the apollo-miner.service file to /etc/systemd/system +cp $APOLLO_DIR/backend/systemd/apollo-miner.service /etc/systemd/system/ + +#SWAP +cp $APOLLO_DIR/backend/systemd/swap.service /etc/systemd/system/ + +#RC LOCAL +cp $APOLLO_DIR/backend/rc.local /etc/ +chmod +x /etc/rc.local +cp $APOLLO_DIR/backend/systemd/rc-local.service /etc/systemd/system/ + +echo -e "${GREEN} ---> Copied apollo-api.service, apollo-ui-v2.service and ckpool.service to /etc/systemd/system${NC}" + +# Reload systemd daemon to recognize the new service +systemctl daemon-reload + +echo -e "${GREEN} ---> Reloaded systemd daemon${NC}" + +# Enable and start the services +systemctl enable apollo-api.service +systemctl enable apollo-ui-v2.service +systemctl enable swap.service +systemctl enable rc-local.service +systemctl enable apollo-miner.service +systemctl enable node.service +systemctl enable ckpool.service + +systemctl disable serial-getty@ttyS2.service + +echo -e "${GREEN} ---> Enabled apollo services${NC}" + +echo -e "${GREEN} ---> Installation complete${NC}" \ No newline at end of file diff --git a/backend/rc.local b/backend/rc.local index fe46334..3b167ba 100644 --- a/backend/rc.local +++ b/backend/rc.local @@ -1,4 +1,15 @@ -#MCU Image rc.local +#!/bin/sh -e +# +# rc.local +# +# This script is executed at the end of each multiuser runlevel. +# Make sure that the script will "exit 0" on success or any other +# value on error. +# +# In order to enable or disable this script just change the execution +# bits. +# +# By default this script does nothing. /opt/apolloapi/backend/firewall #configure gpios diff --git a/backend/start_swap.sh b/backend/start_swap.sh index c6ae846..a00894e 100755 --- a/backend/start_swap.sh +++ b/backend/start_swap.sh @@ -1,8 +1,14 @@ #!/bin/bash DEVICE=/dev/nvme0n1p1 +SWAPFILE=/media/nvme/swapfile if [ -b "$DEVICE" ]; then - swapon /media/nvme/swapfile + if [ ! -f "$SWAPFILE" ]; then + fallocate -l 3G $SWAPFILE + chmod 600 $SWAPFILE + mkswap $SWAPFILE + fi + swapon /media/nvme/swapfile else - exit 0 + exit 0 fi diff --git a/backend/swap.service b/backend/swap.service deleted file mode 100755 index b66ff4f..0000000 --- a/backend/swap.service +++ /dev/null @@ -1,13 +0,0 @@ -[Unit] -Description=swap -After=multi-user.target rc-local.service - -[Service] -Type=simple -User=root -#Start: -ExecStart=/opt/apolloapi/backend/start_swap.sh -WorkingDirectory=/media/nvme - -[Install] -WantedBy=multi-user.target diff --git a/backend/systemd/apollo-ui-v2.service b/backend/systemd/apollo-ui-v2.service index eac083b..94c132d 100644 --- a/backend/systemd/apollo-ui-v2.service +++ b/backend/systemd/apollo-ui-v2.service @@ -2,6 +2,7 @@ Description=Apollo App Documentation=https://nextjs.org/ After=network.target +After=apollo-api.service [Service] Type=simple diff --git a/backend/systemd/apollo-ui.service b/backend/systemd/apollo-ui.service deleted file mode 100644 index b9a58e9..0000000 --- a/backend/systemd/apollo-ui.service +++ /dev/null @@ -1,15 +0,0 @@ -[Unit] -Description=Apollo API & UI app -After=network.target -After=rc-local.service - -[Service] -Environment=NODE_ENV=production -Type=simple -User=futurebit -ExecStart=/usr/local/nvm/versions/node/v14.16.1/bin/node /opt/apolloapi/src/init.js -WorkingDirectory=/opt/apolloapi -Restart=on-failure - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/backend/systemd/ckpool.service b/backend/systemd/ckpool.service index 2f8431d..c4e04de 100644 --- a/backend/systemd/ckpool.service +++ b/backend/systemd/ckpool.service @@ -1,6 +1,7 @@ [Unit] Description=ckpool -After=multi-user.target +After=multi-user.target +After=node.service [Service] Type=forking diff --git a/backend/systemd/node.service b/backend/systemd/node.service index 54a9d7d..623c875 100755 --- a/backend/systemd/node.service +++ b/backend/systemd/node.service @@ -1,7 +1,7 @@ [Unit] Description=node After=multi-user.target -After=apollo-ui.service +After=apollo-ui-v2.service [Service] Type=forking diff --git a/backend/systemd/rc-local.service b/backend/systemd/rc-local.service new file mode 100755 index 0000000..627068c --- /dev/null +++ b/backend/systemd/rc-local.service @@ -0,0 +1,14 @@ +[Unit] + Description=/etc/rc.local Compatibility + ConditionPathExists=/etc/rc.local + +[Service] + Type=forking + ExecStart=/etc/rc.local start + TimeoutSec=0 + StandardOutput=tty + RemainAfterExit=yes + SysVStartPriority=99 + +[Install] + WantedBy=multi-user.target \ No newline at end of file diff --git a/backend/systemd/start_swap.sh b/backend/systemd/start_swap.sh deleted file mode 100755 index 15084f8..0000000 --- a/backend/systemd/start_swap.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -DEVICE=/dev/nvme0n1p1 -SWAPFILE=/media/nvme/swapfile - -if [ -b "$DEVICE" ]; then - if [ ! -f "$SWAPFILE" ]; then - fallocate -l 2G $SWAPFILE - chmod 600 $SWAPFILE - mkswap $SWAPFILE - fi - swapon /media/nvme/swapfile -else - exit 0 -fi