// Package provision implements only the devnet EC2 stage, not chain deployment. package provision import ( "crypto/sha256" "encoding/hex" "encoding/json" "errors" "fmt" "reflect" "sort" "strconv" "strings" "github.com/dashpay/dash-network-go/internal/spec" ) const ManagedBy = "dash-network-go" // Succeeds is empty for the first plan under a network key. A plan that reuses // the name of a destroyed network names the retired plan it replaces: the ID // covers it, so every client token, tag and journal condition of the new fleet // differs from the old one's, even when nothing else in the network changed. type Plan struct { APIVersion string `json:"apiVersion"` Kind string `json:"kind"` ID string `json:"id"` Succeeds string `json:"succeeds,omitempty"` Network spec.Network `json:"network"` Targets []Target `json:"targets"` } type Target struct { Name string `json:"name"` Group string `json:"group"` Role string `json:"role"` Architecture string `json:"architecture"` InstanceType string `json:"instanceType"` AMI string `json:"ami"` RootDevice string `json:"rootDevice"` } func digest(v any) string { b, _ := json.Marshal(v) sum := sha256.Sum256(b) return hex.EncodeToString(sum[:]) } func build(n spec.Network, roots map[string]string, succeeds string) Plan { p := Plan{APIVersion: spec.Version, Kind: "EC2ProvisionPlan", Succeeds: succeeds, Network: n, Targets: []Target{}} for _, g := range n.Nodes { for i := 1; i <= g.Count; i++ { p.Targets = append(p.Targets, Target{Name: fmt.Sprintf("%s-%03d", g.Name, i), Group: g.Name, Role: g.Role, Architecture: g.Architecture, InstanceType: g.InstanceType, AMI: n.AWS.Provision.AMIs[g.Architecture].ID, RootDevice: roots[g.Architecture]}) } } sort.Slice(p.Targets, func(i, j int) bool { return p.Targets[i].Name < p.Targets[j].Name }) p.ID = digest(p) return p } func (p Plan) Validate() error { if err := p.Network.Validate(); err != nil { return err } if err := p.Network.ValidateProvision(); err != nil { return err } roots := map[string]string{} for _, t := range p.Targets { if !strings.HasPrefix(t.RootDevice, "/dev/") || len(t.RootDevice) > 64 || strings.ContainsAny(t.RootDevice, " \n\r\t") { return errors.New("invalid root device in provision plan") } if old, ok := roots[t.Architecture]; ok && old != t.RootDevice { return errors.New("inconsistent root devices") } roots[t.Architecture] = t.RootDevice } if p.Succeeds != "" && !hex64.MatchString(p.Succeeds) { return errors.New("provision plan succeeds an invalid plan ID") } if !reflect.DeepEqual(p, build(p.Network, roots, p.Succeeds)) { return errors.New("provision plan is altered, incomplete, or has an invalid ID; regenerate it") } return nil } // Successor is p as the plan that replaces the retired plan with the given ID // under the same network key. Only the journal decides whether that plan is // retired: see Record.Retired and the journal's Supersede. func (p Plan) Successor(retired string) (Plan, error) { if !hex64.MatchString(retired) { return Plan{}, errors.New("a successor plan needs the retired plan's ID") } if err := p.Validate(); err != nil { return Plan{}, err } roots := map[string]string{} for _, t := range p.Targets { roots[t.Architecture] = t.RootDevice } next := build(p.Network, roots, retired) return next, next.Validate() } // Key excludes generation: a changed generation cannot bypass the network lock // or silently replace a previous fleet. A key is reused only by a plan that // succeeds the retired plan the journal holds under it. func (p Plan) Key() string { return p.Network.AWS.AccountID + "/" + p.Network.AWS.Region + "/" + p.Network.Metadata.Name } // ArchiveKey is where the journal keeps this plan's operation, as it stood at // the given revision, once its network key has been reused. The revision is // part of the key so that an archive is never compared with, or blocked by, a // copy of some other state of the same operation. A network name cannot // contain '#'. func (p Plan) ArchiveKey(revision int64) string { return p.Key() + "#retired#" + p.ID + "#" + strconv.FormatInt(revision, 10) } func (p Plan) Token(t Target) string { return digest([]string{p.ID, t.Name}) } func (p Plan) Tags(t Target) map[string]string { return map[string]string{ "Name": p.Network.Metadata.Name + "-" + t.Name, p.Network.AWS.NetworkTagKey: p.Network.Metadata.Name, "dashnet:managed-by": ManagedBy, "dashnet:network": p.Network.Metadata.Name, "dashnet:generation": fmt.Sprint(p.Network.Chain.Generation), "dashnet:plan": p.ID, "dashnet:node": t.Name, "dashnet:role": t.Role, } }