Skip to content

Commit

Permalink
feat: Rclone Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
iPromKnight committed Jan 3, 2025
1 parent 4c693de commit 74065bc
Show file tree
Hide file tree
Showing 16 changed files with 671 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.qodo
**/local-dev/
.idea/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 iPromKnight

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
services:
rclone-manager:
image: ipromknight/rclone-manager:latest
container_name: rclone-manager
restart: unless-stopped
stop_signal: SIGTERM
stop_grace_period: 30s
ports:
- "5572:5572" # Rclone RC API
- "8080:8080"
devices:
- /dev/fuse:/dev/fuse:rwm
cap_add:
- SYS_ADMIN
security_opt:
- apparmor:unconfined
volumes:
- ./config.yaml:/data/config.yaml
- ./rclone.conf:/data/rclone.conf
- ./local-dev/mnt/rclone:/mnt/rclone:shared
- ./local-dev/caches/rclone:/caches/rclone
environment:
# General
RCLONE_BUFFER_SIZE: 0
RCLONE_BWLIMIT: 100M
RCLONE_BIND: 0.0.0.0
RCLONE_LOG_LEVEL: INFO
RCLONE_CACHE_DIR: /caches/rclone
RCLONE_DIR_CACHE_TIME: 10s
RCLONE_TIMEOUT: 10m
RCLONE_UMASK: 002
RCLONE_UID: 1000
RCLONE_GID: 1000
RCLONE_ALLOW_NON_EMPTY: "true"
RCLONE_ALLOW_OTHER: "true"
RCLONE_CONFIG: /data/rclone.conf

# RCD API
RCLONE_RC_ADDR: :5572
RCLONE_RC_NO_AUTH: "true"
RCLONE_RC_WEB_GUI: "true"
RCLONE_RC_WEB_GUI_NO_OPEN_BROWSER: "true"

# VFS DEFAULTS
RCLONE_VFS_MIN_FREE_SPACE: off
RCLONE_VFS_CACHE_MAX_AGE: 24h
RCLONE_VFS_MAX_CACHE_SIZE: 100G
RCLONE_VFS_CACHE_MODE: writes
RCLONE_VFS_READ_CHUNK_LIMIT: 64M
RCLONE_VFS_READ_CHUNK_SIZE: 5M

# MOUNT DEFAULTS
RCLONE_NO_TRAVERSE: "true"
RCLONE_IGNORE_EXISTING: "true"
RCLONE_POLL_INTERVAL: 0
8 changes: 8 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mounts:
- backendName: "AllDebrid"
mountPoint: "/mnt/rclone/alldebrid"

serves:
- backendName: "AllDebrid"
protocol: "webdav"
addr: "0.0.0.0:8080"
6 changes: 6 additions & 0 deletions rclone.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[AllDebrid]
type = webdav
url = https://webdav.debrid.it
vendor = other
user = (your api key here)
pass = PLp0KArAAjtURUCflsNLRpceDocB4w # this is the password used for all debrid which is 'eeeeee'. Its value has been obscured so rclone can use it - see: https://rclone.org/commands/rclone_obscure/
26 changes: 26 additions & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM golang:1.23-alpine AS build

WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o rclone-manager ./cmd/main.go



FROM alpine:latest

RUN apk add --no-cache \
fuse3 \
curl \
rclone

RUN mkdir -p /data

RUN ln -s /usr/bin/fusermount3 /usr/bin/fusermount

COPY --from=build /app/rclone-manager /usr/local/bin/rclone-manager

ENTRYPOINT ["/usr/local/bin/rclone-manager"]

HEALTHCHECK --interval=10s --timeout=30s --start-period=5s --retries=3 CMD curl -f http://localhost:5572 || exit 1
30 changes: 30 additions & 0 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"github.com/rs/zerolog"
"os"
"os/signal"
"rclone-manager/internal/rclone_manager"
"syscall"
"time"
)

var logger = zerolog.New(os.Stdout).With().Timestamp().Logger()

func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

conf := rclone_manager.InitializeRCD(logger)

for {
select {
case sig := <-sigs:
logger.Warn().Msgf("Received signal %v, shutting down...", sig)
rclone_manager.StopRcloneRemoteDaemon(conf, logger)
os.Exit(0)
default:
time.Sleep(1 * time.Second)
}
}
}
14 changes: 14 additions & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module rclone-manager

go 1.23

require (
github.com/rs/zerolog v1.33.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.12.0 // indirect
)
19 changes: 19 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
35 changes: 35 additions & 0 deletions src/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package config

import (
"gopkg.in/yaml.v3"
"os"
)

const YAMLPath = "/data/config.yaml"

type Config struct {
Serves []struct {
BackendName string `yaml:"backendName"`
Protocol string `yaml:"protocol"`
Addr string `yaml:"addr"`
} `yaml:"serves"`

Mounts []struct {
BackendName string `yaml:"backendName"`
MountPoint string `yaml:"mountPoint"`
} `yaml:"mounts"`
}

func LoadConfig() (*Config, error) {
data, err := os.ReadFile(YAMLPath)
if err != nil {
return nil, err
}

var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return &config, nil
}
96 changes: 96 additions & 0 deletions src/internal/mount_manager/mount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package mount_manager

import (
"github.com/rs/zerolog"
"os"
"os/exec"
"rclone-manager/internal/config"
"syscall"
"time"
)

func InitializeMounts(conf *config.Config, logger zerolog.Logger) {
if len(conf.Mounts) == 0 {
logger.Debug().Msg("No rclone mount endpoints defined... Skipping...")
return
}

logger.Info().Msg("Initializing all Mounts")
for _, mount := range conf.Mounts {
StartMountWithRetries(mount.BackendName, mount.MountPoint, logger)
}
}

func StartMountWithRetries(backend string, mountPoint string, logger zerolog.Logger) {
retries := 0
for retries < 3 {
ensureMountPointExists(mountPoint, logger)
cmd := exec.Command("rclone", "rc", "mount/mount", "fs="+backend+":", "mountPoint="+mountPoint)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err == nil {
logger.Info().Str("backend", backend).Str("mountPoint", mountPoint).
Msg("Mount successful.")
return
}
logger.Warn().Err(err).Msgf("Mount failed. Retrying %d/3...", retries+1)
retries++
time.Sleep(5 * time.Second)
}
logger.Error().Str("backend", backend).Msg("Failed to mount after 3 attempts.")
}

func StopAllMountsViaRCD(logger zerolog.Logger) {
logger.Info().Msg("Unmounting all rclone mounts")
cmd := exec.Command("rclone", "rc", "mount/umountall")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

err := cmd.Start()
if err != nil {
logger.Error().AnErr("error", err).Msg("Failed to unmount all rclone mounts")
return
}

logger.Info().Msg("Unmounted all rclone mounts successfully.")
}

func UnmountAllByPath(conf *config.Config, logger zerolog.Logger) {
logger.Info().Msg("Unmounting all paths listed in config...")

for _, mount := range conf.Mounts {
logger.Info().Str("path", mount.MountPoint).Msg("Unmounting...")
cmd := exec.Command("fusermount", "-u", mount.MountPoint)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
logger.Warn().Err(err).Str("path", mount.MountPoint).Msg("Failed to unmount path. It may not be mounted.")
} else {
logger.Info().Str("path", mount.MountPoint).Msg("Unmounted successfully.")
}
}
}

func ReloadMounts(conf *config.Config, logger zerolog.Logger) {
UnmountAllByPath(conf, logger)
logger.Info().Msg("Reloading all mounts from config...")

for _, mount := range conf.Mounts {
StartMountWithRetries(mount.BackendName, mount.MountPoint, logger)
}
}

func ensureMountPointExists(mountPoint string, logger zerolog.Logger) {
if _, err := os.Stat(mountPoint); os.IsNotExist(err) {
logger.Info().Str("mountPoint", mountPoint).Msg("Creating mount point...")
err := os.MkdirAll(mountPoint, 0777)
if err != nil {
logger.Error().Err(err).Str("mountPoint", mountPoint).Msg("Failed to create mount point")
} else {
logger.Info().Str("mountPoint", mountPoint).Msg("Mount point created successfully.")
}
}
}
54 changes: 54 additions & 0 deletions src/internal/rclone_manager/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package rclone_manager

import (
"github.com/rs/zerolog"
"rclone-manager/internal/config"
"rclone-manager/internal/mount_manager"
"rclone-manager/internal/utils"
"time"
)

var (
shouldMonitorProcesses bool
)

func MonitorRCDProcess(conf *config.Config, logger zerolog.Logger) {
defer func() {
if r := recover(); r != nil {
logger.Error().Msgf("MonitorRCD crashed: %v", r)
}
}()

logger.Info().Msg("Starting rclone serve process monitor...")
shouldMonitorProcesses = true

for {
if !shouldMonitorProcesses {
logger.Info().Msg("Stopping rclone serve process monitor")
break
}
processMap.Range(func(key, value interface{}) bool {
rCloneProcess := value.(*RCloneProcess)

if time.Since(rCloneProcess.StartedAt) < rCloneProcess.GracePeriod {
logger.Debug().Int("pid", rCloneProcess.PID).Msg("Skipping process check (within grace period)")
return true
}

if !utils.ProcessIsRunning(rCloneProcess.PID) {
logger.Warn().Msgf("Process (PID: %d) died. Restarting...", rCloneProcess.PID)
mount_manager.UnmountAllByPath(conf, logger)
newProcess := StartRcloneRemoteDaemon(logger)

if newProcess != nil {
processMap.Store(key, newProcess)
logger.Info().Msgf("Successfully restarted rclone in RCD mode with new PID: %d", newProcess.PID)
} else {
logger.Error().Msg("Failed to restart rclone RCD process")
}
}
return true
})
time.Sleep(10 * time.Second)
}
}
Loading

0 comments on commit 74065bc

Please sign in to comment.