forked from codetainerapp/codetainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
178 lines (144 loc) · 3.43 KB
/
db.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
package codetainer
import (
"errors"
"runtime"
docker "github.com/fsouza/go-dockerclient"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
)
var (
//
// Default command to start in a container
//
DefaultExecCommand string = "/bin/bash"
)
func findDockerImageInList(id string, dockerImages []docker.APIImages) *docker.APIImages {
for _, img := range dockerImages {
if img.ID == id {
return &img
}
for _, tag := range img.RepoTags {
if tag == id {
return &img
}
}
}
return nil
}
func lookupImageInDocker(id string) *docker.APIImages {
client, err := GlobalConfig.GetDockerClient()
if err != nil {
Log.Error("Unable to fetch docker client", err)
return nil
}
opts := docker.ListImagesOptions{}
imgs, err := client.ListImages(opts)
if err != nil {
Log.Error("Unable to fetch image", err)
return nil
}
return findDockerImageInList(id, imgs)
}
type Database struct {
engine *xorm.Engine
}
func CloseDb(db *Database) {
db.engine.Close()
}
func NewDatabase(dbPath string) (*Database, error) {
db := &Database{}
engine, err := xorm.NewEngine("sqlite3", dbPath)
engine.Logger.SetLevel(core.LOG_WARNING)
if err != nil {
return nil, err
}
engine.Sync(new(Codetainer), new(CodetainerImage), new(CodetainerConfig))
db.engine = engine
runtime.SetFinalizer(db, CloseDb)
return db, nil
}
func (db *Database) ListCodetainerImages() (*[]CodetainerImage, error) {
var containerImages []CodetainerImage = make([]CodetainerImage, 0)
var doneImages []CodetainerImage = make([]CodetainerImage, 0)
client, err := GlobalConfig.GetDockerClient()
if err != nil {
return nil, err
}
opts := docker.ListImagesOptions{}
dockerImages, err := client.ListImages(opts)
if err != nil {
return nil, err
}
err = db.engine.Find(&containerImages, &CodetainerImage{Enabled: true})
if err != nil {
return nil, err
}
// filter codetainer images by stuff in docker.
for _, img := range containerImages {
if findDockerImageInList(img.Id, dockerImages) != nil {
doneImages = append(doneImages, img)
}
}
return &doneImages, nil
}
//
// List codetainer images
//
func (db *Database) LookupCodetainerImage(id string) (*CodetainerImage, error) {
image := CodetainerImage{Id: id}
has, err := db.engine.Get(&image)
if err != nil {
return nil, err
}
if has && err == nil {
return &image, nil
}
// lookup by tag
imgs, err := db.ListCodetainerImages()
if err != nil {
return nil, err
}
for _, img := range *imgs {
for _, tag := range img.Tags {
if tag == id {
return &img, nil
}
}
}
return nil, errors.New("No image found: " + id)
}
//
// List all running codetainers
//
func (db *Database) ListCodetainers() (*[]Codetainer, error) {
var containers []Codetainer = make([]Codetainer, 0)
client, err := GlobalConfig.GetDockerClient()
if err != nil {
return nil, err
}
opts := docker.ListContainersOptions{}
dockers, err := client.ListContainers(opts)
if err != nil {
return nil, err
}
err = db.engine.Where("defunct = ?", 0).Find(&containers)
if err != nil {
return nil, err
}
// filter codetainer images by stuff in docker.
for i, img := range containers {
if findContainerInList(img.Id, dockers) != nil {
containers[i].Running = true
}
}
return &containers, nil
}
func findContainerInList(id string, containers []docker.APIContainers) *docker.APIContainers {
for _, c := range containers {
if c.ID == id {
return &c
}
}
return nil
}