Skip to content

Latest commit

 

History

History
167 lines (128 loc) · 7.04 KB

DO-Instructions.md

File metadata and controls

167 lines (128 loc) · 7.04 KB

Hosting it on a Digital Ocean Droplet

These instructions describe a process for manually hosting the bot on a Digital Ocean Droplet. This will not have automated deployments.

Startup

  1. Follow the general instructions to create your bot and collect your environment variables that will be needed later.

  2. Create a PostgreSQL database somewhere (probably Supabase) and collect your database URL. You can find instructions on how to create a PostgreSQL database on Supabase here.

  3. Choose a Linux distrubution, e.g. Ubuntu. The supported platforms are linux/amd64, linux/arm64, and linux/arm/v7. The rest of the instructions assume you chose Ubuntu.

  4. For the cheapest viable option, choose a shared CPU (Basic), with the cheaper processor and no volumes. Choose whatever datacenter you want, leave the VPC network to default, and preferrably use SSH keys for authentication (follow their instructions). You won't need to manually SSH into the droplet, so adding authentication is optional.

  5. Click on your droplet, go to Access and then click "Launch Droplet Console" to connect your droplet on the web. You can SSH into your droplet manually if you want, but this is unnecessary work.

  6. Assuming you chose Ubuntu for your distribution, Git will already be installed and you will have access to apt. If you use another distribution, you may need to install this software in different ways (e.g. your distribution may not have APT). At the end of the day, you need to have Docker installed and running. NPM and Git are optional, but they allow you to conveniently run scripts from the Git repo. So we will make sure all three are installed.

    apt install docker.io npm docker-compose
    
  7. As previously mentioned, this is optional, but useful. If you skip this step, you must replace npm run ... in all future steps with whatever that script actually does.

    git clone https://github.com/mikeyaworski/utility-discord-bot.git
    cd utility-discord-bot
    
  8. Pull the latest Docker image.

    cd ~/utility-discord-bot
    npm run docker-pull
    

    Or, if you want to use a specific Docker image (other than latest), find the tag from https://hub.docker.com/repository/docker/mikeyaworski/utility-discord-bot and use this command, where ... is the tag you want to use:

    docker pull mikeyaworski/utility-discord-bot:...
    

    If you are wanting to use a specific tag, you will also need to update the start:docker script to use that tag.

  9. Create a .env file with all of the environment variables filled in. This means your secrets are written to the instance's disk. If this is a security concern for you, then there are alternative ways to define secrets, but are more effort.

    You can see the structure of the .env file here. Make sure to replace the DATABASE_URL value with whatever your hosted database URL is (probably Supabase from the instructions linked in step 2). The example environment variables show values for local development, so several of them will need to be changed for the deployment.

    If unfamiliar with the command line, here are instructions to create the .env file using vim:

    1. Create it on your local computer and copy the contents of the file.
    2. In your SSH session, run vi .env (make sure you are inside the utility-discord-bot folder).
    3. Press i to enter Insert mode
    4. Paste. This pastes the content of the .env file. If on Windows WSL, you may need to right click your WSL bar, click Properties and check "Use Ctrl+Shift+C/V as Copy/Paste" first. And then use Ctrl + Shift + V to paste.
    5. Type :x to save and quit.

    You can use something like nano instead of vim if you struggle with the instructions above.

  10. (Optional) If you are using text-to-speech or otherwise using Google Applications, create a service account and place the JSON file here:

    .data/google-application-service-account.json
    
  11. (Optional) If you want to expose your app to the outside world over HTTPS and a custom domain, then generate an SSL certificate and run the nginx server:

    1. Create a DNS A Record for your domain, and point it to the public IP address of your Digital Ocean Droplet.
    2. npm run dhparam
    3. Update deploy/nginx-conf-http/nginx.conf and deploy/nginx-conf-https/nginx.conf by replacing the server name api.utilitydiscordbot.com (and possibly port number) with your own.
    4. Update deploy/docker-compose.yml by replacing api.utilitydiscordbot.com and michael@mikeyaworski.com with your own.
  12. Start the bot:

    npm run start:docker
    

    Or, if you are running the nginx server mentioned in the previous step, instead start the bot with:

    npm run start:docker-compose
    
  13. View logs to see if everything is successful:

    npm run logs:bot
    

    Use Ctrl + C to get out of the logs.

    If you are running the nginx server with HTTPS, then you may also want to view the logs of your nginx servers or certbots. You can do that with:

    docker logs certbot -f
    docker logs http-server -f
    docker logs https-server -f
    
  14. (Optional) If you enabled HTTPS, then you should also create a cronjob to run the deploy/renew-ssl.sh script. This will both renew the SSL certificate and restart the https nginx server, so that the server will use the new certificate.

    1. Type pwd and observe the result.
    2. Ensure that in deploy/renew-ssl.sh, the cd command uses the same path as your output from pwd.
    3. Type crontab -e and choose whichever option you want to open a text editor.
    4. Add this to the bottom of the file (replace your_pwd_path):
      0 12 * * * /your_pwd_path/deploy/renew-ssl.sh >> /var/log/cron.log 2>&1
      
      This runs the renewal script on a daily basis. If all previous instructions were followed precisely, then this should be the line put at the bottom of the file:
      0 12 * * * /root/utility-discord-bot/deploy/renew-ssl.sh >> /var/log/cron.log 2>&1
      

Restarting

Without HTTPS:

cd ~/utility-discord-bot
npm run restart:docker

With HTTPS:

cd ~/utility-discord-bot
npm run docker-pull
npm run restart:docker-compose

Updating

Without HTTPS:

cd ~/utility-discord-bot
npm run docker-pull
npm run restart:docker

With HTTPS:

cd ~/utility-discord-bot
npm run docker-pull
npm run restart:docker-compose

Stopping

Without HTTPS:

cd ~/utility-discord-bot
npm run stop:docker

With HTTPS:

cd ~/utility-discord-bot
npm run stop:docker-compose

Reading Logs

cd ~/utility-discord-bot
npm run logs:bot

Use Ctrl + C to get out of the logs.

If you are running the nginx server with HTTPS, then you may also want to view the logs of your nginx servers or certbots. You can do that with:

npm run logs:cert
npm run logs:http
npm run logs:https

You may also want to read the cronjob logs for renewing your SSL certificate. You can do that with:

npm run logs:cron