2017-08-30 01:12:10 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2017-08-31 13:16:25 +00:00
|
|
|
"path/filepath"
|
2017-08-30 01:12:10 +00:00
|
|
|
|
|
|
|
"github.com/go-zoo/bone"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerInfo stores information about containers
|
|
|
|
type ContainerInfo struct {
|
|
|
|
Pid int `json:"pid"`
|
|
|
|
Image string `json:"image"`
|
|
|
|
CreatedTime int64 `json:"created_time"`
|
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
Annotations map[string]string `json:"annotations"`
|
2017-08-31 13:16:25 +00:00
|
|
|
LogPath string `json:"log_path"`
|
|
|
|
Root string `json:"root"`
|
2017-09-01 18:50:18 +00:00
|
|
|
Sandbox string `json:"sandbox"`
|
2017-08-30 01:12:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 23:25:48 +00:00
|
|
|
// CrioInfo stores information about the crio daemon
|
|
|
|
type CrioInfo struct {
|
|
|
|
StorageDriver string `json:"storage_driver"`
|
|
|
|
StorageRoot string `json:"storage_root"`
|
|
|
|
}
|
|
|
|
|
2017-09-01 10:37:10 +00:00
|
|
|
// GetInfoMux returns the mux used to serve info requests
|
|
|
|
func (s *Server) GetInfoMux() *bone.Mux {
|
2017-08-30 01:12:10 +00:00
|
|
|
mux := bone.New()
|
|
|
|
|
2017-08-30 23:25:48 +00:00
|
|
|
mux.Get("/info", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
ci := CrioInfo{
|
|
|
|
StorageDriver: s.config.Config.Storage,
|
|
|
|
StorageRoot: s.config.Config.Root,
|
|
|
|
}
|
|
|
|
js, err := json.Marshal(ci)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
}))
|
|
|
|
|
2017-08-30 01:12:10 +00:00
|
|
|
mux.Get("/containers/:id", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
containerID := bone.GetValue(req, "id")
|
|
|
|
ctr := s.GetContainer(containerID)
|
|
|
|
if ctr == nil {
|
2017-09-01 15:50:17 +00:00
|
|
|
ctr = s.getInfraContainer(containerID)
|
|
|
|
if ctr == nil {
|
|
|
|
http.Error(w, fmt.Sprintf("container with id: %s not found", containerID), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2017-08-30 01:12:10 +00:00
|
|
|
}
|
|
|
|
ctrState := ctr.State()
|
|
|
|
if ctrState == nil {
|
|
|
|
http.Error(w, fmt.Sprintf("container %s state is nil", containerID), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ci := ContainerInfo{
|
|
|
|
Pid: ctrState.Pid,
|
|
|
|
Image: ctr.Image(),
|
|
|
|
CreatedTime: ctrState.Created.UnixNano(),
|
|
|
|
Labels: ctr.Labels(),
|
|
|
|
Annotations: ctr.Annotations(),
|
2017-08-31 13:16:25 +00:00
|
|
|
Root: ctr.MountPoint(),
|
|
|
|
LogPath: filepath.Dir(ctr.LogPath()),
|
2017-09-01 18:50:18 +00:00
|
|
|
Sandbox: ctr.Sandbox(),
|
2017-08-30 01:12:10 +00:00
|
|
|
}
|
|
|
|
js, err := json.Marshal(ci)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
}))
|
|
|
|
|
2017-09-01 10:37:10 +00:00
|
|
|
return mux
|
2017-08-30 01:12:10 +00:00
|
|
|
}
|