/
search.go
198 lines (185 loc) · 5.05 KB
/
search.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"html/template"
"net/http"
"os"
"path/filepath"
"sync"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/search"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
log "github.com/sirupsen/logrus"
)
const (
containersIndexName = "containers"
imagesIndexName = "images"
)
func splitResultByTypes(results search.DocumentMatchCollection) (containers []string, images []string) {
for _, result := range results {
switch result.Index {
case containersIndexName:
containers = append(containers, result.ID)
case imagesIndexName:
images = append(images, result.ID)
default:
log.Error("Unknown index type")
}
}
return
}
func (s *Server) resolveContainers(containersID ...string) ([]types.Container, error) {
if len(containersID) == 0 {
return []types.Container{}, nil
}
containerListOptions := types.ContainerListOptions{All: true, Filters: filters.NewArgs()}
for _, id := range containersID {
containerListOptions.Filters.Add("id", id)
}
return s.docker.ContainerList(context.Background(), containerListOptions)
}
func (s *Server) resolveImages(imagesID ...string) ([]types.ImageInspect, error) {
if len(imagesID) == 0 {
return []types.ImageInspect{}, nil
}
images := make([]types.ImageInspect, len(imagesID))
for index, id := range imagesID {
image, _, err := s.docker.ImageInspectWithRaw(context.Background(), id)
if err == nil {
images[index] = image
}
}
return images, nil
}
func (s *Server) buildIndex() error {
cacheDir, err := os.UserCacheDir()
if err != nil {
return err
}
os.Mkdir(filepath.Join(cacheDir, applicationName), os.ModePerm)
containerIndexFilePath := filepath.Join(cacheDir, applicationName, "containers.index")
err = os.RemoveAll(containerIndexFilePath)
if err != nil {
log.Error("New index", err)
}
containersIndex, err := bleve.New(containerIndexFilePath, bleve.NewIndexMapping())
if err != nil {
log.Error("New index", err)
return err
}
containersIndex.SetName(containersIndexName)
containers, err := s.docker.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
log.Error("Search container list", err)
return err
}
for _, container := range containers {
err = containersIndex.Index(container.ID, container)
if err != nil {
log.Error("Container index error", err)
return err
}
}
imagesIndexFilePath := filepath.Join(cacheDir, applicationName, "images.index")
err = os.RemoveAll(imagesIndexFilePath)
if err != nil {
log.Error("New index", err)
}
imagesIndex, err := bleve.New(imagesIndexFilePath, bleve.NewIndexMapping())
if err != nil {
log.Error("New index", err)
return err
}
imagesIndex.SetName(imagesIndexName)
images, err := s.docker.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
log.Error("Search images list", err)
return err
}
for _, image := range images {
err = imagesIndex.Index(image.ID, image)
if err != nil {
log.Error("Images index error", err)
return err
}
}
s.index = bleve.NewIndexAlias(containersIndex, imagesIndex)
docCount, _ := s.index.DocCount()
log.Infof("%d containers indexed.", docCount)
return nil
}
func (s *Server) handleSearch() http.HandlerFunc {
go s.buildIndex()
var (
init sync.Once
tpl *template.Template
err error
)
type container struct {
ID string
Name string
}
type image struct {
ID string
Name string
}
type searchResponse struct {
Hits int
Containers []container
Images []image
}
return func(w http.ResponseWriter, r *http.Request) {
init.Do(func() {
tpl, err = s.parseTemplate("search.html")
})
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
q := r.URL.Query().Get("q")
if q != "" {
search := bleve.NewSearchRequest(bleve.NewMatchQuery(q))
searchResults, err := s.index.Search(search)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
containersID, imagesID := splitResultByTypes(searchResults.Hits)
containers, err := s.resolveContainers(containersID...)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
images, err := s.resolveImages(imagesID...)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
searchResponse := searchResponse{
Hits: int(searchResults.Total),
Containers: make([]container, len(containers)),
Images: make([]image, len(images)),
}
for index, c := range containers {
searchResponse.Containers[index] = container{ID: c.ID, Name: c.Names[0][1:]}
}
for index, img := range images {
searchResponse.Images[index] = image{ID: img.ID, Name: ""}
if len(img.RepoTags) > 0 {
searchResponse.Images[index].Name = img.RepoTags[0]
}
}
err = tpl.ExecuteTemplate(w, "search.html", searchResponse)
} else {
err = tpl.ExecuteTemplate(w, "search.html", nil)
}
if err != nil {
log.Error(err)
}
}
}