feat: proxmox idlewatcher (#88)

* feat: idle sleep for proxmox LXCs

* refactor: replace deprecated docker api types

* chore(api): remove debug task list endpoint

* refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon

* refactor: introduce Pool interface, move agent_pool to agent module

* refactor: simplify api code

* feat: introduce debug api

* refactor: remove net.URL and net.CIDR types, improved unmarshal handling

* chore: update Makefile for debug build tag, update README

* chore: add gperr.Unwrap method

* feat: relative time and duration formatting

* chore: add ROOT_DIR environment variable, refactor

* migration: move homepage override and icon cache to $BASE_DIR/data, add migration code

* fix: nil dereference on marshalling service health

* fix: wait for route deletion

* chore: enhance tasks debuggability

* feat: stdout access logger and MultiWriter

* fix(agent): remove agent properly on verify error

* fix(metrics): disk exclusion logic and added corresponding tests

* chore: update schema and prettify, fix package.json and Makefile

* fix: I/O buffer not being shrunk before putting back to pool

* feat: enhanced error handling module

* chore: deps upgrade

* feat: better value formatting and handling

---------

Co-authored-by: yusing <yusing@6uo.me>
This commit is contained in:
Yuzerion
2025-04-16 14:52:33 +08:00
committed by GitHub
parent 88f3a95b61
commit 57292f0fe8
173 changed files with 4131 additions and 2096 deletions

View File

@@ -1,8 +1,11 @@
package route
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/yusing/go-proxy/agent/pkg/agent"
@@ -10,7 +13,9 @@ import (
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/homepage"
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
net "github.com/yusing/go-proxy/internal/net/types"
"github.com/yusing/go-proxy/internal/logging"
gpnet "github.com/yusing/go-proxy/internal/net"
"github.com/yusing/go-proxy/internal/proxmox"
"github.com/yusing/go-proxy/internal/task"
"github.com/yusing/go-proxy/internal/utils/strutils"
"github.com/yusing/go-proxy/internal/watcher/health"
@@ -43,6 +48,8 @@ type (
Homepage *homepage.ItemConfig `json:"homepage,omitempty"`
AccessLog *accesslog.Config `json:"access_log,omitempty"`
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
Metadata `deserialize:"-"`
}
@@ -52,16 +59,16 @@ type (
Provider string `json:"provider,omitempty"`
// private fields
LisURL *net.URL `json:"lurl,omitempty"`
ProxyURL *net.URL `json:"purl,omitempty"`
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
LisURL *url.URL `json:"lurl,omitempty"`
ProxyURL *url.URL `json:"purl,omitempty"`
impl route.Route
}
Routes map[string]*Route
)
const DefaultHost = "localhost"
func (r Routes) Contains(alias string) bool {
_, ok := r[alias]
return ok
@@ -81,6 +88,70 @@ func (r *Route) Validate() (err gperr.Error) {
}
}
if r.Idlewatcher != nil && r.Idlewatcher.Proxmox != nil {
node := r.Idlewatcher.Proxmox.Node
vmid := r.Idlewatcher.Proxmox.VMID
if node == "" {
return gperr.Errorf("node (proxmox node name) is required")
}
if vmid <= 0 {
return gperr.Errorf("vmid (lxc id) is required")
}
if r.Host == DefaultHost {
containerName := r.Idlewatcher.ContainerName()
// get ip addresses of the vmid
node, ok := proxmox.Nodes.Get(node)
if !ok {
return gperr.Errorf("proxmox node %s not found in pool", node)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ips, err := node.LXCGetIPs(ctx, vmid)
if err != nil {
return gperr.Errorf("failed to get ip addresses of vmid %d: %w", vmid, err)
}
if len(ips) == 0 {
return gperr.Multiline().
Addf("no ip addresses found for %s", containerName).
Adds("make sure you have set static ip address for container instead of dhcp").
Subject(containerName)
}
l := logging.With().Str("container", containerName).Logger()
l.Info().Msg("checking if container is running")
running, err := node.LXCIsRunning(ctx, vmid)
if err != nil {
return gperr.New("failed to check container state").With(err)
}
if !running {
l.Info().Msg("starting container")
if err := node.LXCAction(ctx, vmid, proxmox.LXCStart); err != nil {
return gperr.New("failed to start container").With(err)
}
}
l.Info().Msgf("finding reachable ip addresses")
for _, ip := range ips {
if ok, _ := gpnet.PingWithTCPFallback(ctx, ip, r.Port.Proxy); ok {
r.Host = ip.String()
l.Info().Msgf("using ip %s", r.Host)
break
}
}
if r.Host == DefaultHost {
return gperr.Multiline().
Addf("no reachable ip addresses found, tried %d IPs", len(ips)).
AddLines(ips).
Subject(containerName)
}
}
}
errs := gperr.NewBuilder("entry validation failed")
if r.Scheme == route.SchemeFileServer {
@@ -88,19 +159,19 @@ func (r *Route) Validate() (err gperr.Error) {
if err != nil {
errs.Add(err)
}
r.ProxyURL = gperr.Collect(errs, net.ParseURL, "file://"+r.Root)
r.ProxyURL = gperr.Collect(errs, url.Parse, "file://"+r.Root)
r.Host = ""
r.Port.Proxy = 0
} else {
switch r.Scheme {
case route.SchemeHTTP, route.SchemeHTTPS:
if r.Port.Listening != 0 {
errs.Addf("unexpected listening port for %s scheme", r.Scheme)
case route.SchemeHTTP, route.SchemeHTTPS:
if r.Port.Listening != 0 {
errs.Addf("unexpected listening port for %s scheme", r.Scheme)
}
case route.SchemeTCP, route.SchemeUDP:
r.LisURL = gperr.Collect(errs, url.Parse, fmt.Sprintf("%s://:%d", r.Scheme, r.Port.Listening))
}
case route.SchemeTCP, route.SchemeUDP:
r.LisURL = gperr.Collect(errs, net.ParseURL, fmt.Sprintf("%s://:%d", r.Scheme, r.Port.Listening))
}
r.ProxyURL = gperr.Collect(errs, net.ParseURL, fmt.Sprintf("%s://%s:%d", r.Scheme, r.Host, r.Port.Proxy))
r.ProxyURL = gperr.Collect(errs, url.Parse, fmt.Sprintf("%s://%s:%d", r.Scheme, r.Host, r.Port.Proxy))
}
if !r.UseHealthCheck() && (r.UseLoadBalance() || r.UseIdleWatcher()) {
@@ -146,8 +217,8 @@ func (r *Route) Started() bool {
}
func (r *Route) Reference() string {
if r.Docker != nil {
return r.Docker.Image.Name
if r.Container != nil {
return r.Container.Image.Name
}
return r.Alias
}
@@ -160,7 +231,7 @@ func (r *Route) TargetName() string {
return r.Alias
}
func (r *Route) TargetURL() *net.URL {
func (r *Route) TargetURL() *url.URL {
return r.ProxyURL
}
@@ -190,6 +261,10 @@ func (r *Route) HealthMonitor() health.HealthMonitor {
}
func (r *Route) IdlewatcherConfig() *idlewatcher.Config {
cont := r.Container
if cont != nil && cont.IdlewatcherConfig != nil {
return cont.IdlewatcherConfig
}
return r.Idlewatcher
}
@@ -255,7 +330,8 @@ func (r *Route) UseLoadBalance() bool {
}
func (r *Route) UseIdleWatcher() bool {
return r.Idlewatcher != nil && r.Idlewatcher.IdleTimeout > 0
cfg := r.IdlewatcherConfig()
return cfg != nil && cfg.IdleTimeout > 0
}
func (r *Route) UseHealthCheck() bool {
@@ -276,7 +352,7 @@ func (r *Route) Finalize() {
if r.Host == "" {
switch {
case !isDocker:
r.Host = "localhost"
r.Host = DefaultHost
case cont.PrivateHostname != "":
r.Host = cont.PrivateHostname
case cont.PublicHostname != "":
@@ -379,7 +455,7 @@ func (r *Route) FinalizeHomepageConfig() {
panic("alias is empty")
}
isDocker := r.Container != nil
isDocker := r.IsDocker()
if r.Homepage == nil {
r.Homepage = &homepage.ItemConfig{Show: true}