From 9d0012ce4d0429e7fea4c441ec1adcee40637329 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 3 Sep 2023 05:22:12 +0000 Subject: [PATCH] first commit --- Makefile | 17 ++++ README.md | 0 luasrc/controller/jellyfin.lua | 7 ++ luasrc/model/cbi/jellyfin.lua | 55 +++++++++++ luasrc/model/jellyfin.lua | 55 +++++++++++ luasrc/view/jellyfin/status.htm | 31 ++++++ root/etc/config/jellyfin | 7 ++ root/etc/uci-defaults/luci-app-jellyfin | 11 +++ root/usr/libexec/apps/jellyfin.sh | 98 +++++++++++++++++++ .../share/rpcd/acl.d/luci-app-jellyfin.json | 11 +++ 10 files changed, 292 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 luasrc/controller/jellyfin.lua create mode 100644 luasrc/model/cbi/jellyfin.lua create mode 100644 luasrc/model/jellyfin.lua create mode 100644 luasrc/view/jellyfin/status.htm create mode 100644 root/etc/config/jellyfin create mode 100644 root/etc/uci-defaults/luci-app-jellyfin create mode 100644 root/usr/libexec/apps/jellyfin.sh create mode 100644 root/usr/share/rpcd/acl.d/luci-app-jellyfin.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bc48fa0 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ + + +include $(TOPDIR)/rules.mk + + +LUCI_TITLE:=LuCI support for jellyfin +LUCI_PKGARCH:=all +LUCI_DEPENDS:=+lsblk +docker +luci-lib-taskd + +define Package/luci-app-jellyfin/conffiles +/etc/config/jellyfin +endef + +include $(TOPDIR)/feeds/luci/luci.mk + +# call BuildPackage - OpenWrt buildroot signature + diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/luasrc/controller/jellyfin.lua b/luasrc/controller/jellyfin.lua new file mode 100644 index 0000000..029bc93 --- /dev/null +++ b/luasrc/controller/jellyfin.lua @@ -0,0 +1,7 @@ + +module("luci.controller.jellyfin", package.seeall) + +function index() + entry({"admin", "apps", "jellyfin"}, alias("admin", "apps", "jellyfin", "config"), _("Jellyfin"), 30).dependent = true + entry({"admin", "apps", "jellyfin", "config"}, cbi("jellyfin")) +end diff --git a/luasrc/model/cbi/jellyfin.lua b/luasrc/model/cbi/jellyfin.lua new file mode 100644 index 0000000..f31eedd --- /dev/null +++ b/luasrc/model/cbi/jellyfin.lua @@ -0,0 +1,55 @@ +--[[ +LuCI - Lua Configuration Interface +]]-- + +local taskd = require "luci.model.tasks" +local jellyfin_model = require "luci.model.jellyfin" +local m, s, o + +m = taskd.docker_map("jellyfin", "jellyfin", "/usr/libexec/apps/jellyfin/jellyfin.sh", + translate("Jellyfin"), + translate("Jellyfin is the volunteer-built media solution that puts you in control of your media. Stream to any device from your own server, with no strings attached. Your media, your server, your way.")) + +s = m:section(SimpleSection, translate("Service Status"), translate("Jellyfin status:")) +s:append(Template("jellyfin/status")) + +s = m:section(TypedSection, "jellyfin", translate("Setup"), + translate("The initial installation of Jellyfin requires at least 2GB of space, please make sure that the Docker data directory has enough space. It is recommended to migrate Docker to a hard drive before installing Jellyfin.") + .. "
" .. translate("The following parameters will only take effect during installation or upgrade:")) +s.addremove=false +s.anonymous=true + +o = s:option(Flag, "hostnet", translate("Host network"), translate("Jellyfin running in host network, for DLNA application, port is always 8096 if enabled")) +o.default = 0 +o.rmempty = false + +o = s:option(Value, "port", translate("Port").."*") +o.default = "8096" +o.datatype = "port" +o:depends("hostnet", 0) + +local blocks = jellyfin_model.blocks() +local home = jellyfin_model.home() + +o = s:option(Value, "config_path", translate("Config path").."*") +o.rmempty = false +o.datatype = "string" + +local paths, default_path = jellyfin_model.find_paths(blocks, home, "Configs") +for _, val in pairs(paths) do + o:value(val, val) +end +o.default = default_path + +o = s:option(Value, "media_path", translate("Media path"), translate("Not required, all disk is mounted in") .. " /mnt") +o.datatype = "string" + +o = s:option(Value, "cache_path", translate("Transcode cache path"), translate("Default use 'transcodes' in 'config path' if not set, please make sure there has enough space")) +o.datatype = "string" +local paths, default_path = jellyfin_model.find_paths(blocks, home, "Caches") +for _, val in pairs(paths) do + o:value(val, val) +end +o.default = default_path + +return m diff --git a/luasrc/model/jellyfin.lua b/luasrc/model/jellyfin.lua new file mode 100644 index 0000000..444085b --- /dev/null +++ b/luasrc/model/jellyfin.lua @@ -0,0 +1,55 @@ +local util = require "luci.util" +local jsonc = require "luci.jsonc" + +local jellyfin = {} + +jellyfin.blocks = function() + local f = io.popen("lsblk -s -f -b -o NAME,FSSIZE,MOUNTPOINT --json", "r") + local vals = {} + if f then + local ret = f:read("*all") + f:close() + local obj = jsonc.parse(ret) + for _, val in pairs(obj["blockdevices"]) do + local fsize = val["fssize"] + if fsize ~= nil and string.len(fsize) > 10 and val["mountpoint"] then + -- fsize > 1G + vals[#vals+1] = val["mountpoint"] + end + end + end + return vals +end + +jellyfin.home = function() + local uci = require "luci.model.uci".cursor() + local home_dirs = {} + home_dirs["main_dir"] = uci:get_first("quickstart", "main", "main_dir", "/root") + home_dirs["Configs"] = uci:get_first("quickstart", "main", "conf_dir", home_dirs["main_dir"].."/Configs") + home_dirs["Public"] = uci:get_first("quickstart", "main", "pub_dir", home_dirs["main_dir"].."/Public") + home_dirs["Downloads"] = uci:get_first("quickstart", "main", "dl_dir", home_dirs["Public"].."/Downloads") + home_dirs["Caches"] = uci:get_first("quickstart", "main", "tmp_dir", home_dirs["main_dir"].."/Caches") + return home_dirs +end + +jellyfin.find_paths = function(blocks, home_dirs, path_name) + local default_path = '' + local configs = {} + + default_path = home_dirs[path_name] .. "/Jellyfin" + if #blocks == 0 then + table.insert(configs, default_path) + else + for _, val in pairs(blocks) do + table.insert(configs, val .. "/" .. path_name .. "/Jellyfin") + end + local without_conf_dir = "/root/" .. path_name .. "/Jellyfin" + if default_path == without_conf_dir then + default_path = configs[1] + end + end + + return configs, default_path +end + +return jellyfin diff --git a/luasrc/view/jellyfin/status.htm b/luasrc/view/jellyfin/status.htm new file mode 100644 index 0000000..49cff61 --- /dev/null +++ b/luasrc/view/jellyfin/status.htm @@ -0,0 +1,31 @@ +<% +local util = require "luci.util" +local container_status = util.trim(util.exec("/usr/libexec/apps/jellyfin.sh status")) +local container_install = (string.len(container_status) > 0) +local container_running = container_status == "running" +-%> +
+ +
+ <% if container_running then %> + + <% else %> + + <% end %> +
+
+<% +if container_running then + local port=util.trim(util.exec("/usr/libexec/apps/jellyfin.sh port")) + if port == "" then + port="8096" + end +-%> +
+ +
+ + +
+
+<% end %> diff --git a/root/etc/config/jellyfin b/root/etc/config/jellyfin new file mode 100644 index 0000000..737a58f --- /dev/null +++ b/root/etc/config/jellyfin @@ -0,0 +1,7 @@ +config jellyfin + option 'hostnet' '0' + option 'port' '8096' + option 'image' 'default' + option 'media_path' '/opt/docker2/compose/media' + option 'config_path' '/opt/docker2/compose/jellyfin/config' + option 'cache_path' '' diff --git a/root/etc/uci-defaults/luci-app-jellyfin b/root/etc/uci-defaults/luci-app-jellyfin new file mode 100644 index 0000000..b5f3662 --- /dev/null +++ b/root/etc/uci-defaults/luci-app-jellyfin @@ -0,0 +1,11 @@ +#!/bin/sh + +image_name=`uci get jellyfin.@jellyfin[0].image 2>/dev/null` + +if [ "$image_name" == "jjm2473/jellyfin-rtk:latest" ]; then + uci -q batch <<-EOF >/dev/null + set jellyfin.@jellyfin[0].image="default" + commit jellyfin +EOF +fi +exit 0 diff --git a/root/usr/libexec/apps/jellyfin.sh b/root/usr/libexec/apps/jellyfin.sh new file mode 100644 index 0000000..b66c11c --- /dev/null +++ b/root/usr/libexec/apps/jellyfin.sh @@ -0,0 +1,98 @@ +#!/bin/sh + + +ACTION=${1} +shift 1 + +ARCH="default" +IMAGE_NAME='default' + +get_image() { + IMAGE_NAME="jellyfin/jellyfin" +} + +do_install() { + get_image + echo "docker pull ${IMAGE_NAME}" + docker pull ${IMAGE_NAME} + docker rm -f jellyfin + + do_install_detail +} + +do_install_detail() { + local hostnet=`uci get jellyfin.@jellyfin[0].hostnet 2>/dev/null` + local media=`uci get jellyfin.@jellyfin[0].media_path 2>/dev/null` + local config=`uci get jellyfin.@jellyfin[0].config_path 2>/dev/null` + local cache=`uci get jellyfin.@jellyfin[0].cache_path 2>/dev/null` + local port=`uci get jellyfin.@jellyfin[0].port 2>/dev/null` + local dev + + if [ -z "$config" ]; then + echo "config path is empty!" + exit 1 + fi + + [ -z "$port" ] && port=8096 + + local cmd="docker run --restart=unless-stopped -d -v \"$config:/config\" " + + if [ "$hostnet" = 1 ]; then + cmd="$cmd\ + --dns=127.0.0.1 \ + --network=host " + else + cmd="$cmd\ + --dns=172.17.0.1 \ + -p $port:8096 " + fi + + local tz="`uci get system.@system[0].zonename`" + [ -z "$tz" ] || cmd="$cmd -e TZ=$tz" + + [ -z "$cache" ] || cmd="$cmd -v \"$cache:/config/transcodes\"" + [ -z "$media" ] || cmd="$cmd -v \"$media:/media\"" + + cmd="$cmd -v /mnt:/mnt" + mountpoint -q /mnt && cmd="$cmd:rslave" + cmd="$cmd --name jellyfin \"$IMAGE_NAME\"" + + echo "$cmd" + eval "$cmd" + +} + +usage() { + echo "usage: $0 sub-command" + echo "where sub-command is one of:" + echo " install Install the jellyfin" + echo " upgrade Upgrade the jellyfin" + echo " rm/start/stop/restart Remove/Start/Stop/Restart the jellyfin" + echo " status Jellyfin status" + echo " port Jellyfin port" +} + +case ${ACTION} in + "install") + do_install + ;; + "upgrade") + do_install + ;; + "rm") + docker rm -f jellyfin + ;; + "start" | "stop" | "restart") + docker ${ACTION} jellyfin + ;; + "status") + docker ps --all -f 'name=jellyfin' --format '{{.State}}' + ;; + "port") + docker ps --all -f 'name=jellyfin' --format '{{.Ports}}' | grep -om1 '0.0.0.0:[0-9]*' | sed 's/0.0.0.0://' + ;; + *) + usage + exit 1 + ;; +esac diff --git a/root/usr/share/rpcd/acl.d/luci-app-jellyfin.json b/root/usr/share/rpcd/acl.d/luci-app-jellyfin.json new file mode 100644 index 0000000..11faab1 --- /dev/null +++ b/root/usr/share/rpcd/acl.d/luci-app-jellyfin.json @@ -0,0 +1,11 @@ +{ + "luci-app-jellyfin": { + "description": "Grant UCI access for luci-app-jellyfin", + "read": { + "uci": [ "jellyfin" ] + }, + "write": { + "uci": [ "jellyfin" ] + } + } +}