Moved web Dashboard to xds-agent
[src/xds/xds-server.git] / lib / apiv1 / agent.go
1 package apiv1
2
3 import (
4         "net/http"
5         "path"
6         "strings"
7
8         "path/filepath"
9
10         "github.com/gin-gonic/gin"
11         common "github.com/iotbzh/xds-common/golib"
12 )
13
14 // XDSAgentTarball .
15 type XDSAgentTarball struct {
16         OS         string `json:"os"`
17         Arch       string `json:"arch"`
18         Version    string `json:"version"`
19         RawVersion string `json:"raw-version"`
20         FileURL    string `json:"fileUrl"`
21 }
22
23 // XDSAgentInfo .
24 type XDSAgentInfo struct {
25         Tarballs []XDSAgentTarball `json:"tarballs"`
26 }
27
28 // getXdsAgentInfo : return various information about Xds Agent
29 func (s *APIService) getXdsAgentInfo(c *gin.Context) {
30
31         res := XDSAgentInfo{}
32         tarballURL := "assets/xds-agent-tarballs"
33         tarballDir := filepath.Join(s.cfg.FileConf.WebAppDir, "assets", "xds-agent-tarballs")
34         if common.Exists(tarballDir) {
35                 files, err := filepath.Glob(path.Join(tarballDir, "xds-agent_*.zip"))
36                 if err != nil {
37                         s.log.Debugf("Error while retrieving xds-agent tarballs: dir=%s, error=%v", tarballDir, err)
38                 }
39                 for _, ff := range files {
40                         file := filepath.Base(ff)
41                         // Assume that tarball name format is: xds-agent_OS-ARCH-RAWVERSION.zip
42                         fs := strings.TrimSuffix(strings.TrimPrefix(file, "xds-agent_"), ".zip")
43                         f := strings.Split(fs, "-")
44
45                         if len(f) >= 3 {
46                                 vers := strings.Split(f[2], "_")
47                                 ver := f[2]
48                                 if len(vers) > 1 {
49                                         ver = vers[0]
50                                 }
51
52                                 newT := XDSAgentTarball{
53                                         OS:         f[0],
54                                         Arch:       f[1],
55                                         Version:    ver,
56                                         RawVersion: f[2],
57                                         FileURL:    filepath.Join(tarballURL, file),
58                                 }
59
60                                 s.log.Infof("Added XDS-Agent tarball: %s", file)
61                                 res.Tarballs = append(res.Tarballs, newT)
62
63                         } else {
64                                 s.log.Debugf("Error while retrieving xds-agent, decoding failure: file:%v", ff)
65                         }
66                 }
67         }
68
69         c.JSON(http.StatusOK, res)
70 }