Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move install.ztnet package to main repo #185

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/install-ztnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Deploy to install.ztnet.network

# Controls when the workflow will run
on:
push:
branches:
- main
paths:
- 'install.ztnet/**'
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
prepear:
# The type of runner that the job will run on
runs-on: self-hosted
steps:
- uses: actions/checkout@v2
with:
clean: false
- name: Enviorment
run: |
touch .env
echo NODE_ENV="production" >> .env
echo NODE_MAILER_USER="${{ secrets.NODE_MAILER_USER }}" >> .env
echo NODE_MAILER_PASSWORD="${{ secrets.NODE_MAILER_PASSWORD }}" >> .env

- name: install dependencies
run: npm install

deploy:
runs-on: self-hosted
needs: prepear
steps:
- name: Build Application
run: npm run build

- name: Restart Server
run: pm2 restart ecosystem.config.js

- name: Sleep 10sec
run: sleep 10s
4 changes: 4 additions & 0 deletions install.ztnet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.env
dist
bin/
37 changes: 37 additions & 0 deletions install.ztnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## ztnet installation script

This is the code and installation scripts running at install.ztnet.network.

### Installation Steps for Debian and Ubuntu

1. Open a terminal window.
2. Run the following command to download and execute the installation script:
```bash
curl -s http://install.ztnet.network | sudo bash
```
3. Follow any on-screen instructions to complete the installation.

After completing these steps, ztnet should be successfully installed on your system.

### Running the Server (Development)

To run the server, follow these steps:

1. Install the dependencies:

```bash
npm install
```

2. To start the server in development mode, run:

```bash
npm run start
```

3. To build the project, run:
```bash
npm run build
```

These commands are specified in the `package.json` under the `scripts` section.
6 changes: 6 additions & 0 deletions install.ztnet/bash/error.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

printf "\n${YELLOW}Could not find any ztnet installation file, please make sure you typed the command correctly!${NC}\n\n"
printf "${YELLOW}please refer to https://docs.ztnet.network/${NC}\n\n"
225 changes: 225 additions & 0 deletions install.ztnet/bash/ztnet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#!/bin/bash

# exit if any command fails
set -e

INSTALL_DIR="/tmp/ztnet"
TARGET_DIR="/opt/ztnet"
NODE_MAJOR=18

ARCH=$(dpkg --print-architecture)
OS=$(grep -Eoi 'Debian|Ubuntu' /etc/issue)

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

ARCH="$(uname -m)"
case "$ARCH" in
"x86_64")
ARCH="amd64"
;;
"aarch64")
ARCH="arm64"
;;
*)
echo ">>> Unsupported architecture: $ARCH"
exit 1
;;
esac

while getopts v: option
do
case "${option}"
in
v) CUSTOM_VERSION=${OPTARG};;
esac
done

if [[ "$(lsb_release -is)" != "Debian" && "$(lsb_release -is)" != "Ubuntu" ]]; then
echo "This script is only for Debian and Ubuntu. Exiting."
exit 1
fi

# Show info text to user
printf "\n\n${YELLOW}Welcome to the installation script.${NC}\n"
printf "${YELLOW}This script will perform the following actions:${NC}\n"
printf " 1. Check if PostgreSQL is installed. If not, it will be installed.\n"
printf " 2. Check if Node.js version 18 is installed. If not, it will be installed.\n"
printf " 3. Clone ztnet repo into /tmp folder and build artifacts .\n"
printf " 4. Copy artifacts to /opt/ztnet folder.\n"
printf "${YELLOW}Please note:${NC}\n"
printf " - You will have the option to set a custom password for the PostgreSQL user 'postgres'.\n"
printf "Press space to proceed with the installation..." >&2
read -n1 -s < /dev/tty

# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}

# install git curl openssl
if ! command_exists git; then
sudo apt install git -y
fi

if ! command_exists curl; then
sudo apt install curl -y
fi

if ! command_exists openssl; then
sudo apt install openssl -y
fi

# Remove directories and then recreate the target directory
rm -rf "$INSTALL_DIR" "$TARGET_DIR/.next" "$TARGET_DIR/prisma" "$TARGET_DIR/src"
mkdir -p "$TARGET_DIR"

POSTGRES_PASSWORD="postgres"

# Install PostgreSQL
if ! command_exists psql; then
sudo apt install postgresql postgresql-contrib -y
# Ask user if they want to set a custom password for PostgreSQL
printf "${YELLOW}Do you want to set a custom password for the PostgreSQL user 'postgres'? (Default is 'postgres'):${NC}\n"
printf "yes / no ==> " >&2
read setCustomPassword < /dev/tty

if [[ "$setCustomPassword" == "yes" || "$setCustomPassword" == "y" ]]; then
printf "Enter the custom password: " >&2
read POSTGRES_PASSWORD < /dev/tty
echo "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" | sudo -u postgres psql
else
echo "ALTER USER postgres WITH PASSWORD 'postgres';" | sudo -u postgres psql
fi
fi

# Install Node.js if it's not installed or if installed version is not 18
if ! command_exists node; then
INSTALL_NODE=true
else
NODE_VERSION=$(node -v | cut -d 'v' -f 2 | cut -d '.' -f 1)
if [ "$NODE_VERSION" -lt 18 ]; then
INSTALL_NODE=true
fi
fi

if [ "$INSTALL_NODE" = true ]; then
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt-get update
sudo apt-get install nodejs -y
fi

# Install ZeroTier
curl -s https://install.zerotier.com | sudo bash

# Setup Ztnet
# Clone Ztnet repository into /opt folder
if [[ ! -d "$INSTALL_DIR" ]]; then
git clone https://github.com/sinamics/ztnet.git $INSTALL_DIR
echo "Cloned Ztnet repository."
else
echo "$INSTALL_DIR already exists. Updating the repository."
cd $INSTALL_DIR
git pull origin main
fi

cd $INSTALL_DIR
git fetch --tags
latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
echo "Checking out tag: ${CUSTOM_VERSION:-$latestTag}"
git checkout ${CUSTOM_VERSION:-$latestTag}
npm install

# Copy mkworld binary
cp "$INSTALL_DIR/ztnodeid/build/linux_$ARCH/ztmkworld" /usr/local/bin/ztmkworld

# File path to the .env file
env_file="$INSTALL_DIR/.env"

# A function to set or update an environment variable in .env file
set_env_var() {
local key="$1"
local value="$2"
if grep -qE "^$key=" "$env_file"; then
# If key exists, update it
sed -i "s|^$key=.*|$key=$value|" "$env_file"
else
# If key doesn't exist, append it
echo "$key=$value" >> "$env_file"
fi
}

# Check if .env file exists, if not create it
[[ ! -f "$env_file" ]] && touch "$env_file" && echo "Created new .env file"

# Variables with default values
DATABASE_URL="postgresql://postgres:$POSTGRES_PASSWORD@127.0.0.1:5432/ztnet?schema=public"
ZT_ADDR="http://127.0.0.1:9993"
NEXT_PUBLIC_SITE_NAME="ZTnet"
NEXTAUTH_URL="http://localhost:3000"
NEXT_PUBLIC_APP_VERSION="${CUSTOM_VERSION:-$latestTag}"

# Set or update environment variables
set_env_var "DATABASE_URL" "$DATABASE_URL"
set_env_var "ZT_ADDR" "$ZT_ADDR"
set_env_var "NEXT_PUBLIC_SITE_NAME" "$NEXT_PUBLIC_SITE_NAME"
set_env_var "NEXTAUTH_URL" "$NEXTAUTH_URL"
set_env_var "NEXT_PUBLIC_APP_VERSION" "$NEXT_PUBLIC_APP_VERSION"

# Handle NEXTAUTH_SECRET specifically to retain value
if ! grep -q "NEXTAUTH_SECRET" "$env_file"; then
randomSecret=$(openssl rand -hex 32)
set_env_var "NEXTAUTH_SECRET" "$randomSecret"
echo "Generated and saved a new NEXTAUTH_SECRET"
fi

# Populate PostgreSQL and build Next.js
npx prisma migrate deploy
npx prisma db seed
npm run build

# Ensure the target directories exist
mkdir -p "$TARGET_DIR"
mkdir -p "$TARGET_DIR/.next/standalone"
mkdir -p "$TARGET_DIR/prisma"

# Copy relevant files and directories
cp "$INSTALL_DIR/next.config.mjs" "$TARGET_DIR/"
cp -r "$INSTALL_DIR/public" "$TARGET_DIR/public"
cp "$INSTALL_DIR/package.json" "$TARGET_DIR/package.json"

# Copy .next and prisma directories
cp -a "$INSTALL_DIR/.next/standalone/." "$TARGET_DIR/"
cp -r "$INSTALL_DIR/.next/static" "$TARGET_DIR/.next/static"
cp -r "$INSTALL_DIR/prisma" "$TARGET_DIR/prisma"

# Setup systemd service for Ztnet
cat > /etc/systemd/system/ztnet.service <<EOL
[Unit]
Description=ZTnet Service
After=network.target

[Service]
ExecStart=/usr/bin/node "$TARGET_DIR/server.js"
Restart=always

[Install]
WantedBy=multi-user.target
EOL

# Enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable ztnet
sudo systemctl restart ztnet

# Detect local IP address
local_ip=$(hostname -I | awk '{print $1}')

rm -rf "$INSTALL_DIR"
printf "\n\nYou can now open ZTnet at: ${YELLOW}http://${local_ip}:3000${NC}\n"
12 changes: 12 additions & 0 deletions install.ztnet/ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
apps: [
{
name: 'ztnet_installer',
script: './dist/index.js',
log_date_format: 'YYYY-MM-DD HH:mm Z',
env: {
NODE_ENV: 'production',
},
},
],
};
23 changes: 23 additions & 0 deletions install.ztnet/nginx/install.ztnet.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
upstream backend {
server localhost:9090; #node app address
}
# redirect https => http
server {
listen 443 ssl;
server_name install.ztnet.network;
return 301 http://$host$request_uri;
}
server {
listen 80;
server_name install.ztnet.network;

location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Loading
Loading