X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fapiv1%2Fagent.go;h=925f12ba320b2926bbfeed1a254dc4c933d5f52e;hb=347bd1674bbf67ccb6209951a4bf8f2971715532;hp=74345450a5de8d386d46465ecbdb09d55a6dc9b6;hpb=61ca475685c6b7b33654edaad637c7d53bdf8d34;p=src%2Fxds%2Fxds-server.git diff --git a/lib/apiv1/agent.go b/lib/apiv1/agent.go index 7434545..925f12b 100644 --- a/lib/apiv1/agent.go +++ b/lib/apiv1/agent.go @@ -2,36 +2,69 @@ package apiv1 import ( "net/http" + "path" + "strings" "path/filepath" "github.com/gin-gonic/gin" + common "github.com/iotbzh/xds-common/golib" ) +// XDSAgentTarball . type XDSAgentTarball struct { - OS string `json:"os"` - FileURL string `json:"fileUrl"` + OS string `json:"os"` + Arch string `json:"arch"` + Version string `json:"version"` + RawVersion string `json:"raw-version"` + FileURL string `json:"fileUrl"` } + +// XDSAgentInfo . type XDSAgentInfo struct { Tarballs []XDSAgentTarball `json:"tarballs"` } // getXdsAgentInfo : return various information about Xds Agent func (s *APIService) getXdsAgentInfo(c *gin.Context) { - // TODO: retrieve link dynamically by reading assets/xds-agent-tarballs - tarballDir := "assets/xds-agent-tarballs" - response := XDSAgentInfo{ - Tarballs: []XDSAgentTarball{ - XDSAgentTarball{ - OS: "linux", - FileURL: filepath.Join(tarballDir, "xds-agent_linux-amd64-v0.0.1_3cdf92c.zip"), - }, - XDSAgentTarball{ - OS: "windows", - FileURL: filepath.Join(tarballDir, "xds-agent_windows-386-v0.0.1_3cdf92c.zip"), - }, - }, + + res := XDSAgentInfo{} + tarballURL := "assets/xds-agent-tarballs" + tarballDir := filepath.Join(s.cfg.FileConf.WebAppDir, "assets", "xds-agent-tarballs") + if common.Exists(tarballDir) { + files, err := filepath.Glob(path.Join(tarballDir, "xds-agent_*.zip")) + if err != nil { + s.log.Debugf("Error while retrieving xds-agent tarballs: dir=%s, error=%v", tarballDir, err) + } + for _, ff := range files { + file := filepath.Base(ff) + // Assume that tarball name format is: xds-agent_OS-ARCH-RAWVERSION.zip + fs := strings.TrimSuffix(strings.TrimPrefix(file, "xds-agent_"), ".zip") + f := strings.Split(fs, "-") + + if len(f) >= 3 { + vers := strings.Split(f[2], "_") + ver := f[2] + if len(vers) > 1 { + ver = vers[0] + } + + newT := XDSAgentTarball{ + OS: f[0], + Arch: f[1], + Version: ver, + RawVersion: f[2], + FileURL: filepath.Join(tarballURL, file), + } + + s.log.Infof("Added XDS-Agent tarball: %s", file) + res.Tarballs = append(res.Tarballs, newT) + + } else { + s.log.Debugf("Error while retrieving xds-agent, decoding failure: file:%v", ff) + } + } } - c.JSON(http.StatusOK, response) + c.JSON(http.StatusOK, res) }