2023-03-28 16:09:14 +02:00
|
|
|
package mtdaws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-11 12:56:07 +02:00
|
|
|
"time"
|
2023-03-28 16:09:14 +02:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ec2"
|
|
|
|
"github.com/thefeli73/polemos/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AWSMoveInstance moves a specified instance to a new availability region
|
|
|
|
func AWSMoveInstance(config state.Config) (state.Config) {
|
|
|
|
instance := config.MTD.Services[0] // for testing use first instance
|
|
|
|
region, instanceID := DecodeCloudID(instance.CloudID)
|
|
|
|
awsConfig := NewConfig(region, config.AWS.CredentialsPath)
|
|
|
|
svc := ec2.NewFromConfig(awsConfig)
|
|
|
|
|
|
|
|
realInstance, err := getInstanceDetailsFromString(svc, instanceID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error getting instance details:", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
imageName, err := createImage(svc, instanceID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating image:", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
fmt.Println("Created image: ", imageName)
|
|
|
|
|
2023-04-11 12:56:07 +02:00
|
|
|
err = waitForImageReady(svc, imageName, 5*time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error waiting for image to be ready:", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
fmt.Println("Image is ready:", imageName)
|
|
|
|
|
2023-03-28 16:09:14 +02:00
|
|
|
newInstanceID, err := launchInstance(svc, realInstance, imageName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error launching instance:", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
fmt.Println("Launched new instance:", newInstanceID)
|
|
|
|
|
|
|
|
err = terminateInstance(svc, instanceID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error terminating instance:", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
fmt.Println("Terminated original instance:", instanceID)
|
|
|
|
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|