2023-03-28 16:09:14 +02:00
|
|
|
package mtdaws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-17 15:20:40 +02:00
|
|
|
"net/netip"
|
2023-04-11 12:56:07 +02:00
|
|
|
"time"
|
2023-03-28 16:09:14 +02:00
|
|
|
|
2023-04-12 10:16:22 +02:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
2023-03-28 16:09:14 +02:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ec2"
|
2023-04-17 15:54:42 +02:00
|
|
|
"github.com/google/uuid"
|
2023-03-28 16:09:14 +02:00
|
|
|
"github.com/thefeli73/polemos/state"
|
|
|
|
)
|
|
|
|
|
2023-04-17 15:20:40 +02:00
|
|
|
// AWSUpdateService updates a specified service config to match a newly moved instance
|
|
|
|
func AWSUpdateService(config state.Config, region string, service state.CustomUUID, newInstanceID string) (state.Config) {
|
|
|
|
awsConfig := NewConfig(region, config.AWS.CredentialsPath)
|
|
|
|
svc := ec2.NewFromConfig(awsConfig)
|
|
|
|
instance, err := getInstanceDetailsFromString(svc, newInstanceID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error getting instance details:\t", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
var publicAddr string
|
|
|
|
if instance.PublicIpAddress != nil {
|
|
|
|
publicAddr = aws.ToString(instance.PublicIpAddress)
|
|
|
|
}
|
|
|
|
formattedinstance := AwsInstance{
|
|
|
|
InstanceID: aws.ToString(instance.InstanceId),
|
|
|
|
Region: region,
|
|
|
|
PublicIP: publicAddr,
|
|
|
|
PrivateIP: aws.ToString(instance.PrivateIpAddress),
|
|
|
|
}
|
|
|
|
cloudid := GetCloudID(formattedinstance)
|
|
|
|
serviceip := netip.MustParseAddr(publicAddr)
|
|
|
|
config.MTD.Services[service] = state.Service{CloudID: cloudid, ServiceIP: serviceip}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2023-03-28 16:09:14 +02:00
|
|
|
// AWSMoveInstance moves a specified instance to a new availability region
|
|
|
|
func AWSMoveInstance(config state.Config) (state.Config) {
|
2023-04-17 15:54:42 +02:00
|
|
|
|
2023-04-12 15:31:11 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-04-17 15:54:42 +02:00
|
|
|
fmt.Println("MTD move service:\t", uuid.UUID.String(uuid.UUID(serviceUUID)))
|
2023-04-12 15:31:11 +02:00
|
|
|
|
2023-03-28 16:09:14 +02:00
|
|
|
region, instanceID := DecodeCloudID(instance.CloudID)
|
|
|
|
awsConfig := NewConfig(region, config.AWS.CredentialsPath)
|
|
|
|
svc := ec2.NewFromConfig(awsConfig)
|
|
|
|
|
|
|
|
realInstance, err := getInstanceDetailsFromString(svc, instanceID)
|
|
|
|
if err != nil {
|
2023-04-12 10:16:22 +02:00
|
|
|
fmt.Println("Error getting instance details:\t", err)
|
2023-03-28 16:09:14 +02:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
imageName, err := createImage(svc, instanceID)
|
|
|
|
if err != nil {
|
2023-04-12 10:16:22 +02:00
|
|
|
fmt.Println("Error creating image:\t", err)
|
2023-03-28 16:09:14 +02:00
|
|
|
return config
|
|
|
|
}
|
2023-04-12 15:31:11 +02:00
|
|
|
fmt.Println("Created image:\t\t", imageName)
|
2023-03-28 16:09:14 +02:00
|
|
|
|
2023-04-11 12:56:07 +02:00
|
|
|
err = waitForImageReady(svc, imageName, 5*time.Minute)
|
|
|
|
if err != nil {
|
2023-04-12 10:16:22 +02:00
|
|
|
fmt.Println("Error waiting for image to be ready:\t", err)
|
2023-04-11 12:56:07 +02:00
|
|
|
return config
|
|
|
|
}
|
2023-04-12 15:31:11 +02:00
|
|
|
fmt.Println("Image is ready:\t\t", imageName)
|
2023-04-11 12:56:07 +02:00
|
|
|
|
2023-04-17 15:54:42 +02:00
|
|
|
newInstanceID, err := launchInstance(svc, realInstance, imageName, region)
|
2023-03-28 16:09:14 +02:00
|
|
|
if err != nil {
|
2023-04-12 10:16:22 +02:00
|
|
|
fmt.Println("Error launching instance:\t", err)
|
2023-03-28 16:09:14 +02:00
|
|
|
return config
|
|
|
|
}
|
2023-04-12 10:16:22 +02:00
|
|
|
fmt.Println("Launched new instance:\t", newInstanceID)
|
2023-03-28 16:09:14 +02:00
|
|
|
|
|
|
|
err = terminateInstance(svc, instanceID)
|
|
|
|
if err != nil {
|
2023-04-12 10:16:22 +02:00
|
|
|
fmt.Println("Error terminating instance:\t", err)
|
2023-03-28 16:09:14 +02:00
|
|
|
return config
|
|
|
|
}
|
2023-04-12 15:31:11 +02:00
|
|
|
fmt.Println("Killed old instance:\t", instanceID)
|
2023-03-28 16:09:14 +02:00
|
|
|
|
2023-04-12 10:16:22 +02:00
|
|
|
image, err := describeImage(svc, imageName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error describing image:\t", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
err = deregisterImage(svc, imageName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error deregistering image:\t", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
fmt.Println("Deregistered image:\t", imageName)
|
|
|
|
|
|
|
|
if len(image.BlockDeviceMappings) > 0 {
|
|
|
|
snapshotID := aws.ToString(image.BlockDeviceMappings[0].Ebs.SnapshotId)
|
|
|
|
err = deleteSnapshot(svc, snapshotID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error deleting snapshot:\t", err)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
fmt.Println("Deleted snapshot:\t", snapshotID)
|
|
|
|
}
|
2023-03-28 16:09:14 +02:00
|
|
|
|
2023-04-17 15:20:40 +02:00
|
|
|
AWSUpdateService(config, region, serviceUUID, newInstanceID)
|
|
|
|
|
2023-03-28 16:09:14 +02:00
|
|
|
return config
|
|
|
|
}
|