polemos/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

2023-03-14 15:12:28 +01:00
package main
import (
"fmt"
"net/netip"
"github.com/google/uuid"
2023-03-21 13:34:52 +01:00
"github.com/thefeli73/polemos/mtdaws"
"github.com/thefeli73/polemos/state"
2023-03-14 15:12:28 +01:00
)
2023-03-21 13:34:52 +01:00
// ConfigPath is a string of the location for the configfile
var ConfigPath string
2023-03-14 15:12:28 +01:00
func main() {
fmt.Println("Starting Polemos")
ConfigPath = "config.yaml"
config := state.LoadConf(ConfigPath)
state.SaveConf(ConfigPath, config)
config = indexInstances(config)
2023-03-21 13:34:52 +01:00
}
func indexInstances(config state.Config) state.Config {
fmt.Println("Indexing instances")
//index AWS instances
awsInstances := mtdaws.GetInstances(config)
for _, instance := range awsInstances {
cloudID := mtdaws.GetCloudID(instance)
ip, err := netip.ParseAddr(instance.PublicIP)
if err != nil {
fmt.Println("Error converting ip:", err)
continue
}
2023-03-22 14:12:12 +01:00
newService, found := indexInstance(config, cloudID, ip)
if !found {
config.MTD.Services = append(config.MTD.Services, newService)
state.SaveConf(ConfigPath, config)
}
}
return config
}
2023-03-22 14:12:12 +01:00
func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) (state.Service, bool) {
found := false
for _, service := range config.MTD.Services {
if service.CloudID == cloudID {
2023-03-22 14:12:12 +01:00
found = true
}
}
u := uuid.New()
newService := state.Service{
ID: state.CustomUUID(u),
CloudID: cloudID,
ServiceIP: serviceIP}
2023-03-22 14:12:12 +01:00
return newService, found
2023-03-22 14:24:56 +01:00
}