timers, counters, misc

This commit is contained in:
schulze 2023-04-19 11:39:11 +02:00
parent 5571b66d52
commit e8bb5506f5
3 changed files with 36 additions and 23 deletions

20
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"net/netip"
"time"
"github.com/google/uuid"
"github.com/thefeli73/polemos/mtdaws"
@ -38,6 +39,9 @@ func mtdLoop(config state.Config) {
config = movingTargetDefense(config)
state.SaveConf(ConfigPath, config)
fmt.Println("Sleeping for 5 seconds")
time.Sleep(5*time.Second)
//TODO: proxy commands
}
}
@ -50,6 +54,7 @@ func movingTargetDefense(config state.Config) state.Config{
func indexAllInstances(config state.Config) state.Config {
fmt.Println("Indexing instances")
t := time.Now()
for _, service := range config.MTD.Services {
service.Active = false
@ -57,7 +62,7 @@ func indexAllInstances(config state.Config) state.Config {
//index AWS instances
awsNewInstanceCounter := 0
awsRemovedInstanceCounter := 0
awsInactiveInstanceCounter := len(config.MTD.Services)
awsInstanceCounter := 0
awsInstances := mtdaws.GetInstances(config)
for _, instance := range awsInstances {
@ -69,11 +74,16 @@ func indexAllInstances(config state.Config) state.Config {
}
var found bool
config, found = indexInstance(config, cloudID, ip)
if !found {awsNewInstanceCounter++}
if !found {
awsNewInstanceCounter++
} else {
awsInactiveInstanceCounter--
}
awsInstanceCounter++
}
// TODO: Purge instances in config that are not found in the cloud
fmt.Printf("Found %d AWS instances (%d newly added, %d removed)\n", awsInstanceCounter, awsNewInstanceCounter, awsRemovedInstanceCounter)
fmt.Printf("Found %d active AWS instances (%d newly added, %d inactive) (took %s)\n",
awsInstanceCounter, awsNewInstanceCounter, awsInactiveInstanceCounter, time.Since(t).Round(100*time.Millisecond).String())
return config
@ -97,7 +107,9 @@ func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) (s
state.SaveConf(ConfigPath, config)
} else {
config.MTD.Services[foundUUID] = state.Service{Active: true}
s := config.MTD.Services[foundUUID]
s.Active = true
config.MTD.Services[foundUUID] = s
state.SaveConf(ConfigPath, config)
}
return config, found

View File

@ -53,6 +53,8 @@ func AWSMoveInstance(config state.Config) (state.Config) {
for key, service := range config.MTD.Services {
serviceUUID = key
instance = service
if !instance.AdminEnabled {continue}
if !instance.Active {continue}
break
}
@ -67,61 +69,63 @@ func AWSMoveInstance(config state.Config) (state.Config) {
fmt.Println("Error getting instance details:\t", err)
return config
}
if !instance.AdminEnabled {
fmt.Println("Error, Service is Disabled!")
return config
}
if !instance.Active {
fmt.Println("Error, Service is not active!")
return config
}
if !isInstanceRunning(realInstance) {
fmt.Println("Error, Instance is not running!")
return config
}
//Create image
t := time.Now()
imageName, err := createImage(svc, instanceID)
if err != nil {
fmt.Println("Error creating image:\t", err)
return config
}
fmt.Println("Created image:\t\t", imageName)
fmt.Printf("Created image:\t\t%s (took %s)\n", imageName, time.Since(t).Round(100*time.Millisecond).String())
// Wait for image
t = time.Now()
err = waitForImageReady(svc, imageName, 5*time.Minute)
if err != nil {
fmt.Println("Error waiting for image to be ready:\t", err)
return config
}
fmt.Println("Image is ready:\t\t", imageName)
fmt.Printf("Image is ready:\t\t%s (took %s)\n", imageName, time.Since(t).Round(100*time.Millisecond).String())
// Launch new instance
t = time.Now()
newInstanceID, err := launchInstance(svc, realInstance, imageName, region)
if err != nil {
fmt.Println("Error launching instance:\t", err)
return config
}
fmt.Println("Launched new instance:\t", newInstanceID)
fmt.Printf("Launched new instance:\t%s (took %s)\n", newInstanceID, time.Since(t).Round(100*time.Millisecond).String())
// Terminate old instance
t = time.Now()
err = terminateInstance(svc, instanceID)
if err != nil {
fmt.Println("Error terminating instance:\t", err)
return config
}
fmt.Println("Killed old instance:\t", instanceID)
fmt.Printf("Killed old instance:\t%s (took %s)\n", instanceID, time.Since(t).Round(100*time.Millisecond).String())
// Deregister old image
t = time.Now()
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)
fmt.Printf("Deregistered image:\t%s (took %s)\n", imageName, time.Since(t).Round(100*time.Millisecond).String())
// Delete old snapshot
t = time.Now()
if len(image.BlockDeviceMappings) > 0 {
snapshotID := aws.ToString(image.BlockDeviceMappings[0].Ebs.SnapshotId)
err = deleteSnapshot(svc, snapshotID)
@ -129,7 +133,7 @@ func AWSMoveInstance(config state.Config) (state.Config) {
fmt.Println("Error deleting snapshot:\t", err)
return config
}
fmt.Println("Deleted snapshot:\t", snapshotID)
fmt.Printf("Deleted snapshot:\t%s (took %s)\n", snapshotID, time.Since(t).Round(100*time.Millisecond).String())
}
AWSUpdateService(config, region, serviceUUID, newInstanceID)

View File

@ -224,10 +224,7 @@ func terminateInstance(svc *ec2.Client, instanceID string) error {
input := &ec2.TerminateInstancesInput{
InstanceIds: []string{instanceID},
}
_, err := svc.TerminateInstances(context.TODO(), input)
// TODO: remove config for old instance
return err
}