2021-09-04 12:16:14 +00:00
|
|
|
package server
|
2021-08-31 14:26:09 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-01 21:18:52 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-31 14:26:09 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
|
|
|
)
|
|
|
|
|
2021-09-03 22:09:03 +00:00
|
|
|
// getIdentification assembles the discovery packet that contains hotname and set of labels describing a single server, in this case the local server.
|
2021-09-04 12:16:14 +00:00
|
|
|
// Parameter initialLabels usually coming from configuration of the app.
|
|
|
|
// If hostname is empty it will be discovered automatically.
|
|
|
|
func GetIdentification(hostname string, initialLabels Labels, labelsPath string) (Discovery, error) {
|
|
|
|
discovery := Discovery{}
|
2021-08-31 14:26:09 +00:00
|
|
|
|
2021-09-04 12:16:14 +00:00
|
|
|
localLabels, err := loadLocalLabels(initialLabels, labelsPath)
|
2021-08-31 14:26:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return discovery, err
|
|
|
|
}
|
2021-09-01 21:18:52 +00:00
|
|
|
|
2021-09-04 12:16:14 +00:00
|
|
|
if len(hostname) == 0 {
|
2021-09-01 21:18:52 +00:00
|
|
|
info, err := host.Info()
|
|
|
|
if err != nil {
|
|
|
|
return discovery, err
|
|
|
|
}
|
|
|
|
discovery.Hostname = info.Hostname
|
|
|
|
} else {
|
2021-09-04 12:16:14 +00:00
|
|
|
discovery.Hostname = hostname
|
2021-09-01 21:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 12:16:14 +00:00
|
|
|
discovery.Labels = append(initialLabels, localLabels...)
|
2021-08-31 14:26:09 +00:00
|
|
|
|
|
|
|
return discovery, nil
|
|
|
|
}
|
2021-09-01 21:18:52 +00:00
|
|
|
|
|
|
|
// loadLocalLabels scans local directory where labels are stored and adds them to the labels configured as environment variables.
|
|
|
|
// Filename in LabelsPath is not importent and each file can contain multiple labels, one per each line.
|
2021-09-04 12:16:14 +00:00
|
|
|
func loadLocalLabels(skipLabels Labels, labelsPath string) (Labels, error) {
|
|
|
|
labels := Labels{}
|
2021-09-03 22:09:03 +00:00
|
|
|
var found bool
|
2021-09-01 21:18:52 +00:00
|
|
|
|
2021-09-04 12:16:14 +00:00
|
|
|
if _, err := os.Stat(labelsPath); !os.IsNotExist(err) {
|
|
|
|
files, err := ioutil.ReadDir(labelsPath)
|
2021-09-01 21:18:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return labels, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, filename := range files {
|
2021-09-04 12:16:14 +00:00
|
|
|
fullPath := path.Join(labelsPath, filename.Name())
|
|
|
|
|
|
|
|
content, err := os.ReadFile(fullPath)
|
2021-09-01 21:18:52 +00:00
|
|
|
if err != nil {
|
2021-09-04 12:16:14 +00:00
|
|
|
return labels, fmt.Errorf("read file error: %v", err)
|
2021-09-01 21:18:52 +00:00
|
|
|
|
|
|
|
}
|
2021-09-04 12:16:14 +00:00
|
|
|
fmt.Println(string(content))
|
2021-09-01 21:18:52 +00:00
|
|
|
|
2021-09-04 12:16:14 +00:00
|
|
|
for _, line := range strings.Split(string(content), "\n") {
|
2021-09-01 21:18:52 +00:00
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if len(line) > 0 {
|
2021-09-03 22:09:03 +00:00
|
|
|
found = false
|
|
|
|
for _, skipLabel := range skipLabels {
|
2021-09-04 12:16:14 +00:00
|
|
|
if skipLabel == Label(line) {
|
2021-09-03 22:09:03 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2021-09-04 12:16:14 +00:00
|
|
|
labels = append(labels, Label(line))
|
2021-09-03 22:09:03 +00:00
|
|
|
}
|
2021-09-01 21:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-04 12:16:14 +00:00
|
|
|
fmt.Println("LABELS", labels)
|
2021-09-01 21:18:52 +00:00
|
|
|
return labels, nil
|
|
|
|
}
|