switch from list (slice) to dict (map)

This commit is contained in:
schulze 2023-04-12 15:31:11 +02:00
parent c6465ec429
commit 03eff229c6
3 changed files with 22 additions and 7 deletions

View File

@ -17,9 +17,14 @@ func main() {
ConfigPath = "config.yaml" ConfigPath = "config.yaml"
config := state.LoadConf(ConfigPath) // Initialize the config.Services map
var config state.Config
config.MTD.Services = make(map[state.CustomUUID]state.Service)
config = state.LoadConf(ConfigPath)
state.SaveConf(ConfigPath, config) state.SaveConf(ConfigPath, config)
config = indexAllInstances(config) config = indexAllInstances(config)
//TODO: figure out migration (MTD) //TODO: figure out migration (MTD)

View File

@ -11,7 +11,17 @@ import (
// AWSMoveInstance moves a specified instance to a new availability region // AWSMoveInstance moves a specified instance to a new availability region
func AWSMoveInstance(config state.Config) (state.Config) { func AWSMoveInstance(config state.Config) (state.Config) {
instance := config.MTD.Services[0] // for testing use first instance // pseudorandom instance from all services for testing
var serviceUUID state.CustomUUID
var instance state.Service
for key, service := range config.MTD.Services {
serviceUUID = key
instance = service
break
}
fmt.Println("MTD move service:\t", serviceUUID)
region, instanceID := DecodeCloudID(instance.CloudID) region, instanceID := DecodeCloudID(instance.CloudID)
awsConfig := NewConfig(region, config.AWS.CredentialsPath) awsConfig := NewConfig(region, config.AWS.CredentialsPath)
svc := ec2.NewFromConfig(awsConfig) svc := ec2.NewFromConfig(awsConfig)
@ -27,14 +37,14 @@ func AWSMoveInstance(config state.Config) (state.Config) {
fmt.Println("Error creating image:\t", err) fmt.Println("Error creating image:\t", err)
return config return config
} }
fmt.Println("Created image:\t", imageName) fmt.Println("Created image:\t\t", imageName)
err = waitForImageReady(svc, imageName, 5*time.Minute) err = waitForImageReady(svc, imageName, 5*time.Minute)
if err != nil { if err != nil {
fmt.Println("Error waiting for image to be ready:\t", err) fmt.Println("Error waiting for image to be ready:\t", err)
return config return config
} }
fmt.Println("Image is ready:\t", imageName) fmt.Println("Image is ready:\t\t", imageName)
newInstanceID, err := launchInstance(svc, realInstance, imageName) newInstanceID, err := launchInstance(svc, realInstance, imageName)
if err != nil { if err != nil {
@ -48,7 +58,7 @@ func AWSMoveInstance(config state.Config) (state.Config) {
fmt.Println("Error terminating instance:\t", err) fmt.Println("Error terminating instance:\t", err)
return config return config
} }
fmt.Println("Terminated old instance:\t", instanceID) fmt.Println("Killed old instance:\t", instanceID)
image, err := describeImage(svc, imageName) image, err := describeImage(svc, imageName)
if err != nil { if err != nil {

View File

@ -17,12 +17,12 @@ type Config struct {
} }
type mtdconf struct { type mtdconf struct {
Services []Service `yaml:"services"` Services map[CustomUUID]Service `yaml:"services"`
} }
// Service contains all necessary information about a service to identify it in the cloud as well as configuring a proxy for it // Service contains all necessary information about a service to identify it in the cloud as well as configuring a proxy for it
type Service struct { type Service struct {
ID CustomUUID `yaml:"id"`
CloudID string `yaml:"cloud_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"`