Spliiting to far may not be the solution

This commit is contained in:
Samuel Ortion 2022-08-22 06:22:30 +02:00
parent 9d0cb08792
commit 048e93fef0
17 changed files with 333 additions and 50 deletions

View File

@ -2,3 +2,6 @@ var
.venv
.github
.ideas
media
daemon/systemd
analyzer

11
.env
View File

@ -1 +1,12 @@
# BirdNET-Analyzer environment
CUDA_VISIBLE_DEVICES=""
# BirdNET-stream environment
DATABASE_USER="birdnet"
DATABASE_PASSWORD="secret"
DATABASE_PORT="3306" # Change this if you have already a running MySQL server on the host
MYSQL_ROOT_PASSWORD="secret"
RECORDS_FOLDER="./var/chunks"
CHARTS_FOLDER="./var/charts"
SERVER_NAME="birdnet.lan"

2
.gitignore vendored
View File

@ -2,6 +2,8 @@ var/
/.venv/
.env
.env.local
!.env.local.example
species_list.txt

View File

@ -1,36 +1,76 @@
version: '3.8'
version: '3.9'
services:
# recording:
# container_name: birdnet_recording
# build:
# context: .
# dockerfile: ./docker/recording/Dockerfile
# restart: unless-stopped
# environment:
# - CHUNK_FOLDER=${CHUNK_FOLDER:-/media/birdnet/records}
# volumes:
# - ./var/:/media/birdnet/records
# # Allow container to access to the hosts microphone
# devices:
# - /dev/snd
# analyzer:
# container_name: birdnet_analyzer
# build:
# context: ./analyzer/
# dockerfile: ./Dockerfile
# db:
# container_name: birdnet_database
# image: mariadb:latest
# command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
# ports:
# - '3307:3306'
# networks:
# - birdnet_network
# environment:
# MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-secret}'
# MYSQL_DATABASE: ${DATABASE:-birdnet}
# MYSQL_USER: ${DATABASE_USER:-birdnet}
# MYSQL_PASSWORD: ${DATABASE_PASSWORD:-secret}
# volumes:
# - ./docker/database/init:/docker-entrypoint-initdb.d
# restart: unless-stopped
symfony:
container_name: birdnet_symfony
hostname: birdnet.symfony
build:
context: .
dockerfile: ./docker/symfony/Dockerfile
environment:
restart: unless-stopped
volumes:
networks:
- birdnet_network
depends_on:
- db
nginx:
container_name: birdnet_nginx
hostname: birdnet.nginx
build:
context: .
dockerfile: ./docker/nginx/Dockerfile
ports:
- '81:80'
- '444:443'
environment:
- PHP_FPM_PORT=${PHP_FPM_PORT:-9001}
- PHP_FPM_HOST=${PHP_FPM_HOST:-birdnet.php-fpm}
networks:
- birdnet_network
restart: unless-stopped
networks:
birdnet_network:
services:
# database:
# container_name: birdnet_database
# image:
php:
container_name: birdnet_php
image: php:8.1-fpm
ports:
- "${PHP_FPM_PORT:-9001}:9000"
nginx:
container_name: birdnet_nginx
build:
context: ./docker/
environment:
SERVER_NAME: ${SERVER_NAME:-birdnet.local}
PHP_FPM_PORT: ${PHP_FPM_PORT:-9001}
restart: unless-stopped
volumes:
- ./www:/var/www/birdnet/
- ./www/nginx.conf:/etc/nginx/conf.d/birdnet.conf
ports:
- "81:80"
dependends_on:
- php
birdnet:
container_name: birdnet_analyzer
image:

View File

@ -4,19 +4,24 @@ FROM debian:bullseye
ENV REPOSITORY=${REPOSITORY:-https://github.com/UncleSamulus/BirdNET-stream.git}
# DEBUG defaults to 1 for descriptive DEBUG logs, 0 for error logs only
ENV DEBUG=${DEBUG:-1}
RUN useradd birdnet
WORKDIR /home/birdnet
RUN useradd -m -s /bin/bash -G sudo birdnet
USER birdnet
# Upgrade system
RUN apt-get update && apt-get upgrade -y
# Install sudo
# Install some dependencies
RUN apt-get install -y \
sudo \
git
git \
curl \
bash \
vim \
systemctl
COPY ./install.sh install.sh
RUN bash ./install.sh
RUN ./install.sh
USER birdnet
EXPOSE 443

View File

@ -0,0 +1,12 @@
#! /usr/bin/bash
if [[ -z "${MYSQL_USER}" ]]; then
MYSQL_USER="birdnet"
echo "Defaults MYSQL_USER to $MYSQL_USER"
fi
if [[ -z "${MYSQL_USER_PASSWORD}" ]]; then
echo "MYSQL_ROOT_PASSWORD is not set"
exit 1
fi
sed -i "s/<MYSQL_USER>/$MYSQL_USER/g" ./01-databases.sql.template ./01-databases.sql
sed -i "s/<MYSQL_USER_PASSWORD>/$MYSQL_USER_PASSWORD/g" ./01-databases.sql.template ./01-databases.sql

View File

@ -0,0 +1,6 @@
CREATE DATABASE IF NOT EXISTS `birdnet_observations`;
CREATE DATABASE IF NOT EXISTS `birdnet_default`;
CREATE USER '<MYSQL_USER>'@'localhost' IDENTIFIED BY '<MYSQL_PASSWORD>';
GRANT ALL PRIVILEGES ON birdnet_observations.* TO '<MYSQL_USER>'@'%';
GRANT ALL PRIVILEGES ON birdnet_default.* TO '<MYSQL_USER>'@'%';

25
docker/nginx/Dockerfile Normal file
View File

@ -0,0 +1,25 @@
FROM nginx:latest
ENV PHP_FPM_PORT=${PHP_FPM_PORT:-9000}
ENV SERVER_NAME=${SERVER_NAME:-birdnet.lan}
ENV PROJECT_ROOT=${PROJECT_ROOT:-/opt/birdnet}
ENV SYMFONY_PUBLIC=${SYMFONY_PUBLIC:-${PROJECT_ROOT}/www/public}
USER root
COPY docker/nginx/nginx.conf.template /etc/nginx/sites-available/birdnet.conf
RUN ln -s /etc/nginx/sites-available/birdnet.conf /etc/nginx/sites-enabled/birdnet.conf \
&& sed -i "s|<SERVER_NAME>|${SERVER_NAME}|g" /etc/nginx/sites-available/birdnet.conf \
&& sed -i "s|<SYMFONY_PUBLIC>|${SYMFONY_PUBLIC}|g" /etc/nginx/sites-available/birdnet.conf \
&& sed -i "s|<RECORDS_DIR>|${RECORDS_DIR}|g" /etc/nginx/sites-available/birdnet.conf \
&& sed -i "s|<CHARTS_DIR>|${CHARTS_DIR}|g" /etc/nginx/sites-available/birdnet.conf
RUN mkdir -p /etc/nginx/certs/birdnet
WORKDIR /etc/nginx/certs/birdnet
RUN openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out fullchain.pem -sha256 -days 365 -nodes --subj "/CN=${SERVER_NAME}"
RUN sed -i "s|<CERTIFICATE>|/etc/nginx/certs/birdnet/fullchain.pem|g" /etc/nginx/sites-available/birdnet.conf \
&& sed -i "s|<PRIVATE_KEY>|/etc/nginx/certs/birdnet/privkey.pem|g" /etc/nginx/sites-available/birdnet.conf
RUN nginx -t
EXPOSE 443
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

View File

@ -0,0 +1,56 @@
server {
listen 80;
server_name <SERVER_NAME>;
location / {
return 302 https://$host$request_uri;
}
location /.well-known/acme-challenge {
alias /var/www/html/.well-known/acme-challenge;
allow all;
}
}
server {
listen 443 ssl;
server_name <SERVER_NAME>;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
root <SYMFONY_PUBLIC>;
ssl_certificate <CERTIFICATE>;
ssl_certificate_key <PRIVATE_KEY>;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ^~ /media/records {
autoindex on;
alias <RECORDS_FOLDER>;
}
location ^~ /media/charts {
autoindex on;
alias <CHARTS_FOLDER>;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location /stream {
proxy_pass http://localhost:8000/stream;
}
access_log /var/log/nginx/birdnet-access.log;
error_log /var/log/nginx/birdnet-error.log error;
}

View File

@ -1,16 +1,25 @@
# Recording container for BirdNET-stream
# Reference: https://leimao.github.io/blog/Docker-Container-Audio/
# References:
# - https://leimao.github.io/blog/Docker-Container-Audio/
# - https://askubuntu.com/questions/972510/how-to-set-alsa-default-device-to-pulseaudio-sound-server-on-docker
FROM debian:bullseye
ENV DEBIAN_FRONTEND noninteractive
# Install packages dependencies
RUN apt-get update && \
apt-get install apt-utils \
&& apt-get install -y --no-install-recommends \
RUN apt-get update && apt-get upgrade -y \
&& apt-get install -y \
--no-install-recommends \
libasound2 \
alsa-utils \
libsndfile1-dev \
&& apt-get install -y ffmpeg \
&& apt-get clean
RUN mkdir -p /opt/birdnet/
WORKDIR /opt/birdnet/
COPY config ./config
COPY daemon/birdnet_recording.sh /usr/local/bin/birdnet_recording.sh
ENTRYPOINT ["/usr/local/bin/birdnet_recording.sh"]

56
docker/symfony/Dockerfile Normal file
View File

@ -0,0 +1,56 @@
ARG PHP_VERSION=${PHP_VERSION:-8.1}
FROM php:${PHP_VERSION}
ENV PROJECT_ROOT=${PROJECT_ROOT:-/opt/birdnet}
ENV SYMFONY_PUBLIC=${SYMFONY_PUBLIC:-${PROJECT_ROOT}/www/public}
RUN apt-get update && apt-get upgrade -y \
&& apt-get install -y \
nginx \
curl \
gzip \
git \
vim \
&& apt-get clean
# Install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php composer-setup.php --install-dir=/usr/local/bin --filename=composer
ENV NODE_VERSION="16.17.0"
ENV NVM_DIR="/usr/local/nvm"
RUN mkdir ${NVM_DIR}
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
RUN . "$NVM_DIR/nvm.sh" \
&& nvm install ${NODE_VERSION} \
&& nvm use ${NODE_VERSION} \
&& nvm alias default ${NODE_VERSION} \
&& npm install -g yarn
ENV PATH="$PATH:/usr/local/nvm/versions/node/v${NODE_VERSION}/bin"
WORKDIR ${PROJECT_ROOT}
RUN chown -R www-data:www-data "${PROJECT_ROOT}"
# Install composer dependencies
USER www-data
WORKDIR ${PROJECT_ROOT}/www
RUN composer install
# Install yarn dependencies
RUN . "$NVM_DIR/nvm.sh" && yarn install && yarn build
USER root
COPY docker/www/nginx.conf.template /etc/nginx/sites-available/birdnet.conf
RUN ln -s /etc/nginx/sites-available/birdnet.conf /etc/nginx/sites-enabled/birdnet.conf \
&& sed -i "s|<SERVER_NAME>|${SERVER_NAME}|g" /etc/nginx/sites-available/birdnet.conf \
&& sed -i "s|<SYMFONY_PUBLIC>|${SYMFONY_PUBLIC}|g" /etc/nginx/sites-available/birdnet.conf \
&& sed -i "s|<RECORDS_DIR>|${RECORDS_DIR}|g" /etc/nginx/sites-available/birdnet.conf \
&& sed -i "s|<CHARTS_DIR>|${CHARTS_DIR}|g" /etc/nginx/sites-available/birdnet.conf
RUN mkdir -p /etc/nginx/certs/birdnet
WORKDIR /etc/nginx/certs/birdnet
RUN openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out fullchain.pem -sha256 -days 365 -nodes --subj "/CN=${SERVER_NAME}"
RUN sed -i "s|<CERTIFICATE>|/etc/nginx/certs/birdnet/fullchain.pem|g" /etc/nginx/sites-available/birdnet.conf \
&& sed -i "s|<PRIVATE_KEY>|/etc/nginx/certs/birdnet/privkey.pem|g" /etc/nginx/sites-available/birdnet.conf
RUN nginx -t
EXPOSE 443
EXPOSE 80
CMD [""]
CMD ["nginx", "-g", "daemon off;"]

View File

60
docs/DOCKER.md Normal file
View File

@ -0,0 +1,60 @@
# Use docker to run BirdNET-stream
There are two ways to run BirdNET-stream using docker: a "all in one" container, running all services on the same container, or using a splitted approach, running each service on a separate container.
## Prerequisites
- docker
- docker-compose (for splitted approach)
- git
## Using the all in one container (not working yet)
The all in one container is a container that runs all services on the same container.
You can follow the instructions in [./docker/all/README.md](./docker/all/README.md) to create this container.
## Using the splitted approach (recommended)
The splitted approach uses docker-compose and a docker container for each service.
This is the recommended approach to run BirdNET-stream while using docker.
Thirst of of all, you need to clone the repository.
```bash
mkdir ~/Documents/BirdNET-stream
cd ~/Documents/BirdNET-stream
git clone -b main https://github.com/UncleSamulus/BirdNET-stream.git .
```
Then, run docker-compose:
```bash
docker-compose up
```
## Building and running each of the containers
### birdnet_recording container
Building:
```bash
docker build -f ./docker/recording/Dockerfile -t birdnet_recording:latest .
```
Running
```bash
docker run --rm --device /dev/snd birdnet_recording:latest
```
### birdnet_www container
Building:
```bash
docker build -f ./docker/www/Dockerfile -t birdnet_www:latest .
```
Running
```bash
docker run --rm birdnet_www:latest
```

View File

@ -27,6 +27,7 @@ uninstall_birdnet_services() {
sudo systemctl stop "$service"
sudo systemctl disable "$service"
sudo rm -f "/etc/systemd/system/$service"
sudo systemctl daemon-reload
done
debug "Done removing systemd services"
}
@ -36,6 +37,4 @@ uninstall_webapp() {
debug "Removing nginx server configuration"
sudo unlink /etc/nginx/sites-enabled/birdnet-stream.conf
sudo systemctl restart nginx
debug "Removing webapp directory"
sudo rm -rf $WORKDIR
}

1
www/.gitignore vendored
View File

@ -8,7 +8,6 @@
/public/bundles/
/var/
/vendor/
yarn.lock
###< symfony/framework-bundle ###
###> symfony/webpack-encore-bundle ###

View File

@ -1,6 +1,6 @@
server {
listen 80;
server_name birdnet.lab.home;
server_name <SERVER_NAME>;
location / {
return 302 https://$host$request_uri;
@ -14,7 +14,7 @@ server {
server {
listen 443 ssl;
server_name birdnet.lab.home;
server_name <SERVER_NAME>;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

View File

@ -77,7 +77,7 @@ class HomeController extends AbstractController
$files = glob($this->getParameter('kernel.project_dir') . '/../var/charts/*.png');
usort($files, function($a, $b) {
return filemtime($b) - filemtime($a);
return filemtime($a) - filemtime($b);
});
$last_chart = basename(array_pop($files));
return $last_chart;