| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/boxchoice" |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | ) |
| 11 | |
| 12 | // ClaimLoads counts, from one listing of control/hosts/ (ListClaims), the |
| 13 | // hosts whose claim names each box as op.exec, in total and per |
| 14 | // "acct/app": choose()'s second rule until spec 2 (region HA §5.2, claim |
| 15 | // P5-D2). The count only balances, so a claim that cannot be read is |
| 16 | // logged and left out; a listing that fails is an error, because a count |
| 17 | // of nothing would send every create to one box. |
| 18 | func ClaimLoads(ctx context.Context, s release.Store) (map[string]boxchoice.Load, error) { |
| 19 | hosts, err := ListClaims(ctx, s) |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | out := map[string]boxchoice.Load{} |
| 24 | for _, host := range hosts { |
| 25 | obs, err := hostclaim.Read(ctx, s, host) |
| 26 | if err != nil { |
| 27 | log.Printf("hostops: claim loads: %s: %v", host, err) |
| 28 | continue |
| 29 | } |
| 30 | if obs.Absent || obs.Claim.Holder == nil || obs.Claim.Op.Exec == "" || obs.Claim.Op.Exec == "none" { |
| 31 | continue |
| 32 | } |
| 33 | box, h := obs.Claim.Op.Exec, obs.Claim.Holder |
| 34 | l := out[box] |
| 35 | if l.Apps == nil { |
| 36 | l.Apps = map[string]int{} |
| 37 | } |
| 38 | l.Hosts++ |
| 39 | l.Apps[h.Account+"/"+h.App]++ |
| 40 | out[box] = l |
| 41 | } |
| 42 | return out, nil |
| 43 | } |
| 44 | |