summaryrefslogtreecommitdiffstats
path: root/lib/apiv1/agent.go
blob: 925f12ba320b2926bbfeed1a254dc4c933d5f52e (plain)
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

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Consta
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"`
	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) {

	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, res)
}