This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanager.go
180 lines (154 loc) · 3.79 KB
/
manager.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"errors"
"strings"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/julienschmidt/sse"
"golang.org/x/net/context"
)
// Structs
type Instance struct {
sync.Mutex
Name string `json:"name"`
Args interface{} `json:"args"`
LastUpdate Update `json:"lastupdate"`
LastUpdateImages map[uint64]UpdateImage `json:"lastupdateimages"`
}
type Update struct {
Epoch uint64 `json:"epoch"`
TrainingLoss float64 `json:"training_loss"`
Stats interface{} `json:"stats"`
}
type UpdateImage struct {
ID uint64 `json:"id"`
Type string `json:"type"`
Url []byte `json:"url"`
}
type Event struct {
UUID string `json:"uuid"`
Data interface{} `json:"data"`
}
type DockerNewParams struct {
ShouldPull bool `json:"docker-shouldpull"`
Image string `json:"docker-image"`
Args string `json:"docker-args"`
}
// Manager
type Manager struct {
sync.Mutex
Streamer *sse.Streamer
instances map[string]*Instance
url string
}
func (m *Manager) Index() (map[string]*Instance, error) {
return m.instances, nil
}
func (m *Manager) New(uuid string, instance Instance) error {
_, ok := m.instances[uuid]
if ok {
return errors.New("stuff exists")
}
m.Lock()
m.instances[uuid] = &instance
m.instances[uuid].LastUpdateImages = make(map[uint64]UpdateImage)
m.Unlock()
m.Streamer.SendJSON(uuid, "New", Event{uuid, instance})
return nil
}
func (m *Manager) Get(uuid string) (*Instance, error) {
instance, ok := m.instances[uuid]
if !ok {
return nil, errors.New("stuff doesn't exist")
}
return instance, nil
}
func (m *Manager) Update(uuid string, update Update) error {
instance, ok := m.instances[uuid]
if !ok {
return errors.New("stuff doesn't exist")
}
instance.Lock()
instance.LastUpdate = update
instance.Unlock()
m.Streamer.SendJSON(uuid, "Update", Event{uuid, update})
return nil
}
func (m *Manager) UpdateImage(uuid string, updateImage UpdateImage) error {
instance, ok := m.instances[uuid]
if !ok {
return errors.New("stuff doesn't exist")
}
instance.Lock()
instance.LastUpdateImages[updateImage.ID] = updateImage
instance.Unlock()
m.Streamer.SendJSON(uuid, "UpdateImage", Event{uuid, updateImage})
return nil
}
func (m *Manager) Delete(uuid string) error {
_, ok := m.instances[uuid]
if !ok {
return errors.New("stuff doesn't exist")
}
m.Lock()
delete(m.instances, uuid)
m.Unlock()
m.Streamer.SendJSON(uuid, "Delete", Event{uuid, nil})
return nil
}
// Docker
func (m *Manager) DockerNew(params DockerNewParams) error {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
return err
}
if params.ShouldPull {
_, err := cli.ImagePull(ctx, params.Image, types.ImagePullOptions{})
if err != nil {
return err
}
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: params.Image,
Cmd: strings.Split(params.Args, " "),
Env: []string{"URL=" + m.url},
}, &container.HostConfig{
NetworkMode: "host",
}, nil, "")
if err != nil {
return err
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return err
}
return nil
}
func (m *Manager) DockerDelete(uuid string) error {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
return err
}
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return err
}
for _, c := range containers {
if c.Labels["uuid"] == uuid {
if err := cli.ContainerStop(ctx, c.ID, nil); err != nil {
return err
}
}
}
return nil
}
func NewManager(url string) *Manager {
return &Manager{
Streamer: sse.New(),
instances: make(map[string]*Instance),
url: url,
}
}