Skip to content

Commit

Permalink
Added RegisterRuntime hook
Browse files Browse the repository at this point in the history
  • Loading branch information
runabol committed Oct 1, 2023
1 parent 7290d75 commit 9c85f77
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions engine/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func RegisterMounter(runtime, name string, mounter runtime.Mounter) {
defaultEngine.RegisterMounter(runtime, name, mounter)
}

func RegisterRuntime(rt runtime.Runtime) {
defaultEngine.RegisterRuntime(rt)
}

func RegisterDatastoreProvider(name string, provider datastore.Provider) {
defaultEngine.RegisterDatastoreProvider(name, provider)
}
Expand Down
11 changes: 11 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Engine struct {
broker mq.Broker
ds datastore.Datastore
mounters map[string]*runtime.MultiMounter
runtime runtime.Runtime
coordinator *coordinator.Coordinator
worker *worker.Worker
dsProviders map[string]datastore.Provider
Expand Down Expand Up @@ -281,6 +282,16 @@ func (e *Engine) RegisterMounter(rt string, name string, mounter runtime.Mounter
mounters.RegisterMounter(name, mounter)
}

func (e *Engine) RegisterRuntime(rt runtime.Runtime) {
e.mu.Lock()
defer e.mu.Unlock()
e.mustState(StateIdle)
if e.runtime != nil {
panic("engine: RegisterRuntime called twice")
}
e.runtime = rt
}

func (e *Engine) RegisterDatastoreProvider(name string, provider datastore.Provider) {
e.mu.Lock()
defer e.mu.Unlock()
Expand Down
15 changes: 15 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/runabol/tork"
"github.com/runabol/tork/datastore"
"github.com/runabol/tork/input"
"github.com/runabol/tork/runtime/shell"

"github.com/runabol/tork/mq"
"github.com/runabol/tork/runtime"
Expand Down Expand Up @@ -224,6 +225,20 @@ func TestRegisterMounter(t *testing.T) {
assert.NoError(t, err)
}

func TestRegisterRuntime(t *testing.T) {
eng := New(Config{Mode: ModeStandalone})
assert.Equal(t, StateIdle, eng.state)

eng.RegisterRuntime(shell.NewShellRuntime(shell.Config{}))

err := eng.Start()
assert.NoError(t, err)
assert.Equal(t, StateRunning, eng.state)

err = eng.Terminate()
assert.NoError(t, err)
}

func TestRegisterDatastoreProvider(t *testing.T) {
eng := New(Config{Mode: ModeStandalone})
assert.Equal(t, StateIdle, eng.state)
Expand Down
3 changes: 3 additions & 0 deletions engine/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (e *Engine) initWorker() error {
}

func (e *Engine) initRuntime() (runtime.Runtime, error) {
if e.runtime != nil {
return e.runtime, nil
}
runtimeType := conf.StringDefault("runtime.type", runtime.Docker)
switch runtimeType {
case runtime.Docker:
Expand Down
6 changes: 6 additions & 0 deletions runtime/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func NewShellRuntime(cfg Config) *ShellRuntime {
if cfg.Rexec == nil {
cfg.Rexec = reexec.Command
}
if cfg.UID == "" {
cfg.UID = DEFAULT_UID
}
if cfg.GID == "" {
cfg.GID = DEFAULT_GID
}
return &ShellRuntime{
cmds: new(syncx.Map[string, *exec.Cmd]),
shell: cfg.CMD,
Expand Down

0 comments on commit 9c85f77

Please sign in to comment.