forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
84 lines (72 loc) · 1.76 KB
/
fake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package scheduler
import (
"fmt"
"io"
"sync"
"github.com/remind101/pkg/timex"
"golang.org/x/net/context"
)
type FakeScheduler struct {
sync.Mutex
apps map[string]*App
}
func NewFakeScheduler() *FakeScheduler {
return &FakeScheduler{
apps: make(map[string]*App),
}
}
func (m *FakeScheduler) Submit(ctx context.Context, app *App, ss StatusStream) error {
m.Lock()
defer m.Unlock()
m.apps[app.ID] = app
return nil
}
func (m *FakeScheduler) Restart(ctx context.Context, app *App, ss StatusStream) error {
return m.Submit(ctx, app, ss)
}
func (m *FakeScheduler) Scale(ctx context.Context, app string, ptype string, instances uint) error {
if a, ok := m.apps[app]; ok {
var process *Process
for _, p := range a.Processes {
if p.Type == ptype {
process = p
}
}
if process != nil {
process.Instances = instances
}
}
return nil
}
func (m *FakeScheduler) Remove(ctx context.Context, appID string) error {
delete(m.apps, appID)
return nil
}
func (m *FakeScheduler) Instances(ctx context.Context, appID string) ([]*Instance, error) {
var instances []*Instance
if a, ok := m.apps[appID]; ok {
for _, p := range a.Processes {
pp := *p
pp.Env = Env(a, p)
for i := uint(1); i <= p.Instances; i++ {
instances = append(instances, &Instance{
ID: fmt.Sprintf("%d", i),
Host: Host{ID: "i-aa111aa1"},
State: "running",
Process: &pp,
UpdatedAt: timex.Now(),
})
}
}
}
return instances, nil
}
func (m *FakeScheduler) Stop(ctx context.Context, instanceID string) error {
return nil
}
func (m *FakeScheduler) Run(ctx context.Context, app *App, p *Process, in io.Reader, out io.Writer) error {
if out != nil {
fmt.Fprintf(out, "Fake output for `%s` on %s\n", p.Command, app.Name)
}
return nil
}