-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (100 loc) · 2.72 KB
/
main.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
package main
import (
"fmt"
"os"
"time"
"github.com/docker/docker/client"
"github.com/golang-collections/collections/queue"
"github.com/google/uuid"
"github.com/haveheartt/orchestrator-from-scratch/manager"
"github.com/haveheartt/orchestrator-from-scratch/node"
"github.com/haveheartt/orchestrator-from-scratch/task"
"github.com/haveheartt/orchestrator-from-scratch/worker"
)
func main() {
t := task.Task{
ID: uuid.New(),
Name: "task-1",
State: task.Pending,
Image: "image-1",
Memory: 1024,
Disk: 1,
}
te := task.TaskEvent{
ID: uuid.New(),
State: task.Pending,
Timestamp: time.Now(),
Task: t,
}
fmt.Printf("task: %v\n", t)
fmt.Printf("task event: %v\n", te)
w := worker.Worker{
Name: "worker-1",
Queue: *queue.New(),
Db: make(map[uuid.UUID]*task.Task),
}
fmt.Printf("worker: %v\n", w)
w.CollectStats()
w.RunTask()
w.StartTask()
w.StopTask()
m := manager.Manager{
Pending: *queue.New(),
TaskDb: make(map[string][]*task.Task),
EventDb: make(map[string][]*task.TaskEvent),
Workers: []string{w.Name},
}
fmt.Printf("manager: %v\n", m)
m.SelectWorker()
m.UpdateTasks()
m.SendWork()
n := node.Node{
Name: "node-1",
Ip: "192.168.1.1",
Cores: 4,
Memory: 1024,
Disk: 25,
Role: "worker",
}
fmt.Printf("node: %v\n", n)
fmt.Printf("create a test container\n")
dockerTask, createResult := createContainer()
if createResult.Error != nil {
fmt.Printf("%v", createResult.Error)
os.Exit(1)
}
time.Sleep(time.Second * 5)
fmt.Printf("stopping container %s\n", createResult.ContainerId)
_ = stopContainer(dockerTask, createResult.ContainerId)
}
func createContainer() (*task.Docker, *task.DockerResult) {
c := task.Config{
Name: "test-container-1",
Image: "postgres:13",
Env: []string{
"POSTGRES_USER=cube",
"POSTGRES_PASSWORD=secret",
},
}
dc, _ := client.NewClientWithOpts(client.FromEnv)
d := task.Docker{
Client: dc,
Config: c,
}
result := d.Run()
if result.Error != nil {
fmt.Printf("errooooou: %v\n", result.Error)
return nil, nil
}
fmt.Printf("Container %s is running with config %v\n", result.ContainerId, c)
return &d, &result
}
func stopContainer(d *task.Docker, id string) *task.DockerResult {
result := d.Stop(id)
if result.Error != nil {
fmt.Printf("%v\n", result.Error)
return nil
}
fmt.Printf("Container %s has been stopped and removed\n", result.ContainerId)
return &result
}