Skip to content

Commit

Permalink
11 support netconf plugin (#12)
Browse files Browse the repository at this point in the history
* add netconf support

* fix doc descriptions

* add netconf support

* fix issue

* move all docker calls in one new package

* fix image name

* change version display on main page

* change version before merging

---------

Co-authored-by: roysoldier <[email protected]>
  • Loading branch information
door7302 and roysoldier authored Jul 15, 2024
1 parent e6ac39a commit 3ea323a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 92 deletions.
48 changes: 15 additions & 33 deletions association/stackconfig.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package association

import (
"context"
"io"
"jtso/config"
"jtso/container"
"jtso/kapacitor"
"jtso/logger"
"jtso/sqlite"
Expand All @@ -13,9 +13,6 @@ import (
"strings"
"text/template"
"unicode"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)

const PATH_VMX string = "/var/shared/telegraf/vmx/telegraf.d/"
Expand Down Expand Up @@ -210,8 +207,10 @@ func ConfigueStack(cfg *config.ConfigContainer, family string) error {
for filename, v := range perVersion {

rendRtrs := make([]string, 0)
rendRtrsNet := make([]string, 0)
for _, r := range v {
rendRtrs = append(rendRtrs, r.Hostname+":"+strconv.Itoa(cfg.Gnmi.Port))
rendRtrsNet = append(rendRtrsNet, r.Hostname)

}
// render profile
Expand All @@ -232,7 +231,15 @@ func ConfigueStack(cfg *config.ConfigContainer, family string) error {
continue
}
defer renderFile.Close()
err = temp.Execute(renderFile, map[string]interface{}{"rtrs": rendRtrs, "username": sqlite.ActiveCred.GnmiUser, "password": sqlite.ActiveCred.GnmiPwd, "tls": tls, "skip": skip, "tls_client": clienttls})
err = temp.Execute(renderFile, map[string]interface{}{"rtrs": rendRtrs,
"username": sqlite.ActiveCred.GnmiUser,
"password": sqlite.ActiveCred.GnmiPwd,
"tls": tls,
"skip": skip,
"tls_client": clienttls,
"usernetconf": sqlite.ActiveCred.NetconfUser,
"pwdnetconf": sqlite.ActiveCred.NetconfPwd,
"rtrs_netconf": rendRtrsNet})
if err != nil {
logger.Log.Errorf("Unable to write into render telegraf file - err: %v", err)
continue
Expand Down Expand Up @@ -349,23 +356,8 @@ func ConfigueStack(cfg *config.ConfigContainer, family string) error {
// Enable active scripts
kapacitor.StartTick(kapaStart)

// restart Containers :
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logger.Log.Errorf("Unable to open Docker session: %v", err)
return err
}
defer cli.Close()

timeout := 10

// Restart grafana
err = cli.ContainerRestart(context.Background(), "grafana", container.StopOptions{Signal: "SIGTERM", Timeout: &timeout})
if err != nil {
logger.Log.Errorf("Unable to restart Grafana container: %v", err)
return err
}
logger.Log.Info("Grafana container has been restarted")
container.RestartContainer("grafana")

// Restart telegraf instance(s)
for _, f := range families {
Expand All @@ -376,19 +368,9 @@ func ConfigueStack(cfg *config.ConfigContainer, family string) error {

// if cntr == 0 prefer shutdown the telegraf container
if cntr == 0 {
err = cli.ContainerStop(context.Background(), "telegraf_"+f, container.StopOptions{Signal: "SIGTERM", Timeout: &timeout})
if err != nil {
logger.Log.Errorf("Unable to stop telegraf_"+f+" container: %v", err)
continue
}
logger.Log.Info("telegraf_" + f + " container has been stopped - no more router attached")
container.StopContainer("telegraf_" + f)
} else {
err = cli.ContainerRestart(context.Background(), "telegraf_"+f, container.StopOptions{Signal: "SIGTERM", Timeout: &timeout})
if err != nil {
logger.Log.Errorf("Unable to restart telegraf_"+f+" container: %v", err)
continue
}
logger.Log.Info("telegraf_" + f + " container has been restarted")
container.RestartContainer("telegraf_" + f)
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/viper"
)

const JTSO_VERSION string = "1.0.2"
const JTSO_VERSION string = "1.0.3"

type PortalConfig struct {
Https bool
Expand Down
104 changes: 104 additions & 0 deletions container/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package container

import (
"context"
"jtso/logger"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)

func ListContainers() []types.Container {
// Open Docker API
var containers []types.Container
containers = make([]types.Container, 0)

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logger.Log.Errorf("Unable to open Docker session: %v", err)
}
defer cli.Close()

containers, err = cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
logger.Log.Errorf("Unable to list container state: %v", err)
}
logger.Log.Info(" List of containers has been retrieved")
return containers
}

func RestartContainer(name string) {
timeout := 30

// Open Docker API
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logger.Log.Errorf("Unable to open Docker session: %v", err)
return
}
defer cli.Close()

// Restart container
err = cli.ContainerRestart(context.Background(), name, container.StopOptions{Signal: "SIGTERM", Timeout: &timeout})
if err != nil {
logger.Log.Errorf("Unable to restart %s container: %v", name, err)
return
}
logger.Log.Infof("%s container has been restarted", name)

}

func StopContainer(name string) {
timeout := 30

// Open Docker API
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logger.Log.Errorf("Unable to open Docker session: %v", err)
return
}
defer cli.Close()

err = cli.ContainerStop(context.Background(), name, container.StopOptions{Signal: "SIGTERM", Timeout: &timeout})
if err != nil {
logger.Log.Errorf("Unable to stop %s container: %v", name, err)
return
}
logger.Log.Infof("%s container has been stopped - no more router attached", name)

}

func GetVersionLabel(names []string) string {

// Open Docker API
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logger.Log.Errorf("Unable to open Docker session: %v", err)
return "N/A"
}
defer cli.Close()

version := ""
for _, name := range names {
// Get the image details using the Docker API
imageInspect, _, err := cli.ImageInspectWithRaw(context.Background(), "compose-"+name)
if err != nil {
logger.Log.Errorf("Unable to retrieve Docker %s inspect data: %v", "compose-"+name, err)
version += name + "(N/A) "
continue
}

// Extract the version label from imageInspect.Config.Labels
vers, ok := imageInspect.Config.Labels["version"]
if !ok {
logger.Log.Errorf("Unable to retrieve Docker %s version", name)
version += name + "(N/A) "
continue
}
version += name + "(" + vers + ") "
logger.Log.Infof("%s container version is %s", name, version)
}
return version

}
2 changes: 1 addition & 1 deletion html/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ <h4 class="card-title">Stack Overview</h4>
</svg>
</br>
</div>
<i><p style="font-size: 0.8rem;">JTS Version - {{.JTS_VERS}} - JTSO Version {{.JTSO_VERS}} - JTS Telegraf {{.JTS_TELE_VERS}}</p></i>
<i><p style="font-size: 0.8rem;">OpenJTS Version: {{.JTS_VERS}} - JTSO Version: {{.JTSO_VERS}} - JTS Telegraf(s): {{.JTS_TELE_VERS}}</p></i>
</div>
</div>
<script src="js/alertify.min.js"></script>
Expand Down
66 changes: 9 additions & 57 deletions portal/webapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package portal

import (
"bufio"
"context"
"encoding/json"
"fmt"
"html/template"
"io"
"jtso/association"
"jtso/config"
"jtso/container"
"jtso/influx"
"jtso/logger"
"jtso/netconf"
Expand All @@ -23,14 +23,10 @@ import (

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)

const PATH_CERT string = "/var/cert/"
const PATH_JTS_VERS string = "/etc/jtso/openjts.version"
const PATH_TELE_VERS string = "/var/metadata/telegraf.version"

type WebApp struct {
listen string
Expand Down Expand Up @@ -119,17 +115,8 @@ func routeIndex(c echo.Context) error {
teleVmx, teleMx, telePtx, teleAcx, influx, grafana, kapacitor, jtso := "f8cecc", "f8cecc", "f8cecc", "f8cecc", "f8cecc", "f8cecc", "f8cecc", "f8cecc"
// check containers state

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logger.Log.Errorf("Unable to open Docker session: %v", err)
containers := container.ListContainers()

}
defer cli.Close()
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
logger.Log.Errorf("Unable to list container state: %v", err)

}
for _, container := range containers {
switch container.Names[0] {
case "/telegraf_vmx":
Expand Down Expand Up @@ -193,7 +180,6 @@ func routeIndex(c echo.Context) error {
// Retrieve module's version
jtsoVersion := config.JTSO_VERSION
jtsVersion := "N/A"
teleVersion := "N/A"

// Open the OpenJTS version's file
file_jts, err := os.Open(PATH_JTS_VERS)
Expand All @@ -211,21 +197,8 @@ func routeIndex(c echo.Context) error {
}
}

// Open the Telegraf version's file
file_tele, err := os.Open(PATH_TELE_VERS)
if err != nil {
logger.Log.Errorf("Unable to open %s file: %v", PATH_TELE_VERS, err)
} else {
defer file_tele.Close()
scanner := bufio.NewScanner(file_tele)
if scanner.Scan() {
teleVersion = scanner.Text()
}
// Check for any errors during scanning
if err := scanner.Err(); err != nil {
logger.Log.Errorf("Unable to parse %s file: %v", PATH_TELE_VERS, err)
}
}
// get the Telegraf version -
teleVersion := container.GetVersionLabel([]string{"telegraf_vmx", "telegraf_mx", "telegraf_ptx", "telegraf_acx"})

return c.Render(http.StatusOK, "index.html", map[string]interface{}{"TeleVmx": teleVmx, "TeleMx": teleMx, "TelePtx": telePtx, "TeleAcx": teleAcx,
"Grafana": grafana, "Kapacitor": kapacitor, "Influx": influx, "Jtso": jtso, "NumVMX": numVMX, "NumMX": numMX, "NumPTX": numPTX, "NumACX": numACX,
Expand Down Expand Up @@ -683,41 +656,20 @@ func routeUptDoc(c echo.Context) error {

tele := ""
for _, v := range p.Definition.TelCfg.VmxCfg {
if v.Version == "all" {
tele += "For VMX version " + v.Version + ": " + v.Config + "</br>"
} else {
tele += "For VMX version <=" + v.Version + ": " + v.Config + "</br>"
}
tele += "For VMX version " + v.Version + ": " + v.Config + "</br>"
}
for _, v := range p.Definition.TelCfg.MxCfg {
if v.Version == "all" {
tele += "For MX version " + v.Version + ": " + v.Config + "</br>"
} else {
tele += "For MX version <=" + v.Version + ": " + v.Config + "</br>"
}
tele += "For MX version " + v.Version + ": " + v.Config + "</br>"
}
for _, v := range p.Definition.TelCfg.PtxCfg {
if v.Version == "all" {
tele += "For PTX version " + v.Version + ": " + v.Config + "</br>"
} else {
tele += "For PTX version <=" + v.Version + ": " + v.Config + "</br>"
}
tele += "For PTX version " + v.Version + ": " + v.Config + "</br>"
}
for i, v := range p.Definition.TelCfg.AcxCfg {
if i == len(p.Definition.TelCfg.AcxCfg)-1 {
if v.Version == "all" {
tele += "For ACX version " + v.Version + ": " + v.Config
} else {
tele += "For ACX version <=" + v.Version + ": " + v.Config + "</br>"
}
tele += "For ACX version " + v.Version + ": " + v.Config
} else {
if v.Version == "all" {
tele += "For ACX version " + v.Version + ": " + v.Config + "</br>"
} else {
tele += "For ACX version <=" + v.Version + ": " + v.Config + "</br>"
}
tele += "For ACX version " + v.Version + ": " + v.Config + "</br>"
}

}
if tele == "" {
tele = "No Telegraf configuration attached to this profile"
Expand Down

0 comments on commit 3ea323a

Please sign in to comment.