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