From f439ddfcf66ebd691343e04df041c1859c83d807 Mon Sep 17 00:00:00 2001
From: Fritz Elfert <fritz@fritz-elfert.de>
Date: Feb 11 2021 06:50:07 +0000
Subject: [PATCH 1/3] Misc packaging fixes.


  - Bundle syslog dependency
  - Add dependencies on Xorg, alsa-utils, icewm, curl and ffmpeg
  - Inject maven-jar-plugin to create proper manifest with main class
    and classpath
  - Fix tmpfiles configuration
  - Add stub script for finalizing a recording
  - Update README-fedora

---

diff --git a/jibri/0001-log-to-syslog.patch b/jibri/0001-log-to-syslog.patch
index 9c627b5..2338892 100644
--- a/jibri/0001-log-to-syslog.patch
+++ b/jibri/0001-log-to-syslog.patch
@@ -1,11 +1,28 @@
-diff -Naur a/jvb/lib/logging.properties b/jvb/lib/logging.properties
---- a/lib/logging.properties	2020-08-24 22:46:22.000000000 +0200
-+++ b/lib/logging.properties	2020-09-26 15:39:19.118869706 +0200
-@@ -1,5 +1,5 @@
--handlers= java.util.logging.ConsoleHandler
--#handlers= java.util.logging.ConsoleHandler, com.agafua.syslog.SyslogHandler
-+#handlers= java.util.logging.ConsoleHandler
-+handlers= java.util.logging.ConsoleHandler, com.agafua.syslog.SyslogHandler
- #handlers= java.util.logging.ConsoleHandler, io.sentry.jul.SentryHandler
- 
- java.util.logging.ConsoleHandler.level = ALL
+diff -Naur a/lib/logging.properties b/lib/logging.properties
+--- a/lib/logging.properties	2019-08-21 19:10:53.000000000 +0200
++++ b/lib/logging.properties	2021-02-07 19:47:51.898638399 +0100
+@@ -1,4 +1,5 @@
+-handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
++#handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
++handlers = java.util.logging.ConsoleHandler, com.agafua.syslog.SyslogHandler
+ 
+ java.util.logging.FileHandler.level = FINE
+ java.util.logging.FileHandler.pattern   = /var/log/jitsi/jibri/log.%g.txt
+@@ -25,7 +26,16 @@
+ org.jitsi.jibri.selenium.util.BrowserFileHandler.limit = 10000000
+ 
+ java.util.logging.ConsoleHandler.level = FINE
+-java.util.logging.ConsoleHandler.formatter = net.java.sip.communicator.util.ScLogFormatter
++java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
++
++com.agafua.syslog.SyslogHandler.transport = udp
++com.agafua.syslog.SyslogHandler.facility = local0
++com.agafua.syslog.SyslogHandler.port = 514
++com.agafua.syslog.SyslogHandler.hostname = localhost
++com.agafua.syslog.SyslogHandler.formatter = java.util.logging.SimpleFormatter
++com.agafua.syslog.SyslogHandler.escapeNewlines = false
++
++java.util.logging.SimpleFormatter.format = %4$s %2$s %5$s %6$s%
+ 
+ org.jitsi.level = FINE
+ 
diff --git a/jibri/0002-fix-install-paths.patch b/jibri/0002-fix-install-paths.patch
deleted file mode 100644
index a014bb3..0000000
--- a/jibri/0002-fix-install-paths.patch
+++ /dev/null
@@ -1,8 +0,0 @@
-diff -Naur a/resources/debian-package/opt/jitsi/jibri/launch.sh b/resources/debian-package/opt/jitsi/jibri/launch.sh
---- a/resources/debian-package/opt/jitsi/jibri/launch.sh	2019-08-21 19:10:53.000000000 +0200
-+++ b/resources/debian-package/opt/jitsi/jibri/launch.sh	2020-09-26 14:58:59.011225840 +0200
-@@ -1,3 +1,3 @@
- #!/bin/bash
- 
--exec java -Djava.util.logging.config.file=/etc/jitsi/jibri/logging.properties -jar /opt/jitsi/jibri/jibri.jar --config "/etc/jitsi/jibri/config.json"
-+exec java -Djava.util.logging.config.file=/etc/jibri/logging.properties -jar /usr/share/jibri/jibri.jar --config "/etc/jibri/config.json"
diff --git a/jibri/README.fedora b/jibri/README.fedora
index 9a1c376..8dea888 100644
--- a/jibri/README.fedora
+++ b/jibri/README.fedora
@@ -43,7 +43,7 @@ Prosody configuration for Jitsi is found in `/etc/prosody/conf.d/jitsi-meet.cfg.
 
 ```
 $ prosodyctl cert generate __jitsiFQDN__
-$ prosodyctl cert generate auth __jitsiFQDN__
+$ prosodyctl cert generate auth.__jitsiFQDN__
 $ trust anchor /var/lib/prosody/__jitsiFQDN__
 $ trust anchor /var/lib/prosody/auth.__jitsiFQDN__
 ```
@@ -76,6 +76,15 @@ Jitsi-videobridge communicates on port 10000/udp, so it needs to be opened or fo
 
 Start `jitsi-videobridge.service`. Additional service configuration can be found in `/etc/sysconfig/jitsi-videobridge`
 
+#### Jibri
+
+Configuration is located in `/etc/jibri/config.json`. Replace `__jitsiFQDN__`, `__jibriUserSecret__` and `__recorderUserSecret__`.
+Add users to prosody:
+```
+$ prosodyctl register jibri auth.__jitsiFQDN__ __jibriUserSecret__
+$ prosodyctl register recorder auth.__jitsiFQDN__ __recorderUserSecret__
+```
+
 ### Troubleshooting
 
  - Check the logs. *Jicofo* and *Jitsi Videobridge* log to syslog, prosody by default logs to `/var/log/prosody/*`. The former's logs are a mess, but usually informative. Errors are usually related to a) leftover placeholder variables and b) issues with the certificates.
diff --git a/jibri/config.json b/jibri/config.json
index 1c890d0..7363de2 100644
--- a/jibri/config.json
+++ b/jibri/config.json
@@ -3,9 +3,9 @@
     // values from your environment
 
     // Where recording files should be temporarily stored
-    "recording_directory":"/tmp/recordings",
+    "recording_directory":"/var/spool/jibri",
     // The path to the script which will be run on completed recordings
-    "finalize_recording_script_path": "/usr/share/jibri/cleanup-script.sh",
+    "finalize_recording_script_path": "/etc/jibri/finalize_recording.sh",
     "xmpp_environments": [
         {
             // A friendly name for this environment which can be used
@@ -24,7 +24,7 @@
                 "domain": "auth.__jitsiFQDN__",
                 // The credentials for logging in
                 "username": "jibri",
-                "password": "__jibriUserSecret"
+                "password": "__jibriUserSecret__"
             },
             // Using the control_login information above, Jibri will join
             //  a control muc as a means of announcing its availability
diff --git a/jibri/finalize_recording.sh b/jibri/finalize_recording.sh
new file mode 100644
index 0000000..f603158
--- /dev/null
+++ b/jibri/finalize_recording.sh
@@ -0,0 +1,3 @@
+#! /bin/sh
+# This script is called after recording has finished.
+# TODO: Implement something useful.
diff --git a/jibri/jibri-icewm.service b/jibri/jibri-icewm.service
new file mode 100644
index 0000000..c35fa5f
--- /dev/null
+++ b/jibri/jibri-icewm.service
@@ -0,0 +1,19 @@
+[Unit]
+Description=Jibri Window Manager
+Requires=jibri-xorg.service
+After=jibri-xorg.service
+
+[Service]
+User=jibri
+Group=jibri
+Environment=DISPLAY=:0
+EnvironmentFile=-/etc/sysconfig/jibri
+ExecStartPre=/usr/bin/cp /etc/jibri/asoundrc /run/jibri/.asoundrc
+ExecStartPre=/usr/bin/cp /etc/jibri/icewm.preferences /run/jibri/.icewm/settings
+ExecStart=/usr/bin/icewm-session
+Restart=on-failure
+RestartPreventExitStatus=255
+Type=simple
+
+[Install]
+WantedBy=jibri.service
diff --git a/jibri/jibri-modules.conf b/jibri/jibri-modules.conf
new file mode 100644
index 0000000..6619319
--- /dev/null
+++ b/jibri/jibri-modules.conf
@@ -0,0 +1 @@
+snd_aloop
diff --git a/jibri/jibri-xorg.service b/jibri/jibri-xorg.service
new file mode 100644
index 0000000..82156e4
--- /dev/null
+++ b/jibri/jibri-xorg.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=Jibri Xorg Process
+After=network.target
+
+[Service]
+Environment=DISPLAY=:0
+EnvironmentFile=-/etc/sysconfig/jibri
+ExecStart=/usr/bin/Xorg -nocursor -noreset  +extension RANDR +extension RENDER -logfile /var/log/Xorg-jibri.log  -config /etc/jibri/xorg-video-dummy.conf $DISPLAY
+ExecReload=/bin/kill -HUP $MAINPID
+KillMode=process
+Restart=on-failure
+RestartPreventExitStatus=255
+Type=simple
+
+[Install]
+WantedBy=jibri.service jibri-icewm.service
diff --git a/jibri/jibri.service b/jibri/jibri.service
new file mode 100644
index 0000000..581175c
--- /dev/null
+++ b/jibri/jibri.service
@@ -0,0 +1,29 @@
+[Unit]
+Description=Jibri Process
+Requires=jibri-icewm.service jibri-xorg.service
+After=network.target
+
+[Service]
+User=jibri
+Group=jibri
+EnvironmentFile=-/etc/sysconfig/jibri
+ExecStart=/usr/libexec/jibri start
+ExecStop=/usr/libexec/jibri graceful
+ExecReload=/usr/libexec/jibri graceful
+KillMode=process
+Restart=always
+RestartPreventExitStatus=255
+Type=simple
+WorkingDirectory=~
+
+# Hardening
+NoNewPrivileges=yes
+PrivateTmp=yes
+PrivateDevices=yes
+ProtectHome=yes
+ProtectKernelTunables=yes
+ProtectControlGroups=yes
+ProtectSystem=strict
+
+[Install]
+WantedBy=multi-user.target
diff --git a/jibri/jibri.sh b/jibri/jibri.sh
new file mode 100644
index 0000000..c9a6ff4
--- /dev/null
+++ b/jibri/jibri.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+case "$1" in
+    start)
+        exec java -Djava.util.logging.config.file=/etc/jibri/logging.properties -jar /usr/share/jibri/jibri.jar --config /etc/jibri/config.json
+        ;;
+    graceful)
+        exec curl -X POST http://127.0.0.1:3333/jibri/api/internal/v1.0/gracefulShutdown
+        ;;
+    stop)
+        exec curl -X POST http://127.0.0.1:3333/jibri/api/internal/v1.0/shutdown
+        ;;
+    *)
+        echo "Invalid action specified" >&2
+        exit 1
+esac
diff --git a/jibri/jibri.spec b/jibri/jibri.spec
index c1991e1..ad57c2b 100644
--- a/jibri/jibri.spec
+++ b/jibri/jibri.spec
@@ -8,18 +8,29 @@ Summary:    Jitsi Broadcasting Infrastructure
 Url:        https://jitsi.org
 License:    ASL 2.0
 Source0:    https://github.com/%{project}/%{name}/archive/v%{version}.tar.gz
-
+Source1:    %{name}.service
+Source2:    %{name}-icewm.service
+Source3:    %{name}-xorg.service
 Source4:    %{name}.sysusers
 Source5:    %{name}.tmpfiles
 Source6:    README.fedora
-#Patch1:     0001-log-to-syslog.patch
-Patch2:     0002-fix-install-paths.patch
+Source7:    jibri-modules.conf
+Source8:    jibri.sh
+Source9:    config.json
+Source10:   finalize_recording.sh
+Patch1:     0001-log-to-syslog.patch
 
 BuildArch:      noarch
 BuildRequires:  maven
+BuildRequires:  maven-local
 BuildRequires:  java-openjdk-devel
 BuildRequires:  systemd-rpm-macros
 
+Requires:       ffmpeg
+Requires:       alsa-utils
+Requires:       icewm
+Requires:       xorg-x11-drv-dummy
+Requires:       curl
 Requires:       jre-headless
 Requires:       systemd
 %{?sysusers_requires_compat}
@@ -41,6 +52,9 @@ instructions.
 #-- PREP, BUILD & INSTALL -----------------------------------------------------#
 %prep
 %autosetup -p1
+%pom_add_dep rusv:agafua-syslog:0.4:runtime
+%pom_add_plugin :maven-jar-plugin:2.3 . \
+    '<configuration><archive><manifest><addClasspath>true</addClasspath><useUniqueVersions>false</useUniqueVersions><classpathPrefix>lib</classpathPrefix><mainClass>${exec.mainClass}</mainClass></manifest></archive></configuration>'
 
 %build
 # build & copy dependencies
@@ -53,21 +67,30 @@ mvn dependency:copy-dependencies -DincludeScope=runtime
 # program
 install -D -m 644 -t %{buildroot}%{_datadir}/%{name}/lib/ target/dependency/*
 install -m 644 target/%{name}-%{version}.jar %{buildroot}%{_datadir}/%{name}/%{name}.jar
-install -m 755 -t %{buildroot}%{_datadir}/%{name}/ resources/debian-package/opt/%{project}/%{name}/*.sh
 
 # config
 install -D -m 640 -t %{buildroot}%{_sysconfdir}/%{name}/ lib/logging.properties resources/debian-package/etc/%{project}/%{name}/*
+install -D -m 640 %{SOURCE9} %{buildroot}%{_sysconfdir}/%{name}/config.json
+# helper script
+install -D -m 755 -t %{buildroot}%{_sysconfdir}/%{name}/ %{SOURCE10}
 
 # rundir
 install -d -m 0755 %{buildroot}%{_rundir}/%{name}/
+install -d -m 0755 %{buildroot}%{_var}/spool/%{name}/
 # Create an empty key file and pid file to be marked as a ghost file below.
 # i.e it is not actually included in the rpm, only the record of it is.
 touch %{buildroot}%{_rundir}/%{name}/%{name}.pid
+# same for .asoundrc and .icewm/settings
+touch %{buildroot}%{_rundir}/%{name}/.asoundrc
+install -d -m 0755 %{buildroot}%{_rundir}/%{name}/.icewm
+touch %{buildroot}%{_rundir}/%{name}/.icewm/settings
 
 # system config
-install -D -m 644 -t %{buildroot}%{_unitdir}/ resources/debian-package/etc/systemd/system/*.service
+install -D -m 644 -t %{buildroot}%{_unitdir}/ %{SOURCE1} %{SOURCE2} %{SOURCE3}
 install -D -m 644 %{SOURCE4} %{buildroot}%{_sysusersdir}/%{name}.conf
 install -D -m 644 %{SOURCE5} %{buildroot}%{_tmpfilesdir}/%{name}.conf
+install -D -m 644 -t %{buildroot}%{_sysconfdir}/modules-load.d/ %{SOURCE7}
+install -D -m 755 %{SOURCE8} %{buildroot}%{_libexecdir}/%{name}
 
 # documentation
 install -D -m 644 -t %{buildroot}/%{_pkgdocdir}/ *.md
@@ -79,9 +102,11 @@ install -D -m 644 %{SOURCE6} %{buildroot}/%{_pkgdocdir}/README-fedora.md
 %sysusers_create_compat %{SOURCE4}
 
 %post
+/usr/sbin/modprobe snd_aloop
 %systemd_post %{name}.service %{name}-icewm.service %{name}-xorg.service
 
 %preun
+test "$1" == "0" && rm -rf %{_rundir}/%{name}/{.cache,.config,.local} || true
 %systemd_preun %{name}.service %{name}-icewm.service %{name}-xorg.service
 
 %postun
@@ -94,15 +119,23 @@ install -D -m 644 %{SOURCE6} %{buildroot}/%{_pkgdocdir}/README-fedora.md
 
 # package files/dirs
 %{_datadir}/%{name}/
-%dir %attr(0700,%{user},%{project}) %{_sysconfdir}/%{name}/
-%config(noreplace) %attr(0644,%{user},%{project}) %{_sysconfdir}/%{name}/*
+%dir %attr(0750,root,%{user}) %{_sysconfdir}/%{name}/
+%config(noreplace) %attr(0644,root,%{user}) %{_sysconfdir}/%{name}/*.{preferences,properties,conf,json}
+%config(noreplace) %attr(0644,root,%{user}) %{_sysconfdir}/%{name}/asoundrc
+%config(noreplace) %attr(0755,root,%{user}) %{_sysconfdir}/%{name}/*.sh
 %dir %attr(0755,%{user},%{user}) %{_rundir}/%{name}/
+%dir %attr(0755,%{user},%{user}) %{_rundir}/%{name}/.icewm
+%dir %attr(0755,%{user},%{user}) %{_var}/spool/%{name}/
 %ghost %attr(0644,%{user},%{user}) %{_rundir}/%{name}/%{name}.pid
+%ghost %attr(0644,%{user},%{user}) %{_rundir}/%{name}/.asoundrc
+%ghost %attr(0644,%{user},%{user}) %{_rundir}/%{name}/.icewm/settings
 
 # system config
 %{_unitdir}/*.service
 %{_tmpfilesdir}/%{name}.conf
 %{_sysusersdir}/%{name}.conf
+%{_sysconfdir}/modules-load.d/%{name}-*.conf
+%{_libexecdir}/%{name}
 
 #-- CHANGELOG -----------------------------------------------------------------#
 %changelog
diff --git a/jibri/jibri.tmpfiles b/jibri/jibri.tmpfiles
index 4128c24..42d00e8 100644
--- a/jibri/jibri.tmpfiles
+++ b/jibri/jibri.tmpfiles
@@ -1,5 +1,6 @@
 # create rundir
-d %{_rundir}/%{name} 0755 %{user} %{user} -
+d /run/jibri 0755 jibri jibri -
 # ensure correct permissions for all files in config dir
-Z %{_sysconfdir}/%{project}/%{name} 0640 %{user} %{user}
-z %{_sysconfdir}/%{project}/%{name} 0750 %{user} %{user}
+Z /etc/jibri 0640 root jibri
+z /etc/jibri 0750 root jibri
+z /etc/jibri/finalize_recording.sh 0750 root jibri

From 3fb5fd4b8b1c9b245f97947153e2831abeb001e7 Mon Sep 17 00:00:00 2001
From: Fritz Elfert <fritz@fritz-elfert.de>
Date: Feb 11 2021 06:50:07 +0000
Subject: [PATCH 2/3] Fix config permissions, add Requires for chromedriver


---

diff --git a/jibri/jibri.spec b/jibri/jibri.spec
index ad57c2b..5fb525f 100644
--- a/jibri/jibri.spec
+++ b/jibri/jibri.spec
@@ -26,6 +26,7 @@ BuildRequires:  maven-local
 BuildRequires:  java-openjdk-devel
 BuildRequires:  systemd-rpm-macros
 
+Requires:       chromedriver
 Requires:       ffmpeg
 Requires:       alsa-utils
 Requires:       icewm
@@ -120,9 +121,9 @@ test "$1" == "0" && rm -rf %{_rundir}/%{name}/{.cache,.config,.local} || true
 # package files/dirs
 %{_datadir}/%{name}/
 %dir %attr(0750,root,%{user}) %{_sysconfdir}/%{name}/
-%config(noreplace) %attr(0644,root,%{user}) %{_sysconfdir}/%{name}/*.{preferences,properties,conf,json}
-%config(noreplace) %attr(0644,root,%{user}) %{_sysconfdir}/%{name}/asoundrc
-%config(noreplace) %attr(0755,root,%{user}) %{_sysconfdir}/%{name}/*.sh
+%config(noreplace) %attr(0640,root,%{user}) %{_sysconfdir}/%{name}/*.{preferences,properties,conf,json}
+%config(noreplace) %attr(0640,root,%{user}) %{_sysconfdir}/%{name}/asoundrc
+%config(noreplace) %attr(0750,root,%{user}) %{_sysconfdir}/%{name}/*.sh
 %dir %attr(0755,%{user},%{user}) %{_rundir}/%{name}/
 %dir %attr(0755,%{user},%{user}) %{_rundir}/%{name}/.icewm
 %dir %attr(0755,%{user},%{user}) %{_var}/spool/%{name}/

From 2486bb232f733a5e4a08b0fd62ecb63c6325c020 Mon Sep 17 00:00:00 2001
From: Fritz Elfert <fritz@fritz-elfert.de>
Date: Feb 11 2021 06:50:07 +0000
Subject: [PATCH 3/3] Add Requires for xdotool


---

diff --git a/jibri/jibri.spec b/jibri/jibri.spec
index 5fb525f..71674d7 100644
--- a/jibri/jibri.spec
+++ b/jibri/jibri.spec
@@ -26,6 +26,7 @@ BuildRequires:  maven-local
 BuildRequires:  java-openjdk-devel
 BuildRequires:  systemd-rpm-macros
 
+Requires:       xdotool
 Requires:       chromedriver
 Requires:       ffmpeg
 Requires:       alsa-utils

