support for defualt configs, index aws instances
This commit is contained in:
parent
5d7c5b3d26
commit
81dbb99017
49
main.go
49
main.go
@ -2,20 +2,57 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"net/netip"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/thefeli73/polemos/mtdaws"
|
"github.com/thefeli73/polemos/mtdaws"
|
||||||
"github.com/thefeli73/polemos/state"
|
"github.com/thefeli73/polemos/state"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConfigPath is a string of the location for the configfile
|
||||||
|
var ConfigPath string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Starting Polemos")
|
fmt.Println("Starting Polemos")
|
||||||
|
|
||||||
config, err := state.LoadConf("config.yaml")
|
ConfigPath = "config.yaml"
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error loading config:", err)
|
config := state.LoadConf(ConfigPath)
|
||||||
os.Exit(1)
|
state.SaveConf(ConfigPath, config)
|
||||||
|
|
||||||
|
config = indexInstances(config)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mtdaws.IndexInstances(config)
|
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
|
||||||
|
}
|
||||||
|
config = indexInstance(config, cloudID, ip)
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) state.Config {
|
||||||
|
for _, service := range config.MTD.Services {
|
||||||
|
if service.CloudID == cloudID {
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
}
|
||||||
|
u := uuid.New()
|
||||||
|
newService := state.Service{
|
||||||
|
ID: state.CustomUUID(u),
|
||||||
|
CloudID: cloudID,
|
||||||
|
ServiceIP: serviceIP}
|
||||||
|
config.MTD.Services = append(config.MTD.Services, newService)
|
||||||
|
state.SaveConf(ConfigPath, config)
|
||||||
|
return config
|
||||||
}
|
}
|
@ -31,8 +31,13 @@ func NewConfig(region string, credentials string) aws.Config {
|
|||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndexInstances scans all configured regions for instances and add them to services
|
// GetCloudID returns a string to find the instance based on information from aws
|
||||||
func IndexInstances(config state.Config) []AwsInstance {
|
func GetCloudID(instance AwsInstance) string {
|
||||||
|
return "aws_" + instance.Region + "_" + instance.InstanceID
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInstances scans all configured regions for instances and add them to services
|
||||||
|
func GetInstances(config state.Config) []AwsInstance {
|
||||||
awsInstances := []AwsInstance{}
|
awsInstances := []AwsInstance{}
|
||||||
for _, region := range config.AWS.Regions {
|
for _, region := range config.AWS.Regions {
|
||||||
awsConfig := NewConfig(region, config.AWS.CredentialsPath)
|
awsConfig := NewConfig(region, config.AWS.CredentialsPath)
|
@ -1,8 +1,10 @@
|
|||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@ -15,47 +17,77 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type mtdconf struct {
|
type mtdconf struct {
|
||||||
Services []service `yaml:"services"`
|
Services []Service `yaml:"services"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
// Service contains all necessary information about a service to identify it in the cloud as well as configuring a proxy for it
|
||||||
ID customUUID `yaml:"id"`
|
type Service struct {
|
||||||
ServiceID string `yaml:"cloud_id"`
|
ID CustomUUID `yaml:"id"`
|
||||||
|
CloudID string `yaml:"cloud_id"`
|
||||||
EntryIP netip.Addr `yaml:"entry_ip"`
|
EntryIP netip.Addr `yaml:"entry_ip"`
|
||||||
EntryPort uint16 `yaml:"entry_port"`
|
EntryPort uint16 `yaml:"entry_port"`
|
||||||
ServiceIP netip.Addr `yaml:"service_ip"`
|
ServiceIP netip.Addr `yaml:"service_ip"`
|
||||||
ServicePort uint16 `yaml:"service_port"`
|
ServicePort uint16 `yaml:"service_port"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type customUUID uuid.UUID
|
// CustomUUID is an alias for uuid.UUID to enable custom unmarshal function
|
||||||
|
type CustomUUID uuid.UUID
|
||||||
|
|
||||||
type aws struct {
|
type aws struct {
|
||||||
Regions []string `yaml:"regions"`
|
Regions []string `yaml:"regions"`
|
||||||
CredentialsPath string `yaml:"credentials_path"`
|
CredentialsPath string `yaml:"credentials_path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *customUUID) UnmarshalYAML(value *yaml.Node) error {
|
// UnmarshalYAML parses uuid in yaml to CustomUUID type
|
||||||
|
func (u *CustomUUID) UnmarshalYAML(value *yaml.Node) error {
|
||||||
id, err := uuid.Parse(value.Value)
|
id, err := uuid.Parse(value.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*u = customUUID(id)
|
*u = CustomUUID(id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalYAML parses CustomUUID type to uuid string for yaml
|
||||||
|
func (u CustomUUID) MarshalYAML() (interface{}, error) {
|
||||||
|
return uuid.UUID(u).String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConf loads config from a yaml file
|
// LoadConf loads config from a yaml file
|
||||||
func LoadConf(filename string) (Config, error) {
|
func LoadConf(filename string) (Config) {
|
||||||
var config Config
|
var config Config
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(filename)
|
data, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
fmt.Println("Error reading file:", err)
|
||||||
|
|
||||||
|
fmt.Println("Attempting to load default config")
|
||||||
|
data, err = ioutil.ReadFile("config.default.yaml")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading file:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = yaml.Unmarshal([]byte(data), &config)
|
err = yaml.Unmarshal([]byte(data), &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config, err
|
fmt.Println("Error importing config:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println("Imported config succesfully!")
|
||||||
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
// SaveConf saves config to yaml file
|
||||||
|
func SaveConf(filename string, config Config) (error) {
|
||||||
|
yamlBytes, err := yaml.Marshal(&config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filename, yamlBytes, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user