2021-08-31 14:26:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-01 21:18:52 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-31 14:26:09 +00:00
|
|
|
"github.com/rosti-cz/server_lobby/server"
|
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getIdentification() (server.Discovery, error) {
|
|
|
|
discovery := server.Discovery{}
|
|
|
|
|
2021-09-01 21:18:52 +00:00
|
|
|
localLabels, err := loadLocalLabels()
|
2021-08-31 14:26:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return discovery, err
|
|
|
|
}
|
2021-09-01 21:18:52 +00:00
|
|
|
|
|
|
|
if len(config.HostName) == 0 {
|
|
|
|
info, err := host.Info()
|
|
|
|
if err != nil {
|
|
|
|
return discovery, err
|
|
|
|
}
|
|
|
|
discovery.Hostname = info.Hostname
|
|
|
|
} else {
|
|
|
|
discovery.Hostname = config.HostName
|
|
|
|
}
|
|
|
|
|
|
|
|
discovery.Labels = append(config.Labels, 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.
|
|
|
|
func loadLocalLabels() ([]string, error) {
|
|
|
|
labels := []string{}
|
|
|
|
|
|
|
|
if _, err := os.Stat(config.LabelsPath); !os.IsNotExist(err) {
|
|
|
|
files, err := ioutil.ReadDir(config.LabelsPath)
|
|
|
|
if err != nil {
|
|
|
|
return labels, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, filename := range files {
|
|
|
|
fullPath := path.Join(config.LabelsPath, filename.Name())
|
|
|
|
fp, err := os.OpenFile(fullPath, os.O_RDONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return labels, fmt.Errorf("open file error: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
defer fp.Close()
|
|
|
|
|
|
|
|
rd := bufio.NewReader(fp)
|
|
|
|
for {
|
|
|
|
line, err := rd.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return labels, fmt.Errorf("read file line error: %v", err)
|
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
if len(line) > 0 {
|
|
|
|
labels = append(labels, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return labels, nil
|
|
|
|
}
|