Add EC2 Launch Template strain genesis for AWS horizontal scale.
Ship POST /api/v1/forge/launch-template with cloud-init user-data embedding genesis snapshot hash and strain card ID; first template-tagged auth pins SpreadGeneration=0 and ParentAgentID=template.
This commit is contained in:
44
server/internal/spreadgenesis/launch_template.go
Normal file
44
server/internal/spreadgenesis/launch_template.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package spreadgenesis
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"crypto-miner-server/internal/models"
|
||||
"crypto-miner-server/internal/strategy"
|
||||
)
|
||||
|
||||
const (
|
||||
JoinLaneLaunchTemplate = "launch_template"
|
||||
LaunchTemplateParentAgentID = "template"
|
||||
)
|
||||
|
||||
type AuthProbe struct {
|
||||
JoinLane string
|
||||
ParentAgentID string
|
||||
GenesisSnapshotHash string
|
||||
}
|
||||
|
||||
func IsFirstAuth(isNewAgent bool, auth AuthProbe) bool {
|
||||
if !isNewAgent {
|
||||
return false
|
||||
}
|
||||
if strings.TrimSpace(auth.GenesisSnapshotHash) != "" {
|
||||
return true
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(auth.ParentAgentID), LaunchTemplateParentAgentID) {
|
||||
return true
|
||||
}
|
||||
return strings.TrimSpace(auth.JoinLane) == JoinLaneLaunchTemplate
|
||||
}
|
||||
|
||||
func ApplyFirstAuth(agent *models.Agent, isNewAgent bool, auth AuthProbe) {
|
||||
if agent == nil || !IsFirstAuth(isNewAgent, auth) {
|
||||
return
|
||||
}
|
||||
agent.ParentAgentID = LaunchTemplateParentAgentID
|
||||
agent.SpreadGeneration = 0
|
||||
agent.JoinLane = JoinLaneLaunchTemplate
|
||||
if strings.TrimSpace(agent.SpreadStrain) == "" {
|
||||
agent.SpreadStrain = strategy.SpreadStrainFromJoinLane(JoinLaneLaunchTemplate)
|
||||
}
|
||||
}
|
||||
24
server/internal/spreadgenesis/launch_template_test.go
Normal file
24
server/internal/spreadgenesis/launch_template_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package spreadgenesis
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"crypto-miner-server/internal/models"
|
||||
)
|
||||
|
||||
func TestIsFirstAuth(t *testing.T) {
|
||||
if !IsFirstAuth(true, AuthProbe{GenesisSnapshotHash: "abc"}) {
|
||||
t.Fatal("expected tag")
|
||||
}
|
||||
if IsFirstAuth(false, AuthProbe{GenesisSnapshotHash: "abc"}) {
|
||||
t.Fatal("reconnect skip")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFirstAuth(t *testing.T) {
|
||||
agent := &models.Agent{SpreadGeneration: 3}
|
||||
ApplyFirstAuth(agent, true, AuthProbe{ParentAgentID: "template"})
|
||||
if agent.SpreadGeneration != 0 || agent.ParentAgentID != LaunchTemplateParentAgentID {
|
||||
t.Fatalf("%+v", agent)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user