| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // Publishing a website (oficina/equipo discussion 11): after Equipo merges |
| 10 | // a task whose candidate carries a static site (release/preview.json), the |
| 11 | // creator is asked, on a separate card, whether to publish the merged |
| 12 | // commit's site on Carloku. Only their answer starts a publish step; the |
| 13 | // creator's primary runs one fixed carlos command for it and confirms the |
| 14 | // site by its marker over https. |
| 15 | |
| 16 | // SpecialistPublish publishes a merged commit's static site. |
| 17 | const SpecialistPublish = "publish" |
| 18 | |
| 19 | // CardPublish asks the task's creator, once per merged commit, whether to |
| 20 | // publish the site (Name the task's title, TaskID the task, Site what |
| 21 | // would go live). ChoicePublish publishes it; ChoiceNotNow leaves it. |
| 22 | const ( |
| 23 | CardPublish = "publish" |
| 24 | ChoicePublish = "publish" |
| 25 | ) |
| 26 | |
| 27 | // LeasePurposePublish is a LeaseCheck's Purpose right before a publish |
| 28 | // step's deploy, with the export's Digest; RefusedPublishNotBuilt refuses |
| 29 | // one whose built report the instance has not applied yet. |
| 30 | const ( |
| 31 | LeasePurposePublish = "site_publish" |
| 32 | RefusedPublishNotBuilt = "publish_not_built" |
| 33 | ) |
| 34 | |
| 35 | // PublishDispatch is a publish step's target (DispatchCommand.Publish): |
| 36 | // the Carloku app the instance derived from the project, and the default |
| 37 | // branch the merged commit (CandidateSHA) must be on. |
| 38 | type PublishDispatch struct { |
| 39 | App string `json:"app"` |
| 40 | Into string `json:"into"` |
| 41 | // ProtocolPlaces: the confirmed place it publishes to (console host, |
| 42 | // account sqid and name), the place's and the project binding's |
| 43 | // revisions the person's tap saw, and the SiteDestination of all of |
| 44 | // it. The runner deploys there and nowhere else. |
| 45 | Console string `json:"console,omitempty"` |
| 46 | Account string `json:"account,omitempty"` |
| 47 | AccountName string `json:"account_name,omitempty"` |
| 48 | Place string `json:"place,omitempty"` |
| 49 | PlaceRevision int64 `json:"place_revision,omitempty"` |
| 50 | BindingRevision int64 `json:"binding_revision,omitempty"` |
| 51 | Destination string `json:"destination,omitempty"` |
| 52 | // Mode is what the person's tap allowed (SiteSetup, SiteMint or |
| 53 | // SitePublish); DeployName the key a setup or mint asks for. The |
| 54 | // runner never changes mode. |
| 55 | Mode string `json:"mode,omitempty"` |
| 56 | DeployName string `json:"deploy_name,omitempty"` |
| 57 | } |
| 58 | |
| 59 | // Publish modes. Setup creates the app with the owner's login, then mints |
| 60 | // its deploy key; mint only mints the key with that login (the app |
| 61 | // exists), then deploys with the key; publish deploys with the held key. |
| 62 | const ( |
| 63 | SiteSetup = "setup" |
| 64 | SiteMint = "mint" |
| 65 | SitePublish = "publish" |
| 66 | ) |
| 67 | |
| 68 | // LeasePurposeMint is a LeaseCheck's Purpose right before a setup's or a |
| 69 | // mint's services create, with the destination (ProtocolPlaces). |
| 70 | const LeasePurposeMint = "site_mint" |
| 71 | |
| 72 | // PublishResult is a publish step's word: Phase built with the export's |
| 73 | // digest, files and bytes; then, done, the app, the site's public URL and |
| 74 | // the Outcome (published once the marker answered over https, unconfirmed |
| 75 | // when it never did). |
| 76 | type PublishResult struct { |
| 77 | Phase string `json:"phase"` |
| 78 | App string `json:"app,omitempty"` |
| 79 | URL string `json:"url,omitempty"` |
| 80 | Version string `json:"version,omitempty"` |
| 81 | Digest string `json:"digest"` |
| 82 | Files int `json:"files"` |
| 83 | Bytes int64 `json:"bytes"` |
| 84 | Outcome string `json:"outcome,omitempty"` |
| 85 | // DeployHeld (ProtocolPlaces): a setup's or a mint's done report says |
| 86 | // the runner holds the site's deploy key now. Only a recovery card |
| 87 | // reads it; the key's evidence is the runner's reach. |
| 88 | DeployHeld bool `json:"deploy_held,omitempty"` |
| 89 | } |
| 90 | |
| 91 | // Publish phases and outcomes. |
| 92 | const ( |
| 93 | PublishBuilt = "built" |
| 94 | PublishDone = "done" |
| 95 | PublishPublished = "published" |
| 96 | PublishUnconfirmed = "unconfirmed" |
| 97 | ) |
| 98 | |
| 99 | // TaskSite is a merged task's website as the task shows it: the Carloku |
| 100 | // app, the merged commit it publishes, where its publishing stands and, |
| 101 | // once it answered, its public URL. |
| 102 | type TaskSite struct { |
| 103 | App string `json:"app"` |
| 104 | SHA string `json:"sha"` |
| 105 | State string `json:"state"` |
| 106 | URL string `json:"url,omitempty"` |
| 107 | At *time.Time `json:"at,omitempty"` |
| 108 | } |
| 109 | |
| 110 | // Site states (TaskSite.State). |
| 111 | const ( |
| 112 | SiteAsked = "asked" |
| 113 | SiteDeclined = "declined" |
| 114 | SitePublishing = "publishing" |
| 115 | SitePublished = "published" |
| 116 | SiteUnconfirmed = "unconfirmed" |
| 117 | SiteFailed = "failed" |
| 118 | ) |
| 119 | |
| 120 | // CardSite is what a publish card offers: the app and the merged commit; |
| 121 | // State and URL follow the publish it started. |
| 122 | type CardSite struct { |
| 123 | App string `json:"app"` |
| 124 | SHA string `json:"sha"` |
| 125 | State string `json:"state"` |
| 126 | URL string `json:"url,omitempty"` |
| 127 | } |
| 128 | |
| 129 | // MaxSiteApp is Carloku's longest app name. |
| 130 | const MaxSiteApp = 32 |
| 131 | |
| 132 | // siteApp is a publish step's app: site- and a slug of lower-case letters |
| 133 | // and digits in hyphen-separated runs, Carloku's app rules within. |
| 134 | var siteApp = regexp.MustCompile(`^site-[a-z0-9]+(-[a-z0-9]+)*$`) |
| 135 | |
| 136 | // ValidSiteApp reports whether app is a publishable site's app name. |
| 137 | func ValidSiteApp(app string) bool { return len(app) <= MaxSiteApp && siteApp.MatchString(app) } |
| 138 | |
| 139 | // downgradePublish clears a body's ProtocolPublish fields and refuses a |
| 140 | // publish dispatch or check outright: an older peer has no words for them. |
| 141 | func downgradePublish(body any, protocol int) (bool, error) { |
| 142 | switch v := body.(type) { |
| 143 | case *DispatchCommand: |
| 144 | if v.Specialist == SpecialistPublish { |
| 145 | return false, fmt.Errorf("%w: a publish step at protocol %d", ErrProtocol, protocol) |
| 146 | } |
| 147 | set := v.Publish != nil |
| 148 | v.Publish = nil |
| 149 | return set, nil |
| 150 | case *StepReport: |
| 151 | set := v.Publish != nil |
| 152 | v.Publish = nil |
| 153 | return set, nil |
| 154 | case *LeaseCheck: |
| 155 | if v.Purpose == LeasePurposePublish { |
| 156 | return false, fmt.Errorf("%w: a publish check at protocol %d", ErrProtocol, protocol) |
| 157 | } |
| 158 | case *Hello: |
| 159 | return WithoutSites(v), nil |
| 160 | } |
| 161 | return false, nil |
| 162 | } |
| 163 | |
| 164 | // WithoutSites drops a hello's sites capability, in a copy. The first |
| 165 | // hello is sent before the instance says its protocol: an older |
| 166 | // instance's strict decoder would refuse it. |
| 167 | func WithoutSites(h *Hello) bool { |
| 168 | if h.Capabilities == nil || !h.Capabilities.Sites { |
| 169 | return false |
| 170 | } |
| 171 | c := *h.Capabilities |
| 172 | c.Sites = false |
| 173 | h.Capabilities = &c |
| 174 | return true |
| 175 | } |
| 176 | |