Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(exec): re-write exec handler #1

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/define/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package define
const (
FFMPEG = "ffmpeg"
FFPROBE = "ffprobe"
SHELL = "bash"
InstallFFMPEG = "install_ffmpeg"
StudioDir = ".oomol-studio"
FFReleaseURL = "https://github.com/oomol/builded/releases/download/v1.4/ffmpeg_macos_arm64_ventura.tar.xz"
Expand Down
16 changes: 2 additions & 14 deletions pkg/ffmpeg/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ffmpeg
import (
"context"
"github.com/gliderlabs/ssh"
"os/exec"
"sshd/pkg/handler"
)

type Runner struct {
Expand All @@ -14,17 +14,5 @@ type Runner struct {
}

func (r *Runner) Run(ctx context.Context) error {
cmd := exec.CommandContext(ctx, r.File, r.Args...)
cmd.Env = append(cmd.Env, r.Envs...)
cmd.Stdout = r.Session
cmd.Stderr = r.Session.Stderr()
if err := cmd.Start(); err != nil {
return err
}

if err := cmd.Wait(); err != nil {
return err
}

return nil
return handler.ExecHandler(r.Session, ctx, r.File, r.Envs, r.Args)
}
73 changes: 71 additions & 2 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package handler

import "context"
import (
"context"
"github.com/gliderlabs/ssh"
"github.com/sirupsen/logrus"
"io"
"os/exec"
"sshd/pkg/sio"
)

type Installer interface {
Download(ctx context.Context) error // download binaries assets
Expand All @@ -10,5 +17,67 @@ type Installer interface {
}

type Runner interface {
Runner(ctx context.Context) error // A function that responsible for ssh client command line
Run(ctx context.Context) error // A function that responsible for ssh client command line
}

func ExecHandler(s ssh.Session, ctx context.Context, targetBin string, envs, args []string) error {
logrus.Info("ExecCmd Middleware")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
// The context is canceled when the client's connection closes or I/ O operation fails.
<-s.Context().Done()
cancel()
}()

cmd := exec.CommandContext(ctx, targetBin, args...)
cmd.Env = append(cmd.Env, envs...)
logrus.Infof("Run %q with args %q with env %q", targetBin, args, envs)

stdOut, err := cmd.StdoutPipe()
if err != nil {
sio.Fatalf(s, "cmd.StdoutPipe() error: %s", err)
return err
}

stdErr, err := cmd.StderrPipe()
if err != nil {
sio.Fatalf(s, "cmd.StderrPipe() error: %s", err)
return err
}

stdIn, err := cmd.StdinPipe()
if err != nil {
sio.Fatalf(s, "cmd.StdinPipe() error: %s", err)
return err
}

// Copy cmd stdout to ssh session
go func() {
_, _ = io.Copy(s, stdOut)
}()

// Copy cmd stderr to ssh session's stderr
go func() {
_, _ = io.Copy(s.Stderr(), stdErr)
}()

// Copy stdin from session to cmd stdin
go func() {
_, _ = io.Copy(stdIn, s)
}()

if err = cmd.Start(); err != nil {
sio.Fatalf(s, "cmd.Start() error: %v", err.Error())
return err
}

if err = cmd.Wait(); err != nil {
sio.Fatalf(s, "cmd.Wait() error: %v", err.Error())
return err
}

return nil

}
76 changes: 8 additions & 68 deletions pkg/sshd/sshd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"github.com/gliderlabs/ssh"
"github.com/sirupsen/logrus"
"io"
"os/exec"
"path/filepath"
"sshd/pkg/define"
"sshd/pkg/ffmpeg"
Expand Down Expand Up @@ -48,19 +46,18 @@ func RunFFMPEG(next ssh.Handler) ssh.Handler {
}
// ffmpeg or ffprobe
ffBin := filepath.Join(studioHome, "host-shared", "ffmpeg", "bin", s.Command()[0])

env := define.DYLD_LIBRARY_PATH + "=" + filepath.Join(studioHome, "host-shared", "ffmpeg", "lib")

ffmpegELF := ffmpeg.Runner{
File: ffBin, // ffmpeg
runner := ffmpeg.Runner{
File: ffBin, // ffmpeg/ffprobe is the target binary
Args: s.Command()[1:], // Pass the rest of the command to ffmpeg
Envs: []string{env},
Session: s,
}

logrus.Infof("Run ffmpeg: %q with args %q with env %q", ffmpegELF.File, ffmpegELF.Args, ffmpegELF.Envs)
if err = ffmpegELF.Run(ctx); err != nil {
sio.Fatalf(s, "Run ffmpeg error: %s\n", err.Error())
logrus.Infof("Run ffmpeg: %q with args %q with env %q", runner.File, runner.Args, runner.Envs)
if err = runner.Run(ctx); err != nil {
logrus.Errorf("Run cmd error: %s", err.Error())
}
} else {
next(s)
Expand Down Expand Up @@ -116,66 +113,9 @@ func InstallFFMPEG(next ssh.Handler) ssh.Handler {
}
}

func ExecCmd(next ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
logrus.Info("ExecCmd Middleware")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
// The context is canceled when the client's connection closes or I/ O operation fails.
<-s.Context().Done()
cancel()
}()

str := s.Command()
if len(str) == 0 {
logrus.Warn("Empty command")
_ = s.Exit(128)
return
}

logrus.Infof("Command: %q", str)
cmd := exec.CommandContext(ctx, str[0], str[1:]...)
stdOut, err := cmd.StdoutPipe()
if err != nil {
logrus.Errorf("cmd.StdoutPipe() error: %s", err)
_ = s.Exit(100)
return
}

stdErr, err := cmd.StderrPipe()
if err != nil {
logrus.Errorf("cmd.StderrPipe() error: %s", err)
_ = s.Exit(100)
return
}

err = cmd.Start()
if err != nil {
logrus.Errorf("cmd.Start() error: %s", err)
_ = s.Exit(127) //nolint:mnd
}

// Copy cmd stdout to ssh session
go func() {
_, _ = io.Copy(s, stdOut)
}()

// Copy cmd stderr to ssh session's stderr
go func() {
_, _ = io.Copy(s.Stderr(), stdErr)
}()

if err = cmd.Wait(); err != nil {
_ = s.Exit(cmd.ProcessState.ExitCode())
logrus.Errorf("cmd.Wait() error: %s", err)
} else {
logrus.Infof("Command %q finished", str)
}
next(s)
}
}
//func ExecCmd(next ssh.Handler) ssh.Handler {
// return handler.ExecHandler()
//}

func Sanitizers(next ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
Expand Down