From 46249b968c8621134b794712b09fb7eb8635b196 Mon Sep 17 00:00:00 2001 From: beeinger Date: Fri, 3 May 2024 10:54:20 +0200 Subject: [PATCH 1/2] added stop command --- Makefile.toml | 8 ++++++++ scripts/stop.sh | 9 +++++++++ 2 files changed, 17 insertions(+) create mode 100755 scripts/stop.sh diff --git a/Makefile.toml b/Makefile.toml index 17e48aa..d379bba 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -45,6 +45,14 @@ echo "Restart the program..." ./scripts/compile.sh ''' +[tasks.stop] +workspace = false +description = "Stop the L2 indexer." +script = ''' +echo "Stop the program..." +./scripts/stop.sh +''' + [tasks.cleanup] workspace = false description = "Clean up the setup by running the cleanup script." diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100755 index 0000000..ea56edf --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,9 @@ +# Step 1: Stop the application using PM2 +echo "Starting the application using PM2..." +pm2 stop pm2.config.js + +# Step 2: Stop docker container +echo "Stopping the docker container..." +docker compose down + +echo "Stopped." \ No newline at end of file From 27b6f201bf96e424e1ef15d15d1a8af39cc746cc Mon Sep 17 00:00:00 2001 From: beeinger Date: Fri, 3 May 2024 11:45:49 +0200 Subject: [PATCH 2/2] Instance setup + CI/CD --- .github/workflows/cicd.yaml | 87 +++++++++++++++++++++++++++++++++++++ .ubuntu/setup-copy-paste.sh | 57 ++++++++++++++++++++++++ scripts/setup.sh | 4 -- 3 files changed, 144 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/cicd.yaml create mode 100644 .ubuntu/setup-copy-paste.sh diff --git a/.github/workflows/cicd.yaml b/.github/workflows/cicd.yaml new file mode 100644 index 0000000..04b0f6b --- /dev/null +++ b/.github/workflows/cicd.yaml @@ -0,0 +1,87 @@ +name: Rust CI + +on: + pull_request: + branches: + - develop + - main + + push: + branches: + - main + +env: + CARGO_TERM_COLOR: always + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - uses: actions/cache@v3 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }} + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + + - name: Toolchain info + run: | + cargo --version --verbose + rustc --version + cargo clippy --version + + - name: Install prebuild binaries of cargo-make + uses: taiki-e/install-action@v2 + with: + tool: cargo-make + + - name: Run Format + run: cargo make format + + - name: Run Build + run: cargo make build + + deploy: + needs: test + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' && github.event_name == 'push' + steps: + - uses: actions/checkout@v2 + - name: Get Commit SHA + run: | + echo "Commit SHA: ${{ github.sha }}" + + - name: rsync deployments + uses: burnett01/rsync-deployments@5.2.1 + with: + switches: -avzr --delete --exclude=.github --exclude=data --exclude=target --exclude=.env --exclude=.env.dist --exclude=.env.test --exclude=mocks --exclude=.git + path: . + remote_path: /root/l2-indexer + remote_host: ${{ secrets.SSH_HOST }} + remote_user: root + remote_key: ${{ secrets.SSH_DEPLOY_KEY }} + + # - name: Run docker-compose on server + - name: Setup envs and run docker-compose on server + run: | + echo "${{ secrets.SSH_DEPLOY_KEY }}" > ./l2-indexer.pem &&\ + chmod 600 ./l2-indexer.pem &&\ + ssh -o StrictHostKeyChecking=no -i ./l2-indexer.pem root@${{ secrets.SSH_HOST }} "\ + echo -e '## Git sha: ${{ github.sha }}\nDate UNIX: $(date +%s)\n' >> version.md &&\ + echo '${{ secrets.ENV_CONFIG }}' > /root/l2-indexer/.env &&\ + docker image prune -f &&\ + docker builder prune -f &&\ + cargo make setup" + shell: bash diff --git a/.ubuntu/setup-copy-paste.sh b/.ubuntu/setup-copy-paste.sh new file mode 100644 index 0000000..e98cbf6 --- /dev/null +++ b/.ubuntu/setup-copy-paste.sh @@ -0,0 +1,57 @@ +echo "DOCKER" &&\ +sudo apt update &&\ +sudo apt install docker.io -y &&\ +sudo systemctl enable docker &&\ +sudo systemctl start docker &&\ +sudo usermod -aG docker ${USER} &&\ + +echo "DOCKER-COMPOSE" &&\ +sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose &&\ +sudo chmod +x /usr/local/bin/docker-compose &&\ + +echo "RUST" &&\ +sudo apt install build-essential -y &&\ +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh &&\ +source "$HOME/.cargo/env" &&\ +cargo install --force cargo-make &&\ + +echo "PM2" &&\ +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash &&\ +source ~/.bashrc &&\ +nvm install 22 &&\ +npm install pm2 -g &&\ +pm2 startup &&\ + +echo "NGINX" &&\ +sudo apt update -y &&\ +sudo apt install nginx git make -y &&\ +sudo systemctl enable nginx &&\ +sudo systemctl start nginx &&\ + +echo "CERTBOT" &&\ +sudo apt install python3-venv python3-pip -y &&\ +sudo python3 -m venv /opt/certbot/ &&\ +sudo /opt/certbot/bin/pip install --upgrade pip &&\ +sudo /opt/certbot/bin/pip install certbot certbot-nginx &&\ +sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot &&\ +sudo certbot --version &&\ + +echo "HTTPS (NGINX + CERTBOT)" &&\ +echo "user www-data; +worker_processes auto; + +events { + worker_connections 1024; +} + +http { + server { + server_name l2-indexer.api.herodotus.cloud; + location / { + proxy_set_header X-Forwarded-For \$remote_addr; + proxy_set_header Host \$http_host; + proxy_pass \"http://0.0.0.0:8000\"; + } + } +}" > tmp.conf && sudo cp tmp.conf /etc/nginx/nginx.conf && rm tmp.conf &&\ +sudo certbot --nginx -d l2-indexer.api.herodotus.cloud --non-interactive --agree-tos --email aws+api.production@herodotus.dev diff --git a/scripts/setup.sh b/scripts/setup.sh index b977ffd..c4adff6 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -8,10 +8,6 @@ docker-compose up --build -d echo "Building the Rust application..." cargo build --release -# Step 3: Check if the database exists and create it if it does not -echo "Waiting for PostgreSQL to start..." -sleep 10 # This might need adjustment depending on your system - # Step 4: Start the application using PM2 echo "Starting the application using PM2..." pm2 start pm2.config.js