Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca33a3db82 | ||
|
|
507af1dbff | ||
|
|
45bc9fe27c |
@@ -6,7 +6,7 @@
|
||||
# ComfyUI --listen 0.0.0.0 --port 8189).
|
||||
services:
|
||||
gpu-turnstile:
|
||||
image: git.rambossek.at/public/gpu-turnstile:v0.2.1
|
||||
image: git.rambossek.at/public/gpu-turnstile:v0.2.2
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
# Each consumer is enabled by setting its URL; leave one unset to
|
||||
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"gpu-turnstile/internal/supervise"
|
||||
)
|
||||
|
||||
// Name matches the Windows service name; the systemd unit is Name + ".service".
|
||||
@@ -66,7 +68,8 @@ func Run(run func(ctx context.Context) error) error {
|
||||
// BindPaths hole through ProtectHome/ProtectSystem: it reads its venv and
|
||||
// writes output/temp/user data under COMFY_DIR. A venv whose base
|
||||
// interpreter (pyvenv.cfg home) lives outside COMFY_DIR gets an additional
|
||||
// read-only bind.
|
||||
// read-only bind, and a Comfy-Desktop shared data dir (models, input,
|
||||
// output) a read-write one.
|
||||
func renderUnit(exePath, configPath, comfyDir string) string {
|
||||
bind := ""
|
||||
if comfyDir != "" {
|
||||
@@ -74,6 +77,9 @@ func renderUnit(exePath, configPath, comfyDir string) string {
|
||||
if home := comfyVenvHome(comfyDir); home != "" {
|
||||
bind += "BindReadOnlyPaths=" + home + "\n"
|
||||
}
|
||||
if shared := supervise.DesktopSharedDir(comfyDir); shared != "" {
|
||||
bind += "BindPaths=" + shared + "\n"
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf(`[Unit]
|
||||
Description=gpu-turnstile GPU arbitration proxy for Ollama and ComfyUI
|
||||
|
||||
@@ -24,6 +24,8 @@ import (
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/svc"
|
||||
"golang.org/x/sys/windows/svc/mgr"
|
||||
|
||||
"gpu-turnstile/internal/supervise"
|
||||
)
|
||||
|
||||
// Name is the Windows service name.
|
||||
@@ -382,6 +384,13 @@ func grantAll(exe, configPath string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Comfy-Desktop keeps models/input/output in a shared dir next
|
||||
// to the install; the managed instance writes output there.
|
||||
if shared := supervise.DesktopSharedDir(comfyDir); shared != "" {
|
||||
if err := grantAccessTree(shared, "(OI)(CI)(M)"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -43,7 +44,9 @@ func ComfyLayout(goos, dir string) (python, script string) {
|
||||
// DefaultComfyCommand builds the launch command for the standard venv
|
||||
// layout (see ComfyLayout): the script is passed relative to dir so dir
|
||||
// stays the working directory, and --port is taken from comfyURL when the
|
||||
// URL carries one.
|
||||
// URL carries one. On a Comfy-Desktop standalone install the shared data
|
||||
// directory (models, input, output) is added as --*-directory flags so the
|
||||
// managed instance sees the desktop app's models.
|
||||
func DefaultComfyCommand(goos, dir, comfyURL string) string {
|
||||
python, script := ComfyLayout(goos, dir)
|
||||
rel, err := filepath.Rel(dir, script)
|
||||
@@ -54,9 +57,34 @@ func DefaultComfyCommand(goos, dir, comfyURL string) string {
|
||||
if u, err := url.Parse(comfyURL); err == nil && u.Port() != "" {
|
||||
cmd += " --port " + u.Port()
|
||||
}
|
||||
if shared := DesktopSharedDir(dir); shared != "" {
|
||||
for _, sub := range []string{"models", "input", "output"} {
|
||||
p := filepath.Join(shared, sub)
|
||||
if st, err := os.Stat(p); err == nil && st.IsDir() {
|
||||
cmd += ` --` + sub + `-directory "` + p + `"`
|
||||
}
|
||||
}
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
// DesktopSharedDir returns the Comfy-Desktop shared data directory
|
||||
// (<root>/ComfyUI-Shared) when dir looks like a desktop standalone install
|
||||
// (<root>/ComfyUI-Installs/<name>/ComfyUI) and the shared models directory
|
||||
// exists; "" otherwise. The desktop app keeps models, input and output
|
||||
// there rather than inside the ComfyUI tree.
|
||||
func DesktopSharedDir(dir string) string {
|
||||
installs := filepath.Dir(filepath.Dir(dir))
|
||||
if filepath.Base(installs) != "ComfyUI-Installs" {
|
||||
return ""
|
||||
}
|
||||
shared := filepath.Join(filepath.Dir(installs), "ComfyUI-Shared")
|
||||
if st, err := os.Stat(filepath.Join(shared, "models")); err == nil && st.IsDir() {
|
||||
return shared
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Process is one managed child process.
|
||||
type Process struct {
|
||||
name string
|
||||
@@ -185,6 +213,9 @@ func (p *Process) EnsureRunning() error {
|
||||
p.external = false
|
||||
cmd := exec.Command(p.argv[0], p.argv[1:]...)
|
||||
cmd.Dir = p.dir
|
||||
// Ask the child not to colorize (ComfyUI ignores this and colors
|
||||
// anyway, so pipeLog also strips escape sequences).
|
||||
cmd.Env = append(os.Environ(), "NO_COLOR=1", "TERM=dumb")
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -278,7 +309,9 @@ func (p *Process) WatchIdle(ctx context.Context, idleTimeout time.Duration, gpuI
|
||||
}
|
||||
|
||||
// pipeLog forwards one child output stream to the log at INFO, line by
|
||||
// line, prefixed with the process name.
|
||||
// line, prefixed with the process name. ANSI escape sequences are
|
||||
// stripped: ComfyUI colorizes unconditionally, and the escapes only
|
||||
// render as garbage in a log file.
|
||||
func (p *Process) pipeLog(r io.Reader) {
|
||||
buf := make([]byte, 4096)
|
||||
var line string
|
||||
@@ -290,18 +323,25 @@ func (p *Process) pipeLog(r io.Reader) {
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
p.log.Info(p.name + ": " + strings.TrimRight(line[:i], "\r"))
|
||||
p.log.Info(p.name + ": " + stripANSI(strings.TrimRight(line[:i], "\r")))
|
||||
line = line[i+1:]
|
||||
}
|
||||
if err != nil {
|
||||
if strings.TrimSpace(line) != "" {
|
||||
p.log.Info(p.name + ": " + line)
|
||||
p.log.Info(p.name + ": " + stripANSI(line))
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ansiPattern matches CSI escape sequences (colors, cursor moves, …).
|
||||
var ansiPattern = regexp.MustCompile("\x1b\\[[0-9;?]*[a-zA-Z]")
|
||||
|
||||
func stripANSI(s string) string {
|
||||
return ansiPattern.ReplaceAllString(s, "")
|
||||
}
|
||||
|
||||
// stopTree kills cmd's process, including its children on Windows (python
|
||||
// launchers tend to spawn some). The Wait goroutine reaps it.
|
||||
func stopTree(cmd *exec.Cmd) {
|
||||
|
||||
@@ -239,3 +239,38 @@ func TestComfyLayoutAndDefaultCommand(t *testing.T) {
|
||||
t.Errorf("missing-layout script = %s", script)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripANSI(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"\x1b[32m[INFO]\x1b[0m Starting server": "[INFO] Starting server",
|
||||
"\x1b[1m\x1b[31m[ERROR]\x1b[0m boom": "[ERROR] boom",
|
||||
"plain line": "plain line",
|
||||
"\x1b[33mWARN\x1b[0m: \x1b[1mbold\x1b[0m": "WARN: bold",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := stripANSI(in); got != want {
|
||||
t.Errorf("stripANSI(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDesktopSharedDir(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
comfy := filepath.Join(root, "ComfyUI-Installs", "rtx5080", "ComfyUI")
|
||||
if err := os.MkdirAll(comfy, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := DesktopSharedDir(comfy); got != "" {
|
||||
t.Fatalf("no shared dir yet: got %q, want empty", got)
|
||||
}
|
||||
shared := filepath.Join(root, "ComfyUI-Shared")
|
||||
if err := os.MkdirAll(filepath.Join(shared, "models"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := DesktopSharedDir(comfy); got != shared {
|
||||
t.Fatalf("got %q, want %q", got, shared)
|
||||
}
|
||||
if got := DesktopSharedDir(filepath.Join(root, "plain", "ComfyUI")); got != "" {
|
||||
t.Fatalf("non-desktop layout: got %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user