@stacksjs/ts-cloud 0.16.78 → 0.16.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/{chunk-hkqq09p2.js → chunk-6kx0fddd.js} +1 -1
- package/dist/bin/{chunk-2xtxktj3.js → chunk-b074y2np.js} +1 -1
- package/dist/bin/{chunk-vgcscxh1.js → chunk-f0djc6da.js} +1 -1
- package/dist/bin/{chunk-n5sapbxq.js → chunk-jhydr9dp.js} +61 -59
- package/dist/bin/chunk-m1w3p30q.js +1 -0
- package/dist/bin/chunk-qssb91v3.js +11 -0
- package/dist/bin/cli.js +7 -7
- package/dist/bin/dashboard-server.js +1 -1
- package/dist/{chunk-m95n1mbv.js → chunk-kjkkh11z.js} +1 -1
- package/dist/{chunk-wxy6jk7d.js → chunk-z52n96sa.js} +89 -85
- package/dist/deploy/index.js +1 -1
- package/dist/drivers/index.js +1 -1
- package/dist/drivers/shared/backups.test.d.ts +1 -0
- package/dist/drivers/shared/runtime-isolation.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/ui/404.html +1 -1
- package/dist/ui/accept-invitation.html +1 -1
- package/dist/ui/access-denied.html +1 -1
- package/dist/ui/account/automation.html +6 -6
- package/dist/ui/account/security.html +2 -2
- package/dist/ui/applications/compose.html +6 -6
- package/dist/ui/applications/new.html +2 -2
- package/dist/ui/data/backups.html +6 -6
- package/dist/ui/data/services.html +6 -6
- package/dist/ui/data/volumes.html +6 -6
- package/dist/ui/index.html +6 -6
- package/dist/ui/infrastructure/topology.html +6 -6
- package/dist/ui/integrations.html +1 -1
- package/dist/ui/operations/alerts.html +5 -5
- package/dist/ui/operations/configuration.html +6 -6
- package/dist/ui/operations/jobs.html +5 -5
- package/dist/ui/operations/maintenance.html +5 -5
- package/dist/ui/operations/observability.html +6 -6
- package/dist/ui/operations/previews.html +6 -6
- package/dist/ui/operations/queue.html +6 -6
- package/dist/ui/operations/regions.html +5 -5
- package/dist/ui/operations/releases.html +6 -6
- package/dist/ui/operations/spend.html +6 -6
- package/dist/ui/operations/workloads.html +6 -6
- package/dist/ui/security.html +1 -1
- package/dist/ui/server/actions.html +6 -6
- package/dist/ui/server/activity.html +2 -2
- package/dist/ui/server/capacity.html +6 -6
- package/dist/ui/server/database.html +6 -6
- package/dist/ui/server/deployments.html +5 -5
- package/dist/ui/server/diagnostics.html +2 -2
- package/dist/ui/server/firewall.html +6 -6
- package/dist/ui/server/fleet.html +6 -6
- package/dist/ui/server/logs.html +6 -6
- package/dist/ui/server/metrics.html +5 -5
- package/dist/ui/server/security.html +2 -2
- package/dist/ui/server/services.html +2 -2
- package/dist/ui/server/sites.html +6 -6
- package/dist/ui/server/ssh-keys.html +6 -6
- package/dist/ui/server/team.html +6 -6
- package/dist/ui/server/terminal.html +2 -2
- package/dist/ui/serverless/alarms.html +6 -6
- package/dist/ui/serverless/assets.html +2 -2
- package/dist/ui/serverless/cost.html +2 -2
- package/dist/ui/serverless/data.html +6 -6
- package/dist/ui/serverless/deployments.html +1 -1
- package/dist/ui/serverless/firewall.html +1 -1
- package/dist/ui/serverless/functions.html +6 -6
- package/dist/ui/serverless/logs.html +6 -6
- package/dist/ui/serverless/metrics.html +2 -2
- package/dist/ui/serverless/queues.html +6 -6
- package/dist/ui/serverless/secrets.html +5 -5
- package/dist/ui/serverless/traces.html +6 -6
- package/dist/ui/serverless.html +5 -5
- package/package.json +3 -3
- package/dist/bin/chunk-nzky7rgv.js +0 -1
- package/dist/bin/chunk-ps0pzqm0.js +0 -9
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{p}from"./chunk-540wkkhs.js";import{pa,I,A,ne,jr,ga,Jo,le,U,T,er,Vr}from"./chunk-wayhpmn7.js";import{v}from"./chunk-4vtyjj7c.js";import{Q}from"./chunk-9ktyfegk.js";import{g}from"./chunk-gqstb8zn.js";import{W,ki,Us,w,R,Tt,Ve,it,Io,Ni,Do,wt,Kc,ko,No,_i,xo,Oi,Ht,Vs,Jc,$i,zs,Hs,Gs,qs,_o,hn,Ws,yn,Ks,Oo}from"./chunk-ps0pzqm0.js";import{zo,Ho,Ys}from"./chunk-0r8k7qth.js";import{Ms}from"./chunk-k3cyh23p.js";import{Pr}from"./chunk-hhbmtgkq.js";import{bn}from"./chunk-ma0d990n.js";import{el}from"./chunk-hyrvmjq0.js";class Ze{client;region;constructor(e="us-east-1",t){this.region=e,this.client=new p}async putParameter(e){let t={Name:e.Name,Value:e.Value};if(e.Type)t.Type=e.Type;if(e.Description)t.Description=e.Description;if(e.KeyId)t.KeyId=e.KeyId;if(e.Overwrite!==void 0)t.Overwrite=e.Overwrite;if(e.AllowedPattern)t.AllowedPattern=e.AllowedPattern;if(e.Tags&&e.Tags.length>0)t.Tags=e.Tags;if(e.Tier)t.Tier=e.Tier;if(e.DataType)t.DataType=e.DataType;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.PutParameter","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Version:r.Version,Tier:r.Tier}}async getParameter(e){let t={Name:e.Name};if(e.WithDecryption!==void 0)t.WithDecryption=e.WithDecryption;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetParameter","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameter:r.Parameter?this.parseParameter(r.Parameter):void 0}}async getParameters(e){let t={Names:e.Names};if(e.WithDecryption!==void 0)t.WithDecryption=e.WithDecryption;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetParameters","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameters:r.Parameters?.map((n)=>this.parseParameter(n)),InvalidParameters:r.InvalidParameters}}async getParametersByPath(e){let t={Path:e.Path};if(e.Recursive!==void 0)t.Recursive=e.Recursive;if(e.WithDecryption!==void 0)t.WithDecryption=e.WithDecryption;if(e.MaxResults)t.MaxResults=e.MaxResults;if(e.NextToken)t.NextToken=e.NextToken;if(e.ParameterFilters&&e.ParameterFilters.length>0)t.ParameterFilters=e.ParameterFilters;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetParametersByPath","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameters:r.Parameters?.map((n)=>this.parseParameter(n)),NextToken:r.NextToken}}async deleteParameter(e){let t={Name:e.Name};await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.DeleteParameter","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})}async deleteParameters(e){let t={Names:e},r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.DeleteParameters","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{DeletedParameters:r.DeletedParameters,InvalidParameters:r.InvalidParameters}}async describeParameters(e){let t={};if(e?.Filters&&e.Filters.length>0)t.Filters=e.Filters;if(e?.ParameterFilters&&e.ParameterFilters.length>0)t.ParameterFilters=e.ParameterFilters;if(e?.MaxResults)t.MaxResults=e.MaxResults;if(e?.NextToken)t.NextToken=e.NextToken;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.DescribeParameters","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameters:r.Parameters?.map((n)=>this.parseParameter(n)),NextToken:r.NextToken}}async getParameterHistory(e){let t={Name:e.Name};if(e.WithDecryption!==void 0)t.WithDecryption=e.WithDecryption;if(e.MaxResults)t.MaxResults=e.MaxResults;if(e.NextToken)t.NextToken=e.NextToken;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetParameterHistory","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameters:r.Parameters?.map((n)=>({Name:n.Name,Type:n.Type,KeyId:n.KeyId,LastModifiedDate:n.LastModifiedDate,LastModifiedUser:n.LastModifiedUser,Description:n.Description,Value:n.Value,Version:n.Version,Labels:n.Labels,Tier:n.Tier})),NextToken:r.NextToken}}async labelParameterVersion(e){let t={Name:e.Name,Labels:e.Labels};if(e.ParameterVersion!==void 0)t.ParameterVersion=e.ParameterVersion;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.LabelParameterVersion","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{InvalidLabels:r.InvalidLabels,ParameterVersion:r.ParameterVersion}}async addTagsToResource(e){let t={ResourceType:e.ResourceType,ResourceId:e.ResourceId,Tags:e.Tags};await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.AddTagsToResource","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})}async removeTagsFromResource(e){let t={ResourceType:e.ResourceType,ResourceId:e.ResourceId,TagKeys:e.TagKeys};await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.RemoveTagsFromResource","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})}async listTagsForResource(e){let t={ResourceType:e.ResourceType,ResourceId:e.ResourceId};return{TagList:(await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.ListTagsForResource","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})).TagList}}async setString(e,t,r){return this.putParameter({Name:e,Value:t,Type:"String",Description:r?.description,Overwrite:r?.overwrite??!0,Tags:r?.tags})}async setSecureString(e,t,r){return this.putParameter({Name:e,Value:t,Type:"SecureString",Description:r?.description,Overwrite:r?.overwrite??!0,KeyId:r?.kmsKeyId,Tags:r?.tags})}async getValue(e){return(await this.getParameter({Name:e,WithDecryption:!0})).Parameter?.Value}async getAllByPath(e,t=!0){let r=[],n;do{let s=await this.getParametersByPath({Path:e,Recursive:t,WithDecryption:!0,NextToken:n});if(s.Parameters)r.push(...s.Parameters);n=s.NextToken}while(n);return r}parseParameter(e){return{Name:e.Name,Type:e.Type,Value:e.Value,Version:e.Version,LastModifiedDate:e.LastModifiedDate,ARN:e.ARN,DataType:e.DataType,Description:e.Description,AllowedPattern:e.AllowedPattern,KeyId:e.KeyId,Tier:e.Tier}}async sendCommand(e){if(!e.InstanceIds&&!e.Targets)throw Error("SendCommand requires either InstanceIds or Targets");let t={DocumentName:e.DocumentName};if(e.InstanceIds)t.InstanceIds=e.InstanceIds;if(e.Targets)t.Targets=e.Targets;if(e.Parameters)t.Parameters=e.Parameters;if(e.TimeoutSeconds)t.TimeoutSeconds=e.TimeoutSeconds;if(e.Comment)t.Comment=e.Comment;if(e.OutputS3BucketName)t.OutputS3BucketName=e.OutputS3BucketName;if(e.OutputS3KeyPrefix)t.OutputS3KeyPrefix=e.OutputS3KeyPrefix;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.SendCommand","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{CommandId:r.Command?.CommandId,Status:r.Command?.Status,StatusDetails:r.Command?.StatusDetails}}async getCommandInvocation(e){let t={CommandId:e.CommandId,InstanceId:e.InstanceId},r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetCommandInvocation","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Status:r.Status,StatusDetails:r.StatusDetails,StandardOutputContent:r.StandardOutputContent,StandardErrorContent:r.StandardErrorContent,ResponseCode:r.ResponseCode}}async runShellCommand(e,t,r){let n=await this.sendCommand({InstanceIds:[e],DocumentName:"AWS-RunShellScript",Parameters:{commands:t},TimeoutSeconds:r?.timeoutSeconds||600});if(!n.CommandId)return{success:!1,error:"Failed to send command"};if(r?.waitForCompletion===!1)return{success:!0,status:"Pending"};let s=r?.pollIntervalMs||2000,i=r?.maxWaitMs||300000,o=Date.now();while(Date.now()-o<i){await new Promise((a)=>setTimeout(a,s));try{let a=await this.getCommandInvocation({CommandId:n.CommandId,InstanceId:e});if(a.Status==="Success")return{success:!0,output:a.StandardOutputContent,error:a.StandardErrorContent,status:a.Status};if(a.Status==="Failed"||a.Status==="Cancelled"||a.Status==="TimedOut")return{success:!1,output:a.StandardOutputContent,error:a.StandardErrorContent||a.StatusDetails,status:a.Status}}catch(a){if(!a.message?.includes("InvocationDoesNotExist"))return{success:!1,error:a.message}}}return{success:!1,error:"Command timed out waiting for completion"}}async listCommandInvocations(e){let t={CommandId:e.CommandId};if(e.Details)t.Details=!0;return((await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.ListCommandInvocations","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})).CommandInvocations||[]).map((s)=>({InstanceId:s.InstanceId,Status:s.Status,StatusDetails:s.StatusDetails,StandardOutputContent:s.CommandPlugins?.[0]?.Output,StandardErrorContent:s.CommandPlugins?.[0]?.StandardErrorUrl}))}async sendCommandByTags(e){let t=Object.entries(e.tags).map(([c,l])=>({Key:`tag:${c}`,Values:[l]})),r=await this.sendCommand({Targets:t,DocumentName:"AWS-RunShellScript",Parameters:{commands:e.commands},TimeoutSeconds:e.timeoutSeconds||600,Comment:e.comment});if(!r.CommandId)return{success:!1,instanceCount:0,perInstance:[],error:"Failed to send command"};let n=e.pollIntervalMs||3000,s=e.maxWaitMs||600000,i=Date.now(),o=new Set(["Success","Failed","Cancelled","TimedOut"]),a=[];while(Date.now()-i<s){await new Promise((c)=>setTimeout(c,n));try{if(a=await this.listCommandInvocations({CommandId:r.CommandId,Details:!0}),a.length===0)continue;if(a.every((l)=>o.has(l.Status||""))){let l=a.map((d)=>({instanceId:d.InstanceId,status:d.Status||"Unknown",output:d.StandardOutputContent,error:d.StandardErrorContent||d.StatusDetails})),u=a.every((d)=>d.Status==="Success");return{success:u,instanceCount:a.length,perInstance:l,error:u?void 0:"One or more instances reported a non-success status"}}}catch(c){if(!c.message?.includes("InvocationDoesNotExist"))return{success:!1,instanceCount:0,perInstance:[],error:c.message}}}return{success:!1,instanceCount:a.length,perInstance:a.map((c)=>({instanceId:c.InstanceId,status:c.Status||"Unknown"})),error:"Command timed out waiting for all instances to complete"}}}function ks(e={}){let t=new Set;for(let r of Object.values(e)){if(!r.domain)continue;if(!(r.redirect||r.deploy==="server"||r.start))continue;t.add(r.domain);for(let n of r.aliases??[]){let s=String(n).trim().toLowerCase();if(s)t.add(s)}}return t}function Dr(e){return e.replace(/\.$/,"").toLowerCase()}function La(e,t,r){let n=Dr(e.name),s=Dr(t),i=Dr(r),o=i===s?"@":i.endsWith(`.${s}`)?i.slice(0,-(s.length+1)):i;return n===i||n===o||o==="@"&&(n===""||n===s)}async function ka(e,t,r,n,s="A"){let i=await e.listRecords(t);if(!i.success)return[`could not list ${s} records: ${i.message||"unknown provider error"}`];let o=i.records.filter((l)=>l.type===s&&La(l,t,r)),a=o.findIndex((l)=>l.content===n);if(o.length<=1||a===-1)return[];let c=[];for(let[l,u]of o.entries()){if(l===a)continue;let d=await e.deleteRecord(t,u);if(!d.success)c.push(`could not remove stale ${u.name} ${s} ${u.content}: ${d.message||"unknown provider error"}`)}return c}function me(e){if(!e)return;let t=e.trim();if(!t)return;if(!t.includes("/"))return t;let[r]=t.split("/");if(!r.endsWith("::"))return r||void 0;return`${r}1`}var Na=new Set(["mail","smtp","imap","mx"]);function po(e){return!Na.has(e.split(".")[0]??"")}async function Ei(e){let{provider:t,zone:r,fqdn:n,ipv4:s,ipv6:i,ttl:o=600}=e,a={published:[],warnings:[]},c=[{type:"A",content:s}];if(i&&po(n))c.push({type:"AAAA",content:i});for(let{type:l,content:u}of c){let d=await t.upsertRecord(r,{name:n,type:l,content:u,ttl:o});if(d?.success===!1){a.warnings.push(`${n} → ${u} failed: ${d.error||d.message||"unknown error"}`);continue}if(a.warnings.push(...(await ka(t,r,n,u,l)).map((m)=>`${n} cleanup: ${m}`)),await mo(t,r,n,u,l))a.published.push({fqdn:n,type:l,content:u});else a.warnings.push(`${n} → ${u} reported success at ${t.name} but no matching ${l} record exists — create it manually: ${l} ${n} → ${u}`)}return a}async function mo(e,t,r,n,s){try{if(typeof e?.listRecords!=="function")return!0;let i=await e.listRecords(t);if(!i?.success||!Array.isArray(i.records))return!0;return i.records.some((o)=>{let a=typeof o?.name==="string"?o.name.replace(/\.$/,""):"";return o?.type===s&&a===r&&o?.content===n})}catch{return!0}}import{readFileSync as Bs}from"node:fs";import{join as Rc}from"node:path";function Lr(e){return(Buffer.isBuffer(e)?e.toString("utf8"):typeof e==="string"?e:"").replace(/(^|\n)(\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_]*=).*$/gm,"$1$2[redacted]").replace(/encrypted:[A-Za-z0-9+/=]+/g,"encrypted:[redacted]").trim().slice(-8000)}function Pt(e,t){let r=Lr(e?.stderr),n=/line (\d+): syntax error/.exec(r);if(!n)return"";let s=Number(n[1]),i=t.split(`
|
|
1
|
+
import{p}from"./chunk-540wkkhs.js";import{pa,I,A,ne,jr,ga,Jo,le,U,T,er,Vr}from"./chunk-wayhpmn7.js";import{v}from"./chunk-4vtyjj7c.js";import{Q}from"./chunk-9ktyfegk.js";import{g}from"./chunk-gqstb8zn.js";import{W,ki,Us,w,R,Tt,Ve,it,Io,Ni,Do,wt,Kc,ko,No,_i,xo,Oi,Ht,Vs,Jc,$i,zs,Hs,Gs,qs,_o,hn,Ws,yn,Ks,Oo}from"./chunk-qssb91v3.js";import{zo,Ho,Ys}from"./chunk-0r8k7qth.js";import{Ms}from"./chunk-k3cyh23p.js";import{Pr}from"./chunk-hhbmtgkq.js";import{bn}from"./chunk-ma0d990n.js";import{el}from"./chunk-hyrvmjq0.js";class Ze{client;region;constructor(e="us-east-1",t){this.region=e,this.client=new p}async putParameter(e){let t={Name:e.Name,Value:e.Value};if(e.Type)t.Type=e.Type;if(e.Description)t.Description=e.Description;if(e.KeyId)t.KeyId=e.KeyId;if(e.Overwrite!==void 0)t.Overwrite=e.Overwrite;if(e.AllowedPattern)t.AllowedPattern=e.AllowedPattern;if(e.Tags&&e.Tags.length>0)t.Tags=e.Tags;if(e.Tier)t.Tier=e.Tier;if(e.DataType)t.DataType=e.DataType;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.PutParameter","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Version:r.Version,Tier:r.Tier}}async getParameter(e){let t={Name:e.Name};if(e.WithDecryption!==void 0)t.WithDecryption=e.WithDecryption;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetParameter","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameter:r.Parameter?this.parseParameter(r.Parameter):void 0}}async getParameters(e){let t={Names:e.Names};if(e.WithDecryption!==void 0)t.WithDecryption=e.WithDecryption;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetParameters","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameters:r.Parameters?.map((n)=>this.parseParameter(n)),InvalidParameters:r.InvalidParameters}}async getParametersByPath(e){let t={Path:e.Path};if(e.Recursive!==void 0)t.Recursive=e.Recursive;if(e.WithDecryption!==void 0)t.WithDecryption=e.WithDecryption;if(e.MaxResults)t.MaxResults=e.MaxResults;if(e.NextToken)t.NextToken=e.NextToken;if(e.ParameterFilters&&e.ParameterFilters.length>0)t.ParameterFilters=e.ParameterFilters;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetParametersByPath","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameters:r.Parameters?.map((n)=>this.parseParameter(n)),NextToken:r.NextToken}}async deleteParameter(e){let t={Name:e.Name};await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.DeleteParameter","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})}async deleteParameters(e){let t={Names:e},r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.DeleteParameters","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{DeletedParameters:r.DeletedParameters,InvalidParameters:r.InvalidParameters}}async describeParameters(e){let t={};if(e?.Filters&&e.Filters.length>0)t.Filters=e.Filters;if(e?.ParameterFilters&&e.ParameterFilters.length>0)t.ParameterFilters=e.ParameterFilters;if(e?.MaxResults)t.MaxResults=e.MaxResults;if(e?.NextToken)t.NextToken=e.NextToken;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.DescribeParameters","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameters:r.Parameters?.map((n)=>this.parseParameter(n)),NextToken:r.NextToken}}async getParameterHistory(e){let t={Name:e.Name};if(e.WithDecryption!==void 0)t.WithDecryption=e.WithDecryption;if(e.MaxResults)t.MaxResults=e.MaxResults;if(e.NextToken)t.NextToken=e.NextToken;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetParameterHistory","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Parameters:r.Parameters?.map((n)=>({Name:n.Name,Type:n.Type,KeyId:n.KeyId,LastModifiedDate:n.LastModifiedDate,LastModifiedUser:n.LastModifiedUser,Description:n.Description,Value:n.Value,Version:n.Version,Labels:n.Labels,Tier:n.Tier})),NextToken:r.NextToken}}async labelParameterVersion(e){let t={Name:e.Name,Labels:e.Labels};if(e.ParameterVersion!==void 0)t.ParameterVersion=e.ParameterVersion;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.LabelParameterVersion","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{InvalidLabels:r.InvalidLabels,ParameterVersion:r.ParameterVersion}}async addTagsToResource(e){let t={ResourceType:e.ResourceType,ResourceId:e.ResourceId,Tags:e.Tags};await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.AddTagsToResource","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})}async removeTagsFromResource(e){let t={ResourceType:e.ResourceType,ResourceId:e.ResourceId,TagKeys:e.TagKeys};await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.RemoveTagsFromResource","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})}async listTagsForResource(e){let t={ResourceType:e.ResourceType,ResourceId:e.ResourceId};return{TagList:(await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.ListTagsForResource","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})).TagList}}async setString(e,t,r){return this.putParameter({Name:e,Value:t,Type:"String",Description:r?.description,Overwrite:r?.overwrite??!0,Tags:r?.tags})}async setSecureString(e,t,r){return this.putParameter({Name:e,Value:t,Type:"SecureString",Description:r?.description,Overwrite:r?.overwrite??!0,KeyId:r?.kmsKeyId,Tags:r?.tags})}async getValue(e){return(await this.getParameter({Name:e,WithDecryption:!0})).Parameter?.Value}async getAllByPath(e,t=!0){let r=[],n;do{let s=await this.getParametersByPath({Path:e,Recursive:t,WithDecryption:!0,NextToken:n});if(s.Parameters)r.push(...s.Parameters);n=s.NextToken}while(n);return r}parseParameter(e){return{Name:e.Name,Type:e.Type,Value:e.Value,Version:e.Version,LastModifiedDate:e.LastModifiedDate,ARN:e.ARN,DataType:e.DataType,Description:e.Description,AllowedPattern:e.AllowedPattern,KeyId:e.KeyId,Tier:e.Tier}}async sendCommand(e){if(!e.InstanceIds&&!e.Targets)throw Error("SendCommand requires either InstanceIds or Targets");let t={DocumentName:e.DocumentName};if(e.InstanceIds)t.InstanceIds=e.InstanceIds;if(e.Targets)t.Targets=e.Targets;if(e.Parameters)t.Parameters=e.Parameters;if(e.TimeoutSeconds)t.TimeoutSeconds=e.TimeoutSeconds;if(e.Comment)t.Comment=e.Comment;if(e.OutputS3BucketName)t.OutputS3BucketName=e.OutputS3BucketName;if(e.OutputS3KeyPrefix)t.OutputS3KeyPrefix=e.OutputS3KeyPrefix;let r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.SendCommand","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{CommandId:r.Command?.CommandId,Status:r.Command?.Status,StatusDetails:r.Command?.StatusDetails}}async getCommandInvocation(e){let t={CommandId:e.CommandId,InstanceId:e.InstanceId},r=await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.GetCommandInvocation","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)});return{Status:r.Status,StatusDetails:r.StatusDetails,StandardOutputContent:r.StandardOutputContent,StandardErrorContent:r.StandardErrorContent,ResponseCode:r.ResponseCode}}async runShellCommand(e,t,r){let n=await this.sendCommand({InstanceIds:[e],DocumentName:"AWS-RunShellScript",Parameters:{commands:t},TimeoutSeconds:r?.timeoutSeconds||600});if(!n.CommandId)return{success:!1,error:"Failed to send command"};if(r?.waitForCompletion===!1)return{success:!0,status:"Pending"};let s=r?.pollIntervalMs||2000,i=r?.maxWaitMs||300000,o=Date.now();while(Date.now()-o<i){await new Promise((a)=>setTimeout(a,s));try{let a=await this.getCommandInvocation({CommandId:n.CommandId,InstanceId:e});if(a.Status==="Success")return{success:!0,output:a.StandardOutputContent,error:a.StandardErrorContent,status:a.Status};if(a.Status==="Failed"||a.Status==="Cancelled"||a.Status==="TimedOut")return{success:!1,output:a.StandardOutputContent,error:a.StandardErrorContent||a.StatusDetails,status:a.Status}}catch(a){if(!a.message?.includes("InvocationDoesNotExist"))return{success:!1,error:a.message}}}return{success:!1,error:"Command timed out waiting for completion"}}async listCommandInvocations(e){let t={CommandId:e.CommandId};if(e.Details)t.Details=!0;return((await this.client.request({service:"ssm",region:this.region,method:"POST",path:"/",headers:{"X-Amz-Target":"AmazonSSM.ListCommandInvocations","Content-Type":"application/x-amz-json-1.1"},body:JSON.stringify(t)})).CommandInvocations||[]).map((s)=>({InstanceId:s.InstanceId,Status:s.Status,StatusDetails:s.StatusDetails,StandardOutputContent:s.CommandPlugins?.[0]?.Output,StandardErrorContent:s.CommandPlugins?.[0]?.StandardErrorUrl}))}async sendCommandByTags(e){let t=Object.entries(e.tags).map(([c,l])=>({Key:`tag:${c}`,Values:[l]})),r=await this.sendCommand({Targets:t,DocumentName:"AWS-RunShellScript",Parameters:{commands:e.commands},TimeoutSeconds:e.timeoutSeconds||600,Comment:e.comment});if(!r.CommandId)return{success:!1,instanceCount:0,perInstance:[],error:"Failed to send command"};let n=e.pollIntervalMs||3000,s=e.maxWaitMs||600000,i=Date.now(),o=new Set(["Success","Failed","Cancelled","TimedOut"]),a=[];while(Date.now()-i<s){await new Promise((c)=>setTimeout(c,n));try{if(a=await this.listCommandInvocations({CommandId:r.CommandId,Details:!0}),a.length===0)continue;if(a.every((l)=>o.has(l.Status||""))){let l=a.map((d)=>({instanceId:d.InstanceId,status:d.Status||"Unknown",output:d.StandardOutputContent,error:d.StandardErrorContent||d.StatusDetails})),u=a.every((d)=>d.Status==="Success");return{success:u,instanceCount:a.length,perInstance:l,error:u?void 0:"One or more instances reported a non-success status"}}}catch(c){if(!c.message?.includes("InvocationDoesNotExist"))return{success:!1,instanceCount:0,perInstance:[],error:c.message}}}return{success:!1,instanceCount:a.length,perInstance:a.map((c)=>({instanceId:c.InstanceId,status:c.Status||"Unknown"})),error:"Command timed out waiting for all instances to complete"}}}function ks(e={}){let t=new Set;for(let r of Object.values(e)){if(!r.domain)continue;if(!(r.redirect||r.deploy==="server"||r.start))continue;t.add(r.domain);for(let n of r.aliases??[]){let s=String(n).trim().toLowerCase();if(s)t.add(s)}}return t}function Dr(e){return e.replace(/\.$/,"").toLowerCase()}function La(e,t,r){let n=Dr(e.name),s=Dr(t),i=Dr(r),o=i===s?"@":i.endsWith(`.${s}`)?i.slice(0,-(s.length+1)):i;return n===i||n===o||o==="@"&&(n===""||n===s)}async function ka(e,t,r,n,s="A"){let i=await e.listRecords(t);if(!i.success)return[`could not list ${s} records: ${i.message||"unknown provider error"}`];let o=i.records.filter((l)=>l.type===s&&La(l,t,r)),a=o.findIndex((l)=>l.content===n);if(o.length<=1||a===-1)return[];let c=[];for(let[l,u]of o.entries()){if(l===a)continue;let d=await e.deleteRecord(t,u);if(!d.success)c.push(`could not remove stale ${u.name} ${s} ${u.content}: ${d.message||"unknown provider error"}`)}return c}function me(e){if(!e)return;let t=e.trim();if(!t)return;if(!t.includes("/"))return t;let[r]=t.split("/");if(!r.endsWith("::"))return r||void 0;return`${r}1`}var Na=new Set(["mail","smtp","imap","mx"]);function po(e){return!Na.has(e.split(".")[0]??"")}async function Ei(e){let{provider:t,zone:r,fqdn:n,ipv4:s,ipv6:i,ttl:o=600}=e,a={published:[],warnings:[]},c=[{type:"A",content:s}];if(i&&po(n))c.push({type:"AAAA",content:i});for(let{type:l,content:u}of c){let d=await t.upsertRecord(r,{name:n,type:l,content:u,ttl:o});if(d?.success===!1){a.warnings.push(`${n} → ${u} failed: ${d.error||d.message||"unknown error"}`);continue}if(a.warnings.push(...(await ka(t,r,n,u,l)).map((m)=>`${n} cleanup: ${m}`)),await mo(t,r,n,u,l))a.published.push({fqdn:n,type:l,content:u});else a.warnings.push(`${n} → ${u} reported success at ${t.name} but no matching ${l} record exists — create it manually: ${l} ${n} → ${u}`)}return a}async function mo(e,t,r,n,s){try{if(typeof e?.listRecords!=="function")return!0;let i=await e.listRecords(t);if(!i?.success||!Array.isArray(i.records))return!0;return i.records.some((o)=>{let a=typeof o?.name==="string"?o.name.replace(/\.$/,""):"";return o?.type===s&&a===r&&o?.content===n})}catch{return!0}}import{readFileSync as Bs}from"node:fs";import{join as Rc}from"node:path";function Lr(e){return(Buffer.isBuffer(e)?e.toString("utf8"):typeof e==="string"?e:"").replace(/(^|\n)(\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_]*=).*$/gm,"$1$2[redacted]").replace(/encrypted:[A-Za-z0-9+/=]+/g,"encrypted:[redacted]").trim().slice(-8000)}function Pt(e,t){let r=Lr(e?.stderr),n=/line (\d+): syntax error/.exec(r);if(!n)return"";let s=Number(n[1]),i=t.split(`
|
|
2
2
|
`),o=Math.max(1,s-12),a=Math.min(i.length,s+2),c=i.slice(o-1,a).map((l,u)=>{let d=o+u,m=/^[A-Z_][A-Z0-9_]*=/.test(l)?l.replace(/=.*/,"=<redacted>"):l;return` ${String(d).padStart(4)}${d===s?" >":" "} ${m}`});return["",`The deploy script bash rejected is ${i.length} lines; it stopped at ${s}.`,...c].join(`
|
|
3
3
|
`)}function Ue(e){let t=e,r=typeof t?.status==="number"?` (exit ${t.status})`:"",n=t?.signal?` (signal ${t.signal})`:"",s=[Lr(t?.stderr),Lr(t?.stdout)].filter(Boolean).join(`
|
|
4
4
|
`);return`Remote SSH command failed${r}${n}${s?`
|
|
@@ -177,7 +177,8 @@ ${o.join(`
|
|
|
177
177
|
# aborts with "HOME: unbound variable" and /usr/local/bin/bun never appears —
|
|
178
178
|
# breaking a from-scratch provision (adopting an existing box masked it).
|
|
179
179
|
export HOME="\${HOME:-/root}"
|
|
180
|
-
export BUN_INSTALL="/
|
|
180
|
+
export BUN_INSTALL="/opt/ts-cloud/bun"
|
|
181
|
+
install -d -m 755 /opt/ts-cloud /opt/ts-cloud/bun /opt/ts-cloud/bun/bin
|
|
181
182
|
# bun.sh's installer references $HOME internally and runs under this
|
|
182
183
|
# script's \`set -u\` (inherited by the piped bash) — cloud-init's runcmd
|
|
183
184
|
# environment doesn't export HOME by default, so without this the
|
|
@@ -190,8 +191,9 @@ export BUN_INSTALL="/root/.bun"
|
|
|
190
191
|
# actually done.
|
|
191
192
|
export HOME="\${HOME:-/root}"
|
|
192
193
|
curl -fsSL https://bun.sh/install | bash${r==="latest"?"":` -s "bun-v${r}"`}
|
|
193
|
-
|
|
194
|
-
|
|
194
|
+
chmod 755 /opt/ts-cloud/bun/bin/bun
|
|
195
|
+
ln -sf /opt/ts-cloud/bun/bin/bun /usr/local/bin/bun
|
|
196
|
+
echo 'export BUN_INSTALL="/opt/ts-cloud/bun"' > /etc/profile.d/bun.sh
|
|
195
197
|
echo 'export PATH="$BUN_INSTALL/bin:$PATH"' >> /etc/profile.d/bun.sh
|
|
196
198
|
`;else if(t==="node"){let f=r==="latest"||!r?"20":r.split(".")[0];m+=`
|
|
197
199
|
curl -fsSL https://deb.nodesource.com/setup_${f}.x | bash -
|
|
@@ -252,7 +254,7 @@ ${f.join(`
|
|
|
252
254
|
`}return m+=`
|
|
253
255
|
echo "ts-cloud bootstrap complete — instance is ready for site deploys"
|
|
254
256
|
`,m}var Es="/aws/service/canonical/ubuntu/server/24.04/stable/current/amd64/hvm/ebs-gp3/ami-id";function Ds(e,t){let r=[{port:22,protocol:"tcp",cidr:"0.0.0.0/0"},{port:80,protocol:"tcp",cidr:"0.0.0.0/0"},{port:443,protocol:"tcp",cidr:"0.0.0.0/0"}],n=new Set;for(let s of Object.values(e.sites||{}))if(s&&typeof s.port==="number"&&![22,80,443].includes(s.port))n.add(s.port);for(let s of jr(ne(e,{environment:t})))n.add(s);for(let s of n)r.push({port:s,protocol:"tcp",cidr:"0.0.0.0/0"});return r}function Is(e,t){let r={...e.infrastructure?.compute,proxy:le(e,{environment:t})},n=Fe(e,{environment:t}),s=r.proxy?.engine==="rpx"?$e({proxy:r.proxy,config:Te(e.sites??{},{proxy:r.proxy,slug:e.project.slug,mail:ne(e,{environment:t})}),slug:e.project.slug,bunBin:r.runtime==="node"||r.runtime==="deno"?void 0:"/usr/local/bin/bun"}):void 0;return ye({runtime:n.runtime,runtimeVersion:n.runtimeVersion,systemPackages:r.systemPackages,database:e.infrastructure?.database,phpProvision:n.phpProvision,servicesProvision:n.servicesProvision,rpxProvision:s,baked:r.bakedImage===!0,swapGb:r.swapGb})}function As(e){return Buffer.from(e,"utf8").toString("base64")}function Ls(e){return e.infrastructure?.compute?.image??null}function Vt(e,t){return`${e}-${t}-app-sg`}function Wr(e,t,r="app"){return[{Key:"Project",Value:e},{Key:"Environment",Value:t},{Key:"Role",Value:r},{Key:"ManagedBy",Value:"ts-cloud"}]}function Pc(e,t,r){if(!e?.length)return!1;let n=(s)=>e.find((i)=>i.Key===s)?.Value;return n("Project")===t&&n("Environment")===r}function Fs(e,t){let r=Vt(t.slug,t.environment);return e.find((n)=>{if(n.GroupName!==r)return!1;if(t.vpcId&&n.VpcId&&n.VpcId!==t.vpcId)return!1;if(!!n.Tags?.some((i)=>i.Key==="Project"))return Pc(n.Tags,t.slug,t.environment);return!!t.vpcId&&n.VpcId===t.vpcId})}function Ec(e){try{let t=Bs(Rc(process.cwd(),"storage/cloud/state",`${e}.json`),"utf8"),r=JSON.parse(t);return typeof r.instanceId==="string"&&r.instanceId.length>0?r.instanceId:null}catch{return null}}class qt{name="aws";usesCloudFormation=!0;region;ec2Client;cfnClient;constructor(e={}){this.region=e.region||"us-east-1",this.ec2Client=e.ec2Client??((t)=>new Q(t)),this.cfnClient=e.cfnClient??((t)=>new v(t))}resolveRegion(e){return e.project.region||this.region}async provisionComputeInfrastructure(e){let{config:t,environment:r}=e,n=this.resolveRegion(t),s=t.project.slug,i=t.infrastructure?.compute;if(!i)throw Error("infrastructure.compute is required to provision AWS compute");let o=this.ec2Client(n),a=await this.findComputeTargets({slug:s,environment:r,role:"app",region:n});if(a.length>0){let H=a[0];return{appInstanceId:H.id,appPublicIp:H.publicIp,appPublicIpv6:H.publicIpv6,sshUser:"ubuntu",deployStoragePath:"/var/ts-cloud/staging"}}let c=Ls(t);if(!c){if(c=(await new Ze(n).getParameter({Name:Es})).Parameter?.Value??null,!c)throw Error("Could not resolve the Ubuntu 24.04 AMI from SSM")}let l=await o.describeVpcs(),u=(l.Vpcs||[]).find((H)=>H.IsDefault)||(l.Vpcs||[])[0];if(!u?.VpcId)throw Error("No VPC found to launch the instance into");let d=(await o.describeSubnets({Filters:[{Name:"vpc-id",Values:[u.VpcId]}]})).Subnets||[],m=d.find((H)=>H.MapPublicIpOnLaunch)||d[0],h=m?.SubnetId;if(!m?.MapPublicIpOnLaunch)console.warn("ts-cloud: no public subnet found; the instance may not get a public IP (deploys/SSL need one).");let f=Vt(s,r),x=(await o.describeSecurityGroups({Filters:[{Name:"group-name",Values:[f]},{Name:"vpc-id",Values:[u.VpcId]}]})).SecurityGroups?.[0]?.GroupId;if(!x)x=(await o.createSecurityGroup({GroupName:f,Description:`ts-cloud ${s}/${r} app`,VpcId:u.VpcId,TagSpecifications:[{ResourceType:"security-group",Tags:Wr(s,r)}]})).GroupId;let B=Ds(t,r);await o.authorizeSecurityGroupIngress({GroupId:x,IpPermissions:B.map((H)=>({IpProtocol:H.protocol,FromPort:H.port,ToPort:H.port,IpRanges:[{CidrIp:H.cidr}]}))}).catch((H)=>{if(!/InvalidPermission\.Duplicate/.test(H instanceof Error?H.message:""))throw H});let L=As(Is(t,r)),N=i.server?.instanceType||"t3.small",E=(await o.runInstances({ImageId:c,InstanceType:N,MinCount:1,MaxCount:1,SecurityGroupIds:x?[x]:void 0,SubnetId:h,UserData:L,IamInstanceProfile:i.server?.iamInstanceProfile?{Name:i.server.iamInstanceProfile}:void 0,TagSpecifications:[{ResourceType:"instance",Tags:[{Key:"Name",Value:`${s}-${r}-app`},...Wr(s,r)]}]})).Instances?.[0]?.InstanceId;if(!E)throw Error("RunInstances did not return an instance id");let M=await o.waitForInstanceState(E,"running");if(!M)throw Error(`Instance ${E} did not reach 'running' before timeout`);return{appInstanceId:E,appPublicIp:M.PublicIpAddress,appPublicIpv6:me(M.Ipv6Address),sshUser:"ubuntu",deployStoragePath:"/var/ts-cloud/staging"}}async destroyCompute(e){let{config:t,environment:r}=e,n=t.project.slug,s=this.resolveRegion(t),i=this.ec2Client(s),o=[],c=(await this.findComputeTargets({slug:n,environment:r,role:"app",region:s})).map((f)=>f.id),l;if(c.length>0)l=(await i.describeInstances({InstanceIds:c}).catch(()=>({Reservations:[]}))).Reservations?.flatMap((S)=>S.Instances??[]).find((S)=>S.VpcId)?.VpcId;if(c.length>0)await i.terminateInstances(c),o.push(...c.map((f)=>`instance ${f}`)),await Promise.all(c.map((f)=>i.waitForInstanceState(f,"terminated").catch(()=>{return})));let u=Vt(n,r),d=[{Name:"group-name",Values:[u]}];if(l)d.push({Name:"vpc-id",Values:[l]});let m=await i.describeSecurityGroups({Filters:d}).catch(()=>({SecurityGroups:[]})),h=Fs(m.SecurityGroups??[],{slug:n,environment:r,vpcId:l});if(h?.GroupId)for(let f=0;f<10;f++)try{await i.deleteSecurityGroup(h.GroupId),o.push(`security group ${u}`);break}catch{await new Promise((S)=>setTimeout(S,3000))}return{destroyed:o}}async getComputeOutputs(e){let t=this.resolveRegion(e.config),r=T(e.config,e.environment),n=this.cfnClient(t);try{let s=await n.getStackOutputs(r);return{deployBucketName:s.deployBucketName,appInstanceId:s.appInstanceId,appPublicIp:s.appPublicIp,sshUser:"ec2-user"}}catch(s){if(!/does not exist|ValidationError/i.test(s instanceof Error?s.message:""))throw s;let o=(await this.findComputeTargets({slug:e.config.project.slug,environment:e.environment,role:"app",region:t}))[0];return{appInstanceId:o?.id,appPublicIp:o?.publicIp,appPublicIpv6:o?.publicIpv6,sshUser:"ubuntu",deployStoragePath:"/var/ts-cloud/staging"}}}async uploadRelease(e){let t=this.resolveRegion(e.config),n=(await this.getComputeOutputs({config:e.config,environment:e.environment})).deployBucketName;if(!n)throw Error("No deployBucketName in stack outputs. Re-deploy infrastructure to add the staging bucket.");return await new g(t).putObject({bucket:n,key:e.remoteKey,body:Bs(e.localPath),contentType:"application/gzip"}),{artifactRef:`s3://${n}/${e.remoteKey}`}}async findComputeTargets(e){let t=e.region??this.region,r=this.ec2Client(t),n=[{Name:"tag:Project",Values:[e.slug]},{Name:"tag:Environment",Values:[e.environment]},{Name:"tag:Role",Values:[e.role||"app"]},{Name:"instance-state-name",Values:["running","pending"]}],s=await r.describeInstances({Filters:n}),i=this.reservationsToTargets(s.Reservations);if(i.length>0||(e.role||"app")!=="app")return i;let o=e.stackName??`${e.slug}-${e.environment}`,a=Ec(o);if(!a)try{a=(await this.cfnClient(t).getStackOutputs(o)).appInstanceId??null}catch{a=null}if(!a)return[];try{let c=await r.describeInstances({InstanceIds:[a],Filters:[{Name:"instance-state-name",Values:["running","pending"]}]});return this.reservationsToTargets(c.Reservations)}catch{return[]}}reservationsToTargets(e){let t=[];for(let r of e||[])for(let n of r.Instances||[]){if(!n.InstanceId)continue;let s=n.Tags?.find((i)=>i.Key==="Name")?.Value;t.push({id:n.InstanceId,name:s,publicIp:n.PublicIpAddress,publicIpv6:me(n.Ipv6Address),privateIp:n.PrivateIpAddress,status:n.State?.Name})}return t}bashWrap(e){return["bash <<'TS_CLOUD_BASH_EOF'",e.join(`
|
|
255
|
-
`),"TS_CLOUD_BASH_EOF"]}async runRemoteDeploy(e){let t=this.region,r=new Ze(t);if(e.targets.length>0){let s=await r.sendCommand({InstanceIds:e.targets.map((i)=>i.id),DocumentName:"AWS-RunShellScript",Parameters:{commands:this.bashWrap(e.commands)},TimeoutSeconds:e.timeoutSeconds||600,Comment:e.comment});if(!s.CommandId)return{success:!1,instanceCount:0,perInstance:[],error:"Failed to send SSM command"};return this.pollSsmCommand(r,s.CommandId,e.targets.length,((e.timeoutSeconds||600)+30)*1000)}if(!e.tags||Object.keys(e.tags).length===0)return{success:!1,instanceCount:0,perInstance:[],error:"No targets or tags provided for AWS deploy"};let n=await r.sendCommandByTags({tags:e.tags,commands:this.bashWrap(e.commands),timeoutSeconds:e.timeoutSeconds||600,comment:e.comment});return{success:n.success,instanceCount:n.instanceCount,perInstance:n.perInstance.map((s)=>({instanceId:s.instanceId,status:s.status,output:s.output,error:s.error})),error:n.error}}async pollSsmCommand(e,t,r,n=600000){let i=Date.now(),o=new Set(["Success","Failed","Cancelled","TimedOut"]),a=[];while(Date.now()-i<n){await new Promise((u)=>setTimeout(u,3000));try{if(a=await e.listCommandInvocations({CommandId:t,Details:!0}),a.length>=r&&a.every((d)=>o.has(d.Status||"")))break}catch{}}let c=a.map((u)=>({instanceId:u.InstanceId,status:u.Status||"Unknown",output:u.StandardOutputContent,error:u.StandardErrorContent})),l=c.length>0&&c.every((u)=>u.status==="Success");return{success:l,instanceCount:c.length,perInstance:c,error:l?void 0:Me(c,"One or more SSM command invocations failed")}}}import{execFileSync as
|
|
257
|
+
`),"TS_CLOUD_BASH_EOF"]}async runRemoteDeploy(e){let t=this.region,r=new Ze(t);if(e.targets.length>0){let s=await r.sendCommand({InstanceIds:e.targets.map((i)=>i.id),DocumentName:"AWS-RunShellScript",Parameters:{commands:this.bashWrap(e.commands)},TimeoutSeconds:e.timeoutSeconds||600,Comment:e.comment});if(!s.CommandId)return{success:!1,instanceCount:0,perInstance:[],error:"Failed to send SSM command"};return this.pollSsmCommand(r,s.CommandId,e.targets.length,((e.timeoutSeconds||600)+30)*1000)}if(!e.tags||Object.keys(e.tags).length===0)return{success:!1,instanceCount:0,perInstance:[],error:"No targets or tags provided for AWS deploy"};let n=await r.sendCommandByTags({tags:e.tags,commands:this.bashWrap(e.commands),timeoutSeconds:e.timeoutSeconds||600,comment:e.comment});return{success:n.success,instanceCount:n.instanceCount,perInstance:n.perInstance.map((s)=>({instanceId:s.instanceId,status:s.status,output:s.output,error:s.error})),error:n.error}}async pollSsmCommand(e,t,r,n=600000){let i=Date.now(),o=new Set(["Success","Failed","Cancelled","TimedOut"]),a=[];while(Date.now()-i<n){await new Promise((u)=>setTimeout(u,3000));try{if(a=await e.listCommandInvocations({CommandId:t,Details:!0}),a.length>=r&&a.every((d)=>o.has(d.Status||"")))break}catch{}}let c=a.map((u)=>({instanceId:u.InstanceId,status:u.Status||"Unknown",output:u.StandardOutputContent,error:u.StandardErrorContent})),l=c.length>0&&c.every((u)=>u.status==="Success");return{success:l,instanceCount:c.length,perInstance:c,error:l?void 0:Me(c,"One or more SSM command invocations failed")}}}import{execFileSync as fl,execSync as vi}from"node:child_process";import{createHash as gl,randomUUID as bi}from"node:crypto";import{createReadStream as hl,existsSync as yl,readFileSync as Sl}from"node:fs";async function js(e,t){let r=await import("node:fs"),n=await import("node:path"),s=await import("node:child_process"),i="/etc/ts-cloud/compute-storage.json",o="/var/lib/ts-cloud/compute-storage-state.json",a="/run/ts-cloud-root-web",c=t[0]??"status";if(!["plan","status","check","apply","ensure-site","reclaim"].includes(c))throw Error(`Unknown compute storage command: ${c}`);let l=(y,k)=>{try{return JSON.parse(r.readFileSync(y,"utf8"))}catch(b){if(b.code==="ENOENT")return k;throw b}},u=(y,k)=>{r.mkdirSync(n.dirname(y),{recursive:!0});let b=`${y}.${process.pid}.tmp`;r.writeFileSync(b,`${JSON.stringify(k,null,2)}
|
|
256
258
|
`,{mode:420}),r.renameSync(b,y)},d=(y,k)=>s.execFileSync(y,k,{encoding:"utf8",maxBuffer:16777216}),m=(y,k)=>{let b=s.spawnSync(y,k,{encoding:"utf8"});return b.status===0?b.stdout.trim():""},h=(y)=>!!m("findmnt",["-n","--mountpoint",y]),f=(y,k)=>{try{let b=r.statSync(y),C=r.statSync(k);return b.dev===C.dev&&b.ino===C.ino}catch{return!1}},S=l("/var/lib/ts-cloud/compute-storage-state.json",{entries:[]}),x=e.deploymentStorage,B=e.volumes.find((y)=>y.key===x?.volume),L=(y,k)=>{if(!B)return[];let b=[];for(let C of r.readdirSync(e.webRoot,{withFileTypes:!0})){if(!C.isDirectory()||C.isSymbolicLink())continue;let P=n.join(e.webRoot,C.name);if(y&&P!==y&&P!==n.join(e.webRoot,`${k}-shared`))continue;for(let G of["releases",...x?.includeBackups===!1?[]:["backups"]]){let K=n.join(P,G);if(!r.existsSync(K)||!r.lstatSync(K).isDirectory())continue;b.push({source:K,destination:n.join(B.mountPath,G,C.name)})}}return b},N=(y)=>{try{return h(y.mountPath)&&r.statSync(y.mountPath).dev===r.statSync(y.device).rdev}catch{return!1}},D=(y)=>{let k=r.statfsSync(y),b=(k.blocks-k.bfree)*k.bsize,C=k.bavail*k.bsize;return{path:y,sizeBytes:k.blocks*k.bsize,usedBytes:b,availableBytes:C,usedPercent:Math.ceil(100*b/(b+C))}},E=()=>{let y=[],k=[D("/")];for(let C of e.volumes)if(!N(C))y.push(`missing or incorrect volume mount: ${C.mountPath}`);else k.push(D(C.mountPath));for(let C of S.entries)if(!h(C.source)||!f(C.source,C.destination))y.push(`missing deployment mount: ${C.source}`);let b=new Set(S.entries.map((C)=>C.source));for(let C of L())if(!b.has(C.source))y.push(`deployment needs volume placement: ${C.source}`);return{timestamp:new Date().toISOString(),volumes:e.volumes.map((C)=>({key:C.key,name:C.name,id:C.id,size:C.size,mountPath:C.mountPath})),disks:k,mounts:S.entries.length,problems:y}};if(c==="plan")return console.log(JSON.stringify({volumes:e.volumes,entries:L(),state:S},null,2));if(c==="status"||c==="check"){let y=E();if(c==="check"){u("/var/lib/ts-cloud/compute-storage-status.json",y);let k="/var/lib/ts-cloud/compute-storage-alert.json",b=l(k,{problems:[]});if(JSON.stringify(b.problems)!==JSON.stringify(y.problems)){let C=y.problems.join("; ")||"all configured mounts are healthy",P=`${y.problems.length?"Storage alert":"Storage recovered"} on ${m("hostname",[])}: ${C}`;d("/usr/local/bin/ts-cloud-notify",[P.length>1800?`${P.slice(0,1600)}... Full report: /var/lib/ts-cloud/compute-storage-status.json`:P])}u(k,{problems:y.problems,timestamp:y.timestamp})}if(console.log(JSON.stringify(y)),y.problems.length)process.exitCode=1;return y}if(process.getuid?.()!==0)throw Error("Compute storage changes require root");let M=()=>{let C=r.readFileSync("/etc/fstab","utf8");if(!r.existsSync("/etc/fstab.before-ts-cloud-storage"))r.copyFileSync("/etc/fstab","/etc/fstab.before-ts-cloud-storage");let P=[];for(let j of e.volumes){let q=d("blkid",["-s","UUID","-o","value",j.device]).trim();if(!q)throw Error(`Volume ${j.name} has no filesystem UUID`);P.push(`UUID=${q} ${j.mountPath} ext4 defaults,noatime,nodev,nosuid,nofail,x-systemd.device-timeout=30s 0 2`)}for(let j of S.entries)P.push(`${j.destination} ${j.source} none bind,nofail,x-systemd.requires-mounts-for=${B.mountPath} 0 0`);let G=`# BEGIN ts-cloud compute storage
|
|
257
259
|
${P.join(`
|
|
258
260
|
`)}
|
|
@@ -267,15 +269,15 @@ RequiresMountsFor=${y.join(" ")}
|
|
|
267
269
|
`)}d("systemctl",["daemon-reload"])};try{let y=[...new Set([...x?.locks??[],"/run/lock/ts-cloud-host-cleanup.lock"])];if(c==="reclaim"){for(let _ of["/run/lock/ts-cloud-compute-storage.lock",...y])if(!te(_,!1))return console.log(`Deferring deployment storage reclamation; lock busy: ${_}`),{deferred:!0,lock:_}}else te("/run/lock/ts-cloud-compute-storage.lock");S=l("/var/lib/ts-cloud/compute-storage-state.json",{entries:[]});let k=l("/etc/ts-cloud/compute-storage.json",null);if(k){for(let _ of k.volumes)if(!e.volumes.some((z)=>z.id===_.id&&z.key===_.key&&z.mountPath===_.mountPath))throw Error(`Refusing to remove or relocate managed volume ${_.name}; migrate its data explicitly first`);if(S.entries.length&&(k.deploymentStorage?.volume!==x?.volume||k.deploymentStorage?.includeBackups!==!1!==(x?.includeBackups!==!1)))throw Error("Refusing to change an active deployment-storage policy; migrate its data explicitly first")}if(c==="apply")H();for(let _ of e.volumes)if(!N(_))throw Error(`Required volume is not mounted: ${_.name}`);if(!x||!B)return{volumes:e.volumes};if(c==="ensure-site"){let[_,z]=t.slice(1);if(!_?.startsWith(`${e.webRoot}/`)||!/^[\w-]+$/.test(n.basename(_))||_.includes("..")||!/^[\w-]+$/.test(z??""))throw Error("Invalid deployment site path or project slug");if(r.mkdirSync(n.join(_,"releases"),{recursive:!0}),x.includeBackups!==!1)r.mkdirSync(n.join(e.webRoot,`${z}-shared`,"backups"),{recursive:!0})}let b=(c==="reclaim"?[]:L(c==="ensure-site"?t[1]:void 0,t[2])).filter((_)=>!S.entries.some((z)=>z.source===_.source));for(let _ of S.entries)if(!h(_.source)||!f(_.source,_.destination))throw Error(`Recorded deployment mount is missing: ${_.source}`);if(!b.length&&c==="ensure-site")return{inherited:!0,volume:B.name};if(!b.length&&c==="apply"){ce();let _=E();return u("/var/lib/ts-cloud/compute-storage-status.json",_),console.log(JSON.stringify(_)),_}if(!x.migrateExisting&&b.some((_)=>r.readdirSync(_.source).length>0))throw Error("Existing deployments require deploymentStorage.migrateExisting: true");if(b.length){let _=d("du",["-scx","--block-size=1",...b.map((ue)=>ue.source)]).trim().split(`
|
|
268
270
|
`).at(-1)?.split(/\s+/)[0],z=r.statfsSync(B.mountPath);if(Number(_)>z.bavail*z.bsize)throw Error("Insufficient free volume capacity for a verified deployment copy")}if(!r.existsSync("/run/ts-cloud-root-web"))r.mkdirSync("/run/ts-cloud-root-web");if(!h("/run/ts-cloud-root-web"))d("mount",["--bind",e.webRoot,"/run/ts-cloud-root-web"]);d("mount",["--make-private","/run/ts-cloud-root-web"]);let C=r.statSync(e.webRoot).dev;for(let _ of S.entries){let z=n.join("/run/ts-cloud-root-web",n.relative(e.webRoot,_.source));if(r.existsSync(z)&&(r.statSync(z).dev!==C||f(z,_.destination)))throw Error(`Root recovery view contains a deployment mount: ${z}`)}for(let _=0;_<b.length;_++){if(h(b[_].source)&&f(b[_].source,b[_].destination))continue;console.log(`copy ${_+1}/${b.length}: ${b[_].source}`),await J(b[_],!1)}if(c!=="reclaim")for(let _ of y)te(_);for(let _ of r.readdirSync(e.webRoot,{withFileTypes:!0}).filter((z)=>z.isDirectory()).sort((z,ue)=>z.name.localeCompare(ue.name)))te(n.join(e.webRoot,_.name,".ts-cloud/deploy.lock"));for(let _ of c==="reclaim"?[]:L(c==="ensure-site"?t[1]:void 0,t[2]))if(!S.entries.some((z)=>z.source===_.source)&&!b.some((z)=>z.source===_.source))b.push(_);if(b.length)console.log("Checking application services and HTTPS routes before activation");let P=b.length?await ie():void 0;if(P)u("/var/lib/ts-cloud/compute-storage-health-before.json",P);let G=Z().filter((_)=>_.write);if(G.length)throw Error(`Live writable files must be shared before migration: ${G.map((_)=>_.file).join(", ")}`);let K={entries:[...S.entries],fstab:r.readFileSync("/etc/fstab","utf8")};if(b.length)d("sync",[]);let j=[];try{for(let _=0;_<b.length;_++){let z=b[_];if(h(z.source)){if(!f(z.source,z.destination))throw Error(`Unrecorded mount occupies ${z.source}`);S.entries.push(z),u("/var/lib/ts-cloud/compute-storage-state.json",S);continue}if(console.log(`verify and mount ${_+1}/${b.length}: ${z.source}`),await J(z,!0),d("mount",["--bind",z.destination,z.source]),!f(z.source,z.destination))throw Error(`Bind verification failed: ${z.source}`);j.push(z),S.entries.push(z),u("/var/lib/ts-cloud/compute-storage-state.json",S),M()}if(M(),d("findmnt",["--verify"]),S.activatedAt=new Date().toISOString(),u("/var/lib/ts-cloud/compute-storage-state.json",S),ce(),P){console.log("Checking application services and HTTPS routes after activation");let _=await ie();u("/var/lib/ts-cloud/compute-storage-health-after.json",_);let z=P.routes.filter((re)=>re.ok&&!_.routes.some((fe)=>fe.domain===re.domain&&fe.path===re.path&&fe.ok)),ue=Object.keys(P.services).filter((re)=>!_.services[re]);if(z.length||ue.length)throw Error(`Application verification failed: ${z.map((re)=>re.domain+re.path).concat(ue).join(", ")}`);console.log(`Verified ${_.routes.length} routes and ${Object.keys(_.services).length} services`)}}catch(_){let z=[];for(let ue of j.reverse())try{d("umount",[ue.source])}catch{z.push(ue),console.error(`Rollback mount busy, original data retained: ${ue.source}`)}if(S.entries=[...K.entries,...z],r.writeFileSync("/etc/fstab",K.fstab),z.length)M();throw u("/var/lib/ts-cloud/compute-storage-state.json",S),d("systemctl",["daemon-reload"]),_}let q=Z().filter((_)=>_.device===C).map((_)=>_.file);for(let _ of S.entries){if(!h(_.source)||!f(_.source,_.destination))throw Error(`Lost deployment mount: ${_.source}`);let z=n.join("/run/ts-cloud-root-web",n.relative(e.webRoot,_.source));if(!r.existsSync(z))continue;if(r.statSync(z).dev!==C||f(z,_.destination))throw Error(`Refusing to clean a deployment volume through the root recovery view: ${z}`);let ue="";try{ue=n.basename(r.realpathSync(n.join(n.dirname(_.source),"current")))}catch{}for(let re of r.readdirSync(z,{withFileTypes:!0})){let fe=n.join(z,re.name);if(n.basename(_.source)==="backups"){if(re.isFile()&&r.existsSync(n.join(_.destination,re.name)))r.unlinkSync(fe)}else if(re.isDirectory()&&!re.isSymbolicLink()&&re.name!==ue&&!q.some((_t)=>_t===n.join(_.source,re.name)||_t.startsWith(`${n.join(_.source,re.name)}/`)))r.rmSync(fe,{recursive:!0})}}d("sync",[]);let se=E();return u("/var/lib/ts-cloud/compute-storage-status.json",se),console.log(JSON.stringify(se)),se}finally{for(let y of Y.reverse())V.symbols.flock(y,8),r.closeSync(y);V.close()}}var qr="/etc/ts-cloud/compute-storage.json",De="/usr/local/lib/ts-cloud/compute-storage.mjs";function Xr(e,t){let r=Object.entries(e.infrastructure?.compute?.volumes??{}).map(([s,i])=>{if(!/^[a-z][a-z0-9-]*$/.test(s))throw Error(`Invalid compute volume key: ${s}`);if(!Number.isInteger(i.size)||i.size<10||i.size>10240)throw Error(`compute.volumes.${s}.size must be a whole number from 10 to 10240 GB`);if(!i.mountPath.startsWith("/")||!i.mountPath.slice(1).split("/").every((a)=>/^[\w-]+$/.test(a))||i.mountPath==="/var"||i.mountPath==="/var/lib"||["/etc","/usr","/var/www","/root","/boot","/dev","/proc","/sys","/run","/bin","/sbin","/lib"].some((a)=>i.mountPath===a||i.mountPath.startsWith(`${a}/`)))throw Error(`Unsafe compute volume mount path: ${i.mountPath}`);let o=i.name??`${e.project.slug}-${t}-${s}`;if(!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/.test(o))throw Error(`Invalid compute volume name: ${o}`);return{...i,key:s,name:o,mountPath:i.mountPath.replace(/\/+$/,"")}});for(let s of r)if(r.some((i)=>i!==s&&(i.name===s.name||i.mountPath===s.mountPath||i.mountPath.startsWith(`${s.mountPath}/`))))throw Error("Compute volumes must have distinct names and non-overlapping mount paths");let n=e.infrastructure?.compute?.deploymentStorage;for(let s of n?.locks??[])if(!s.startsWith("/run/lock/")||!s.slice(10).split("/").every((i)=>/^[\w.-]+$/.test(i)&&i!=="."&&i!=="..")||s==="/run/lock/ts-cloud-compute-storage.lock")throw Error(`Invalid deployment storage lock: ${s}`);for(let s of n?.dependentUnits??[])if(!/^[\w@.-]+\.service$/.test(s))throw Error(`Invalid deployment storage dependent unit: ${s}`);if(n&&!r.some((s)=>s.key===n.volume))throw Error(`deploymentStorage.volume references an undeclared compute volume: ${n.volume}`);return r}function Xt(e){return`'${e.replaceAll("'",`'"'"'`)}'`}function Yr(e){let t=`${qr}.candidate`,r=`#!/usr/bin/env bun
|
|
269
271
|
await (${js.toString()})(JSON.parse(await Bun.file(process.env.TS_CLOUD_STORAGE_POLICY || ${JSON.stringify(qr)}).text()), process.argv.slice(2));
|
|
270
|
-
`;return['command -v bun >/dev/null 2>&1 || { echo "Configured compute storage requires Bun on the host" >&2; exit 1; }',"install -d -m 0755 /etc/ts-cloud /usr/local/lib/ts-cloud /var/lib/ts-cloud",`printf %s ${Xt(Buffer.from(r).toString("base64"))} | base64 -d > ${De}.new`,`chmod 0755 ${De}.new && mv ${De}.new ${De}`,`printf %s ${Xt(JSON.stringify(e))} > ${t}`,`chmod 0644 ${t}`,`TS_CLOUD_STORAGE_POLICY=${t} bun ${De} apply 2>&1 | tee /var/log/ts-cloud-compute-storage.log`,`rm -f ${t}`,"cat > /etc/systemd/system/ts-cloud-storage-check.service <<'TS_CLOUD_STORAGE_SERVICE'","[Unit]","Description=Check configured compute storage and deployment mounts","[Service]","Type=oneshot",`ExecStart=/usr/local/bin/bun ${De} check`,"TS_CLOUD_STORAGE_SERVICE","cat > /etc/systemd/system/ts-cloud-storage-check.timer <<'TS_CLOUD_STORAGE_TIMER'","[Unit]","Description=Check compute storage every minute","[Timer]","OnBootSec=2min","OnUnitActiveSec=1min","[Install]","WantedBy=timers.target","TS_CLOUD_STORAGE_TIMER","systemctl daemon-reload","systemctl enable --now ts-cloud-storage-check.timer","cat > /etc/systemd/system/ts-cloud-storage-reclaim.service <<'TS_CLOUD_RECLAIM_SERVICE'","[Unit]","Description=Reclaim inactive root copies of volume-backed deployments","[Service]","Type=oneshot",`ExecStart=/usr/local/bin/bun ${De} reclaim`,"Nice=19","IOSchedulingClass=idle","TS_CLOUD_RECLAIM_SERVICE","cat > /etc/systemd/system/ts-cloud-storage-reclaim.timer <<'TS_CLOUD_RECLAIM_TIMER'","[Unit]","Description=Reclaim retired deployment copies hourly","[Timer]","OnCalendar=hourly","RandomizedDelaySec=5min","Persistent=true","[Install]","WantedBy=timers.target","TS_CLOUD_RECLAIM_TIMER","systemctl daemon-reload","systemctl enable --now ts-cloud-storage-reclaim.timer"]}function Yt(e,t){return[`if [ -f ${qr} ]; then bun ${De} ensure-site ${Xt(e)} ${Xt(t)}; fi`]}import{createHash as xc}from"node:crypto";function st(e){if(!/^[a-z0-9][a-z0-9-]*$/.test(e))throw Error("Runtime isolation requires a safe project slug");return`tsc-${e.length<=24?e:`${e.slice(0,15)}-${xc("sha256").update(e).digest("hex").slice(0,8)}`}`}function xt(e,t,r){return[`User=${e}`,`Group=${e}`,"NoNewPrivileges=true","PrivateTmp=true","ProtectHome=true","ProtectProc=invisible","InaccessiblePaths=-/run/postgresql -/var/run/postgresql","TemporaryFileSystem=/var/www:ro",`BindPaths=${t} -/var/www/${r}-shared`,"UMask=0077",`Environment=HOME=/var/lib/ts-cloud/${e}`]}function Xs(e,t,r){let n=st(e);if(!t.startsWith(`/var/www/${e}-`)||t.slice(9).includes("/"))throw Error("Runtime isolation requires a project-namespaced application directory");let s=`/var/lib/ts-cloud/${n}`,i=`/var/www/${e}-shared`,o=[...new Set([t,...r])];for(let a of o)if(!/^\/[a-zA-Z0-9_./-]+$/.test(a)||a.split("/").includes("..")||!(a===t||a.startsWith(`${t}/`)||a===i||a.startsWith(`${i}/`)))throw Error(`Runtime isolation cannot take ownership outside project ${e}`);return[
|
|
271
|
-
`)}function Oc(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,"\\\"").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}"`}var Dc=new Set(["sqlite","sqlite3"]),Ic=new Set([":memory:","memory"]);function Ac(e){let t=e?.DB_CONNECTION?.trim().toLowerCase();return t!=null&&Dc.has(t)}function Lc(e){let t=e.trim().replace(/^\.\//,"");if(t===""||t.startsWith("/")||t.startsWith("~"))return;if(t.split("/").includes(".."))return;return t.replace(/\/+$/,"")}function Jr(e,t=[]){if(!Ac(e))return{};let r=e?.DB_DATABASE_PATH?.trim()||e?.DB_DATABASE?.trim();if(r&&Ic.has(r.toLowerCase()))return{};if(!r)return{warning:"DB_CONNECTION is sqlite but neither DB_DATABASE_PATH nor DB_DATABASE names a file, so ts-cloud cannot tell where the database lives. If it is written inside the release directory, the next deploy discards it. Set DB_DATABASE_PATH, or list the file in the site's `sharedPaths`."};let n=Lc(r);if(!n)return{};if(t.some((s)=>Vs(s)===n))return{};return{path:n}}function Zt(e,t=[]){let{path:r}=Jr(e,t);return r?[r]:[]}var ot="/var/ts-cloud/artifacts";var ni=Object.freeze({stagingMaxAgeMinutes:60,artifactMaxAgeDays:2,artifactUploadMaxAgeMinutes:60,bunCacheMaxAgeDays:7,journalMaxAgeDays:14,journalMaxSizeMb:512,containerImageMaxAgeHours:168}),si=Object.freeze({artifactMaxAgeDays:0,bunCacheMaxAgeDays:1,journalMaxAgeDays:3,journalMaxSizeMb:256,containerImageMaxAgeHours:24});function Js(e){return`'${e.replace(/'/g,`'"'"'`)}'`}function Be(e,t,r=0){if(!Number.isInteger(t)||t<r)throw Error(`hostCleanup: ${e} must be a whole number >= ${r} (got ${t})`)}function Zs(e,t){for(let[r,n]of Object.entries(t))Be(`${e}.${r}`,n);Be(`${e}.stagingMaxAgeMinutes`,t.stagingMaxAgeMinutes,15),Be(`${e}.artifactUploadMaxAgeMinutes`,t.artifactUploadMaxAgeMinutes,15),Be(`${e}.journalMaxSizeMb`,t.journalMaxSizeMb,1)}function kc(e,t){let r=`hostCleanup.paths[${t}]`,n=e.path?.replace(/\/+$/,"")??"";if(!n.startsWith("/")||n===""||/[\s'"`$\\]/.test(n)||n.split("/").includes(".."))throw Error(`${r}.path must be an absolute directory with no spaces, quotes or '..' (got ${JSON.stringify(e.path)})`);if(n==="/var/www"||n.startsWith("/var/www/"))throw Error(`${r}.path must not be under /var/www: releases are pruned by keepReleases`);if(!e.pattern||/^[*?]+$/.test(e.pattern)||e.pattern.includes("/")||/[\n'"`]/.test(e.pattern))throw Error(`${r}.pattern must be a file-name glob naming something, not a bare wildcard (got ${JSON.stringify(e.pattern)})`);if(Be(`${r}.maxAgeDays`,e.maxAgeDays),e.type!==void 0&&e.type!=="file"&&e.type!=="directory")throw Error(`${r}.type must be 'file' or 'directory'`)}function Qt(e){let t=typeof e==="object"&&e!==null?e:{},r=e!==!1,n={...ni,...Qs(t.retention)};Zs("hostCleanup.retention",n);let s=null;if(t.pressure!==!1){let a=typeof t.pressure==="object"?t.pressure:{},c=a.lowWaterPercent??50,l=a.highWaterPercent??85;if(Be("hostCleanup.pressure.lowWaterPercent",c),Be("hostCleanup.pressure.highWaterPercent",l,1),l>100||c>=l)throw Error(`hostCleanup.pressure: need 0 <= lowWaterPercent < highWaterPercent <= 100 (got ${c} and ${l})`);let u={...si,...Qs(a.escalated)},d=Object.fromEntries(Object.keys(n).map((m)=>[m,Math.min(n[m],u[m]??n[m])]));Zs("hostCleanup.pressure.escalated",d),s={lowWaterPercent:c,highWaterPercent:l,escalated:d}}let i=t.schedule??"daily";if(!/^[\w*:,./~ -]+$/.test(i))throw Error(`hostCleanup.schedule must be a systemd OnCalendar expression (got ${JSON.stringify(i)})`);let o=t.paths??[];return o.forEach(kc),{enabled:r,timer:r&&t.timer!==!1,schedule:i,retention:n,pressure:s,paths:o}}function Qs(e){return Object.fromEntries(Object.entries(e??{}).filter(([,t])=>t!==void 0))}var Ot={stagingMaxAgeMinutes:"TS_CLOUD_HC_STAGING_MIN",artifactMaxAgeDays:"TS_CLOUD_HC_ARTIFACT_DAYS",artifactUploadMaxAgeMinutes:"TS_CLOUD_HC_UPLOAD_MIN",bunCacheMaxAgeDays:"TS_CLOUD_HC_BUN_DAYS",journalMaxAgeDays:"TS_CLOUD_HC_JOURNAL_DAYS",journalMaxSizeMb:"TS_CLOUD_HC_JOURNAL_MB",containerImageMaxAgeHours:"TS_CLOUD_HC_IMAGE_HOURS"};function Zr(e){return Object.keys(Ot).map((t)=>`${Ot[t]}=${e[t]}`).join(" ")}var Ie=(e)=>`"$${Ot[e]}"`;function ei(e){return[`find /var/ts-cloud/staging -xdev -maxdepth 1 -type f -mmin +${Ie("stagingMaxAgeMinutes")} -delete 2>/dev/null || true`,`find /tmp -xdev -maxdepth 1 -type f -name "*-release.tar.gz" -mmin +${Ie("stagingMaxAgeMinutes")} -delete 2>/dev/null || true`,`find /var/ts-cloud/artifacts -xdev -maxdepth 1 -type f -name "*.tar.gz" -mtime +${Ie("artifactMaxAgeDays")} -delete 2>/dev/null || true`,`find /var/ts-cloud/artifacts -xdev -maxdepth 1 -type f -name ".*.tmp" -mmin +${Ie("artifactUploadMaxAgeMinutes")} -delete 2>/dev/null || true`,...e.map(Nc),`journalctl --vacuum-time=${Ie("journalMaxAgeDays")}d --vacuum-size=${Ie("journalMaxSizeMb")}M >/dev/null 2>&1 || true`]}function Nc(e){let t=Js(e.path.replace(/\/+$/,"")),r=Js(e.pattern);return e.type==="directory"?`find ${t} -xdev -mindepth 1 -maxdepth 1 -type d -name ${r} -mtime +${e.maxAgeDays} -exec rm -rf -- {} + 2>/dev/null || true`:`find ${t} -xdev -mindepth 1 -maxdepth 1 -type f -name ${r} -mtime +${e.maxAgeDays} -delete 2>/dev/null || true`}function ti(){return[`find /root/.bun/install/cache -xdev -type f -mtime +${Ie("bunCacheMaxAgeDays")} -delete 2>/dev/null || true`,`find /root/.bun/install/cache -xdev -depth -type d -empty -mtime +${Ie("bunCacheMaxAgeDays")} -delete 2>/dev/null || true`,`if command -v docker >/dev/null 2>&1; then docker image prune --all --force --filter "until=\${${Ot.containerImageMaxAgeHours}}h" >/dev/null 2>&1 || true; fi`,`if command -v podman >/dev/null 2>&1; then podman image prune --all --force --filter "until=\${${Ot.containerImageMaxAgeHours}}h" >/dev/null 2>&1 || true; fi`,"if command -v apt-get >/dev/null 2>&1; then apt-get clean >/dev/null 2>&1 || true; fi"]}function ri(e){return[`${e}="$(df -P / 2>/dev/null | awk 'NR==2 {sub(/%/, "", $5); print $5}' || true)"`,`case "$${e}" in ''|*[!0-9]*) ${e}=-1 ;; esac`]}function en(e){let{retention:t,pressure:r,paths:n}=Qt(e),s='echo "[ts-cloud] host cleanup (disk before): $(df -h / | tail -1)"',i='echo "[ts-cloud] host cleanup (disk after): $(df -h / | tail -1)"';if(!r)return['echo "[ts-cloud] host cleanup (disk before): $(df -h / | tail -1)"',Zr(t),...ei(n),...ti(),'echo "[ts-cloud] host cleanup (disk after): $(df -h / | tail -1)"'];let{lowWaterPercent:o,highWaterPercent:a,escalated:c}=r;return['echo "[ts-cloud] host cleanup (disk before): $(df -h / | tail -1)"',...ri("TS_CLOUD_HC_BEFORE"),"TS_CLOUD_HC_TIER=normal",Zr(t),`if [ "$TS_CLOUD_HC_BEFORE" -ge 0 ] && [ "$TS_CLOUD_HC_BEFORE" -lt ${o} ]; then`," TS_CLOUD_HC_TIER=light",` echo "[ts-cloud] host cleanup: / at \${TS_CLOUD_HC_BEFORE}% (under ${o}%), skipping cache and image pruning"`,`elif [ "$TS_CLOUD_HC_BEFORE" -ge ${a} ]; then`," TS_CLOUD_HC_TIER=pressure",` ${Zr(c)}`,` echo "[ts-cloud] warning: / at \${TS_CLOUD_HC_BEFORE}% (at or above ${a}%), host cleanup is using shortened retention windows" >&2`,"fi",...ei(n),'if [ "$TS_CLOUD_HC_TIER" != light ]; then',...ti().map((l)=>` ${l}`),"fi",'echo "[ts-cloud] host cleanup (disk after): $(df -h / | tail -1)"',...ri("TS_CLOUD_HC_AFTER"),`echo "[ts-cloud] host-cleanup {\\"event\\":\\"host-cleanup\\",\\"tier\\":\\"$TS_CLOUD_HC_TIER\\",\\"diskBeforePercent\\":$TS_CLOUD_HC_BEFORE,\\"diskAfterPercent\\":$TS_CLOUD_HC_AFTER,\\"lowWaterPercent\\":${o},\\"highWaterPercent\\":${a}}"`,"mkdir -p /var/lib/ts-cloud 2>/dev/null || true",'TS_CLOUD_HC_PREV="$(cat /var/lib/ts-cloud/host-cleanup.alert 2>/dev/null || echo ok)"',`if [ "$TS_CLOUD_HC_AFTER" -ge ${a} ]; then`,` echo "[ts-cloud] warning: / still at \${TS_CLOUD_HC_AFTER}% after host cleanup (high-water ${a}%); something outside ts-cloud's retention rules is filling the disk" >&2`,` if [ "$TS_CLOUD_HC_PREV" != alert ] && [ -x /usr/local/bin/ts-cloud-notify ]; then /usr/local/bin/ts-cloud-notify "⚠️ $(hostname): / at \${TS_CLOUD_HC_AFTER}% after host cleanup (high-water ${a}%)" >/dev/null 2>&1 || true; fi`," echo alert > /var/lib/ts-cloud/host-cleanup.alert 2>/dev/null || true","else",` if [ "$TS_CLOUD_HC_PREV" = alert ] && [ -x /usr/local/bin/ts-cloud-notify ]; then /usr/local/bin/ts-cloud-notify "✅ $(hostname): / back under ${a}% after host cleanup (\${TS_CLOUD_HC_AFTER}%)" >/dev/null 2>&1 || true; fi`," echo ok > /var/lib/ts-cloud/host-cleanup.alert 2>/dev/null || true","fi"]}function ii(e){return["#!/usr/bin/env bash","# Managed by ts-cloud: rewritten on every deploy. Configure compute.hostCleanup instead of editing.","set -uo pipefail","TS_CLOUD_HC_LOCK=/run/lock/ts-cloud-host-cleanup.lock","[ -d /run/lock ] || TS_CLOUD_HC_LOCK=/tmp/ts-cloud-host-cleanup.lock",'if command -v flock >/dev/null 2>&1 && exec 9>"$TS_CLOUD_HC_LOCK"; then',' if ! flock -n 9; then echo "[ts-cloud] host cleanup already running; skipping"; exit 0; fi',"fi",...en(e),"exit 0"]}function Qr(e,t,r,n,s){return[`cat > ${e}.ts-cloud-new <<'${r}' || true`,...t,r,`if [ -s ${e}.ts-cloud-new ] && ! cmp -s ${e}.ts-cloud-new ${e}; then chmod ${n} ${e}.ts-cloud-new && mv -f ${e}.ts-cloud-new ${e}${s?` && ${s}=1`:""} || true; else rm -f ${e}.ts-cloud-new; fi`]}function oi(e){let t=Qt(e),r="/etc/systemd/system/ts-cloud-host-cleanup",n=["if [ -f /etc/systemd/system/ts-cloud-host-cleanup.timer ]; then"," systemctl disable --now ts-cloud-host-cleanup.timer >/dev/null 2>&1 || true"," rm -f /etc/systemd/system/ts-cloud-host-cleanup.timer /etc/systemd/system/ts-cloud-host-cleanup.service"," systemctl daemon-reload >/dev/null 2>&1 || true","fi"];if(!t.enabled)return[...n,"rm -f /usr/local/bin/ts-cloud-host-cleanup.sh"];let s=Qr("/usr/local/bin/ts-cloud-host-cleanup.sh",ii(e),"TS_CLOUD_HOST_CLEANUP_EOF","755");if(!t.timer)return[...s,...n];return[...s,"TS_CLOUD_HC_UNITS_CHANGED=0","if command -v systemctl >/dev/null 2>&1 && [ -d /etc/systemd/system ]; then",...Qr("/etc/systemd/system/ts-cloud-host-cleanup.service",["[Unit]","Description=ts-cloud host cleanup (bounded disk retention)","After=local-fs.target","","[Service]","Type=oneshot","ExecStart=/usr/local/bin/ts-cloud-host-cleanup.sh","Nice=19","IOSchedulingClass=idle"],"TS_CLOUD_HOST_CLEANUP_SVC_EOF","644","TS_CLOUD_HC_UNITS_CHANGED"),...Qr("/etc/systemd/system/ts-cloud-host-cleanup.timer",["[Unit]","Description=Run ts-cloud host cleanup on a schedule, independent of deploys","","[Timer]",`OnCalendar=${t.schedule}`,"RandomizedDelaySec=1h","Persistent=true","","[Install]","WantedBy=timers.target"],"TS_CLOUD_HOST_CLEANUP_TMR_EOF","644","TS_CLOUD_HC_UNITS_CHANGED"),' if [ "$TS_CLOUD_HC_UNITS_CHANGED" -eq 1 ]; then systemctl daemon-reload >/dev/null 2>&1 || true; fi'," systemctl enable --now ts-cloud-host-cleanup.timer >/dev/null 2>&1 || true","fi"]}function tn(e){let t=oi(e);if(!Qt(e).enabled)return t;return[...t,'if [ -x /usr/local/bin/ts-cloud-host-cleanup.sh ]; then /usr/local/bin/ts-cloud-host-cleanup.sh < /dev/null || echo "[ts-cloud] warning: host cleanup exited $?" >&2; fi']}function ui(e,t,r={}){if(!t.length)return[];let n=Object.entries(r).flatMap(([s,i])=>{if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(s))throw Error("Invalid pre-start environment variable name");if(["PORT","PORT_BACKEND","PORT_ADMIN","PORT_FRONTEND"].includes(s))return[];return[`export ${s}=${Ae(String(i))}`]});return[`cd ${e}`,`if [ -d ${R} ] && command -v pantry >/dev/null 2>&1; then`,` ${Ve()}`,"fi",...n,...t]}var Uc=/\.(?:[cm]?[jt]sx?)$/;function sn(e,t,r){let n=t==="bun"?"/usr/local/bin/bun":t==="deno"?"/usr/local/bin/deno":"/usr/local/bin/node",s=e.trim();if(/^(?:bun|node|deno)\s+/.test(s))return`${n} ${s.replace(/^(?:bun|node|deno)\s+/,"")}`;let[i="",...o]=s.split(/\s+/);if(i&&!Uc.test(i)){let a=i.startsWith("/")?i:r&&i.startsWith("./")?`${r.replace(/\/+$/,"")}/${i.replace(/^\.\//,"")}`:void 0;if(a)return[a,...o].join(" ")}return`${n} ${s}`}function ai(e){return["TS_CLOUD_MEM_MB=$(awk '/^MemTotal:/{print int($2/1024)}' /proc/meminfo)","TS_CLOUD_HIGH_MB=$((TS_CLOUD_MEM_MB / 8))",'[ "$TS_CLOUD_HIGH_MB" -lt 512 ] && TS_CLOUD_HIGH_MB=512','[ "$TS_CLOUD_HIGH_MB" -gt 4096 ] && TS_CLOUD_HIGH_MB=4096',`mkdir -p /etc/systemd/system/${e}.d`,`printf '# Written by ts-cloud: memoryHigh resolved from this box.
|
|
272
|
+
`;return['command -v bun >/dev/null 2>&1 || { echo "Configured compute storage requires Bun on the host" >&2; exit 1; }',"install -d -m 0755 /etc/ts-cloud /usr/local/lib/ts-cloud /var/lib/ts-cloud",`printf %s ${Xt(Buffer.from(r).toString("base64"))} | base64 -d > ${De}.new`,`chmod 0755 ${De}.new && mv ${De}.new ${De}`,`printf %s ${Xt(JSON.stringify(e))} > ${t}`,`chmod 0644 ${t}`,`TS_CLOUD_STORAGE_POLICY=${t} bun ${De} apply 2>&1 | tee /var/log/ts-cloud-compute-storage.log`,`rm -f ${t}`,"cat > /etc/systemd/system/ts-cloud-storage-check.service <<'TS_CLOUD_STORAGE_SERVICE'","[Unit]","Description=Check configured compute storage and deployment mounts","[Service]","Type=oneshot",`ExecStart=/usr/local/bin/bun ${De} check`,"TS_CLOUD_STORAGE_SERVICE","cat > /etc/systemd/system/ts-cloud-storage-check.timer <<'TS_CLOUD_STORAGE_TIMER'","[Unit]","Description=Check compute storage every minute","[Timer]","OnBootSec=2min","OnUnitActiveSec=1min","[Install]","WantedBy=timers.target","TS_CLOUD_STORAGE_TIMER","systemctl daemon-reload","systemctl enable --now ts-cloud-storage-check.timer","cat > /etc/systemd/system/ts-cloud-storage-reclaim.service <<'TS_CLOUD_RECLAIM_SERVICE'","[Unit]","Description=Reclaim inactive root copies of volume-backed deployments","[Service]","Type=oneshot",`ExecStart=/usr/local/bin/bun ${De} reclaim`,"Nice=19","IOSchedulingClass=idle","TS_CLOUD_RECLAIM_SERVICE","cat > /etc/systemd/system/ts-cloud-storage-reclaim.timer <<'TS_CLOUD_RECLAIM_TIMER'","[Unit]","Description=Reclaim retired deployment copies hourly","[Timer]","OnCalendar=hourly","RandomizedDelaySec=5min","Persistent=true","[Install]","WantedBy=timers.target","TS_CLOUD_RECLAIM_TIMER","systemctl daemon-reload","systemctl enable --now ts-cloud-storage-reclaim.timer"]}function Yt(e,t){return[`if [ -f ${qr} ]; then bun ${De} ensure-site ${Xt(e)} ${Xt(t)}; fi`]}import{createHash as xc}from"node:crypto";function Oc(){return["if [ -x /usr/local/bin/bun ]; then",' TS_CLOUD_HOST_BUN="$(readlink -f /usr/local/bin/bun)"',' case "$TS_CLOUD_HOST_BUN" in /root/*|/home/*)'," install -d -m 755 /opt/ts-cloud /opt/ts-cloud/bun /opt/ts-cloud/bun/bin",' TS_CLOUD_BUN_STAGE="$(mktemp /opt/ts-cloud/bun/bin/.bun.XXXXXX)"',' install -m 755 "$TS_CLOUD_HOST_BUN" "$TS_CLOUD_BUN_STAGE"',' mv -f "$TS_CLOUD_BUN_STAGE" /opt/ts-cloud/bun/bin/bun'," ln -sfn /opt/ts-cloud/bun/bin/bun /usr/local/bin/bun"," ;; esac","fi"]}function st(e){if(!/^[a-z0-9][a-z0-9-]*$/.test(e))throw Error("Runtime isolation requires a safe project slug");return`tsc-${e.length<=24?e:`${e.slice(0,15)}-${xc("sha256").update(e).digest("hex").slice(0,8)}`}`}function xt(e,t,r){return[`User=${e}`,`Group=${e}`,"NoNewPrivileges=true","PrivateTmp=true","ProtectHome=true","ProtectProc=invisible","InaccessiblePaths=-/run/postgresql -/var/run/postgresql","TemporaryFileSystem=/var/www:ro",`BindPaths=${t} -/var/www/${r}-shared`,"UMask=0077",`Environment=HOME=/var/lib/ts-cloud/${e}`]}function Xs(e,t,r){let n=st(e);if(!t.startsWith(`/var/www/${e}-`)||t.slice(9).includes("/"))throw Error("Runtime isolation requires a project-namespaced application directory");let s=`/var/lib/ts-cloud/${n}`,i=`/var/www/${e}-shared`,o=[...new Set([t,...r])];for(let a of o)if(!/^\/[a-zA-Z0-9_./-]+$/.test(a)||a.split("/").includes("..")||!(a===t||a.startsWith(`${t}/`)||a===i||a.startsWith(`${i}/`)))throw Error(`Runtime isolation cannot take ownership outside project ${e}`);return[...Oc(),`if ! id -u ${n} >/dev/null 2>&1; then useradd --system --user-group --create-home --home-dir ${s} --shell /usr/sbin/nologin ${n}; fi`,`[ "$(id -u ${n})" != 0 ] && [ "$(getent passwd ${n} | cut -d: -f6)" = ${s} ] || { echo 'Unexpected runtime isolation identity' >&2; exit 1; }`,`install -d -m 700 -o ${n} -g ${n} ${s}`,...o.map((a)=>`chown -hR ${n}:${n} ${a}`),`chmod 750 ${t}`]}function Jt(e){return Object.entries(e).map(([t,r])=>`${t}=${Dc(String(r))}`).join(`
|
|
273
|
+
`)}function Dc(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,"\\\"").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}"`}var Ic=new Set(["sqlite","sqlite3"]),Ac=new Set([":memory:","memory"]);function Lc(e){let t=e?.DB_CONNECTION?.trim().toLowerCase();return t!=null&&Ic.has(t)}function kc(e){let t=e.trim().replace(/^\.\//,"");if(t===""||t.startsWith("/")||t.startsWith("~"))return;if(t.split("/").includes(".."))return;return t.replace(/\/+$/,"")}function Jr(e,t=[]){if(!Lc(e))return{};let r=e?.DB_DATABASE_PATH?.trim()||e?.DB_DATABASE?.trim();if(r&&Ac.has(r.toLowerCase()))return{};if(!r)return{warning:"DB_CONNECTION is sqlite but neither DB_DATABASE_PATH nor DB_DATABASE names a file, so ts-cloud cannot tell where the database lives. If it is written inside the release directory, the next deploy discards it. Set DB_DATABASE_PATH, or list the file in the site's `sharedPaths`."};let n=kc(r);if(!n)return{};if(t.some((s)=>Vs(s)===n))return{};return{path:n}}function Zt(e,t=[]){let{path:r}=Jr(e,t);return r?[r]:[]}var ot="/var/ts-cloud/artifacts";var ni=Object.freeze({stagingMaxAgeMinutes:60,artifactMaxAgeDays:2,artifactUploadMaxAgeMinutes:60,bunCacheMaxAgeDays:7,journalMaxAgeDays:14,journalMaxSizeMb:512,containerImageMaxAgeHours:168}),si=Object.freeze({artifactMaxAgeDays:0,bunCacheMaxAgeDays:1,journalMaxAgeDays:3,journalMaxSizeMb:256,containerImageMaxAgeHours:24});function Js(e){return`'${e.replace(/'/g,`'"'"'`)}'`}function Be(e,t,r=0){if(!Number.isInteger(t)||t<r)throw Error(`hostCleanup: ${e} must be a whole number >= ${r} (got ${t})`)}function Zs(e,t){for(let[r,n]of Object.entries(t))Be(`${e}.${r}`,n);Be(`${e}.stagingMaxAgeMinutes`,t.stagingMaxAgeMinutes,15),Be(`${e}.artifactUploadMaxAgeMinutes`,t.artifactUploadMaxAgeMinutes,15),Be(`${e}.journalMaxSizeMb`,t.journalMaxSizeMb,1)}function Nc(e,t){let r=`hostCleanup.paths[${t}]`,n=e.path?.replace(/\/+$/,"")??"";if(!n.startsWith("/")||n===""||/[\s'"`$\\]/.test(n)||n.split("/").includes(".."))throw Error(`${r}.path must be an absolute directory with no spaces, quotes or '..' (got ${JSON.stringify(e.path)})`);if(n==="/var/www"||n.startsWith("/var/www/"))throw Error(`${r}.path must not be under /var/www: releases are pruned by keepReleases`);if(!e.pattern||/^[*?]+$/.test(e.pattern)||e.pattern.includes("/")||/[\n'"`]/.test(e.pattern))throw Error(`${r}.pattern must be a file-name glob naming something, not a bare wildcard (got ${JSON.stringify(e.pattern)})`);if(Be(`${r}.maxAgeDays`,e.maxAgeDays),e.type!==void 0&&e.type!=="file"&&e.type!=="directory")throw Error(`${r}.type must be 'file' or 'directory'`)}function Qt(e){let t=typeof e==="object"&&e!==null?e:{},r=e!==!1,n={...ni,...Qs(t.retention)};Zs("hostCleanup.retention",n);let s=null;if(t.pressure!==!1){let a=typeof t.pressure==="object"?t.pressure:{},c=a.lowWaterPercent??50,l=a.highWaterPercent??85;if(Be("hostCleanup.pressure.lowWaterPercent",c),Be("hostCleanup.pressure.highWaterPercent",l,1),l>100||c>=l)throw Error(`hostCleanup.pressure: need 0 <= lowWaterPercent < highWaterPercent <= 100 (got ${c} and ${l})`);let u={...si,...Qs(a.escalated)},d=Object.fromEntries(Object.keys(n).map((m)=>[m,Math.min(n[m],u[m]??n[m])]));Zs("hostCleanup.pressure.escalated",d),s={lowWaterPercent:c,highWaterPercent:l,escalated:d}}let i=t.schedule??"daily";if(!/^[\w*:,./~ -]+$/.test(i))throw Error(`hostCleanup.schedule must be a systemd OnCalendar expression (got ${JSON.stringify(i)})`);let o=t.paths??[];return o.forEach(Nc),{enabled:r,timer:r&&t.timer!==!1,schedule:i,retention:n,pressure:s,paths:o}}function Qs(e){return Object.fromEntries(Object.entries(e??{}).filter(([,t])=>t!==void 0))}var Ot={stagingMaxAgeMinutes:"TS_CLOUD_HC_STAGING_MIN",artifactMaxAgeDays:"TS_CLOUD_HC_ARTIFACT_DAYS",artifactUploadMaxAgeMinutes:"TS_CLOUD_HC_UPLOAD_MIN",bunCacheMaxAgeDays:"TS_CLOUD_HC_BUN_DAYS",journalMaxAgeDays:"TS_CLOUD_HC_JOURNAL_DAYS",journalMaxSizeMb:"TS_CLOUD_HC_JOURNAL_MB",containerImageMaxAgeHours:"TS_CLOUD_HC_IMAGE_HOURS"};function Zr(e){return Object.keys(Ot).map((t)=>`${Ot[t]}=${e[t]}`).join(" ")}var Ie=(e)=>`"$${Ot[e]}"`;function ei(e){return[`find /var/ts-cloud/staging -xdev -maxdepth 1 -type f -mmin +${Ie("stagingMaxAgeMinutes")} -delete 2>/dev/null || true`,`find /tmp -xdev -maxdepth 1 -type f -name "*-release.tar.gz" -mmin +${Ie("stagingMaxAgeMinutes")} -delete 2>/dev/null || true`,`find /var/ts-cloud/artifacts -xdev -maxdepth 1 -type f -name "*.tar.gz" -mtime +${Ie("artifactMaxAgeDays")} -delete 2>/dev/null || true`,`find /var/ts-cloud/artifacts -xdev -maxdepth 1 -type f -name ".*.tmp" -mmin +${Ie("artifactUploadMaxAgeMinutes")} -delete 2>/dev/null || true`,...e.map(Uc),`journalctl --vacuum-time=${Ie("journalMaxAgeDays")}d --vacuum-size=${Ie("journalMaxSizeMb")}M >/dev/null 2>&1 || true`]}function Uc(e){let t=Js(e.path.replace(/\/+$/,"")),r=Js(e.pattern);return e.type==="directory"?`find ${t} -xdev -mindepth 1 -maxdepth 1 -type d -name ${r} -mtime +${e.maxAgeDays} -exec rm -rf -- {} + 2>/dev/null || true`:`find ${t} -xdev -mindepth 1 -maxdepth 1 -type f -name ${r} -mtime +${e.maxAgeDays} -delete 2>/dev/null || true`}function ti(){return[`find /root/.bun/install/cache -xdev -type f -mtime +${Ie("bunCacheMaxAgeDays")} -delete 2>/dev/null || true`,`find /root/.bun/install/cache -xdev -depth -type d -empty -mtime +${Ie("bunCacheMaxAgeDays")} -delete 2>/dev/null || true`,`if command -v docker >/dev/null 2>&1; then docker image prune --all --force --filter "until=\${${Ot.containerImageMaxAgeHours}}h" >/dev/null 2>&1 || true; fi`,`if command -v podman >/dev/null 2>&1; then podman image prune --all --force --filter "until=\${${Ot.containerImageMaxAgeHours}}h" >/dev/null 2>&1 || true; fi`,"if command -v apt-get >/dev/null 2>&1; then apt-get clean >/dev/null 2>&1 || true; fi"]}function ri(e){return[`${e}="$(df -P / 2>/dev/null | awk 'NR==2 {sub(/%/, "", $5); print $5}' || true)"`,`case "$${e}" in ''|*[!0-9]*) ${e}=-1 ;; esac`]}function en(e){let{retention:t,pressure:r,paths:n}=Qt(e),s='echo "[ts-cloud] host cleanup (disk before): $(df -h / | tail -1)"',i='echo "[ts-cloud] host cleanup (disk after): $(df -h / | tail -1)"';if(!r)return['echo "[ts-cloud] host cleanup (disk before): $(df -h / | tail -1)"',Zr(t),...ei(n),...ti(),'echo "[ts-cloud] host cleanup (disk after): $(df -h / | tail -1)"'];let{lowWaterPercent:o,highWaterPercent:a,escalated:c}=r;return['echo "[ts-cloud] host cleanup (disk before): $(df -h / | tail -1)"',...ri("TS_CLOUD_HC_BEFORE"),"TS_CLOUD_HC_TIER=normal",Zr(t),`if [ "$TS_CLOUD_HC_BEFORE" -ge 0 ] && [ "$TS_CLOUD_HC_BEFORE" -lt ${o} ]; then`," TS_CLOUD_HC_TIER=light",` echo "[ts-cloud] host cleanup: / at \${TS_CLOUD_HC_BEFORE}% (under ${o}%), skipping cache and image pruning"`,`elif [ "$TS_CLOUD_HC_BEFORE" -ge ${a} ]; then`," TS_CLOUD_HC_TIER=pressure",` ${Zr(c)}`,` echo "[ts-cloud] warning: / at \${TS_CLOUD_HC_BEFORE}% (at or above ${a}%), host cleanup is using shortened retention windows" >&2`,"fi",...ei(n),'if [ "$TS_CLOUD_HC_TIER" != light ]; then',...ti().map((l)=>` ${l}`),"fi",'echo "[ts-cloud] host cleanup (disk after): $(df -h / | tail -1)"',...ri("TS_CLOUD_HC_AFTER"),`echo "[ts-cloud] host-cleanup {\\"event\\":\\"host-cleanup\\",\\"tier\\":\\"$TS_CLOUD_HC_TIER\\",\\"diskBeforePercent\\":$TS_CLOUD_HC_BEFORE,\\"diskAfterPercent\\":$TS_CLOUD_HC_AFTER,\\"lowWaterPercent\\":${o},\\"highWaterPercent\\":${a}}"`,"mkdir -p /var/lib/ts-cloud 2>/dev/null || true",'TS_CLOUD_HC_PREV="$(cat /var/lib/ts-cloud/host-cleanup.alert 2>/dev/null || echo ok)"',`if [ "$TS_CLOUD_HC_AFTER" -ge ${a} ]; then`,` echo "[ts-cloud] warning: / still at \${TS_CLOUD_HC_AFTER}% after host cleanup (high-water ${a}%); something outside ts-cloud's retention rules is filling the disk" >&2`,` if [ "$TS_CLOUD_HC_PREV" != alert ] && [ -x /usr/local/bin/ts-cloud-notify ]; then /usr/local/bin/ts-cloud-notify "⚠️ $(hostname): / at \${TS_CLOUD_HC_AFTER}% after host cleanup (high-water ${a}%)" >/dev/null 2>&1 || true; fi`," echo alert > /var/lib/ts-cloud/host-cleanup.alert 2>/dev/null || true","else",` if [ "$TS_CLOUD_HC_PREV" = alert ] && [ -x /usr/local/bin/ts-cloud-notify ]; then /usr/local/bin/ts-cloud-notify "✅ $(hostname): / back under ${a}% after host cleanup (\${TS_CLOUD_HC_AFTER}%)" >/dev/null 2>&1 || true; fi`," echo ok > /var/lib/ts-cloud/host-cleanup.alert 2>/dev/null || true","fi"]}function ii(e){return["#!/usr/bin/env bash","# Managed by ts-cloud: rewritten on every deploy. Configure compute.hostCleanup instead of editing.","set -uo pipefail","TS_CLOUD_HC_LOCK=/run/lock/ts-cloud-host-cleanup.lock","[ -d /run/lock ] || TS_CLOUD_HC_LOCK=/tmp/ts-cloud-host-cleanup.lock",'if command -v flock >/dev/null 2>&1 && exec 9>"$TS_CLOUD_HC_LOCK"; then',' if ! flock -n 9; then echo "[ts-cloud] host cleanup already running; skipping"; exit 0; fi',"fi",...en(e),"exit 0"]}function Qr(e,t,r,n,s){return[`cat > ${e}.ts-cloud-new <<'${r}' || true`,...t,r,`if [ -s ${e}.ts-cloud-new ] && ! cmp -s ${e}.ts-cloud-new ${e}; then chmod ${n} ${e}.ts-cloud-new && mv -f ${e}.ts-cloud-new ${e}${s?` && ${s}=1`:""} || true; else rm -f ${e}.ts-cloud-new; fi`]}function oi(e){let t=Qt(e),r="/etc/systemd/system/ts-cloud-host-cleanup",n=["if [ -f /etc/systemd/system/ts-cloud-host-cleanup.timer ]; then"," systemctl disable --now ts-cloud-host-cleanup.timer >/dev/null 2>&1 || true"," rm -f /etc/systemd/system/ts-cloud-host-cleanup.timer /etc/systemd/system/ts-cloud-host-cleanup.service"," systemctl daemon-reload >/dev/null 2>&1 || true","fi"];if(!t.enabled)return[...n,"rm -f /usr/local/bin/ts-cloud-host-cleanup.sh"];let s=Qr("/usr/local/bin/ts-cloud-host-cleanup.sh",ii(e),"TS_CLOUD_HOST_CLEANUP_EOF","755");if(!t.timer)return[...s,...n];return[...s,"TS_CLOUD_HC_UNITS_CHANGED=0","if command -v systemctl >/dev/null 2>&1 && [ -d /etc/systemd/system ]; then",...Qr("/etc/systemd/system/ts-cloud-host-cleanup.service",["[Unit]","Description=ts-cloud host cleanup (bounded disk retention)","After=local-fs.target","","[Service]","Type=oneshot","ExecStart=/usr/local/bin/ts-cloud-host-cleanup.sh","Nice=19","IOSchedulingClass=idle"],"TS_CLOUD_HOST_CLEANUP_SVC_EOF","644","TS_CLOUD_HC_UNITS_CHANGED"),...Qr("/etc/systemd/system/ts-cloud-host-cleanup.timer",["[Unit]","Description=Run ts-cloud host cleanup on a schedule, independent of deploys","","[Timer]",`OnCalendar=${t.schedule}`,"RandomizedDelaySec=1h","Persistent=true","","[Install]","WantedBy=timers.target"],"TS_CLOUD_HOST_CLEANUP_TMR_EOF","644","TS_CLOUD_HC_UNITS_CHANGED"),' if [ "$TS_CLOUD_HC_UNITS_CHANGED" -eq 1 ]; then systemctl daemon-reload >/dev/null 2>&1 || true; fi'," systemctl enable --now ts-cloud-host-cleanup.timer >/dev/null 2>&1 || true","fi"]}function tn(e){let t=oi(e);if(!Qt(e).enabled)return t;return[...t,'if [ -x /usr/local/bin/ts-cloud-host-cleanup.sh ]; then /usr/local/bin/ts-cloud-host-cleanup.sh < /dev/null || echo "[ts-cloud] warning: host cleanup exited $?" >&2; fi']}function ui(e,t,r={}){if(!t.length)return[];let n=Object.entries(r).flatMap(([s,i])=>{if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(s))throw Error("Invalid pre-start environment variable name");if(["PORT","PORT_BACKEND","PORT_ADMIN","PORT_FRONTEND"].includes(s))return[];return[`export ${s}=${Ae(String(i))}`]});return[`cd ${e}`,`if [ -d ${R} ] && command -v pantry >/dev/null 2>&1; then`,` ${Ve()}`,"fi",...n,...t]}var Mc=/\.(?:[cm]?[jt]sx?)$/;function sn(e,t,r){let n=t==="bun"?"/usr/local/bin/bun":t==="deno"?"/usr/local/bin/deno":"/usr/local/bin/node",s=e.trim();if(/^(?:bun|node|deno)\s+/.test(s))return`${n} ${s.replace(/^(?:bun|node|deno)\s+/,"")}`;let[i="",...o]=s.split(/\s+/);if(i&&!Mc.test(i)){let a=i.startsWith("/")?i:r&&i.startsWith("./")?`${r.replace(/\/+$/,"")}/${i.replace(/^\.\//,"")}`:void 0;if(a)return[a,...o].join(" ")}return`${n} ${s}`}function ai(e){return["TS_CLOUD_MEM_MB=$(awk '/^MemTotal:/{print int($2/1024)}' /proc/meminfo)","TS_CLOUD_HIGH_MB=$((TS_CLOUD_MEM_MB / 8))",'[ "$TS_CLOUD_HIGH_MB" -lt 512 ] && TS_CLOUD_HIGH_MB=512','[ "$TS_CLOUD_HIGH_MB" -gt 4096 ] && TS_CLOUD_HIGH_MB=4096',`mkdir -p /etc/systemd/system/${e}.d`,`printf '# Written by ts-cloud: memoryHigh resolved from this box.
|
|
272
274
|
# An eighth of %sMB RAM. Set memoryHigh in config to override.
|
|
273
275
|
[Service]
|
|
274
276
|
MemoryAccounting=true
|
|
275
277
|
MemoryHigh=%sM
|
|
276
|
-
' "$TS_CLOUD_MEM_MB" "$TS_CLOUD_HIGH_MB" > /etc/systemd/system/${e}.d/50-ts-cloud-memory.conf`,"systemctl daemon-reload",'echo "[ts-cloud] memoryHigh=auto resolved to ${TS_CLOUD_HIGH_MB}M (one eighth of ${TS_CLOUD_MEM_MB}M)"',...nn()]}function tr(e,t={auto:!0}){let r=t.auto===!1?[]:[`/etc/systemd/system/${e}.d/50-ts-cloud-memory.conf`];for(let n of["/etc/systemd/system.control","/run/systemd/system.control"]){if(t.high)r.push(`${n}/${e}.d/50-MemoryHigh.conf`);if(t.max)r.push(`${n}/${e}.d/50-MemoryMax.conf`)}if(r.length===0)return[];return["TS_CLOUD_MEM_CLEARED=0",...r.map((n)=>`if [ -f ${n} ]; then rm -f ${n}; TS_CLOUD_MEM_CLEARED=1; echo "[ts-cloud] removed ${n}: memory limits now come from config"; fi`),'if [ "$TS_CLOUD_MEM_CLEARED" = 1 ]; then systemctl daemon-reload; fi']}function nn(){return[`TS_CLOUD_COMMIT=$(for f in /sys/fs/cgroup/system.slice/*.service/memory.high /sys/fs/cgroup/system.slice/*.slice/*.service/memory.high; do [ -f "$f" ] || continue; v=$(cat "$f" 2>/dev/null); [ "$v" = "max" ] && continue; echo "$v"; done | awk '{s+=$1} END {print int(s/1048576)}')`,"TS_CLOUD_RAM=$(awk '/^MemTotal:/{print int($2/1024)}' /proc/meminfo)",'if [ -n "$TS_CLOUD_COMMIT" ] && [ "$TS_CLOUD_COMMIT" -gt 0 ] && [ "$TS_CLOUD_RAM" -gt 0 ]; then TS_CLOUD_PCT=$((TS_CLOUD_COMMIT * 100 / TS_CLOUD_RAM)); if [ "$TS_CLOUD_PCT" -gt 300 ]; then echo "[ts-cloud] WARNING: services on this box declare ${TS_CLOUD_COMMIT}M of MemoryHigh against ${TS_CLOUD_RAM}M of RAM (${TS_CLOUD_PCT}% committed). Soft ceilings, so this is not fatal - but no ceiling can protect the box once enough of them are drawn on at once."; else echo "[ts-cloud] memory commitment: ${TS_CLOUD_COMMIT}M declared against ${TS_CLOUD_RAM}M RAM (${TS_CLOUD_PCT}%)"; fi; fi']}function Mc(e){return[`TS_CLOUD_SLICE=$(systemctl show ${e} -p Slice --value 2>/dev/null || true)`,'if [ -n "$TS_CLOUD_SLICE" ] && [ "$TS_CLOUD_SLICE" != "system.slice" ]; then systemctl set-property --runtime "$TS_CLOUD_SLICE" MemoryHigh=infinity MemoryMax=infinity 2>/dev/null || true; systemctl set-property "$TS_CLOUD_SLICE" MemoryHigh=infinity MemoryMax=infinity 2>/dev/null || true; fi']}var Ke={path:"/",intervalSeconds:60,failuresBeforeRestart:3,timeoutSeconds:10,startupGraceSeconds:600,maxBackoffSeconds:3600,backoffBaseSeconds:300};function Dt(e,t,r){return typeof e==="number"&&Number.isFinite(e)?Math.max(r,Math.floor(e)):t}function ci(e){let{unitBase:t,unitPattern:r,port:n}=e,s=e.path&&e.path.startsWith("/")?e.path:Ke.path,i=Dt(e.intervalSeconds,Ke.intervalSeconds,10),o=Dt(e.failuresBeforeRestart,Ke.failuresBeforeRestart,1),a=Dt(e.timeoutSeconds,Ke.timeoutSeconds,1),c=Dt(e.startupGraceSeconds,Ke.startupGraceSeconds,0),l=Dt(e.maxBackoffSeconds,Ke.maxBackoffSeconds,0),u=Math.min(Ke.backoffBaseSeconds,l),d=`/usr/local/sbin/${t}-liveness`,m=`/run/${t}-liveness`,h=`${t}-liveness`,f=`127.0.0.1:${n}${s}`,x=r.includes("@*")?`TS_CLOUD_UNIT=$(systemctl list-units --plain --no-legend --type=service '${r}' 2>/dev/null | awk '{print $1}' | head -1)`:`TS_CLOUD_UNIT=${r}`;return[`cat > ${d} <<'TS_CLOUD_LIVENESS_EOF'`,"#!/bin/sh",`# Liveness check for ${t}, written by ts-cloud on every deploy.`,`# Settings: interval=${i}s timeout=${a}s failures=${o} startup-grace=${c}s max-backoff=${l}s`,"set -eu",`TS_CLOUD_STATE=${m}`,'exec 9>"$TS_CLOUD_STATE.lock"',"flock -n 9 || exit 0",x,'[ -n "$TS_CLOUD_UNIT" ] || exit 0','systemctl is-active --quiet "$TS_CLOUD_UNIT" || exit 0',"TS_CLOUD_RC=0",`curl -s -o /dev/null --max-time ${a} "http://${f}" || TS_CLOUD_RC=$?`,'if [ "$TS_CLOUD_RC" -eq 0 ]; then',' rm -f "$TS_CLOUD_STATE.fail"',' if [ -f "$TS_CLOUD_STATE.restarts" ]; then',` TS_CLOUD_LAST=$(awk '{print $2}' "$TS_CLOUD_STATE.restarts" 2>/dev/null || true)`,` case "$TS_CLOUD_LAST" in ''|*[!0-9]*) TS_CLOUD_LAST=0 ;; esac`,` [ $(($(date +%s) - TS_CLOUD_LAST)) -lt ${l} ] || rm -f "$TS_CLOUD_STATE.restarts"`," fi"," exit 0","fi",'case "$TS_CLOUD_RC" in'," 7) TS_CLOUD_WHY='connection refused' ;;",` 28) TS_CLOUD_WHY='no response within ${a}s' ;;`,' *) TS_CLOUD_WHY="curl exit $TS_CLOUD_RC" ;;',"esac",'TS_CLOUD_SINCE=$(systemctl show "$TS_CLOUD_UNIT" -p ActiveEnterTimestampMonotonic --value 2>/dev/null || true)',"TS_CLOUD_UPTIME=$(cut -d' ' -f1 /proc/uptime 2>/dev/null || true)",`TS_CLOUD_AGE=$(awk -v up="$TS_CLOUD_UPTIME" -v since="$TS_CLOUD_SINCE" 'BEGIN { if (since + 0 > 0 && up + 0 > 0) print int(up - since / 1000000); else print -1 }')`,`case "$TS_CLOUD_AGE" in ''|*[!0-9-]*) TS_CLOUD_AGE=-1 ;; esac`,'if [ "$TS_CLOUD_AGE" -lt 0 ]; then',' TS_CLOUD_PID=$(systemctl show "$TS_CLOUD_UNIT" -p MainPID --value 2>/dev/null || true)',` case "$TS_CLOUD_PID" in ''|0|*[!0-9]*) ;; *) TS_CLOUD_AGE=$(ps -o etimes= -p "$TS_CLOUD_PID" 2>/dev/null | tr -d ' ' || true) ;; esac`,` case "$TS_CLOUD_AGE" in ''|*[!0-9]*) TS_CLOUD_AGE=-1 ;; esac`,"fi",`if [ "$TS_CLOUD_AGE" -ge 0 ] && [ "$TS_CLOUD_AGE" -lt ${c} ]; then`,' rm -f "$TS_CLOUD_STATE.fail"',` logger -t ${h} "$TS_CLOUD_UNIT is \${TS_CLOUD_AGE}s old, inside its ${c}s startup grace; not counting this failed check on ${f} ($TS_CLOUD_WHY)"`," exit 0","fi",'TS_CLOUD_FAILS=$(cat "$TS_CLOUD_STATE.fail" 2>/dev/null || echo 0)',`case "$TS_CLOUD_FAILS" in ''|*[!0-9]*) TS_CLOUD_FAILS=0 ;; esac`,"TS_CLOUD_FAILS=$((TS_CLOUD_FAILS + 1))",'echo "$TS_CLOUD_FAILS" > "$TS_CLOUD_STATE.fail"',`if [ "$TS_CLOUD_FAILS" -lt ${o} ]; then`,` logger -t ${h} "check $TS_CLOUD_FAILS of ${o} failed on ${f} ($TS_CLOUD_WHY)"`," exit 0","fi","TS_CLOUD_NOW=$(date +%s)","TS_CLOUD_STREAK=0","TS_CLOUD_LAST=0",'[ ! -f "$TS_CLOUD_STATE.restarts" ] || read -r TS_CLOUD_STREAK TS_CLOUD_LAST < "$TS_CLOUD_STATE.restarts" || true',`case "$TS_CLOUD_STREAK" in ''|*[!0-9]*) TS_CLOUD_STREAK=0 ;; esac`,`case "$TS_CLOUD_LAST" in ''|*[!0-9]*) TS_CLOUD_LAST=0 ;; esac`,'if [ "$TS_CLOUD_STREAK" -gt 0 ]; then',` TS_CLOUD_WAIT=${u}`," TS_CLOUD_I=1",` while [ "$TS_CLOUD_I" -lt "$TS_CLOUD_STREAK" ] && [ "$TS_CLOUD_WAIT" -lt ${l} ]; do TS_CLOUD_WAIT=$((TS_CLOUD_WAIT * 2)); TS_CLOUD_I=$((TS_CLOUD_I + 1)); done`,` [ "$TS_CLOUD_WAIT" -le ${l} ] || TS_CLOUD_WAIT=${l}`," TS_CLOUD_ELAPSED=$((TS_CLOUD_NOW - TS_CLOUD_LAST))",' if [ "$TS_CLOUD_ELAPSED" -lt "$TS_CLOUD_WAIT" ]; then',` logger -t ${h} "holding off: $TS_CLOUD_STREAK liveness restart(s) in a row have not brought $TS_CLOUD_UNIT back; next restart allowed in $((TS_CLOUD_WAIT - TS_CLOUD_ELAPSED))s ($TS_CLOUD_WHY on ${f})"`," exit 0"," fi","fi","TS_CLOUD_STREAK=$((TS_CLOUD_STREAK + 1))",`logger -t ${h} "no HTTP response on ${f} after $TS_CLOUD_FAILS checks ($TS_CLOUD_WHY); restarting $TS_CLOUD_UNIT (liveness restart $TS_CLOUD_STREAK in a row)"`,'echo "$TS_CLOUD_STREAK $TS_CLOUD_NOW" > "$TS_CLOUD_STATE.restarts"','rm -f "$TS_CLOUD_STATE.fail"','systemctl restart "$TS_CLOUD_UNIT"',"TS_CLOUD_LIVENESS_EOF",`chmod +x ${d}`,`rm -f ${m}.fail ${m}.restarts`,`cat > /etc/systemd/system/${t}-liveness.service <<'TS_CLOUD_UNIT_EOF'`,"[Unit]",`Description=Liveness check for ${t} (managed by ts-cloud)`,"","[Service]","Type=oneshot",`ExecStart=${d}`,"TS_CLOUD_UNIT_EOF",`cat > /etc/systemd/system/${t}-liveness.timer <<'TS_CLOUD_UNIT_EOF'`,"[Unit]",`Description=Run the ${t} liveness check every ${i}s`,"","[Timer]",`OnUnitActiveSec=${i}s`,`OnBootSec=${i}s`,"Persistent=true","","[Install]","WantedBy=timers.target","TS_CLOUD_UNIT_EOF","systemctl daemon-reload",`systemctl enable --now ${t}-liveness.timer >/dev/null 2>&1 || true`]}var li=20,rn=3,Fc=5,Bc=60;function jc(e){let{paths:t,unitBase:r,releaseId:n,port:s}=e,i=`${r}@${n}.service`,o=`${r}.service`,a=Math.max(1,e.healthGateSeconds??5),c=e.healthCheckPath?e.healthCheckPath.startsWith("/")?e.healthCheckPath:`/${e.healthCheckPath}`:null,l=c?`curl -sf -o /dev/null --max-time ${Fc} "http://127.0.0.1:${s}${c}"`:null,u=(d)=>`{ echo "[ts-cloud] release ${n} ${d}" >&2; ts_cloud_restore_previous; exit 1; }`;return[`TS_CLOUD_OLD_UNITS=$(systemctl list-units --plain --no-legend --type=service "${r}@*.service" 2>/dev/null | awk '{print $1}' | grep -v "^${i}$" || true)`,`TS_CLOUD_PREV_CURRENT="$(readlink ${t.current} 2>/dev/null || true)"`,"TS_CLOUD_LEGACY_STOPPED=0","ts_cloud_restore_previous() {",` journalctl -u ${i} -n 50 --no-pager >&2 || true`,` systemctl stop ${i} 2>/dev/null || true`,` systemctl disable ${i} 2>/dev/null || true`,` if [ -n "$TS_CLOUD_PREV_CURRENT" ] && [ -d "$TS_CLOUD_PREV_CURRENT" ]; then ln -sfn "$TS_CLOUD_PREV_CURRENT" ${t.current}.tmp && mv -Tf ${t.current}.tmp ${t.current}; fi`,' for TS_CLOUD_RU in ${TS_CLOUD_OLD_UNITS}; do systemctl enable "$TS_CLOUD_RU" 2>/dev/null || true; systemctl start "$TS_CLOUD_RU" 2>/dev/null || true; done',` if [ "$TS_CLOUD_LEGACY_STOPPED" -eq 1 ]; then systemctl start ${o} 2>/dev/null || true; fi`,' if [ -n "$TS_CLOUD_OLD_UNITS" ] || [ "$TS_CLOUD_LEGACY_STOPPED" -eq 1 ]; then echo "[ts-cloud] previous release restored and serving" >&2; else echo "[ts-cloud] there was no previous release to restore" >&2; fi',"}",`if [ -f /etc/systemd/system/${o} ] && systemctl is-active --quiet ${o}; then echo "[ts-cloud] retiring pre-zero-downtime unit ${o} (one-time restart cutover)"; systemctl stop ${o}; TS_CLOUD_LEGACY_STOPPED=1; fi`,'TS_CLOUD_STARTED_AT="@$(date +%s)"',`systemctl restart ${i}`,`TS_CLOUD_GATE_OK=1; for TS_CLOUD_I in $(seq 1 ${a}); do sleep 1; systemctl is-active --quiet ${i} || { TS_CLOUD_GATE_OK=0; break; }; done`,'if [ "$TS_CLOUD_GATE_OK" -ne 1 ]; then',` if journalctl -u ${i} --since "$TS_CLOUD_STARTED_AT" -o cat --no-pager 2>/dev/null | grep -iE 'EADDRINUSE|address already in use|port [0-9]+ (is )?in use|Failed to start server' >/dev/null; then`,` echo "[ts-cloud] release ${n} could not share :${s} with the previous release (no SO_REUSEPORT?) — retiring it and retrying" >&2`,' for TS_CLOUD_RU in ${TS_CLOUD_OLD_UNITS}; do systemctl stop "$TS_CLOUD_RU" 2>/dev/null || true; done',` systemctl restart ${i}`,` for TS_CLOUD_I in $(seq 1 ${a}); do sleep 1; systemctl is-active --quiet ${i} || ${u("failed its health gate on the free port")}; done`," else",` ${u("crashed on start — the previous release was never stopped")}`," fi","fi",`if ss -ltnpH "sport = :${s}" >/dev/null 2>&1; then`," TS_CLOUD_BOUND=0",` for TS_CLOUD_I in $(seq 1 ${Bc}); do`,` TS_CLOUD_NEW_PID="$(systemctl show -p MainPID --value ${i} 2>/dev/null || true)"`,` if [ -n "$TS_CLOUD_NEW_PID" ] && [ "$TS_CLOUD_NEW_PID" != "0" ] && ss -ltnpH "sport = :${s}" 2>/dev/null | grep "pid=$TS_CLOUD_NEW_PID," >/dev/null; then TS_CLOUD_BOUND=1; break; fi`,` systemctl is-active --quiet ${i} || break`,` sleep ${rn}`," done",` [ "$TS_CLOUD_BOUND" -eq 1 ] || ${u(`never took :${s}`)}`,"else",' echo "[ts-cloud] cannot read listener PIDs on this host — the response probes run without the bind check" >&2',"fi",...l?[`TS_CLOUD_HEALTHY=0; for TS_CLOUD_I in $(seq 1 ${li}); do if ${l}; then TS_CLOUD_HEALTHY=1; break; fi; sleep ${rn}; done`,`[ "$TS_CLOUD_HEALTHY" -eq 1 ] || ${u(`never answered ${c}`)}`]:[],...hn(t),`systemctl enable ${i} 2>/dev/null || true`,'for TS_CLOUD_U in ${TS_CLOUD_OLD_UNITS}; do systemctl stop "$TS_CLOUD_U" 2>/dev/null || true; systemctl disable "$TS_CLOUD_U" 2>/dev/null || true; done',`TS_CLOUD_LISTENING=0; for TS_CLOUD_I in $(seq 1 ${a}); do if ss -ltnH "sport = :${s}" 2>/dev/null | grep . >/dev/null; then TS_CLOUD_LISTENING=1; break; fi; sleep 1; done`,`if [ "$TS_CLOUD_LISTENING" -ne 1 ]; then echo "[ts-cloud] nothing is listening on ${s} after retiring the previous release — restarting ${i} on the now-free port" >&2; systemctl restart ${i}; for TS_CLOUD_I in $(seq 1 ${a}); do if ss -ltnH "sport = :${s}" 2>/dev/null | grep . >/dev/null; then TS_CLOUD_LISTENING=1; break; fi; sleep 1; done; fi`,`[ "$TS_CLOUD_LISTENING" -eq 1 ] || ${u(`is active but never bound :${s}`)}`,...l?[`TS_CLOUD_HEALTHY=0; for TS_CLOUD_I in $(seq 1 ${li}); do if ${l}; then TS_CLOUD_HEALTHY=1; break; fi; sleep ${rn}; done`,`[ "$TS_CLOUD_HEALTHY" -eq 1 ] || ${u(`stopped answering ${c} once it served alone`)}`]:[],...Ws(t),`systemctl list-unit-files --plain --no-legend "${r}@*.service" 2>/dev/null | awk '{print $1}' | { grep -v -e "^${i}$" -e "^${r}@\\.service$" || true; } | while read -r TS_CLOUD_U; do systemctl disable "$TS_CLOUD_U" 2>/dev/null || true; done`,`if [ -f /etc/systemd/system/${o} ]; then systemctl disable ${o} 2>/dev/null || true; rm -f /etc/systemd/system/${o}; systemctl daemon-reload; fi`]}function pi(e,t){if(!t.length)return[];let r=Ae(e.release);return[`if [ -e ${r} ] || [ -L ${r} ]; then`,...t,"fi"]}function on(e){let{siteName:t,slug:r,artifactFetch:n,releaseId:s,execStart:i,envEntries:o,port:a,keepReleases:c=_i,preStartCommands:l=[],postActivateCommands:u=[],beforeResetCommands:d=[],healthCheckPath:m,healthGateSeconds:h=5,liveness:f,memoryHigh:S="auto",memoryMax:x,cpuWeight:B,ioWeight:L,tasksMax:N,stopTimeout:D}=e,E=[...B!=null?[`CPUWeight=${B}`]:[],...L!=null?[`IOWeight=${L}`]:[],...N!=null?[`TasksMax=${N}`]:[],...D?[`TimeoutStopSec=${D}`]:[]],M=e.zeroDowntime??a!=null,H=e.appDir??`/var/www/${t}`,F=Ht(H,s),X=`${r}-${t}`,V=`${X}.service`,Y=It(r,t,s),te=e.sharedPaths??[],Z=Jc([".env",...te,...Zt(o,te)]),J=Jt(o),ie=ui(F.release,l,o),ce=["set -euo pipefail",...zs(F),...n,...$i(F,Z),...pi(F,d),Gs(F),...Hs(F),`tar xzf ${Y} -C "$TS_CLOUD_STAGED"`,`rm -f ${Y}`,`cat > ${F.shared}/.env <<'TS_CLOUD_ENV_EOF'`,J,"TS_CLOUD_ENV_EOF",`chmod 600 ${F.shared}/.env`,...qs(F),`find ${F.release} -maxdepth 1 \\( -type f -o -type l \\) -name '.env*' ! -name '.env.example' ! -name '*.example' -delete`,`sed -i -E '/^[[:space:]]*(PORT|PORT_BACKEND|PORT_ADMIN|PORT_FRONTEND)[[:space:]]*=/d' ${F.shared}/.env 2>/dev/null || true`,..._o(F,Z),...ie,...e.isolation?Xs(e.slug||t,F.base,Z.flatMap((y)=>typeof y==="string"||!y.target?[]:[y.target])):[]];if(M&&a!=null){let y=`${X}@${s}.service`,k=m?m.startsWith("/")?m:`/${m}`:null;return[...ce,`cat > /etc/systemd/system/${X}@.service <<'TS_CLOUD_UNIT_EOF'`,"[Unit]",`Description=${t} release %i (managed by ts-cloud)`,"After=network.target","","[Service]","Type=simple",...e.isolation?xt(st(e.slug||t),F.base,e.slug||t):[],`WorkingDirectory=${F.releases}/%i`,`ExecStart=${i}`,"Restart=always","RestartSec=5","MemoryAccounting=true",...S&&S!=="auto"?[`MemoryHigh=${S}`]:[],...x?[`MemoryMax=${x}`]:[],...E,`EnvironmentFile=${F.releases}/%i/.env`,`Environment=PORT=${a}`,`Environment=STX_IMAGE_CACHE_DIR=${xo(F)}`,`Environment=STX_BUNDLE_CACHE_DIR=${Oi(F)}`,`Environment=STX_BUNDLE_CACHE_DIR=${Oi(F)}`,"","[Install]","WantedBy=multi-user.target","TS_CLOUD_UNIT_EOF","systemctl daemon-reload",...S==="auto"?ai(`${X}@.service`):[...tr(`${X}@.service`),...nn()],...Mc(y),...jc({paths:F,unitBase:X,releaseId:s,port:a,healthCheckPath:k,healthGateSeconds:h}),...u,...yn(F,c),...f===!1?[]:ci({...f,unitBase:X,unitPattern:`${X}@*.service`,port:a,path:f?.path??m})]}return[...ce,`cat > /etc/systemd/system/${V} <<'TS_CLOUD_UNIT_EOF'`,"[Unit]",`Description=${t} (managed by ts-cloud)`,"After=network.target","","[Service]","Type=simple",...e.isolation?xt(st(e.slug||t),F.base,e.slug||t):[],`WorkingDirectory=${F.current}`,`ExecStart=${i}`,"Restart=always","RestartSec=5","MemoryAccounting=true",...S&&S!=="auto"?[`MemoryHigh=${S}`]:[],...x?[`MemoryMax=${x}`]:[],...E,`EnvironmentFile=${F.current}/.env`,...a?[`Environment=PORT=${a}`]:[],`Environment=STX_IMAGE_CACHE_DIR=${xo(F)}`,`Environment=STX_BUNDLE_CACHE_DIR=${Oi(F)}`,"","[Install]","WantedBy=multi-user.target","TS_CLOUD_UNIT_EOF","systemctl daemon-reload",...tr(V,{auto:S!=="auto",high:S!=="auto",max:Boolean(x)}),...S==="auto"?ai(V):nn(),`systemctl enable ${V}`,`TS_CLOUD_PREV_CURRENT="$(readlink ${F.current} 2>/dev/null || true)"`,...hn(F),`systemctl restart ${V}`,`for TS_CLOUD_I in $(seq 1 ${Math.max(1,h)}); do sleep 1; systemctl is-active --quiet ${V} || { echo "[ts-cloud] release ${s} of ${V} did not stay up — restoring the previous release" >&2; journalctl -u ${V} -n 50 --no-pager >&2 || true; if [ -n "$TS_CLOUD_PREV_CURRENT" ] && [ -d "$TS_CLOUD_PREV_CURRENT" ] && [ "$(readlink -f "$TS_CLOUD_PREV_CURRENT")" != "$(readlink -f ${F.release})" ]; then ln -sfn "$TS_CLOUD_PREV_CURRENT" ${F.current}.tmp && mv -Tf ${F.current}.tmp ${F.current}; systemctl restart ${V} || true; fi; exit 1; }; done`,...Ws(F),...u,...yn(F,c),...f===!1||!a?[]:ci({...f,unitBase:X,unitPattern:V,port:a,path:f?.path??m})]}function an(e){let{siteName:t,artifactFetch:r,releaseId:n,keepReleases:s=_i,preStartCommands:i=[],postActivateCommands:o=[],beforeResetCommands:a=[]}=e,c=e.appDir??`/var/www/${t}`,l=Ht(c,n),u=It(e.slug,t,n),d=ui(l.release,i);return["set -euo pipefail",...zs(l),...r,...$i(l,[]),...pi(l,a),Gs(l),...Hs(l),`tar xzf ${u} -C "$TS_CLOUD_STAGED"`,...qs(l),`rm -f ${u}`,...d,...hn(l),...o,...yn(l,s)]}function It(e,t,r){return`/tmp/${[e,t,r].filter(Boolean).join("-")}-release.tar.gz`}function fn(e,t,r,n){return[`aws s3 cp "s3://${e}/${t}" ${n} --region ${r}`]}function gn(e,t){return[`mv "${e}" ${t}`]}function Ae(e){return`'${e.replace(/'/g,`'"'"'`)}'`}function rr(e,t,r){return["set -euo pipefail",`chmod 600 ${Ae(e)}`,`mv -f -- ${Ae(e)} ${Ae(t)}`,`cp -- ${Ae(t)} ${Ae(r)}`].join(`
|
|
278
|
+
' "$TS_CLOUD_MEM_MB" "$TS_CLOUD_HIGH_MB" > /etc/systemd/system/${e}.d/50-ts-cloud-memory.conf`,"systemctl daemon-reload",'echo "[ts-cloud] memoryHigh=auto resolved to ${TS_CLOUD_HIGH_MB}M (one eighth of ${TS_CLOUD_MEM_MB}M)"',...nn()]}function tr(e,t={auto:!0}){let r=t.auto===!1?[]:[`/etc/systemd/system/${e}.d/50-ts-cloud-memory.conf`];for(let n of["/etc/systemd/system.control","/run/systemd/system.control"]){if(t.high)r.push(`${n}/${e}.d/50-MemoryHigh.conf`);if(t.max)r.push(`${n}/${e}.d/50-MemoryMax.conf`)}if(r.length===0)return[];return["TS_CLOUD_MEM_CLEARED=0",...r.map((n)=>`if [ -f ${n} ]; then rm -f ${n}; TS_CLOUD_MEM_CLEARED=1; echo "[ts-cloud] removed ${n}: memory limits now come from config"; fi`),'if [ "$TS_CLOUD_MEM_CLEARED" = 1 ]; then systemctl daemon-reload; fi']}function nn(){return[`TS_CLOUD_COMMIT=$(for f in /sys/fs/cgroup/system.slice/*.service/memory.high /sys/fs/cgroup/system.slice/*.slice/*.service/memory.high; do [ -f "$f" ] || continue; v=$(cat "$f" 2>/dev/null); [ "$v" = "max" ] && continue; echo "$v"; done | awk '{s+=$1} END {print int(s/1048576)}')`,"TS_CLOUD_RAM=$(awk '/^MemTotal:/{print int($2/1024)}' /proc/meminfo)",'if [ -n "$TS_CLOUD_COMMIT" ] && [ "$TS_CLOUD_COMMIT" -gt 0 ] && [ "$TS_CLOUD_RAM" -gt 0 ]; then TS_CLOUD_PCT=$((TS_CLOUD_COMMIT * 100 / TS_CLOUD_RAM)); if [ "$TS_CLOUD_PCT" -gt 300 ]; then echo "[ts-cloud] WARNING: services on this box declare ${TS_CLOUD_COMMIT}M of MemoryHigh against ${TS_CLOUD_RAM}M of RAM (${TS_CLOUD_PCT}% committed). Soft ceilings, so this is not fatal - but no ceiling can protect the box once enough of them are drawn on at once."; else echo "[ts-cloud] memory commitment: ${TS_CLOUD_COMMIT}M declared against ${TS_CLOUD_RAM}M RAM (${TS_CLOUD_PCT}%)"; fi; fi']}function Fc(e){return[`TS_CLOUD_SLICE=$(systemctl show ${e} -p Slice --value 2>/dev/null || true)`,'if [ -n "$TS_CLOUD_SLICE" ] && [ "$TS_CLOUD_SLICE" != "system.slice" ]; then systemctl set-property --runtime "$TS_CLOUD_SLICE" MemoryHigh=infinity MemoryMax=infinity 2>/dev/null || true; systemctl set-property "$TS_CLOUD_SLICE" MemoryHigh=infinity MemoryMax=infinity 2>/dev/null || true; fi']}var Ke={path:"/",intervalSeconds:60,failuresBeforeRestart:3,timeoutSeconds:10,startupGraceSeconds:600,maxBackoffSeconds:3600,backoffBaseSeconds:300};function Dt(e,t,r){return typeof e==="number"&&Number.isFinite(e)?Math.max(r,Math.floor(e)):t}function ci(e){let{unitBase:t,unitPattern:r,port:n}=e,s=e.path&&e.path.startsWith("/")?e.path:Ke.path,i=Dt(e.intervalSeconds,Ke.intervalSeconds,10),o=Dt(e.failuresBeforeRestart,Ke.failuresBeforeRestart,1),a=Dt(e.timeoutSeconds,Ke.timeoutSeconds,1),c=Dt(e.startupGraceSeconds,Ke.startupGraceSeconds,0),l=Dt(e.maxBackoffSeconds,Ke.maxBackoffSeconds,0),u=Math.min(Ke.backoffBaseSeconds,l),d=`/usr/local/sbin/${t}-liveness`,m=`/run/${t}-liveness`,h=`${t}-liveness`,f=`127.0.0.1:${n}${s}`,x=r.includes("@*")?`TS_CLOUD_UNIT=$(systemctl list-units --plain --no-legend --type=service '${r}' 2>/dev/null | awk '{print $1}' | head -1)`:`TS_CLOUD_UNIT=${r}`;return[`cat > ${d} <<'TS_CLOUD_LIVENESS_EOF'`,"#!/bin/sh",`# Liveness check for ${t}, written by ts-cloud on every deploy.`,`# Settings: interval=${i}s timeout=${a}s failures=${o} startup-grace=${c}s max-backoff=${l}s`,"set -eu",`TS_CLOUD_STATE=${m}`,'exec 9>"$TS_CLOUD_STATE.lock"',"flock -n 9 || exit 0",x,'[ -n "$TS_CLOUD_UNIT" ] || exit 0','systemctl is-active --quiet "$TS_CLOUD_UNIT" || exit 0',"TS_CLOUD_RC=0",`curl -s -o /dev/null --max-time ${a} "http://${f}" || TS_CLOUD_RC=$?`,'if [ "$TS_CLOUD_RC" -eq 0 ]; then',' rm -f "$TS_CLOUD_STATE.fail"',' if [ -f "$TS_CLOUD_STATE.restarts" ]; then',` TS_CLOUD_LAST=$(awk '{print $2}' "$TS_CLOUD_STATE.restarts" 2>/dev/null || true)`,` case "$TS_CLOUD_LAST" in ''|*[!0-9]*) TS_CLOUD_LAST=0 ;; esac`,` [ $(($(date +%s) - TS_CLOUD_LAST)) -lt ${l} ] || rm -f "$TS_CLOUD_STATE.restarts"`," fi"," exit 0","fi",'case "$TS_CLOUD_RC" in'," 7) TS_CLOUD_WHY='connection refused' ;;",` 28) TS_CLOUD_WHY='no response within ${a}s' ;;`,' *) TS_CLOUD_WHY="curl exit $TS_CLOUD_RC" ;;',"esac",'TS_CLOUD_SINCE=$(systemctl show "$TS_CLOUD_UNIT" -p ActiveEnterTimestampMonotonic --value 2>/dev/null || true)',"TS_CLOUD_UPTIME=$(cut -d' ' -f1 /proc/uptime 2>/dev/null || true)",`TS_CLOUD_AGE=$(awk -v up="$TS_CLOUD_UPTIME" -v since="$TS_CLOUD_SINCE" 'BEGIN { if (since + 0 > 0 && up + 0 > 0) print int(up - since / 1000000); else print -1 }')`,`case "$TS_CLOUD_AGE" in ''|*[!0-9-]*) TS_CLOUD_AGE=-1 ;; esac`,'if [ "$TS_CLOUD_AGE" -lt 0 ]; then',' TS_CLOUD_PID=$(systemctl show "$TS_CLOUD_UNIT" -p MainPID --value 2>/dev/null || true)',` case "$TS_CLOUD_PID" in ''|0|*[!0-9]*) ;; *) TS_CLOUD_AGE=$(ps -o etimes= -p "$TS_CLOUD_PID" 2>/dev/null | tr -d ' ' || true) ;; esac`,` case "$TS_CLOUD_AGE" in ''|*[!0-9]*) TS_CLOUD_AGE=-1 ;; esac`,"fi",`if [ "$TS_CLOUD_AGE" -ge 0 ] && [ "$TS_CLOUD_AGE" -lt ${c} ]; then`,' rm -f "$TS_CLOUD_STATE.fail"',` logger -t ${h} "$TS_CLOUD_UNIT is \${TS_CLOUD_AGE}s old, inside its ${c}s startup grace; not counting this failed check on ${f} ($TS_CLOUD_WHY)"`," exit 0","fi",'TS_CLOUD_FAILS=$(cat "$TS_CLOUD_STATE.fail" 2>/dev/null || echo 0)',`case "$TS_CLOUD_FAILS" in ''|*[!0-9]*) TS_CLOUD_FAILS=0 ;; esac`,"TS_CLOUD_FAILS=$((TS_CLOUD_FAILS + 1))",'echo "$TS_CLOUD_FAILS" > "$TS_CLOUD_STATE.fail"',`if [ "$TS_CLOUD_FAILS" -lt ${o} ]; then`,` logger -t ${h} "check $TS_CLOUD_FAILS of ${o} failed on ${f} ($TS_CLOUD_WHY)"`," exit 0","fi","TS_CLOUD_NOW=$(date +%s)","TS_CLOUD_STREAK=0","TS_CLOUD_LAST=0",'[ ! -f "$TS_CLOUD_STATE.restarts" ] || read -r TS_CLOUD_STREAK TS_CLOUD_LAST < "$TS_CLOUD_STATE.restarts" || true',`case "$TS_CLOUD_STREAK" in ''|*[!0-9]*) TS_CLOUD_STREAK=0 ;; esac`,`case "$TS_CLOUD_LAST" in ''|*[!0-9]*) TS_CLOUD_LAST=0 ;; esac`,'if [ "$TS_CLOUD_STREAK" -gt 0 ]; then',` TS_CLOUD_WAIT=${u}`," TS_CLOUD_I=1",` while [ "$TS_CLOUD_I" -lt "$TS_CLOUD_STREAK" ] && [ "$TS_CLOUD_WAIT" -lt ${l} ]; do TS_CLOUD_WAIT=$((TS_CLOUD_WAIT * 2)); TS_CLOUD_I=$((TS_CLOUD_I + 1)); done`,` [ "$TS_CLOUD_WAIT" -le ${l} ] || TS_CLOUD_WAIT=${l}`," TS_CLOUD_ELAPSED=$((TS_CLOUD_NOW - TS_CLOUD_LAST))",' if [ "$TS_CLOUD_ELAPSED" -lt "$TS_CLOUD_WAIT" ]; then',` logger -t ${h} "holding off: $TS_CLOUD_STREAK liveness restart(s) in a row have not brought $TS_CLOUD_UNIT back; next restart allowed in $((TS_CLOUD_WAIT - TS_CLOUD_ELAPSED))s ($TS_CLOUD_WHY on ${f})"`," exit 0"," fi","fi","TS_CLOUD_STREAK=$((TS_CLOUD_STREAK + 1))",`logger -t ${h} "no HTTP response on ${f} after $TS_CLOUD_FAILS checks ($TS_CLOUD_WHY); restarting $TS_CLOUD_UNIT (liveness restart $TS_CLOUD_STREAK in a row)"`,'echo "$TS_CLOUD_STREAK $TS_CLOUD_NOW" > "$TS_CLOUD_STATE.restarts"','rm -f "$TS_CLOUD_STATE.fail"','systemctl restart "$TS_CLOUD_UNIT"',"TS_CLOUD_LIVENESS_EOF",`chmod +x ${d}`,`rm -f ${m}.fail ${m}.restarts`,`cat > /etc/systemd/system/${t}-liveness.service <<'TS_CLOUD_UNIT_EOF'`,"[Unit]",`Description=Liveness check for ${t} (managed by ts-cloud)`,"","[Service]","Type=oneshot",`ExecStart=${d}`,"TS_CLOUD_UNIT_EOF",`cat > /etc/systemd/system/${t}-liveness.timer <<'TS_CLOUD_UNIT_EOF'`,"[Unit]",`Description=Run the ${t} liveness check every ${i}s`,"","[Timer]",`OnUnitActiveSec=${i}s`,`OnBootSec=${i}s`,"Persistent=true","","[Install]","WantedBy=timers.target","TS_CLOUD_UNIT_EOF","systemctl daemon-reload",`systemctl enable --now ${t}-liveness.timer >/dev/null 2>&1 || true`]}var li=20,rn=3,Bc=5,jc=60;function Gc(e){let{paths:t,unitBase:r,releaseId:n,port:s}=e,i=`${r}@${n}.service`,o=`${r}.service`,a=Math.max(1,e.healthGateSeconds??5),c=e.healthCheckPath?e.healthCheckPath.startsWith("/")?e.healthCheckPath:`/${e.healthCheckPath}`:null,l=c?`curl -sf -o /dev/null --max-time ${Bc} "http://127.0.0.1:${s}${c}"`:null,u=(d)=>`{ echo "[ts-cloud] release ${n} ${d}" >&2; ts_cloud_restore_previous; exit 1; }`;return[`TS_CLOUD_OLD_UNITS=$(systemctl list-units --plain --no-legend --type=service "${r}@*.service" 2>/dev/null | awk '{print $1}' | grep -v "^${i}$" || true)`,`TS_CLOUD_PREV_CURRENT="$(readlink ${t.current} 2>/dev/null || true)"`,"TS_CLOUD_LEGACY_STOPPED=0","ts_cloud_restore_previous() {",` journalctl -u ${i} -n 50 --no-pager >&2 || true`,` systemctl stop ${i} 2>/dev/null || true`,` systemctl disable ${i} 2>/dev/null || true`,` if [ -n "$TS_CLOUD_PREV_CURRENT" ] && [ -d "$TS_CLOUD_PREV_CURRENT" ]; then ln -sfn "$TS_CLOUD_PREV_CURRENT" ${t.current}.tmp && mv -Tf ${t.current}.tmp ${t.current}; fi`,' for TS_CLOUD_RU in ${TS_CLOUD_OLD_UNITS}; do systemctl enable "$TS_CLOUD_RU" 2>/dev/null || true; systemctl start "$TS_CLOUD_RU" 2>/dev/null || true; done',` if [ "$TS_CLOUD_LEGACY_STOPPED" -eq 1 ]; then systemctl start ${o} 2>/dev/null || true; fi`,' if [ -n "$TS_CLOUD_OLD_UNITS" ] || [ "$TS_CLOUD_LEGACY_STOPPED" -eq 1 ]; then echo "[ts-cloud] previous release restored and serving" >&2; else echo "[ts-cloud] there was no previous release to restore" >&2; fi',"}",`if [ -f /etc/systemd/system/${o} ] && systemctl is-active --quiet ${o}; then echo "[ts-cloud] retiring pre-zero-downtime unit ${o} (one-time restart cutover)"; systemctl stop ${o}; TS_CLOUD_LEGACY_STOPPED=1; fi`,'TS_CLOUD_STARTED_AT="@$(date +%s)"',`systemctl restart ${i}`,`TS_CLOUD_GATE_OK=1; for TS_CLOUD_I in $(seq 1 ${a}); do sleep 1; systemctl is-active --quiet ${i} || { TS_CLOUD_GATE_OK=0; break; }; done`,'if [ "$TS_CLOUD_GATE_OK" -ne 1 ]; then',` if journalctl -u ${i} --since "$TS_CLOUD_STARTED_AT" -o cat --no-pager 2>/dev/null | grep -iE 'EADDRINUSE|address already in use|port [0-9]+ (is )?in use|Failed to start server' >/dev/null; then`,` echo "[ts-cloud] release ${n} could not share :${s} with the previous release (no SO_REUSEPORT?) — retiring it and retrying" >&2`,' for TS_CLOUD_RU in ${TS_CLOUD_OLD_UNITS}; do systemctl stop "$TS_CLOUD_RU" 2>/dev/null || true; done',` systemctl restart ${i}`,` for TS_CLOUD_I in $(seq 1 ${a}); do sleep 1; systemctl is-active --quiet ${i} || ${u("failed its health gate on the free port")}; done`," else",` ${u("crashed on start — the previous release was never stopped")}`," fi","fi",`if ss -ltnpH "sport = :${s}" >/dev/null 2>&1; then`," TS_CLOUD_BOUND=0",` for TS_CLOUD_I in $(seq 1 ${jc}); do`,` TS_CLOUD_NEW_PID="$(systemctl show -p MainPID --value ${i} 2>/dev/null || true)"`,` if [ -n "$TS_CLOUD_NEW_PID" ] && [ "$TS_CLOUD_NEW_PID" != "0" ] && ss -ltnpH "sport = :${s}" 2>/dev/null | grep "pid=$TS_CLOUD_NEW_PID," >/dev/null; then TS_CLOUD_BOUND=1; break; fi`,` systemctl is-active --quiet ${i} || break`,` sleep ${rn}`," done",` [ "$TS_CLOUD_BOUND" -eq 1 ] || ${u(`never took :${s}`)}`,"else",' echo "[ts-cloud] cannot read listener PIDs on this host — the response probes run without the bind check" >&2',"fi",...l?[`TS_CLOUD_HEALTHY=0; for TS_CLOUD_I in $(seq 1 ${li}); do if ${l}; then TS_CLOUD_HEALTHY=1; break; fi; sleep ${rn}; done`,`[ "$TS_CLOUD_HEALTHY" -eq 1 ] || ${u(`never answered ${c}`)}`]:[],...hn(t),`systemctl enable ${i} 2>/dev/null || true`,'for TS_CLOUD_U in ${TS_CLOUD_OLD_UNITS}; do systemctl stop "$TS_CLOUD_U" 2>/dev/null || true; systemctl disable "$TS_CLOUD_U" 2>/dev/null || true; done',`TS_CLOUD_LISTENING=0; for TS_CLOUD_I in $(seq 1 ${a}); do if ss -ltnH "sport = :${s}" 2>/dev/null | grep . >/dev/null; then TS_CLOUD_LISTENING=1; break; fi; sleep 1; done`,`if [ "$TS_CLOUD_LISTENING" -ne 1 ]; then echo "[ts-cloud] nothing is listening on ${s} after retiring the previous release — restarting ${i} on the now-free port" >&2; systemctl restart ${i}; for TS_CLOUD_I in $(seq 1 ${a}); do if ss -ltnH "sport = :${s}" 2>/dev/null | grep . >/dev/null; then TS_CLOUD_LISTENING=1; break; fi; sleep 1; done; fi`,`[ "$TS_CLOUD_LISTENING" -eq 1 ] || ${u(`is active but never bound :${s}`)}`,...l?[`TS_CLOUD_HEALTHY=0; for TS_CLOUD_I in $(seq 1 ${li}); do if ${l}; then TS_CLOUD_HEALTHY=1; break; fi; sleep ${rn}; done`,`[ "$TS_CLOUD_HEALTHY" -eq 1 ] || ${u(`stopped answering ${c} once it served alone`)}`]:[],...Ws(t),`systemctl list-unit-files --plain --no-legend "${r}@*.service" 2>/dev/null | awk '{print $1}' | { grep -v -e "^${i}$" -e "^${r}@\\.service$" || true; } | while read -r TS_CLOUD_U; do systemctl disable "$TS_CLOUD_U" 2>/dev/null || true; done`,`if [ -f /etc/systemd/system/${o} ]; then systemctl disable ${o} 2>/dev/null || true; rm -f /etc/systemd/system/${o}; systemctl daemon-reload; fi`]}function pi(e,t){if(!t.length)return[];let r=Ae(e.release);return[`if [ -e ${r} ] || [ -L ${r} ]; then`,...t,"fi"]}function on(e){let{siteName:t,slug:r,artifactFetch:n,releaseId:s,execStart:i,envEntries:o,port:a,keepReleases:c=_i,preStartCommands:l=[],postActivateCommands:u=[],beforeResetCommands:d=[],healthCheckPath:m,healthGateSeconds:h=5,liveness:f,memoryHigh:S="auto",memoryMax:x,cpuWeight:B,ioWeight:L,tasksMax:N,stopTimeout:D}=e,E=[...B!=null?[`CPUWeight=${B}`]:[],...L!=null?[`IOWeight=${L}`]:[],...N!=null?[`TasksMax=${N}`]:[],...D?[`TimeoutStopSec=${D}`]:[]],M=e.zeroDowntime??a!=null,H=e.appDir??`/var/www/${t}`,F=Ht(H,s),X=`${r}-${t}`,V=`${X}.service`,Y=It(r,t,s),te=e.sharedPaths??[],Z=Jc([".env",...te,...Zt(o,te)]),J=Jt(o),ie=ui(F.release,l,o),ce=["set -euo pipefail",...zs(F),...n,...$i(F,Z),...pi(F,d),Gs(F),...Hs(F),`tar xzf ${Y} -C "$TS_CLOUD_STAGED"`,`rm -f ${Y}`,`cat > ${F.shared}/.env <<'TS_CLOUD_ENV_EOF'`,J,"TS_CLOUD_ENV_EOF",`chmod 600 ${F.shared}/.env`,...qs(F),`find ${F.release} -maxdepth 1 \\( -type f -o -type l \\) -name '.env*' ! -name '.env.example' ! -name '*.example' -delete`,`sed -i -E '/^[[:space:]]*(PORT|PORT_BACKEND|PORT_ADMIN|PORT_FRONTEND)[[:space:]]*=/d' ${F.shared}/.env 2>/dev/null || true`,..._o(F,Z),...ie,...e.isolation?Xs(e.slug||t,F.base,Z.flatMap((y)=>typeof y==="string"||!y.target?[]:[y.target])):[]];if(M&&a!=null){let y=`${X}@${s}.service`,k=m?m.startsWith("/")?m:`/${m}`:null;return[...ce,`cat > /etc/systemd/system/${X}@.service <<'TS_CLOUD_UNIT_EOF'`,"[Unit]",`Description=${t} release %i (managed by ts-cloud)`,"After=network.target","","[Service]","Type=simple",...e.isolation?xt(st(e.slug||t),F.base,e.slug||t):[],`WorkingDirectory=${F.releases}/%i`,`ExecStart=${i}`,"Restart=always","RestartSec=5","MemoryAccounting=true",...S&&S!=="auto"?[`MemoryHigh=${S}`]:[],...x?[`MemoryMax=${x}`]:[],...E,`EnvironmentFile=${F.releases}/%i/.env`,`Environment=PORT=${a}`,`Environment=STX_IMAGE_CACHE_DIR=${xo(F)}`,`Environment=STX_BUNDLE_CACHE_DIR=${Oi(F)}`,`Environment=STX_BUNDLE_CACHE_DIR=${Oi(F)}`,"","[Install]","WantedBy=multi-user.target","TS_CLOUD_UNIT_EOF","systemctl daemon-reload",...S==="auto"?ai(`${X}@.service`):[...tr(`${X}@.service`),...nn()],...Fc(y),...Gc({paths:F,unitBase:X,releaseId:s,port:a,healthCheckPath:k,healthGateSeconds:h}),...u,...yn(F,c),...f===!1?[]:ci({...f,unitBase:X,unitPattern:`${X}@*.service`,port:a,path:f?.path??m})]}return[...ce,`cat > /etc/systemd/system/${V} <<'TS_CLOUD_UNIT_EOF'`,"[Unit]",`Description=${t} (managed by ts-cloud)`,"After=network.target","","[Service]","Type=simple",...e.isolation?xt(st(e.slug||t),F.base,e.slug||t):[],`WorkingDirectory=${F.current}`,`ExecStart=${i}`,"Restart=always","RestartSec=5","MemoryAccounting=true",...S&&S!=="auto"?[`MemoryHigh=${S}`]:[],...x?[`MemoryMax=${x}`]:[],...E,`EnvironmentFile=${F.current}/.env`,...a?[`Environment=PORT=${a}`]:[],`Environment=STX_IMAGE_CACHE_DIR=${xo(F)}`,`Environment=STX_BUNDLE_CACHE_DIR=${Oi(F)}`,"","[Install]","WantedBy=multi-user.target","TS_CLOUD_UNIT_EOF","systemctl daemon-reload",...tr(V,{auto:S!=="auto",high:S!=="auto",max:Boolean(x)}),...S==="auto"?ai(V):nn(),`systemctl enable ${V}`,`TS_CLOUD_PREV_CURRENT="$(readlink ${F.current} 2>/dev/null || true)"`,...hn(F),`systemctl restart ${V}`,`for TS_CLOUD_I in $(seq 1 ${Math.max(1,h)}); do sleep 1; systemctl is-active --quiet ${V} || { echo "[ts-cloud] release ${s} of ${V} did not stay up — restoring the previous release" >&2; journalctl -u ${V} -n 50 --no-pager >&2 || true; if [ -n "$TS_CLOUD_PREV_CURRENT" ] && [ -d "$TS_CLOUD_PREV_CURRENT" ] && [ "$(readlink -f "$TS_CLOUD_PREV_CURRENT")" != "$(readlink -f ${F.release})" ]; then ln -sfn "$TS_CLOUD_PREV_CURRENT" ${F.current}.tmp && mv -Tf ${F.current}.tmp ${F.current}; systemctl restart ${V} || true; fi; exit 1; }; done`,...Ws(F),...u,...yn(F,c),...f===!1||!a?[]:ci({...f,unitBase:X,unitPattern:V,port:a,path:f?.path??m})]}function an(e){let{siteName:t,artifactFetch:r,releaseId:n,keepReleases:s=_i,preStartCommands:i=[],postActivateCommands:o=[],beforeResetCommands:a=[]}=e,c=e.appDir??`/var/www/${t}`,l=Ht(c,n),u=It(e.slug,t,n),d=ui(l.release,i);return["set -euo pipefail",...zs(l),...r,...$i(l,[]),...pi(l,a),Gs(l),...Hs(l),`tar xzf ${u} -C "$TS_CLOUD_STAGED"`,...qs(l),`rm -f ${u}`,...d,...hn(l),...o,...yn(l,s)]}function It(e,t,r){return`/tmp/${[e,t,r].filter(Boolean).join("-")}-release.tar.gz`}function fn(e,t,r,n){return[`aws s3 cp "s3://${e}/${t}" ${n} --region ${r}`]}function gn(e,t){return[`mv "${e}" ${t}`]}function Ae(e){return`'${e.replace(/'/g,`'"'"'`)}'`}function rr(e,t,r){return["set -euo pipefail",`chmod 600 ${Ae(e)}`,`mv -f -- ${Ae(e)} ${Ae(t)}`,`cp -- ${Ae(t)} ${Ae(r)}`].join(`
|
|
277
279
|
`)}function nr(e){return`rm -f -- ${Ae(e)} 2>/dev/null || true`}function Sn(e={}){let t=Math.max(1,e.appServers??e.instances??1),r=!!e.servicesServer||t>1,n=t>1||!!e.server?.loadBalancer;return{appServers:t,loadBalancer:n,dedicatedServices:r,servicesOnApp:!r}}function di(e,t){let r={REDIS_HOST:e,MEILISEARCH_HOST:`http://${e}:7700`};if(t?.name){if(r.DB_CONNECTION=t.engine==="postgres"?"pgsql":"mysql",r.DB_HOST=e,r.DB_PORT=String(t.port??(t.engine==="postgres"?5432:3306)),r.DB_DATABASE=t.name,t.username)r.DB_USERNAME=t.username;if(t.password)r.DB_PASSWORD=t.password}return r}function vn(e){let t=e.infrastructure?.compute??{},r=A(e);return[...Do(t.managedServices??{mysql:!0,redis:!0},{bindPrivate:!0}),...wt(r,t.managedServices??{mysql:!0}),...Ee(!0),...Pe(!0),...Qe(t.sshKeys),...ze(e.notifications),...t.backups?.enabled?[...ko({database:r,backups:t.backups})]:[]]}var sr=["-S","ssh"];function mi(e={},t="ssh"){let r=[];if(e.identityFile)r.push("-i",e.identityFile);if(e.port)r.push(t==="scp"?"-P":"-p",String(e.port));return r.push("-o","StrictHostKeyChecking=no","-o","UserKnownHostsFile=/dev/null","-o","LogLevel=ERROR","-o",`ConnectTimeout=${e.connectTimeoutSec??10}`),r}async function wn(e,t,r={}){let n=Bun.spawn(["ssh",...mi(r),`${r.user??"root"}@${e}`,t],{stdout:"pipe",stderr:"pipe"}),[s,i]=await Promise.all([new Response(n.stdout).text(),new Response(n.stderr).text()]);return{code:await n.exited,stdout:s,stderr:i}}async function _e(e,t,r={}){let n=await wn(e,t,r);if(n.code!==0)throw Error(`ssh \`${t}\` on ${e} failed (${n.code}): ${n.stderr.trim()||n.stdout.trim()}`);return n.stdout}async function at(e,t={}){let r=t.timeoutMs??180000,n=t.pollIntervalMs??5000,s=Date.now();while(Date.now()-s<r){let i=await wn(e,"echo ready",t);if(i.code===0&&i.stdout.includes("ready"))return;await new Promise((o)=>setTimeout(o,n))}throw Error(`SSH not ready on ${e} after ${r}ms`)}function At(e={}){return["set -e","ts_cloud_script=$(mktemp /tmp/ts-cloud-deploy.XXXXXXXX)",`trap 'rm -f "$ts_cloud_script"' EXIT`,'cat > "$ts_cloud_script"',e.sudo?'sudo -n -H bash "$ts_cloud_script" < /dev/null':'bash "$ts_cloud_script" < /dev/null'].join(`
|
|
278
|
-
`)}var $n=At();import{homedir as
|
|
280
|
+
`)}var $n=At();import{homedir as Wc}from"node:os";import{join as qc}from"node:path";var ir={location:"fsn1",image:"ubuntu-24.04",sshUser:"root",sshPrivateKeyPath:"~/.ssh/id_ed25519"};function ln(e){return e.startsWith("~/")?qc(Wc(),e.slice(2)):e}function ct(...e){for(let t of e){let r=process.env[t]?.trim();if(r)return r}return}function lt(...e){for(let t of e){let r=t?.trim();if(r)return r}return}function Lt(e,t){return lt(e,t?.hetzner?.apiToken,ct("HCLOUD_TOKEN","HETZNER_API_TOKEN"))}function _n(e,t){return lt(t,e?.hetzner?.location,ct("HCLOUD_LOCATION","HETZNER_LOCATION"))??ir.location}function kt(e,t){let r=e?.infrastructure?.compute;return lt(t,r?.image,e?.hetzner?.image,ct("HCLOUD_IMAGE","HETZNER_IMAGE"))??ir.image}function Xc(e,t){return lt(t,e?.hetzner?.sshUser,ct("HCLOUD_SSH_USER","HETZNER_SSH_USER"))??ir.sshUser}function fi(e,t){return ln(lt(t,e?.hetzner?.sshPrivateKeyPath,ct("HCLOUD_SSH_KEY","HETZNER_SSH_KEY"))??ir.sshPrivateKeyPath)}function Yc(e,t,r){let n=lt(t,e?.hetzner?.sshPublicKeyPath,ct("HCLOUD_SSH_PUBLIC_KEY","HETZNER_SSH_PUBLIC_KEY"));if(n)return ln(n);return`${r??fi(e)}.pub`}function St(e,t={}){let r=fi(e,t.sshPrivateKeyPath);return{apiToken:Lt(t.apiToken,e),location:_n(e,t.location),image:kt(e,t.image),sshUser:Xc(e,t.sshUser),sshPrivateKeyPath:r,sshPublicKeyPath:Yc(e,t.sshPublicKeyPath,r)}}var Zc="https://api.hetzner.cloud/v1";class or extends Error{status;code;details;constructor(e,t,r,n){super(e);this.status=t;this.code=r;this.details=n;this.name="HetznerApiError"}}class ar extends Error{action;constructor(e,t){super(e);this.action=t;this.name="HetznerActionError"}}function gi(e){return e.map((t)=>({type:"server",server:{id:t.server}}))}class Oe{name="hetzner";apiToken;baseUrl;fetchImpl;constructor(e){this.apiToken=e.apiToken,this.baseUrl=e.baseUrl??Zc,this.fetchImpl=e.fetchImpl??fetch}async request(e,t,r){let n=await this.fetchImpl(`${this.baseUrl}${t}`,{method:e,headers:{Authorization:`Bearer ${this.apiToken}`,"Content-Type":"application/json"},body:r===void 0?void 0:JSON.stringify(r)}),s=await n.text(),i;try{i=s?JSON.parse(s):{}}catch{if(!n.ok){let o=s.trim().slice(0,200)||n.statusText||"Hetzner API error";throw Error(`Hetzner API ${e} ${t} (${n.status}): ${o}`)}throw Error(`Hetzner API ${e} ${t}: unexpected non-JSON response`)}if(!n.ok){let o=i.error?.message||n.statusText||"Hetzner API error",a=i.error?.code?` [${i.error.code}]`:"";throw new or(`Hetzner API ${e} ${t} (${n.status})${a}: ${o}`,n.status,i.error?.code,i.error?.details)}return i}async requestAll(e,t){let r=[],n=1;for(;;){let s=e.includes("?")?"&":"?",i=await this.request("GET",`${e}${s}per_page=50&page=${n}`);r.push(...i[t]??[]);let o=i.meta?.pagination?.next_page;if(!o)return r;n=o}}async listServers(){return this.requestAll("/servers","servers")}async getLocation(e){let r=(await this.request("GET",`/locations?name=${encodeURIComponent(e)}`)).locations.find((n)=>n.name===e);if(!r)throw Error(`Hetzner location not found: ${e}`);return r}async getServer(e){return(await this.request("GET",`/servers/${e}`)).server}async listVolumes(){return this.requestAll("/volumes","volumes")}async getVolume(e){return(await this.request("GET",`/volumes/${e}`)).volume}async createVolume(e){return this.request("POST","/volumes",{...e,automount:!1})}async attachVolume(e,t){return(await this.request("POST",`/volumes/${e}/actions/attach`,{server:t,automount:!1})).action}async resizeVolume(e,t){return(await this.request("POST",`/volumes/${e}/actions/resize`,{size:t})).action}async protectVolume(e,t){return(await this.request("POST",`/volumes/${e}/actions/change_protection`,{delete:t})).action}async getServerMetrics(e,t){let r=new URLSearchParams({type:t.types.join(","),start:t.start.toISOString(),end:t.end.toISOString(),step:String(Math.max(60,Math.floor(t.step??60)))});return(await this.request("GET",`/servers/${e}/metrics?${r}`)).metrics}async listServerTypes(e){let t=e?`?name=${encodeURIComponent(e)}`:"";return this.requestAll(`/server_types${t}`,"server_types")}async getServerType(e){return(await this.listServerTypes(e)).find((t)=>t.name===e)??null}async createServer(e){let t=await this.request("POST","/servers",{name:e.name,server_type:e.serverType,image:e.image,location:e.location,datacenter:e.datacenter,ssh_keys:e.sshKeys,user_data:e.userData,labels:e.labels,firewalls:e.firewalls,networks:e.networks,start_after_create:!0});return{server:t.server,action:t.action}}async listNetworks(){return this.requestAll("/networks","networks")}async createNetwork(e){let t=e.ipRange??"10.0.0.0/16";return(await this.request("POST","/networks",{name:e.name,ip_range:t,subnets:[{type:"cloud",ip_range:t,network_zone:"eu-central"}],labels:e.labels})).network}async deleteNetwork(e){await this.request("DELETE",`/networks/${e}`)}async listLoadBalancers(){return this.requestAll("/load_balancers","load_balancers")}async createLoadBalancer(e){return(await this.request("POST","/load_balancers",{name:e.name,load_balancer_type:e.type??"lb11",location:e.location,network_zone:e.network?void 0:e.networkZone??"eu-central",network:e.network,labels:e.labels,targets:[{type:"label_selector",label_selector:{selector:e.labelSelector},use_private_ip:!!e.network}],services:e.services.map((r)=>({protocol:r.protocol??"tcp",listen_port:r.listenPort,destination_port:r.destinationPort,health_check:{protocol:"http",port:80,interval:15,timeout:10,retries:3,http:{path:"/",status_codes:["2??","3??"]}}}))})).load_balancer}async deleteLoadBalancer(e){await this.request("DELETE",`/load_balancers/${e}`)}async deleteServer(e){return(await this.request("DELETE",`/servers/${e}`)).action}async listFirewalls(){return this.requestAll("/firewalls","firewalls")}async createFirewall(e){let t=await this.request("POST","/firewalls",{name:e.name,rules:e.rules,labels:e.labels,apply_to:e.applyTo?gi(e.applyTo):void 0});return{firewall:t.firewall,actions:t.actions}}async setFirewallRules(e,t){return(await this.request("POST",`/firewalls/${e}/actions/set_rules`,{rules:t})).actions??[]}async deleteFirewall(e){await this.request("DELETE",`/firewalls/${e}`)}async applyFirewallToResources(e,t){return(await this.request("POST",`/firewalls/${e}/actions/apply_to_resources`,{apply_to:gi(t)})).actions}async listSshKeys(){return this.requestAll("/ssh_keys","ssh_keys")}async createSshKey(e){return(await this.request("POST","/ssh_keys",{name:e.name,public_key:e.publicKey,labels:e.labels})).ssh_key}async deleteSshKey(e){await this.request("DELETE",`/ssh_keys/${e}`)}async getAction(e){return(await this.request("GET",`/actions/${e}`)).action}async shutdownServer(e){return(await this.request("POST",`/servers/${e}/actions/shutdown`,{})).action}async powerOnServer(e){return(await this.request("POST",`/servers/${e}/actions/poweron`,{})).action}async changeServerType(e,t,r){return(await this.request("POST",`/servers/${e}/actions/change_type`,{server_type:t,upgrade_disk:r})).action}async powerOffServer(e){return(await this.request("POST",`/servers/${e}/actions/poweroff`,{})).action}async stopServer(e,t){if((await this.getServer(e)).status==="off")return;await this.shutdownServer(e);let n=t?.gracefulWaitMs??120000,s=t?.pollIntervalMs??3000,i=Date.now();while(Date.now()-i<n){if((await this.getServer(e)).status==="off")return;await new Promise((a)=>setTimeout(a,s))}let o=await this.powerOffServer(e);await this.waitForAction(o.id),await this.waitForServerStatus(e,"off")}async renameServer(e,t){return(await this.request("PUT",`/servers/${e}`,{name:t})).server}async listImages(e){let t=e?.type?`?type=${encodeURIComponent(e.type)}`:"";return this.requestAll(`/images${t}`,"images")}async getImage(e){return(await this.request("GET",`/images/${e}`)).image}async createImage(e,t){return this.request("POST",`/servers/${e}/actions/create_image`,{description:t.description,type:t.type??"snapshot",labels:t.labels})}async deleteImage(e){await this.request("DELETE",`/images/${e}`)}async rebuildServer(e,t){return(await this.request("POST",`/servers/${e}/actions/rebuild`,{image:t})).action}async listPrimaryIps(){return this.requestAll("/primary_ips","primary_ips")}async unassignPrimaryIp(e){return(await this.request("POST",`/primary_ips/${e}/actions/unassign`,{})).action}async assignPrimaryIp(e,t){return(await this.request("POST",`/primary_ips/${e}/actions/assign`,{assignee_id:t,assignee_type:"server"})).action}async waitForAction(e,t){let r=t?.pollIntervalMs??2000,n=t?.maxWaitMs??300000,s=Date.now();while(Date.now()-s<n){let i=await this.getAction(e);if(i.status==="success")return i;if(i.status==="error")throw new ar(i.error?.message||"Hetzner action failed",i);await new Promise((o)=>setTimeout(o,r))}throw Error(`Timed out waiting for Hetzner action ${e}`)}async waitForServerRunning(e,t){let r=t?.pollIntervalMs??3000,n=t?.maxWaitMs??600000,s=Date.now();while(Date.now()-s<n){let i=await this.getServer(e);if(i.status==="running")return i;await new Promise((o)=>setTimeout(o,r))}throw Error(`Timed out waiting for server ${e} to reach running state`)}async waitForServerStatus(e,t,r){let n=r?.pollIntervalMs??3000,s=r?.maxWaitMs??600000,i=Date.now();while(Date.now()-i<s){let o=await this.getServer(e);if(o.status===t)return o;await new Promise((a)=>setTimeout(a,n))}throw Error(`Timed out waiting for server ${e} to reach ${t} state`)}}function Cr(e,t){let r=Lt(e,t);if(!r)throw Error("Hetzner API token required. Set hetzner.apiToken in cloud.config.ts or HCLOUD_TOKEN / HETZNER_API_TOKEN.");return r}function je(e){let[t,r]=e.trim().split(/\s+/);return r?`${t} ${r}`:t}function hi(e){if(!e)return"";return e.endsWith(`
|
|
279
281
|
`)?e:`${e}
|
|
280
282
|
`}function Ce(e,t={}){let r=t.scriptPath??"/var/lib/cloud/ts-cloud-bootstrap.sh",n=t.permissions??"0755",s=e.split(`
|
|
281
283
|
`).map((i)=>` ${i}`).join(`
|
|
@@ -288,7 +290,7 @@ ${hi(t.before)}write_files:
|
|
|
288
290
|
${s}
|
|
289
291
|
runcmd:
|
|
290
292
|
- [ bash, ${r} ]
|
|
291
|
-
${hi(t.after)}`}function dn(e){let t=new Set([80,443,...e.sitePorts,...e.allowedPorts??[]]);if(e.allowSsh)t.add(22);return[...t].map((r)=>({direction:"in",protocol:"tcp",port:String(r),source_ips:["0.0.0.0/0","::/0"],description:`ts-cloud port ${r}`}))}var Tn={micro:"cpx11",small:"cx23",medium:"cx33",large:"cx43",xlarge:"cx53","2xlarge":"ccx33"};function tt(e){if(!e)return Tn.micro;if(e in Tn)return Tn[e];return e}var oe="ts-cloud";function ae(e,t,r="app"){return{[`${oe}/project`]:e,[`${oe}/environment`]:t,[`${oe}/role`]:r,[`${oe}/managed-by`]:"ts-cloud"}}function Se(e,t,r,n="app"){return Cn(e,t,r)&&e[`${oe}/role`]===n}function Cn(e,t,r){if(!e)return!1;return e[`${oe}/project`]===t&&e[`${oe}/environment`]===r}function
|
|
293
|
+
${hi(t.after)}`}function dn(e){let t=new Set([80,443,...e.sitePorts,...e.allowedPorts??[]]);if(e.allowSsh)t.add(22);return[...t].map((r)=>({direction:"in",protocol:"tcp",port:String(r),source_ips:["0.0.0.0/0","::/0"],description:`ts-cloud port ${r}`}))}var Tn={micro:"cpx11",small:"cx23",medium:"cx33",large:"cx43",xlarge:"cx53","2xlarge":"ccx33"};function tt(e){if(!e)return Tn.micro;if(e in Tn)return Tn[e];return e}var oe="ts-cloud";function ae(e,t,r="app"){return{[`${oe}/project`]:e,[`${oe}/environment`]:t,[`${oe}/role`]:r,[`${oe}/managed-by`]:"ts-cloud"}}function Se(e,t,r,n="app"){return Cn(e,t,r)&&e[`${oe}/role`]===n}function Cn(e,t,r){if(!e)return!1;return e[`${oe}/project`]===t&&e[`${oe}/environment`]===r}function Qc(e){return e.location?.name??e.datacenter?.location.name??"unknown"}function ut(e){return typeof e==="number"&&Number.isFinite(e)?e:0}function tl(e,t){if(e.architecture&&t.architecture&&e.architecture!==t.architecture)throw Error(`Cannot resize ${e.name} (${e.architecture}) to ${t.name} (${t.architecture}) in place.`);let r=[];if(ut(t.cores)<ut(e.cores))r.push("CPU");if(ut(t.memory)<ut(e.memory))r.push("memory");if(ut(t.disk)<ut(e.disk))r.push("disk");if(r.length>0)throw Error(`Refusing an in-place downgrade from ${e.name} to ${t.name}: ${r.join(", ")} would shrink.`)}async function rt(e,t,r,n=!0){let[s,i]=await Promise.all([e.getServer(t),e.getServerType(r)]);if(!i)throw Error(`Hetzner server type ${r} was not found.`);let o=s.server_type,a=o.name===i.name;if(!a)tl(o,i);let c=Qc(s),l=i.locations?.find((u)=>u.name===c);return{serverId:t,serverName:s.name,location:c,currentType:o,targetType:i,capacityAvailable:l?.available!==!1,alreadyComplete:a,upgradeDisk:n}}function yi(e){if(e instanceof or&&e.code==="resource_unavailable")return!0;if(e instanceof ar){if(e.action.error?.code==="resource_unavailable")return!0;if(/no fitting vhost|placement|free resources/i.test(e.action.error?.message??""))return!0}return/resource_unavailable|no fitting vhost|error during placement|free resources/i.test(e instanceof Error?e.message:String(e))}async function Re(e,t,r){await e?.onPhase?.(t,r)}async function Pn(e,t,r){await e.waitForAction(t.id,{pollIntervalMs:r.actionPollIntervalMs,maxWaitMs:r.actionTimeoutMs})}var cr=async({client:e,hooks:t,actionPollIntervalMs:r,actionTimeoutMs:n,serverPollIntervalMs:s,serverTimeoutMs:i},o,a,c)=>{let l=await e.getServer(o.serverId);if(l.status!=="running"){await Re(t,c?"recovering":"powering-on",{serverId:o.serverId});let d=await e.powerOnServer(o.serverId);await Pn(e,d,{actionPollIntervalMs:r,actionTimeoutMs:n}),l=await e.waitForServerRunning(o.serverId,{pollIntervalMs:s,maxWaitMs:i})}await Re(t,"verifying",{serverId:o.serverId,recovered:c});let u=await t?.afterBoot?.(l,o.targetType,a,{recovered:c});return{server:l,verification:u}};async function Tr(e){let{client:t,hooks:r}=e,n=e.upgradeDisk??!0;await Re(r,"planning",{serverId:e.serverId,targetType:e.targetType});let s=await rt(t,e.serverId,e.targetType,n),i=await t.getServer(s.serverId);if(s.alreadyComplete){let c=await cr(e,s,e.manifest,!1);return await Re(r,"complete",{alreadyComplete:!0}),{status:"already-complete",plan:s,verification:c.verification}}if(!s.capacityAvailable&&!e.attemptUnavailable)return await Re(r,"waiting-capacity",{location:s.location,targetType:s.targetType.name}),{status:"waiting-capacity",plan:s};let o=e.manifest;if(i.status==="off"&&!e.resumeFromOff){let c=await cr(e,s,void 0,!0);return{status:"recovered",plan:s,server:c.server,verification:c.verification,retryable:!0,error:"Server was already off without a resumable resize checkpoint; powered it back on."}}if(i.status==="running"){await Re(r,"preflight",{serverId:s.serverId}),o=await r?.preflight?.(i,s.targetType),await Re(r,"shutting-down",{serverId:s.serverId});let c=await t.shutdownServer(s.serverId);await Pn(t,c,e),i=await t.waitForServerStatus(s.serverId,"off",{pollIntervalMs:e.serverPollIntervalMs,maxWaitMs:e.serverTimeoutMs})}try{await Re(r,"resizing",{serverId:s.serverId,from:s.currentType.name,to:s.targetType.name,upgradeDisk:n});let c=await t.changeServerType(s.serverId,s.targetType.name,n);await Pn(t,c,e)}catch(c){let l=await cr(e,s,o,!0);return{status:"recovered",plan:s,server:l.server,manifest:o,verification:l.verification,retryable:yi(c),error:c instanceof Error?c.message:String(c)}}let a=await cr(e,s,o,!1);if(a.verification&&!a.verification.ok)throw Error(`Resize completed but verification failed: ${(a.verification.failures??[]).join("; ")}`);return await Re(r,"complete",{serverId:s.serverId,targetType:s.targetType.name}),{status:"completed",plan:s,server:a.server,manifest:o,verification:a.verification}}var rl=String.raw`
|
|
292
294
|
const { readdir, readFile, readlink, stat } = await import('node:fs/promises')
|
|
293
295
|
|
|
294
296
|
const text = (command) => {
|
|
@@ -380,7 +382,7 @@ console.log(JSON.stringify({
|
|
|
380
382
|
persistentData: persistentData.sort(),
|
|
381
383
|
dataCatalog: dataCatalog.sort(),
|
|
382
384
|
}))
|
|
383
|
-
`;function
|
|
385
|
+
`;function nl(e){return`printf %s ${Buffer.from(e).toString("base64")} | base64 -d | bun -`}async function sl(e){let t=await _e(e.host,nl(rl),e);return JSON.parse(t)}async function il(e,t){let r="";for(let n=1;n<=3;n++){let s=Bun.spawn(["curl","--location","--silent","--show-error","--output","/dev/null","--max-time",String(Math.max(1,Math.ceil(t/1000))),"--write-out","%{http_code}",`https://${e}/`],{stdout:"pipe",stderr:"pipe"}),[i,o,a]=await Promise.all([new Response(s.stdout).text(),new Response(s.stderr).text(),s.exited]),c=Number.parseInt(i.trim(),10);if(a===0&&c>0)return{domain:e,ok:c<500,status:c};if(r=o.trim()||`curl exited ${a}`,n<3)await Bun.sleep(500*n)}return{domain:e,ok:!1,error:r}}async function ol(e,t){let r=[],n=0,s=Array.from({length:Math.min(4,e.length)},async()=>{for(;;){let i=n++,o=e[i];if(!o)return;r[i]=await il(o,t)}});return await Promise.all(s),r}async function un(e){let t=await sl(e),r=await ol(t.routeDomains,e.routeTimeoutMs??15000);return{...t,routeProbes:r}}async function wi(e){let t=await un(e),r=t.routeProbes.filter((n)=>!n.ok);if(t.failedUnits.length>0)throw Error(`Preflight found failed services: ${t.failedUnits.join(", ")}`);if(r.length>0)throw Error(`Preflight found unhealthy routes: ${r.map((n)=>n.domain).join(", ")}`);return await _e(e.host,String.raw`systemctl list-units --type=service --state=running --no-legend --plain | awk '{print $1}' | while read -r unit; do if systemctl cat "$unit" 2>/dev/null | grep -Eq '/var/www|/usr/local/bin/rpx|/opt/ts-cloud'; then systemctl enable "$unit" >/dev/null 2>&1 || true; fi; done; systemctl daemon-reload; sync`,e),t}function pt(e,t){let r=new Set(t);return e.filter((n)=>!r.has(n))}function al(e){return Math.floor((e.memory??0)*1024*1024*1024)}async function cl(e){await _e(e.host,String.raw`set -e
|
|
384
386
|
root_source="$(findmnt -n -o SOURCE /)"
|
|
385
387
|
root_fs="$(findmnt -n -o FSTYPE /)"
|
|
386
388
|
parent="$(lsblk -n -o PKNAME "$root_source" | head -1)"
|
|
@@ -392,18 +394,18 @@ case "$root_fs" in
|
|
|
392
394
|
ext2|ext3|ext4) resize2fs "$root_source" ;;
|
|
393
395
|
xfs) xfs_growfs / ;;
|
|
394
396
|
*) echo "Unsupported root filesystem: $root_fs" >&2; exit 1 ;;
|
|
395
|
-
esac`,e)}async function
|
|
396
|
-
`)).toString("base64");await _e(e.host,`printf %s ${r} | base64 -d | while read -r unit; do [ -z "$unit" ] || systemctl is-active --quiet "$unit" || systemctl start "$unit"; done`,e)}async function Ai(e,t,r,n={recovered:!1}){if(await at(e.host,{...e,timeoutMs:e.sshTimeoutMs??600000}),!n.recovered)await
|
|
397
|
-
`,"utf8"),await
|
|
398
|
-
`))}if(l?.capacityAvailable&&!l.alreadyComplete){let f={host:m[0].publicIp,user:this.sshUser,identityFile:this.sshPrivateKeyPath},S=await Tr({client:this.client,serverId:a.id,targetType:l.targetType.name,upgradeDisk:l.upgradeDisk,hooks:{preflight:()=>wi(f),afterBoot:(x,B,L,N)=>Ai(f,B,L,N)}});if(S.status!=="completed"&&S.status!=="already-complete")throw Error(`Resize did not complete: ${S.status}`)}return u}constructor(e={}){this.residency=e.residency;let t=Lt(e.apiToken);if(this.stateOnly=!e.client&&!t&&e.allowStateOnly===!0,!e.client&&!t&&!this.stateOnly)throw Error("Hetzner API token required. Set hetzner.apiToken in cloud.config.ts or HCLOUD_TOKEN / HETZNER_API_TOKEN.");this.client=e.client??new Oe({apiToken:t??""});let r=St(void 0,{sshPrivateKeyPath:e.sshPrivateKeyPath,sshPublicKeyPath:e.sshPublicKeyPath,sshUser:e.sshUser,location:e.location});this.sshPrivateKeyPath=r.sshPrivateKeyPath,this.sshPublicKeyPath=r.sshPublicKeyPath,this.sshUser=r.sshUser,this.location=r.location,this.waitForBoot=e.waitForBoot??!0,this.bootWait={sshIntervalMs:e.bootWait?.sshIntervalMs??5000,sshTimeoutMs:e.bootWait?.sshTimeoutMs??300000,cloudInitIntervalMs:e.bootWait?.cloudInitIntervalMs??5000,cloudInitTimeoutMs:e.bootWait?.cloudInitTimeoutMs??600000}}async provisionComputeInfrastructure(e){let{config:t,environment:r}=e,n=t.project.slug,s=t.infrastructure?.compute;if(!s)throw Error("infrastructure.compute is required to provision Hetzner compute");if(t.cloud?.attachTo)return this.attachToComputeInfrastructure(t.cloud.attachTo,t,r);let i=T(t,r),o=`${n}-${r}-app`,a=s.runtime==="php"||!!s.php,c=Sn(s),l=t.cloud?.residency||this.residency,{assertComputeResidency:u}=await import("./chunk-h28tfah9.js"),d=(y)=>{if(l)u(l,{country:y.location?.country||y.datacenter?.location?.country,location:y.location?.name||y.datacenter?.location?.name})};if(l){if(c.dedicatedServices||c.appServers>1)throw Error("Residency policy currently requires single-host compute");let y=await this.client.getLocation(t.hetzner?.location||this.location);u(l,{country:y.country,location:y.name})}if(c.dedicatedServices||c.appServers>1)return a?this.provisionFleet(e,c):this.provisionBunFleet(e,c);let m=ae(n,r,"app"),h=t.sites||{},f=ee(s)?[]:this.collectUpstreamPorts(h),S=`${n}-${r}-app-fw`,{firewall:x}=await this.ensureFirewall(S,m,dn({allowSsh:s.allowSsh!==!1,sitePorts:f,allowedPorts:[...s.firewall?.allowedPorts??[],...Rs(t.infrastructure?.sftp),...jr(ne(t,{environment:r}))]})),B=await this.ensureSshKey(n,r,m),L=await ge(i);if(L?.serverId){let y=await this.tryGetServer(L.serverId);if(y&&y.status!=="off"&&y.labels?.["ts-cloud/project"]===n&&y.labels?.["ts-cloud/environment"]===r){if(d(y),s.resize||s.volumes||s.deploymentStorage)await this.applyComputeChanges(e);return this.outputsFromState(L,y)}}let N=await this.findExistingServer(n,r,o);if(N&&N.status!=="off"){d(N);let y={provider:"hetzner",stackName:i,serverId:N.id,serverName:N.name,publicIp:N.public_net.ipv4?.ip,publicIpv6:me(N.public_net.ipv6?.ip),deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};if(await vt(i,y),s.resize||s.volumes||s.deploymentStorage)await this.applyComputeChanges(e);return this.outputsFromState(y,N)}let D=le(t,{environment:r}),E=D?.engine==="rpx"?$e({proxy:D,config:Te(h,{proxy:D,slug:t.project.slug,mail:ne(t,{environment:r})}),slug:t.project.slug,bunBin:s.runtime==="node"||s.runtime==="deno"?void 0:"/usr/local/bin/bun"}):void 0,M=Fe(t,{environment:r}),H=s.bakedImage===!0,F=ye({runtime:M.runtime,runtimeVersion:M.runtimeVersion,systemPackages:s.systemPackages,database:t.infrastructure?.database,phpProvision:M.phpProvision,servicesProvision:M.servicesProvision,rpxProvision:E,baked:H,swapGb:s.swapGb}),X=Ce(F),V=tt(s.size),Y=kt(t),{server:te,action:Z}=await this.client.createServer({name:o,serverType:V,image:Y,location:t.hetzner?.location||this.location,userData:X,labels:m,sshKeys:B?[B]:void 0,firewalls:[{firewall:x.id}]});await this.client.waitForAction(Z.id);let J=await this.client.waitForServerRunning(te.id);d(J);let ie={provider:"hetzner",stackName:i,serverId:J.id,serverName:J.name,firewallId:x.id,publicIp:J.public_net.ipv4?.ip,publicIpv6:me(J.public_net.ipv6?.ip),deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};await vt(i,ie);let ce=J.public_net.ipv4?.ip;if(ce&&this.waitForBoot)await this.waitForSshReady(ce),await this.waitForCloudInit(ce);if(s.resize||s.volumes||s.deploymentStorage)await this.applyComputeChanges(e);return this.outputsFromState(ie,J)}async provisionFleet(e,t){let{config:r,environment:n}=e,s=r.project.slug,i=r.infrastructure.compute,o=T(r,n),a=tt(i.size),c=kt(r),l=r.hetzner?.location||this.location,u=i.bakedImage===!0,d=await ge(o);if(d?.loadBalancerId){let C=(await this.client.listLoadBalancers().catch(()=>[])).find((P)=>P.id===d.loadBalancerId);if(C)return{appPublicIp:C.public_net?.ipv4?.ip,loadBalancerIp:C.public_net?.ipv4?.ip,servicesPrivateIp:d.servicesPrivateIp,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser}}let m=await this.ensureSshKey(s,n,ae(s,n,"app")),h=`${s}-${n}-net`,S=(await this.client.listNetworks().catch(()=>[])).find((b)=>b.name===h)??await this.client.createNetwork({name:h,labels:ae(s,n,"app")}),{firewall:x}=await this.ensureFirewall(`${s}-${n}-app-fw`,ae(s,n,"app"),dn({allowSsh:!0,sitePorts:[],allowedPorts:i.firewall?.allowedPorts})),{firewall:B}=await this.ensureFirewall(`${s}-${n}-services-fw`,ae(s,n,"services"),[{direction:"in",protocol:"tcp",port:"22",source_ips:["0.0.0.0/0","::/0"]},{direction:"in",protocol:"tcp",port:"3306",source_ips:[S.ip_range]},{direction:"in",protocol:"tcp",port:"5432",source_ips:[S.ip_range]},{direction:"in",protocol:"tcp",port:"6379",source_ips:[S.ip_range]},{direction:"in",protocol:"tcp",port:"7700",source_ips:[S.ip_range]}]),L=vn(r),N=Ce(ye({runtime:"php",servicesProvision:L,baked:u,swapGb:i.swapGb})),D=await this.client.listServers().catch(()=>[]),E=[],M=D.find((b)=>Se(b.labels,s,n,"services"));if(!M){let{server:b,action:C}=await this.client.createServer({name:`${s}-${n}-services`,serverType:tt(typeof i.servicesServer==="object"?i.servicesServer.size:i.size),image:c,location:l,userData:N,labels:ae(s,n,"services"),sshKeys:m?[m]:void 0,firewalls:[{firewall:B.id}],networks:[S.id]});await this.client.waitForAction(C.id),M=await this.client.waitForServerRunning(b.id),E.push(b.id)}let H=M.id,F=M.private_net?.[0]?.ip??(await this.client.getServer(H)).private_net?.[0]?.ip,X=[...Ee(!0),...Pe(!0),...Qe(i.sshKeys),...ze(r.notifications)],V=Kt({versions:i.php?.versions,default:i.php?.default,extensions:i.php?.extensions,installNginx:!ee(i),optimizeForProduction:i.php?.optimizeForProduction,ini:i.php?.ini}),Y=Ce(ye({runtime:"php",phpProvision:V,servicesProvision:X,baked:u,swapGb:i.swapGb})),te=D.filter((b)=>Se(b.labels,s,n,"app")),Z=te.map((b)=>b.id);if(te.length>t.appServers)for(let b of te.slice(t.appServers))await this.client.deleteServer(b.id).catch(()=>{}),Z.splice(Z.indexOf(b.id),1);for(let b=te.length;b<t.appServers;b++){let{server:C,action:P}=await this.client.createServer({name:`${s}-${n}-app-${b+1}`,serverType:a,image:c,location:l,userData:Y,labels:ae(s,n,"app"),sshKeys:m?[m]:void 0,firewalls:[{firewall:x.id}],networks:[S.id]});await this.client.waitForAction(P.id),Z.push(C.id),E.push(C.id)}if(this.waitForBoot&&E.length>0)await Promise.all(E.map(async(b)=>{let P=(await this.client.waitForServerRunning(b)).public_net.ipv4?.ip;if(P)await this.waitForSshReady(P),await this.waitForCloudInit(P)}));let J=`${s}-${n}-lb`,ie,ce;if(t.loadBalancer){let C=(await this.client.listLoadBalancers().catch(()=>[])).find((P)=>P.name===J)??await this.client.createLoadBalancer({name:J,location:l,network:S.id,labels:ae(s,n,"lb"),labelSelector:`ts-cloud/project=${s},ts-cloud/environment=${n},ts-cloud/role=app`,services:[{listenPort:80,destinationPort:80},{listenPort:443,destinationPort:443}]});ce=C.id,ie=C.public_net?.ipv4?.ip}let y=ie??(await this.client.getServer(Z[0]).catch(()=>{return}))?.public_net.ipv4?.ip,k={provider:"hetzner",stackName:o,networkId:S.id,loadBalancerId:ce,servicesServerId:H,servicesPrivateIp:F,publicIp:y,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};return await vt(o,k),{appPublicIp:y,loadBalancerIp:ie,servicesPrivateIp:F,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser}}async provisionBunFleet(e,t){let{config:r,environment:n}=e,s=r.project.slug,i=r.infrastructure.compute,o=T(r,n),a=tt(i.size),c=kt(r),l=r.hetzner?.location||this.location,u=i.bakedImage===!0,d=r.sites||{},m=await ge(o);if(m?.lbServerId){let P=await this.tryGetServer(m.lbServerId);if(P&&P.status!=="off"){let G=i.proxy?.engine==="rpx"?i.proxy:{engine:"rpx"},K=await this.findComputeTargets({slug:s,environment:n,role:"app",stackName:o});return this.refreshLbRoutes(P,d,K.map((j)=>({privateIp:j.privateIp,publicIp:j.publicIp})),G,s),{appPublicIp:P.public_net.ipv4?.ip??m.publicIp,loadBalancerIp:P.public_net.ipv4?.ip??m.publicIp,servicesPrivateIp:m.servicesPrivateIp,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser}}}let h=await this.ensureSshKey(s,n,ae(s,n,"app")),f=`${s}-${n}-net`,x=(await this.client.listNetworks().catch(()=>[])).find((P)=>P.name===f)??await this.client.createNetwork({name:f,labels:ae(s,n,"app")}),B=this.collectUpstreamPorts(d),{firewall:L}=await this.ensureFirewall(`${s}-${n}-app-fw`,ae(s,n,"app"),[{direction:"in",protocol:"tcp",port:"22",source_ips:["0.0.0.0/0","::/0"]},...B.map((P)=>({direction:"in",protocol:"tcp",port:String(P),source_ips:[x.ip_range],description:`ts-cloud port ${P} (private, LB-only)`}))]),{firewall:N}=await this.ensureFirewall(`${s}-${n}-lb-fw`,ae(s,n,"lb"),dn({allowSsh:!0,sitePorts:[],allowedPorts:i.firewall?.allowedPorts})),D=await this.client.listServers().catch(()=>[]),E=[],M,H,F=!!i.servicesServer||!!i.managedServices,X=t.dedicatedServices&&F;if(X){let{firewall:P}=await this.ensureFirewall(`${s}-${n}-services-fw`,ae(s,n,"services"),[{direction:"in",protocol:"tcp",port:"22",source_ips:["0.0.0.0/0","::/0"]},{direction:"in",protocol:"tcp",port:"3306",source_ips:[x.ip_range]},{direction:"in",protocol:"tcp",port:"5432",source_ips:[x.ip_range]},{direction:"in",protocol:"tcp",port:"6379",source_ips:[x.ip_range]},{direction:"in",protocol:"tcp",port:"7700",source_ips:[x.ip_range]}]),G=vn(r),K=Ce(ye({runtime:"bun",servicesProvision:G,baked:u,swapGb:i.swapGb})),j=D.find((q)=>Se(q.labels,s,n,"services"));if(!j){let{server:q,action:se}=await this.client.createServer({name:`${s}-${n}-services`,serverType:tt(typeof i.servicesServer==="object"?i.servicesServer.size:i.size),image:c,location:l,userData:K,labels:ae(s,n,"services"),sshKeys:h?[h]:void 0,firewalls:[{firewall:P.id}],networks:[x.id]});await this.client.waitForAction(se.id),j=await this.client.waitForServerRunning(q.id),E.push(q.id)}M=j.id,H=j.private_net?.[0]?.ip??(await this.client.getServer(M)).private_net?.[0]?.ip}let V=X?{...i,managedServices:void 0,backups:void 0}:i,Y=Fe({...r,infrastructure:{...r.infrastructure,compute:V}},{environment:n}),te=Ce(ye({runtime:Y.runtime,runtimeVersion:Y.runtimeVersion,systemPackages:i.systemPackages,database:r.infrastructure?.database,servicesProvision:Y.servicesProvision,baked:u,swapGb:i.swapGb})),Z=D.filter((P)=>Se(P.labels,s,n,"app")),J=Z.map((P)=>P.id);if(Z.length>t.appServers)for(let P of Z.slice(t.appServers))await this.client.deleteServer(P.id).catch(()=>{}),J.splice(J.indexOf(P.id),1);for(let P=Z.length;P<t.appServers;P++){let{server:G,action:K}=await this.client.createServer({name:`${s}-${n}-app-${P+1}`,serverType:a,image:c,location:l,userData:te,labels:ae(s,n,"app"),sshKeys:h?[h]:void 0,firewalls:[{firewall:L.id}],networks:[x.id]});await this.client.waitForAction(K.id),J.push(G.id),E.push(G.id)}if(this.waitForBoot&&E.length>0)await Promise.all(E.map(async(P)=>{let K=(await this.client.waitForServerRunning(P)).public_net.ipv4?.ip;if(K)await this.waitForSshReady(K),await this.waitForCloudInit(K)}));let ie=[];for(let P of J){let G=D.find((K)=>K.id===P);if(!G||!G.private_net?.[0]?.ip)G=await this.client.getServer(P).catch(()=>G);ie.push({privateIp:G?.private_net?.[0]?.ip,publicIp:G?.public_net.ipv4?.ip})}let ce=`${s}-${n}-lb`,y,k;if(t.loadBalancer){let P=D.find((K)=>Se(K.labels,s,n,"lb")),G=i.proxy?.engine==="rpx"?i.proxy:{engine:"rpx"};if(!P){let K=$e({proxy:G,config:Je(d,ie,{proxy:G,slug:s}),slug:s,bunBin:Y.runtime==="node"||Y.runtime==="deno"?void 0:"/usr/local/bin/bun"}),j=Ce(ye({runtime:"bun",rpxProvision:K,baked:!1})),q=typeof i.loadBalancer==="object"?i.loadBalancer.size:void 0,{server:se,action:_}=await this.client.createServer({name:ce,serverType:tt(q??"micro"),image:c,location:l,userData:j,labels:ae(s,n,"lb"),sshKeys:h?[h]:void 0,firewalls:[{firewall:N.id}],networks:[x.id]});if(await this.client.waitForAction(_.id),P=await this.client.waitForServerRunning(se.id),this.waitForBoot){let z=P.public_net.ipv4?.ip;if(z)await this.waitForSshReady(z),await this.waitForCloudInit(z)}}else this.refreshLbRoutes(P,d,ie,G,s);y=P.id,k=P.public_net.ipv4?.ip}let b=k??(await this.client.getServer(J[0]).catch(()=>{return}))?.public_net.ipv4?.ip,C={provider:"hetzner",stackName:o,networkId:x.id,lbServerId:y,appServerIds:J,servicesServerId:M,servicesPrivateIp:H,publicIp:b,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};return await vt(o,C),{appPublicIp:b,loadBalancerIp:k,servicesPrivateIp:H,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser}}async destroyCompute(e){let{config:t,environment:r}=e,n=t.project.slug,s=T(t,r),i=await ge(s),o=[],a=`${n}-${r}-lb`,l=(await this.client.listLoadBalancers().catch(()=>[])).find((L)=>L.name===a);if(l)try{await this.client.deleteLoadBalancer(l.id),o.push(`load balancer ${a}`)}catch{}let u=await this.client.listServers().catch(()=>[]),d=new Set;for(let L of u)if(Se(L.labels,n,r,"app")||Se(L.labels,n,r,"services")||Se(L.labels,n,r,"lb"))d.add(L.id);if(i?.serverId)d.add(i.serverId);if(i?.servicesServerId)d.add(i.servicesServerId);if(i?.lbServerId)d.add(i.lbServerId);for(let L of i?.appServerIds??[])d.add(L);for(let L of d)try{await this.client.deleteServer(L),o.push(`server ${L}`)}catch{}let m=await this.client.listFirewalls().catch(()=>[]);for(let L of[`${n}-${r}-app-fw`,`${n}-${r}-services-fw`,`${n}-${r}-lb-fw`]){let N=m.find((D)=>D.name===L);if(!N)continue;for(let D=0;D<12;D++)try{await this.client.deleteFirewall(N.id),o.push(`firewall ${L}`);break}catch{await this.sleep(3000)}}let h=`${n}-${r}-net`,S=(await this.client.listNetworks().catch(()=>[])).find((L)=>L.name===h);if(S)for(let L=0;L<12;L++)try{await this.client.deleteNetwork(S.id),o.push(`network ${h}`);break}catch{await this.sleep(3000)}let B=(await this.client.listSshKeys().catch(()=>[])).filter((L)=>Cn(L.labels,n,r));for(let L of B)try{await this.client.deleteSshKey(L.id),o.push(`ssh key ${L.name}`)}catch{}return await vt(s,{provider:"hetzner",stackName:s}).catch(()=>{}),{destroyed:o}}async getComputeOutputs(e){let t=T(e.config,e.environment),r=await ge(t);if(r?.serverId){let i=await this.tryGetServer(r.serverId);if(i)return this.outputsFromState(r,i)}if(r?.lbServerId){let o=(await this.tryGetServer(r.lbServerId))?.public_net.ipv4?.ip??r.publicIp;return{...this.outputsFromState(r),appPublicIp:o,loadBalancerIp:o}}if(r?.loadBalancerId||r?.servicesPrivateIp)return this.outputsFromState(r);let s=(await this.findComputeTargets({slug:e.config.project.slug,environment:e.environment,role:"app",stackName:T(e.config,e.environment)}))[0];return{deployStoragePath:"/var/ts-cloud/staging",appInstanceId:s?.id,appPublicIp:s?.publicIp,sshUser:this.sshUser}}async uploadRelease(e){let t=e.targets?.length?e.targets:await this.findComputeTargets({slug:e.config.project.slug,environment:e.environment,role:"app",stackName:T(e.config,e.environment)});if(t.length===0)throw Error("No Hetzner compute targets found for release upload");let s=`/var/ts-cloud/staging/${`${e.remoteKey.replace(/^releases\//,"").replace(/\.tar\.gz$/,"").replace(/\//g,"-")}-${bi()}.tar.gz`}`,i=await Sl(e.localPath),o=ot,a=`${o}/${i}.tar.gz`;for(let c of t){if(!c.publicIp)throw Error(`Target ${c.id} has no public IP for SCP upload`);let l=["set -euo pipefail",`mkdir -p ${Nt(o)} /var/ts-cloud/staging`,`test -s ${Nt(a)}`,`cp -- ${Nt(a)} ${Nt(s)}`].join(`
|
|
397
|
+
esac`,e)}async function ll(e,t){let r=Buffer.from(t.join(`
|
|
398
|
+
`)).toString("base64");await _e(e.host,`printf %s ${r} | base64 -d | while read -r unit; do [ -z "$unit" ] || systemctl is-active --quiet "$unit" || systemctl start "$unit"; done`,e)}async function Ai(e,t,r,n={recovered:!1}){if(await at(e.host,{...e,timeoutMs:e.sshTimeoutMs??600000}),!n.recovered)await cl(e);if(r)await ll(e,r.runningServices);let s=await un(e),i=[],o=r?pt(r.runningServices,s.runningServices):[],a=r?pt(r.releaseLinks,s.releaseLinks):[],c=r?pt(r.routeFragments,s.routeFragments):[],l=r?pt(r.routeIds,s.routeIds):[],u=r?.persistentData.map((D)=>D.split("=")[0])??[],d=s.persistentData.map((D)=>D.split("=")[0]),m=pt(u,d),h=r?pt(r.dataCatalog,s.dataCatalog):[],f=s.routeProbes.filter((D)=>!D.ok),S=n.recovered&&r?r.cpuCores:t.cores??0,x=n.recovered&&r?r.memoryBytes:al(t),B=s.cpuCores>=S,L=s.memoryBytes>=x*0.9,N=n.recovered||s.rootDiskBytes>=(t.disk??0)*1e9*0.95;if(s.failedUnits.length>0)i.push(`Failed services: ${s.failedUnits.join(", ")}`);if(o.length>0)i.push(`Services did not return: ${o.join(", ")}`);if(a.length>0)i.push(`Release links changed: ${a.join(", ")}`);if(c.length>0)i.push(`Route manifests changed: ${c.join(", ")}`);if(l.length>0)i.push(`Routes disappeared: ${l.join(", ")}`);if(m.length>0)i.push(`Persistent data paths disappeared: ${m.join(", ")}`);if(h.length>0)i.push(`Databases or volumes disappeared: ${h.join(", ")}`);if(f.length>0)i.push(`Unhealthy routes: ${f.map((D)=>D.domain).join(", ")}`);if(!B)i.push(`Expected at least ${S} CPU cores, found ${s.cpuCores}`);if(!L)i.push(`Expected at least ${x} bytes of RAM`);if(!N)i.push(`Expected approximately ${t.disk??0} GB root disk`);return{ok:i.length===0,checks:{cpuCores:s.cpuCores,memoryBytes:s.memoryBytes,rootDiskBytes:s.rootDiskBytes,rootFilesystemBytes:s.rootFilesystemBytes,runningServices:s.runningServices.length,releaseLinks:s.releaseLinks.length,routeFragments:s.routeFragments.length,routes:s.routeIds.length,healthyRoutes:s.routeProbes.filter((D)=>D.ok).length,persistentDataPaths:s.persistentData.length,databasesAndVolumes:s.dataCatalog.length,failedUnits:s.failedUnits.length},failures:i}}import{mkdir as ul,readFile as pl,rename as dl,writeFile as ml}from"node:fs/promises";import{join as Si}from"node:path";var Rn="storage/cloud/state";function Le(e=process.cwd()){return pa()?I(e,"state"):Si(e,Rn)}function dt(e,t=process.cwd()){return Si(Le(t),`${e}.json`)}async function Ge(e,t=process.cwd()){try{let r=await pl(dt(e,t),"utf8");return JSON.parse(r)}catch{return null}}async function mt(e,t,r=process.cwd()){let n=dt(e,r);await ul(Le(r),{recursive:!0});let s=`${n}.${process.pid}.tmp`;await ml(s,`${JSON.stringify(t,null,2)}
|
|
399
|
+
`,"utf8"),await dl(s,n)}async function ge(e){return Ge(e)}async function vt(e,t){await mt(e,t)}async function En(e,t,r){let n=r.length?await e.listVolumes():[];return r.map((s)=>{let i=n.filter((l)=>l.name===s.name);if(i.length>1)throw Error(`Ambiguous volume name: ${s.name}`);let o=i[0];if(o&&o.server!==null&&o.server!==t.id)throw Error(`Volume ${o.name} belongs to another server; refusing to detach live storage`);let a=t.location?.name??t.datacenter?.location.name;if(o&&o.location.name!==a)throw Error(`Volume ${o.name} is in ${o.location.name}, server is in ${a}`);if(o&&o.size>s.size)throw Error(`Refusing to shrink volume ${o.name}: ${o.size} GB to ${s.size} GB`);let c=[];if(!o)c.push("create");else{if(o.server===null)c.push("attach");if(o.size<s.size)c.push("grow")}if(!o||o.protection.delete!==(s.deleteProtection!==!1))c.push("protect");return c.push("mount"),{declaration:s,existing:o,actions:c}})}async function xn(e,t,r){let n=[];for(let s of r){let i=s.existing,o=!i;if(!i){let a=await e.createVolume({name:s.declaration.name,size:s.declaration.size,server:t.id,labels:{...t.labels,"ts-cloud/volume":s.declaration.key}});await e.waitForAction(a.action.id);for(let c of a.next_actions??[])await e.waitForAction(c.id);i=await e.getVolume(a.volume.id)}if(i.server===null)await e.waitForAction((await e.attachVolume(i.id,t.id)).id),i=await e.getVolume(i.id);if(i.server!==t.id||i.location.name!==(t.location?.name??t.datacenter?.location.name))throw Error(`Volume attachment verification failed: ${i.name}`);if(i.linux_device!==`/dev/disk/by-id/scsi-0HC_Volume_${i.id}`)throw Error(`Unexpected Hetzner volume device: ${i.name}`);if(i.size<s.declaration.size)await e.waitForAction((await e.resizeVolume(i.id,s.declaration.size)).id);if(i.protection.delete!==(s.declaration.deleteProtection!==!1))await e.waitForAction((await e.protectVolume(i.id,s.declaration.deleteProtection!==!1)).id);n.push({...s.declaration,id:i.id,device:i.linux_device,allowFormat:o})}return n}var On=268435456;function Nt(e){return`'${e.replace(/'/g,`'"'"'`)}'`}async function vl(e){let t=gl("sha256");for await(let r of hl(e))t.update(r);return t.digest("hex")}class ke{name="hetzner";usesCloudFormation=!1;client;sshPrivateKeyPath;sshPublicKeyPath;sshUser;location;stateOnly;residency;waitForBoot;bootWait;async applyComputeChanges(e){let{config:t,environment:r}=e;if(t.cloud?.attachTo)throw Error("Only the compute owner may reconcile server size and volumes");let n=t.infrastructure?.compute;if(n?.resize&&!n.size)throw Error("compute.resize requires an explicit compute.size");let s=Xr(t,r),i=Sn(n??{});if(i.appServers>1||i.dedicatedServices)throw Error("Managed compute volumes currently require a single app server");let o=await this.getComputeOutputs(e);if(!o.appInstanceId)throw Error("No existing compute server found; provision the server first");let a=await this.client.getServer(Number(o.appInstanceId)),c=await En(this.client,a,s),l=n?.resize&&n.size?await rt(this.client,a.id,tt(n.size),n.resize.upgradeDisk!==!1):void 0,u={serverId:String(a.id),changes:c.flatMap((f)=>f.actions.map((S)=>({resource:f.declaration.name,action:S,detail:`${f.declaration.size} GB at ${f.declaration.mountPath}`})))};if(n?.deploymentStorage)u.changes.push({resource:n.deploymentStorage.volume,action:"place-deployments",detail:`Reconcile release directories${n.deploymentStorage.includeBackups!==!1?" and deployment backups":""} on the volume; ${n.deploymentStorage.migrateExisting?"verify and migrate existing files":"refuse nonempty directories"}`});if(l&&!l.alreadyComplete)if(!l.capacityAvailable){if(n?.resize?.whenUnavailable!=="keep-current")throw Error(`${l.targetType.name} is unavailable in ${l.location}`);u.waitingForCapacity=`${l.targetType.name} unavailable in ${l.location}; keeping ${a.server_type.name} running`}else u.changes.push({resource:a.name,action:"resize",detail:`${a.server_type.name} to ${l.targetType.name}`});if(e.dryRun)return u;if(u.waitingForCapacity)console.warn(`[ts-cloud] ${u.waitingForCapacity}`);let d=await xn(this.client,a,c),m=[{id:String(a.id),name:a.name,publicIp:a.public_net.ipv4?.ip}],h=[...ze(t.notifications),...n?.volumes||n?.deploymentStorage?Yr({volumes:d,deploymentStorage:n?.deploymentStorage,webRoot:"/var/www"}):[],...n?.monitoring!==!1?Pe(n?.monitoring):[]];if(h.length){let f=await this.runRemoteDeploy({targets:m,commands:h,timeoutSeconds:7200,comment:"ts-cloud apply configured compute storage"});if(!f.success)throw Error(f.error||f.perInstance.map((S)=>S.error||S.output).join(`
|
|
400
|
+
`))}if(l?.capacityAvailable&&!l.alreadyComplete){let f={host:m[0].publicIp,user:this.sshUser,identityFile:this.sshPrivateKeyPath},S=await Tr({client:this.client,serverId:a.id,targetType:l.targetType.name,upgradeDisk:l.upgradeDisk,hooks:{preflight:()=>wi(f),afterBoot:(x,B,L,N)=>Ai(f,B,L,N)}});if(S.status!=="completed"&&S.status!=="already-complete")throw Error(`Resize did not complete: ${S.status}`)}return u}constructor(e={}){this.residency=e.residency;let t=Lt(e.apiToken);if(this.stateOnly=!e.client&&!t&&e.allowStateOnly===!0,!e.client&&!t&&!this.stateOnly)throw Error("Hetzner API token required. Set hetzner.apiToken in cloud.config.ts or HCLOUD_TOKEN / HETZNER_API_TOKEN.");this.client=e.client??new Oe({apiToken:t??""});let r=St(void 0,{sshPrivateKeyPath:e.sshPrivateKeyPath,sshPublicKeyPath:e.sshPublicKeyPath,sshUser:e.sshUser,location:e.location});this.sshPrivateKeyPath=r.sshPrivateKeyPath,this.sshPublicKeyPath=r.sshPublicKeyPath,this.sshUser=r.sshUser,this.location=r.location,this.waitForBoot=e.waitForBoot??!0,this.bootWait={sshIntervalMs:e.bootWait?.sshIntervalMs??5000,sshTimeoutMs:e.bootWait?.sshTimeoutMs??300000,cloudInitIntervalMs:e.bootWait?.cloudInitIntervalMs??5000,cloudInitTimeoutMs:e.bootWait?.cloudInitTimeoutMs??600000}}async provisionComputeInfrastructure(e){let{config:t,environment:r}=e,n=t.project.slug,s=t.infrastructure?.compute;if(!s)throw Error("infrastructure.compute is required to provision Hetzner compute");if(t.cloud?.attachTo)return this.attachToComputeInfrastructure(t.cloud.attachTo,t,r);let i=T(t,r),o=`${n}-${r}-app`,a=s.runtime==="php"||!!s.php,c=Sn(s),l=t.cloud?.residency||this.residency,{assertComputeResidency:u}=await import("./chunk-h28tfah9.js"),d=(y)=>{if(l)u(l,{country:y.location?.country||y.datacenter?.location?.country,location:y.location?.name||y.datacenter?.location?.name})};if(l){if(c.dedicatedServices||c.appServers>1)throw Error("Residency policy currently requires single-host compute");let y=await this.client.getLocation(t.hetzner?.location||this.location);u(l,{country:y.country,location:y.name})}if(c.dedicatedServices||c.appServers>1)return a?this.provisionFleet(e,c):this.provisionBunFleet(e,c);let m=ae(n,r,"app"),h=t.sites||{},f=ee(s)?[]:this.collectUpstreamPorts(h),S=`${n}-${r}-app-fw`,{firewall:x}=await this.ensureFirewall(S,m,dn({allowSsh:s.allowSsh!==!1,sitePorts:f,allowedPorts:[...s.firewall?.allowedPorts??[],...Rs(t.infrastructure?.sftp),...jr(ne(t,{environment:r}))]})),B=await this.ensureSshKey(n,r,m),L=await ge(i);if(L?.serverId){let y=await this.tryGetServer(L.serverId);if(y&&y.status!=="off"&&y.labels?.["ts-cloud/project"]===n&&y.labels?.["ts-cloud/environment"]===r){if(d(y),s.resize||s.volumes||s.deploymentStorage)await this.applyComputeChanges(e);return this.outputsFromState(L,y)}}let N=await this.findExistingServer(n,r,o);if(N&&N.status!=="off"){d(N);let y={provider:"hetzner",stackName:i,serverId:N.id,serverName:N.name,publicIp:N.public_net.ipv4?.ip,publicIpv6:me(N.public_net.ipv6?.ip),deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};if(await vt(i,y),s.resize||s.volumes||s.deploymentStorage)await this.applyComputeChanges(e);return this.outputsFromState(y,N)}let D=le(t,{environment:r}),E=D?.engine==="rpx"?$e({proxy:D,config:Te(h,{proxy:D,slug:t.project.slug,mail:ne(t,{environment:r})}),slug:t.project.slug,bunBin:s.runtime==="node"||s.runtime==="deno"?void 0:"/usr/local/bin/bun"}):void 0,M=Fe(t,{environment:r}),H=s.bakedImage===!0,F=ye({runtime:M.runtime,runtimeVersion:M.runtimeVersion,systemPackages:s.systemPackages,database:t.infrastructure?.database,phpProvision:M.phpProvision,servicesProvision:M.servicesProvision,rpxProvision:E,baked:H,swapGb:s.swapGb}),X=Ce(F),V=tt(s.size),Y=kt(t),{server:te,action:Z}=await this.client.createServer({name:o,serverType:V,image:Y,location:t.hetzner?.location||this.location,userData:X,labels:m,sshKeys:B?[B]:void 0,firewalls:[{firewall:x.id}]});await this.client.waitForAction(Z.id);let J=await this.client.waitForServerRunning(te.id);d(J);let ie={provider:"hetzner",stackName:i,serverId:J.id,serverName:J.name,firewallId:x.id,publicIp:J.public_net.ipv4?.ip,publicIpv6:me(J.public_net.ipv6?.ip),deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};await vt(i,ie);let ce=J.public_net.ipv4?.ip;if(ce&&this.waitForBoot)await this.waitForSshReady(ce),await this.waitForCloudInit(ce);if(s.resize||s.volumes||s.deploymentStorage)await this.applyComputeChanges(e);return this.outputsFromState(ie,J)}async provisionFleet(e,t){let{config:r,environment:n}=e,s=r.project.slug,i=r.infrastructure.compute,o=T(r,n),a=tt(i.size),c=kt(r),l=r.hetzner?.location||this.location,u=i.bakedImage===!0,d=await ge(o);if(d?.loadBalancerId){let C=(await this.client.listLoadBalancers().catch(()=>[])).find((P)=>P.id===d.loadBalancerId);if(C)return{appPublicIp:C.public_net?.ipv4?.ip,loadBalancerIp:C.public_net?.ipv4?.ip,servicesPrivateIp:d.servicesPrivateIp,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser}}let m=await this.ensureSshKey(s,n,ae(s,n,"app")),h=`${s}-${n}-net`,S=(await this.client.listNetworks().catch(()=>[])).find((b)=>b.name===h)??await this.client.createNetwork({name:h,labels:ae(s,n,"app")}),{firewall:x}=await this.ensureFirewall(`${s}-${n}-app-fw`,ae(s,n,"app"),dn({allowSsh:!0,sitePorts:[],allowedPorts:i.firewall?.allowedPorts})),{firewall:B}=await this.ensureFirewall(`${s}-${n}-services-fw`,ae(s,n,"services"),[{direction:"in",protocol:"tcp",port:"22",source_ips:["0.0.0.0/0","::/0"]},{direction:"in",protocol:"tcp",port:"3306",source_ips:[S.ip_range]},{direction:"in",protocol:"tcp",port:"5432",source_ips:[S.ip_range]},{direction:"in",protocol:"tcp",port:"6379",source_ips:[S.ip_range]},{direction:"in",protocol:"tcp",port:"7700",source_ips:[S.ip_range]}]),L=vn(r),N=Ce(ye({runtime:"php",servicesProvision:L,baked:u,swapGb:i.swapGb})),D=await this.client.listServers().catch(()=>[]),E=[],M=D.find((b)=>Se(b.labels,s,n,"services"));if(!M){let{server:b,action:C}=await this.client.createServer({name:`${s}-${n}-services`,serverType:tt(typeof i.servicesServer==="object"?i.servicesServer.size:i.size),image:c,location:l,userData:N,labels:ae(s,n,"services"),sshKeys:m?[m]:void 0,firewalls:[{firewall:B.id}],networks:[S.id]});await this.client.waitForAction(C.id),M=await this.client.waitForServerRunning(b.id),E.push(b.id)}let H=M.id,F=M.private_net?.[0]?.ip??(await this.client.getServer(H)).private_net?.[0]?.ip,X=[...Ee(!0),...Pe(!0),...Qe(i.sshKeys),...ze(r.notifications)],V=Kt({versions:i.php?.versions,default:i.php?.default,extensions:i.php?.extensions,installNginx:!ee(i),optimizeForProduction:i.php?.optimizeForProduction,ini:i.php?.ini}),Y=Ce(ye({runtime:"php",phpProvision:V,servicesProvision:X,baked:u,swapGb:i.swapGb})),te=D.filter((b)=>Se(b.labels,s,n,"app")),Z=te.map((b)=>b.id);if(te.length>t.appServers)for(let b of te.slice(t.appServers))await this.client.deleteServer(b.id).catch(()=>{}),Z.splice(Z.indexOf(b.id),1);for(let b=te.length;b<t.appServers;b++){let{server:C,action:P}=await this.client.createServer({name:`${s}-${n}-app-${b+1}`,serverType:a,image:c,location:l,userData:Y,labels:ae(s,n,"app"),sshKeys:m?[m]:void 0,firewalls:[{firewall:x.id}],networks:[S.id]});await this.client.waitForAction(P.id),Z.push(C.id),E.push(C.id)}if(this.waitForBoot&&E.length>0)await Promise.all(E.map(async(b)=>{let P=(await this.client.waitForServerRunning(b)).public_net.ipv4?.ip;if(P)await this.waitForSshReady(P),await this.waitForCloudInit(P)}));let J=`${s}-${n}-lb`,ie,ce;if(t.loadBalancer){let C=(await this.client.listLoadBalancers().catch(()=>[])).find((P)=>P.name===J)??await this.client.createLoadBalancer({name:J,location:l,network:S.id,labels:ae(s,n,"lb"),labelSelector:`ts-cloud/project=${s},ts-cloud/environment=${n},ts-cloud/role=app`,services:[{listenPort:80,destinationPort:80},{listenPort:443,destinationPort:443}]});ce=C.id,ie=C.public_net?.ipv4?.ip}let y=ie??(await this.client.getServer(Z[0]).catch(()=>{return}))?.public_net.ipv4?.ip,k={provider:"hetzner",stackName:o,networkId:S.id,loadBalancerId:ce,servicesServerId:H,servicesPrivateIp:F,publicIp:y,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};return await vt(o,k),{appPublicIp:y,loadBalancerIp:ie,servicesPrivateIp:F,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser}}async provisionBunFleet(e,t){let{config:r,environment:n}=e,s=r.project.slug,i=r.infrastructure.compute,o=T(r,n),a=tt(i.size),c=kt(r),l=r.hetzner?.location||this.location,u=i.bakedImage===!0,d=r.sites||{},m=await ge(o);if(m?.lbServerId){let P=await this.tryGetServer(m.lbServerId);if(P&&P.status!=="off"){let G=i.proxy?.engine==="rpx"?i.proxy:{engine:"rpx"},K=await this.findComputeTargets({slug:s,environment:n,role:"app",stackName:o});return this.refreshLbRoutes(P,d,K.map((j)=>({privateIp:j.privateIp,publicIp:j.publicIp})),G,s),{appPublicIp:P.public_net.ipv4?.ip??m.publicIp,loadBalancerIp:P.public_net.ipv4?.ip??m.publicIp,servicesPrivateIp:m.servicesPrivateIp,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser}}}let h=await this.ensureSshKey(s,n,ae(s,n,"app")),f=`${s}-${n}-net`,x=(await this.client.listNetworks().catch(()=>[])).find((P)=>P.name===f)??await this.client.createNetwork({name:f,labels:ae(s,n,"app")}),B=this.collectUpstreamPorts(d),{firewall:L}=await this.ensureFirewall(`${s}-${n}-app-fw`,ae(s,n,"app"),[{direction:"in",protocol:"tcp",port:"22",source_ips:["0.0.0.0/0","::/0"]},...B.map((P)=>({direction:"in",protocol:"tcp",port:String(P),source_ips:[x.ip_range],description:`ts-cloud port ${P} (private, LB-only)`}))]),{firewall:N}=await this.ensureFirewall(`${s}-${n}-lb-fw`,ae(s,n,"lb"),dn({allowSsh:!0,sitePorts:[],allowedPorts:i.firewall?.allowedPorts})),D=await this.client.listServers().catch(()=>[]),E=[],M,H,F=!!i.servicesServer||!!i.managedServices,X=t.dedicatedServices&&F;if(X){let{firewall:P}=await this.ensureFirewall(`${s}-${n}-services-fw`,ae(s,n,"services"),[{direction:"in",protocol:"tcp",port:"22",source_ips:["0.0.0.0/0","::/0"]},{direction:"in",protocol:"tcp",port:"3306",source_ips:[x.ip_range]},{direction:"in",protocol:"tcp",port:"5432",source_ips:[x.ip_range]},{direction:"in",protocol:"tcp",port:"6379",source_ips:[x.ip_range]},{direction:"in",protocol:"tcp",port:"7700",source_ips:[x.ip_range]}]),G=vn(r),K=Ce(ye({runtime:"bun",servicesProvision:G,baked:u,swapGb:i.swapGb})),j=D.find((q)=>Se(q.labels,s,n,"services"));if(!j){let{server:q,action:se}=await this.client.createServer({name:`${s}-${n}-services`,serverType:tt(typeof i.servicesServer==="object"?i.servicesServer.size:i.size),image:c,location:l,userData:K,labels:ae(s,n,"services"),sshKeys:h?[h]:void 0,firewalls:[{firewall:P.id}],networks:[x.id]});await this.client.waitForAction(se.id),j=await this.client.waitForServerRunning(q.id),E.push(q.id)}M=j.id,H=j.private_net?.[0]?.ip??(await this.client.getServer(M)).private_net?.[0]?.ip}let V=X?{...i,managedServices:void 0,backups:void 0}:i,Y=Fe({...r,infrastructure:{...r.infrastructure,compute:V}},{environment:n}),te=Ce(ye({runtime:Y.runtime,runtimeVersion:Y.runtimeVersion,systemPackages:i.systemPackages,database:r.infrastructure?.database,servicesProvision:Y.servicesProvision,baked:u,swapGb:i.swapGb})),Z=D.filter((P)=>Se(P.labels,s,n,"app")),J=Z.map((P)=>P.id);if(Z.length>t.appServers)for(let P of Z.slice(t.appServers))await this.client.deleteServer(P.id).catch(()=>{}),J.splice(J.indexOf(P.id),1);for(let P=Z.length;P<t.appServers;P++){let{server:G,action:K}=await this.client.createServer({name:`${s}-${n}-app-${P+1}`,serverType:a,image:c,location:l,userData:te,labels:ae(s,n,"app"),sshKeys:h?[h]:void 0,firewalls:[{firewall:L.id}],networks:[x.id]});await this.client.waitForAction(K.id),J.push(G.id),E.push(G.id)}if(this.waitForBoot&&E.length>0)await Promise.all(E.map(async(P)=>{let K=(await this.client.waitForServerRunning(P)).public_net.ipv4?.ip;if(K)await this.waitForSshReady(K),await this.waitForCloudInit(K)}));let ie=[];for(let P of J){let G=D.find((K)=>K.id===P);if(!G||!G.private_net?.[0]?.ip)G=await this.client.getServer(P).catch(()=>G);ie.push({privateIp:G?.private_net?.[0]?.ip,publicIp:G?.public_net.ipv4?.ip})}let ce=`${s}-${n}-lb`,y,k;if(t.loadBalancer){let P=D.find((K)=>Se(K.labels,s,n,"lb")),G=i.proxy?.engine==="rpx"?i.proxy:{engine:"rpx"};if(!P){let K=$e({proxy:G,config:Je(d,ie,{proxy:G,slug:s}),slug:s,bunBin:Y.runtime==="node"||Y.runtime==="deno"?void 0:"/usr/local/bin/bun"}),j=Ce(ye({runtime:"bun",rpxProvision:K,baked:!1})),q=typeof i.loadBalancer==="object"?i.loadBalancer.size:void 0,{server:se,action:_}=await this.client.createServer({name:ce,serverType:tt(q??"micro"),image:c,location:l,userData:j,labels:ae(s,n,"lb"),sshKeys:h?[h]:void 0,firewalls:[{firewall:N.id}],networks:[x.id]});if(await this.client.waitForAction(_.id),P=await this.client.waitForServerRunning(se.id),this.waitForBoot){let z=P.public_net.ipv4?.ip;if(z)await this.waitForSshReady(z),await this.waitForCloudInit(z)}}else this.refreshLbRoutes(P,d,ie,G,s);y=P.id,k=P.public_net.ipv4?.ip}let b=k??(await this.client.getServer(J[0]).catch(()=>{return}))?.public_net.ipv4?.ip,C={provider:"hetzner",stackName:o,networkId:x.id,lbServerId:y,appServerIds:J,servicesServerId:M,servicesPrivateIp:H,publicIp:b,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};return await vt(o,C),{appPublicIp:b,loadBalancerIp:k,servicesPrivateIp:H,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser}}async destroyCompute(e){let{config:t,environment:r}=e,n=t.project.slug,s=T(t,r),i=await ge(s),o=[],a=`${n}-${r}-lb`,l=(await this.client.listLoadBalancers().catch(()=>[])).find((L)=>L.name===a);if(l)try{await this.client.deleteLoadBalancer(l.id),o.push(`load balancer ${a}`)}catch{}let u=await this.client.listServers().catch(()=>[]),d=new Set;for(let L of u)if(Se(L.labels,n,r,"app")||Se(L.labels,n,r,"services")||Se(L.labels,n,r,"lb"))d.add(L.id);if(i?.serverId)d.add(i.serverId);if(i?.servicesServerId)d.add(i.servicesServerId);if(i?.lbServerId)d.add(i.lbServerId);for(let L of i?.appServerIds??[])d.add(L);for(let L of d)try{await this.client.deleteServer(L),o.push(`server ${L}`)}catch{}let m=await this.client.listFirewalls().catch(()=>[]);for(let L of[`${n}-${r}-app-fw`,`${n}-${r}-services-fw`,`${n}-${r}-lb-fw`]){let N=m.find((D)=>D.name===L);if(!N)continue;for(let D=0;D<12;D++)try{await this.client.deleteFirewall(N.id),o.push(`firewall ${L}`);break}catch{await this.sleep(3000)}}let h=`${n}-${r}-net`,S=(await this.client.listNetworks().catch(()=>[])).find((L)=>L.name===h);if(S)for(let L=0;L<12;L++)try{await this.client.deleteNetwork(S.id),o.push(`network ${h}`);break}catch{await this.sleep(3000)}let B=(await this.client.listSshKeys().catch(()=>[])).filter((L)=>Cn(L.labels,n,r));for(let L of B)try{await this.client.deleteSshKey(L.id),o.push(`ssh key ${L.name}`)}catch{}return await vt(s,{provider:"hetzner",stackName:s}).catch(()=>{}),{destroyed:o}}async getComputeOutputs(e){let t=T(e.config,e.environment),r=await ge(t);if(r?.serverId){let i=await this.tryGetServer(r.serverId);if(i)return this.outputsFromState(r,i)}if(r?.lbServerId){let o=(await this.tryGetServer(r.lbServerId))?.public_net.ipv4?.ip??r.publicIp;return{...this.outputsFromState(r),appPublicIp:o,loadBalancerIp:o}}if(r?.loadBalancerId||r?.servicesPrivateIp)return this.outputsFromState(r);let s=(await this.findComputeTargets({slug:e.config.project.slug,environment:e.environment,role:"app",stackName:T(e.config,e.environment)}))[0];return{deployStoragePath:"/var/ts-cloud/staging",appInstanceId:s?.id,appPublicIp:s?.publicIp,sshUser:this.sshUser}}async uploadRelease(e){let t=e.targets?.length?e.targets:await this.findComputeTargets({slug:e.config.project.slug,environment:e.environment,role:"app",stackName:T(e.config,e.environment)});if(t.length===0)throw Error("No Hetzner compute targets found for release upload");let s=`/var/ts-cloud/staging/${`${e.remoteKey.replace(/^releases\//,"").replace(/\.tar\.gz$/,"").replace(/\//g,"-")}-${bi()}.tar.gz`}`,i=await vl(e.localPath),o=ot,a=`${o}/${i}.tar.gz`;for(let c of t){if(!c.publicIp)throw Error(`Target ${c.id} has no public IP for SCP upload`);let l=["set -euo pipefail",`mkdir -p ${Nt(o)} /var/ts-cloud/staging`,`test -s ${Nt(a)}`,`cp -- ${Nt(a)} ${Nt(s)}`].join(`
|
|
399
401
|
`);try{this.sshExec(c.publicIp,l);continue}catch{}let u=`${o}/.${i}-${bi()}.tmp`;try{this.scpToHost(c.publicIp,e.localPath,u),this.sshExec(c.publicIp,rr(u,a,s))}catch(d){try{this.sshExec(c.publicIp,nr(u))}catch{}throw d}}return{artifactRef:s}}async findComputeTargets(e){let{assertComputeResidency:t}=await import("./chunk-h28tfah9.js"),r=e.role||"app",n=(o)=>{if(this.residency)t(this.residency,{country:o.location?.country||o.datacenter?.location?.country,location:o.location?.name||o.datacenter?.location?.name});return{id:String(o.id),name:o.name,publicIp:o.public_net.ipv4?.ip,publicIpv6:me(o.public_net.ipv6?.ip),privateIp:o.private_net?.[0]?.ip,status:o.status}};if(r==="app"||r==="lb"||r==="services"){let o=await ge(e.stackName??`${e.slug}-${e.environment}`),a=[...new Set(r==="app"?[o?.serverId,...o?.appServerIds??[]]:r==="lb"?[o?.lbServerId]:[o?.servicesServerId])].filter((u)=>typeof u==="number");if(this.stateOnly){if(this.residency)throw Error("Residency requires fresh provider evidence; a local state pin alone is insufficient");if(a.length>0&&o?.publicIp)return a.map((u)=>({id:String(u),name:u===o.serverId?o.serverName:void 0,publicIp:o.publicIp,publicIpv6:o.publicIpv6,status:"running"}));if(r!=="app")return[];throw Error(`Hetzner API token required because '${e.stackName??`${e.slug}-${e.environment}`}' has no complete local compute state pin.`)}let c=await this.client.listServers();if(a.length>0){let u=[];for(let d of a){let m=c.find((h)=>h.id===d)??await this.tryGetServer(d);if(d===o?.serverId&&o.serverName&&m?.name!==o.serverName)continue;if(m&&m.status!=="off")u.push(m)}if(u.length>0)return u.map(n)}let l=c.filter((u)=>Se(u.labels,e.slug,e.environment,r));if(l.length>0)return l.map(n);if(r==="app"){let u=c.filter((d)=>d.status!=="off"&&d.labels?.[`${oe}/managed-by`]==="ts-cloud"&&d.labels?.[`${oe}/environment`]===e.environment&&d.labels?.[`${oe}/role`]==="app");if(u.length===1)return u.map(n)}return[]}if(this.stateOnly)throw Error(`Hetzner API token required to discover '${r}' compute targets.`);let i=(await this.client.listServers()).filter((o)=>Se(o.labels,e.slug,e.environment,r));if(i.length>0)return i.map(n);return[]}async attachToComputeInfrastructure(e,t,r){let n=await this.client.listServers(),s=(m)=>n.filter((h)=>h.status!=="off"&&Se(h.labels,e,r,m)),i=s("app");if(i.length===0)throw Error(`Cannot attach '${t.project.slug}' to '${e}': no running Hetzner app server is labeled for ${e}/${r}. Deploy the owner project first.`);let o=s("lb")[0],a=s("services")[0],c=i[0],l=o??c,u=T(t,r),d={provider:"hetzner",stackName:u,serverId:c.id,serverName:c.name,appServerIds:i.map((m)=>m.id),lbServerId:o?.id,servicesServerId:a?.id,servicesPrivateIp:a?.private_net?.[0]?.ip,publicIp:l.public_net.ipv4?.ip,deployStoragePath:"/var/ts-cloud/staging",sshUser:this.sshUser};return await vt(u,d),this.outputsFromState(d)}async listReachableResources(){return(await this.client.listServers()).map((t)=>({name:t.name,labels:t.labels}))}async runRemoteDeploy(e){if(e.targets.length===0)return{success:!1,instanceCount:0,perInstance:[],error:"No targets provided"};let t=e.commands.join(`
|
|
400
|
-
`),r=[];for(let s of e.targets){if(!s.publicIp){r.push({instanceId:s.id,status:"Failed",error:"Missing public IP"});continue}try{let i=this.sshExec(s.publicIp,t);qe(i),r.push({instanceId:s.id,status:"Success",output:i})}catch(i){r.push({instanceId:s.id,status:"Failed",error:i.message})}}let n=r.every((s)=>s.status==="Success");return{success:n,instanceCount:e.targets.length,perInstance:r,error:n?void 0:Me(r,"One or more SSH deploy commands failed")}}async ensureSshKey(e,t,r){if(!
|
|
402
|
+
`),r=[];for(let s of e.targets){if(!s.publicIp){r.push({instanceId:s.id,status:"Failed",error:"Missing public IP"});continue}try{let i=this.sshExec(s.publicIp,t);qe(i),r.push({instanceId:s.id,status:"Success",output:i})}catch(i){r.push({instanceId:s.id,status:"Failed",error:i.message})}}let n=r.every((s)=>s.status==="Success");return{success:n,instanceCount:e.targets.length,perInstance:r,error:n?void 0:Me(r,"One or more SSH deploy commands failed")}}async ensureSshKey(e,t,r){if(!yl(this.sshPublicKeyPath))throw Error(`SSH public key not found at ${this.sshPublicKeyPath}. ts-cloud deploys to Hetzner over SSH and needs a public key to authorize on the server. Generate one (\`ssh-keygen -t ed25519\`) or set hetzner.sshPrivateKeyPath / HCLOUD_SSH_PUBLIC_KEY.`);let n=Sl(this.sshPublicKeyPath,"utf8").trim(),s=je(n),o=(await this.client.listSshKeys()).find((c)=>je(c.public_key)===s);if(o)return o.id;return(await this.client.createSshKey({name:`${e}-${t}-deploy`,publicKey:n,labels:r})).id}async tryGetServer(e){try{return await this.client.getServer(e)}catch{return null}}refreshLbRoutes(e,t,r,n,s){let i=e.public_net.ipv4?.ip;if(!i){console.warn(`[ts-cloud] LB server '${e.name}' has no public IP — skipping the rpx route refresh; its routes may be stale.`);return}let o=r.filter((c)=>c.privateIp||c.publicIp);if(o.length===0){console.warn(`[ts-cloud] No routable app boxes found for LB '${e.name}' — leaving its current rpx routes untouched.`);return}let a=Je(t,o,{proxy:n,slug:s});this.sshExec(i,et({config:a,slug:s}).join(`
|
|
401
403
|
`))}async findExistingServer(e,t,r){let n=await this.client.listServers(),s=n.find((o)=>Se(o.labels,e,t,"app")||o.name===r);if(s)return s;let i=n.filter((o)=>o.status!=="off"&&o.labels?.[`${oe}/managed-by`]==="ts-cloud"&&o.labels?.[`${oe}/environment`]===t&&o.labels?.[`${oe}/role`]==="app");if(i.length===1){let o=i[0],a=o.labels?.[`${oe}/project`]??"unknown";return console.warn(`[ts-cloud] No server named '${r}' found; adopting existing ts-cloud app server '${o.name}' (project label '${a}') for project '${e}' — `+"updating it in place instead of provisioning a new server."),o}return}async ensureFirewall(e,t,r){let s=(await this.client.listFirewalls()).find((o)=>o.name===e);if(s)return await this.client.setFirewallRules(s.id,r),{firewall:s};let{firewall:i}=await this.client.createFirewall({name:e,labels:t,rules:r});return{firewall:i}}collectUpstreamPorts(e){let t=new Set;for(let r of Object.values(e))if(typeof r.port==="number")t.add(r.port);return[...t].filter((r)=>![80,443].includes(r))}async sleep(e){await new Promise((t)=>setTimeout(t,e))}async waitForSshReady(e){let{sshIntervalMs:t,sshTimeoutMs:r}=this.bootWait,n=Date.now(),s;while(Date.now()-n<r)try{vi(`ssh ${this.sshBaseArgs(e,["-o","ConnectTimeout=5"]).map(Nt).join(" ")} true`,{stdio:"pipe",maxBuffer:On});return}catch(i){s=i,await this.sleep(t)}throw Error(`Timed out waiting for SSH on ${e} after ${r}ms: ${s?.message??"unknown error"}`)}async waitForCloudInit(e){let{cloudInitIntervalMs:t,cloudInitTimeoutMs:r}=this.bootWait,n=Date.now();while(Date.now()-n<r){try{let s=this.sshExec(e,"if command -v cloud-init >/dev/null 2>&1; then cloud-init status --long 2>/dev/null || true; else echo 'status: done'; fi");if(/status:\s*error/.test(s))throw Error(`cloud-init reported an error on ${e}:
|
|
402
|
-
${s}`);if(/status:\s*(?:done|degraded)/.test(s))return}catch(s){if(s instanceof Error&&/cloud-init reported an error/.test(s.message))throw s}await this.sleep(t)}throw Error(`Timed out waiting for cloud-init to finish on ${e} after ${r}ms`)}outputsFromState(e,t){return{deployStoragePath:e.deployStoragePath||"/var/ts-cloud/staging",appInstanceId:e.serverId?String(e.serverId):void 0,appPublicIp:t?.public_net.ipv4?.ip||e.publicIp,appPublicIpv6:me(t?.public_net.ipv6?.ip)||e.publicIpv6,sshUser:e.sshUser||this.sshUser,servicesPrivateIp:e.servicesPrivateIp}}static SSH_HOST_KEY_OPTS=["-o","StrictHostKeyChecking=no","-o","UserKnownHostsFile=/dev/null","-o","LogLevel=ERROR"];static SSH_KEEPALIVE_OPTS=["-o","ConnectTimeout=30","-o","ServerAliveInterval=15","-o","ServerAliveCountMax=4"];sshBaseArgs(e,t=[]){return["-i",this.sshPrivateKeyPath,...ke.SSH_HOST_KEY_OPTS,...ke.SSH_KEEPALIVE_OPTS,"-o","BatchMode=yes",...t,`${this.sshUser}@${e}`]}scpToHost(e,t,r){let n=["scp",...sr,"-i",this.sshPrivateKeyPath,...ke.SSH_HOST_KEY_OPTS,...ke.SSH_KEEPALIVE_OPTS,"-o","BatchMode=yes",t,`${this.sshUser}@${e}:${r}`].map((i)=>`"${i.replace(/"/g,"\\\"")}"`).join(" "),s=3;for(let i=1;i<=3;i++)try{vi(n,{stdio:"pipe",maxBuffer:On});return}catch(o){if(i===3)throw Error(Ue(o))}}sshExec(e,t){try{return
|
|
403
|
-
`);try{let r=Bun.spawn(["bash","-c",t],{stdout:"pipe",stderr:"pipe",env:process.env}),[n,s,i]=await Promise.all([new Response(r.stdout).text(),new Response(r.stderr).text(),r.exited]);return{success:i===0,instanceCount:1,perInstance:[{instanceId:"localhost",status:i===0?"Success":"Failed",output:n,error:s}]}}catch(r){return{success:!1,instanceCount:1,perInstance:[{instanceId:"localhost",status:"Failed",error:r?.message??String(r)}],error:r?.message??String(r)}}}}function ur(){let e=process.env.TS_CLOUD_DASHBOARD_BOX;return e!=null&&e!==""&&e!=="0"&&e.toLowerCase()!=="false"}import{createHash as
|
|
404
|
+
${s}`);if(/status:\s*(?:done|degraded)/.test(s))return}catch(s){if(s instanceof Error&&/cloud-init reported an error/.test(s.message))throw s}await this.sleep(t)}throw Error(`Timed out waiting for cloud-init to finish on ${e} after ${r}ms`)}outputsFromState(e,t){return{deployStoragePath:e.deployStoragePath||"/var/ts-cloud/staging",appInstanceId:e.serverId?String(e.serverId):void 0,appPublicIp:t?.public_net.ipv4?.ip||e.publicIp,appPublicIpv6:me(t?.public_net.ipv6?.ip)||e.publicIpv6,sshUser:e.sshUser||this.sshUser,servicesPrivateIp:e.servicesPrivateIp}}static SSH_HOST_KEY_OPTS=["-o","StrictHostKeyChecking=no","-o","UserKnownHostsFile=/dev/null","-o","LogLevel=ERROR"];static SSH_KEEPALIVE_OPTS=["-o","ConnectTimeout=30","-o","ServerAliveInterval=15","-o","ServerAliveCountMax=4"];sshBaseArgs(e,t=[]){return["-i",this.sshPrivateKeyPath,...ke.SSH_HOST_KEY_OPTS,...ke.SSH_KEEPALIVE_OPTS,"-o","BatchMode=yes",...t,`${this.sshUser}@${e}`]}scpToHost(e,t,r){let n=["scp",...sr,"-i",this.sshPrivateKeyPath,...ke.SSH_HOST_KEY_OPTS,...ke.SSH_KEEPALIVE_OPTS,"-o","BatchMode=yes",t,`${this.sshUser}@${e}:${r}`].map((i)=>`"${i.replace(/"/g,"\\\"")}"`).join(" "),s=3;for(let i=1;i<=3;i++)try{vi(n,{stdio:"pipe",maxBuffer:On});return}catch(o){if(i===3)throw Error(Ue(o))}}sshExec(e,t){try{return fl("ssh",[...this.sshBaseArgs(e),$n],{encoding:"utf8",input:t,stdio:["pipe","pipe","pipe"],maxBuffer:On})}catch(r){throw Error(`${Ue(r)}${Pt(r,t)}`)}}}class lr{name="ssh";usesCloudFormation=!1;async getComputeOutputs(){return{appPublicIp:"127.0.0.1",sshUser:"root"}}async uploadRelease(e){return{artifactRef:e.localPath}}async findComputeTargets(e){if((e.role||"app")!=="app")return[];return[{id:"localhost",name:"localhost",publicIp:"127.0.0.1",status:"running"}]}async runRemoteDeploy(e){let t=e.commands.join(`
|
|
405
|
+
`);try{let r=Bun.spawn(["bash","-c",t],{stdout:"pipe",stderr:"pipe",env:process.env}),[n,s,i]=await Promise.all([new Response(r.stdout).text(),new Response(r.stderr).text(),r.exited]);return{success:i===0,instanceCount:1,perInstance:[{instanceId:"localhost",status:i===0?"Success":"Failed",output:n,error:s}]}}catch(r){return{success:!1,instanceCount:1,perInstance:[{instanceId:"localhost",status:"Failed",error:r?.message??String(r)}],error:r?.message??String(r)}}}}function ur(){let e=process.env.TS_CLOUD_DASHBOARD_BOX;return e!=null&&e!==""&&e!=="0"&&e.toLowerCase()!=="false"}import{createHash as Ll,randomUUID as to}from"node:crypto";import{createReadStream as kl}from"node:fs";import{isIP as Nl}from"node:net";import{createHash as bl}from"node:crypto";import{appendFile as wl,mkdir as $l,readFile as _l}from"node:fs/promises";import{dirname as Tl}from"node:path";var Ti=268435456,Ci=async(e,t)=>{let r=Bun.spawn(e,{stdin:t===void 0?"ignore":Buffer.from(t),stdout:"pipe",stderr:"pipe"}),[n,s,i]=await Promise.all([r.exited,new Response(r.stdout).text(),new Response(r.stderr).text()]);return{code:n,signal:r.signalCode,stdout:s.slice(-Ti),stderr:i.slice(-Ti)}},xi=["-o","StrictHostKeyChecking=no","-o","UserKnownHostsFile=/dev/null","-o","LogLevel=ERROR"],Di=["-o","ConnectTimeout=30","-o","ServerAliveInterval=15","-o","ServerAliveCountMax=4"];function Li(e){if(e.hostKey==="insecure")return[...xi];return["-o",`StrictHostKeyChecking=${e.hostKey.policy==="pin"?"yes":"accept-new"}`,"-o",`UserKnownHostsFile=${e.hostKey.knownHostsFile}`,"-o","LogLevel=ERROR"]}function Hi(e){let t=[...Di];if(e.connectTimeoutSec!==void 0)t[1]=`ConnectTimeout=${e.connectTimeoutSec}`;return t}function Dn(e,t,r=[]){return["-i",e.identityFile,"-p",String(e.port??22),...Li(e),...Hi(e),"-o","BatchMode=yes",...r,`${e.user}@${t}`]}function Ui(e,t,r,n){return["-i",e.identityFile,"-P",String(e.port??22),...Li(e),...Hi(e),"-o","BatchMode=yes",r,`${e.user}@${t}:${n}`]}function Er(e){return`SHA256:${bl("sha256").update(Buffer.from(e,"base64")).digest("base64").replace(/=+$/,"")}`}async function yo(e,t=22,r=Ci){let n=await r(["ssh-keyscan","-T","10","-p",String(t),"-t","ed25519",e]),s=n.stdout.split(`
|
|
404
406
|
`).find((i)=>i&&!i.startsWith("#"))?.trim().split(/\s+/);if(n.code!==0||!s?.[1]||!s[2])throw Error(`SSH host-key scan failed: ${n.stderr.trim()||"no key returned"}`);return{algorithm:s[1],key:s[2]}}function In(e,t=22){return t===22?e:`[${e}]:${t}`}function Mi(e,t,r){return`${In(e,t)} ${r.algorithm} ${r.key}`}function zi(e,t,r=22){let n=In(t,r);for(let s of e.split(`
|
|
405
|
-
`)){let i=s.trim();if(!i||i.startsWith("#")||i.startsWith("@"))continue;let[o,a,c]=i.split(/\s+/);if(!o||!a||!c)continue;if(o.split(",").includes(n))return{algorithm:a,key:c}}return null}function Ri(e=process.cwd()){return I(e,"ssh","known_hosts")}async function
|
|
406
|
-
`,{mode:384})}class ft{options;run;scan;readKnownHosts;appendKnownHosts;constructor(e,t={}){this.options=e;this.run=t.run??Ci,this.scan=t.scan??yo,this.readKnownHosts=t.readKnownHosts??
|
|
407
|
+
`)){let i=s.trim();if(!i||i.startsWith("#")||i.startsWith("@"))continue;let[o,a,c]=i.split(/\s+/);if(!o||!a||!c)continue;if(o.split(",").includes(n))return{algorithm:a,key:c}}return null}function Ri(e=process.cwd()){return I(e,"ssh","known_hosts")}async function Cl(e){try{return await _l(e,"utf8")}catch{return""}}async function Pl(e,t){await $l(Tl(e),{recursive:!0}),await wl(e,`${t}
|
|
408
|
+
`,{mode:384})}class ft{options;run;scan;readKnownHosts;appendKnownHosts;constructor(e,t={}){this.options=e;this.run=t.run??Ci,this.scan=t.scan??yo,this.readKnownHosts=t.readKnownHosts??Cl,this.appendKnownHosts=t.appendKnownHosts??Pl}runnerFor(e={}){if(e.sudo!==void 0)return At({sudo:e.sudo});return this.options.runner??At()}async exec(e,t,r={}){let n=await this.run(["ssh",...Dn(this.options,e),this.runnerFor(r)],t);if(n.code!==0){let s={status:n.code,signal:n.signal,stdout:n.stdout,stderr:n.stderr};throw Error(`${Ue(s)}${Pt(s,t)}`)}return n.stdout}async scp(e,t,r){let n=["scp",...sr,...Ui(this.options,e,t,r)],s=3;for(let i=1;i<=3;i++){let o=await this.run(n);if(o.code===0)return;if(i===3)throw Error(Ue({status:o.code,signal:o.signal,stdout:o.stdout,stderr:o.stderr}))}}async probe(e){return(await this.run(["ssh",...Dn(this.options,e,["-o","ConnectTimeout=5"]),"true"])).code===0}async ensureHostKey(e){let t=this.options.port??22;if(this.options.hostKey==="insecure")try{return{fingerprint:Er((await this.scan(e,t,this.run)).key),pinnedNow:!1}}catch{return{fingerprint:"",pinnedNow:!1}}let{policy:r,knownHostsFile:n}=this.options.hostKey,s=zi(await this.readKnownHosts(n),e,t);if(s)return{fingerprint:Er(s.key),pinnedNow:!1};let i=await this.scan(e,t,this.run);if(r==="pin")return await this.appendKnownHosts(n,Mi(e,t,i)),{fingerprint:Er(i.key),pinnedNow:!0};return{fingerprint:Er(i.key),pinnedNow:!1}}}import{isIP as Rl}from"node:net";var Ne=1,An="/var/lib/ts-cloud";function Bi(e=Ne){return`${An}/bootstrap.v${e}`}var Ki=1;function El(e){return e==="aarch64"||e==="arm64"}function Fi(e){let t=e.lanIp?.trim()||e.facts?.lanIp?.trim();return t&&Rl(t)!==0?t:void 0}function pr(e,t,r){if(!Ni(e.infrastructure?.compute?.managedServices?.vitess))return;if(t==="raspberry-pi"||El(r))throw Error(`managedServices.vitess is enabled, but Vitess publishes only x86_64 release tarballs and this host is ${r??"a Raspberry Pi (arm64)"}. Drop managedServices.vitess or deploy it to an x86_64 host.`)}function We(e){return`'${e.replace(/'/g,`'"'"'`)}'`}function xl(){return`
|
|
407
409
|
# A Pi 5 has no battery-backed RTC and boots in 1970 until NTP answers. apt
|
|
408
410
|
# rejects Release files "from the future" and ACME rejects a skewed clock, so
|
|
409
411
|
# wait (bounded) for systemd-timesyncd before anything touches the network
|
|
@@ -419,7 +421,7 @@ if command -v timedatectl >/dev/null 2>&1; then
|
|
|
419
421
|
systemctl start systemd-time-wait-sync >/dev/null 2>&1 || true
|
|
420
422
|
fi
|
|
421
423
|
fi
|
|
422
|
-
`}function
|
|
424
|
+
`}function Ol(){return`
|
|
423
425
|
# SD cards wear; a journal allowed to grow to the default 10% of the disk is
|
|
424
426
|
# the single biggest writer on an idle Pi. Bound it.
|
|
425
427
|
install -d -m 0755 /etc/systemd/journald.conf.d
|
|
@@ -430,11 +432,11 @@ MaxRetentionSec=7day
|
|
|
430
432
|
Compress=yes
|
|
431
433
|
TS_CLOUD_JOURNAL_EOF
|
|
432
434
|
systemctl restart systemd-journald
|
|
433
|
-
`}function
|
|
435
|
+
`}function Dl(e){return`
|
|
434
436
|
# The deploy user uploads releases as itself (scp), while the scripts run as
|
|
435
437
|
# root through sudo. setgid group-writable directories let both write.
|
|
436
438
|
install -d -m 2775 -g "$(id -gn ${We(e)})" /var/ts-cloud/artifacts /var/ts-cloud/staging
|
|
437
|
-
`}function
|
|
439
|
+
`}function Il(e){let t=We(e),r=`/home/${e}`;return[`chown -R ${t}:"$(id -gn ${t})" ${We(`${r}/.ssh`)}`,`chmod 700 ${We(`${r}/.ssh`)}`]}function wr(e){let{config:t,environment:r,profile:n,sudoUser:s}=e;pr(t,n,e.facts?.arch);let i={...t.infrastructure?.compute,proxy:le(t,{environment:e.environment,environmentType:e.environment})},o=t.sites??{},a=t.project.slug,c=e.lan?{...e.lan.hostname?{hostname:e.lan.hostname}:{},tls:e.lan.tls??"local-ca",...Fi(e)?{ip:Fi(e)}:{}}:void 0,l=i.proxy?.engine==="rpx"?Te(o,{proxy:i.proxy,slug:a,lan:c,mail:ne(t,{environment:e.environment,environmentType:e.environment})}):void 0,u=i.proxy?.engine==="rpx"&&l?$e({proxy:i.proxy,config:l,slug:a,profile:n,bunBin:i.runtime==="node"||i.runtime==="deno"?void 0:"/usr/local/bin/bun"}):void 0,d=i.proxy?.engine==="rpx"&&l?et({config:l,slug:a}):void 0,m=s?`/home/${s}/.ssh/authorized_keys`:void 0,h=Fe(t,{environment:r,authorizedKeysPath:m}),f=s?[...h.servicesProvision??[],...Il(s)]:h.servicesProvision,S=[...new Set([...i.systemPackages??[],"psmisc","ca-certificates"])],x=i.swapGb??(n==="raspberry-pi"?Ki:void 0),B=ye({runtime:h.runtime,runtimeVersion:h.runtimeVersion,systemPackages:S,database:t.infrastructure?.database,phpProvision:h.phpProvision,servicesProvision:f,rpxProvision:u,swapGb:x}).replace(/^#!\/bin\/bash\nset -euo pipefail\n/,""),L=Bi(),N=l?.localCa?` lan=${l.localCa.hosts.join(",")} tls=local-ca`:c?` lan=${c.hostname??`${a}.local`} tls=${c.tls??"local-ca"}`:"",D=`#!/bin/bash
|
|
438
440
|
set -euo pipefail
|
|
439
441
|
# ts-cloud ssh bootstrap v${Ne} profile=${n}${N}
|
|
440
442
|
|
|
@@ -448,7 +450,7 @@ if [ -e ${We(L)} ]; then
|
|
|
448
450
|
`)}
|
|
449
451
|
`}if(D+=` exit 0
|
|
450
452
|
fi
|
|
451
|
-
`,D+=
|
|
453
|
+
`,D+=xl(),n==="raspberry-pi")D+=Ol();if(s)D+=Dl(s);if(D+=`
|
|
452
454
|
${B}`,D+=`
|
|
453
455
|
install -d -m 0755 ${We(An)}
|
|
454
456
|
date -u +%Y-%m-%dT%H:%M:%SZ > ${We(L)}
|
|
@@ -488,21 +490,21 @@ printf '"privilege":"%s"' "$(sudo -n true >/dev/null 2>&1 && printf sudo || prin
|
|
|
488
490
|
printf '}\\n'
|
|
489
491
|
`;function kn(e){let t=e.split(`
|
|
490
492
|
`).map((r)=>r.trim()).filter(Boolean).at(-1);if(!t||!t.startsWith("{"))throw Error(`Preflight returned no facts:
|
|
491
|
-
${e.trim().slice(-2000)}`);try{return JSON.parse(t)}catch(r){throw Error(`Preflight returned malformed facts: ${r.message}`)}}var ht=1073741824;function
|
|
493
|
+
${e.trim().slice(-2000)}`);try{return JSON.parse(t)}catch(r){throw Error(`Preflight returned malformed facts: ${r.message}`)}}var ht=1073741824;function Al(e){return e==="aarch64"||e==="arm64"}function Nn(e,t){let r=[],n=e.arch??"";if(!Al(n)&&!(n==="x86_64"&&t.profile==="generic"))r.push({code:"arch.not-aarch64",severity:"error",message:t.profile==="raspberry-pi"?`The raspberry-pi profile expects a 64-bit ARM OS; this host reports ${n||"an unknown architecture"}.`:`Unsupported architecture ${n||"(unknown)"}.`,remediation:t.profile==="raspberry-pi"?"Flash the 64-bit Raspberry Pi OS or Ubuntu Server arm64 image.":"Use an x86_64 or arm64 Linux host."});let s=`${e.osId??""} ${e.osIdLike??""}`.toLowerCase();if(!/\b(?:debian|ubuntu|raspbian)\b/.test(s))r.push({code:"os.unsupported",severity:"error",message:`The bootstrap targets Debian and Ubuntu (apt); this host reports ${e.os||"an unknown OS"}.`,remediation:"Install Raspberry Pi OS, Debian or Ubuntu Server."});let i=e.memoryBytes??0;if(i<ht)r.push({code:"memory.low",severity:"error",message:`${(i/ht).toFixed(2)} GiB of memory; the runtime plus a gateway needs at least 1 GiB.`,remediation:"Use a host with 2 GiB or more."});else if(i<2*ht)r.push({code:"memory.low",severity:"warning",message:`${(i/ht).toFixed(2)} GiB of memory; on-box builds and managed services will be tight.`,remediation:"Build releases in CI rather than on the host, and keep managedServices small."});if((e.diskFreeBytes??0)<4*ht)r.push({code:"disk.low",severity:"error",message:`${((e.diskFreeBytes??0)/ht).toFixed(1)} GiB free on /; releases, the artifact cache and swap need at least 4 GiB.`,remediation:"Free space or use a larger card or disk."});if(t.user!=="root"&&!e.sudoOk)r.push({code:"sudo.missing",severity:"error",message:`${t.user} cannot run sudo without a password, and the bootstrap needs root.`,remediation:`Add '${t.user} ALL=(ALL) NOPASSWD:ALL' to /etc/sudoers.d/ts-cloud, or deploy as root.`});if(!e.timeSynced)r.push({code:"time.unsynced",severity:"warning",message:"The clock is not NTP-synchronised yet; the bootstrap waits for it before running apt.",remediation:"Nothing to do unless it never syncs: then check the network and systemd-timesyncd."});if(!e.httpsOk)r.push({code:"https.blocked",severity:"error",message:"The host cannot reach https://github.com, so it cannot download the runtime or packages.",remediation:"Check DNS, the default route and any egress firewall from the host."});if(e.cloudInitPresent&&/running/i.test(e.cloudInitStatus??""))r.push({code:"cloud-init.running",severity:"info",message:"cloud-init is still running its first boot; the adopt waits for it to finish."});if(e.piModel)r.push({code:"pi.model",severity:"info",message:e.piModel});if(e.kernelBits===32)r.push({code:"bits.32",severity:"error",message:"This is a 32-bit userland; bun ships no 32-bit build.",remediation:"Flash a 64-bit OS image."});return r}function Rr(e){return e.some((t)=>t.severity==="error")}function pn(e){if(e.length===0)return"No findings.";let t=Math.max(...e.map((r)=>r.code.length));return e.map((r)=>{let n=`${r.severity.padEnd(7)} ${r.code.padEnd(t)} ${r.message}`;return r.remediation?`${n}
|
|
492
494
|
${" ".repeat(t+10)}${r.remediation}`:n}).join(`
|
|
493
|
-
`)}var Ft="/var/ts-cloud/staging",Hn=ot,ro="TS_CLOUD_SSH_SKIP_BOOTSTRAP",
|
|
495
|
+
`)}var Ft="/var/ts-cloud/staging",Hn=ot,ro="TS_CLOUD_SSH_SKIP_BOOTSTRAP",Hl=["size","instances","appServers","servicesServer","loadBalancer","fleet","image","bakedImage","allowSsh"];function Mt(e){return`'${e.replace(/'/g,`'"'"'`)}'`}async function Ul(e){let t=Ll("sha256");for await(let r of kl(e))t.update(r);return t.digest("hex")}function no(e,t){if(e.cloud?.attachTo)throw Error("cloud.attachTo is not supported by the ssh provider: it deploys to the hosts in ssh.hosts and nothing else.");if(t.hosts.length>1)throw Error(`ssh.hosts lists ${t.hosts.length} hosts; multi-host ssh fleets are not supported yet.`);if(Ni(e.infrastructure?.compute?.managedServices?.vitess))pr(e,t.profile);let r=e.infrastructure?.compute??{};return Hl.filter((n)=>r[n]!==void 0).map((n)=>`infrastructure.compute.${n} has no effect with the ssh provider; the host is what it is.`)}class Ir{name="ssh";usesCloudFormation=!1;settings;cwd;bootWait;transportInstance;constructor(e={}){this.settings=Ar(void 0,{hosts:e.hosts,user:e.user,port:e.port,privateKeyPath:e.privateKeyPath,hostKey:e.hostKey,sudo:e.sudo,profile:e.profile,publicIp:e.publicIp,lan:e.lan}),this.cwd=e.cwd??process.cwd(),this.transportInstance=e.transport,this.bootWait={sshIntervalMs:e.bootWait?.sshIntervalMs??5000,sshTimeoutMs:e.bootWait?.sshTimeoutMs??300000,cloudInitIntervalMs:e.bootWait?.cloudInitIntervalMs??5000,cloudInitTimeoutMs:e.bootWait?.cloudInitTimeoutMs??600000}}get host(){if(this.settings.hosts.length>1)throw Error(`ssh.hosts lists ${this.settings.hosts.length} hosts; multi-host ssh fleets are not supported yet.`);return this.settings.hosts[0]}get transport(){if(!this.transportInstance){let e=this.host;this.transportInstance=new ft({user:e.user,port:e.port,identityFile:e.privateKeyPath,hostKey:this.settings.hostKey==="insecure"?"insecure":{policy:this.settings.hostKey,knownHostsFile:Ri(this.cwd)}})}return this.transportInstance}exec(e,t){return this.transport.exec(e,t,{sudo:this.settings.sudo})}async findComputeTargets(e){if((e.role||"app")!=="app")return[];let{host:t}=this.host;return[{id:t,name:t,publicIp:t,status:"running"}]}async getComputeOutputs(){let{host:e,user:t}=this.host;return{appInstanceId:e,appPublicIp:e,sshUser:t,deployStoragePath:Ft}}async uploadRelease(e){let t=e.targets?.length?e.targets:await this.findComputeTargets({slug:e.config.project.slug,environment:e.environment,role:"app",stackName:T(e.config,e.environment)});if(t.length===0)throw Error("No ssh compute targets found for release upload");let n=`${e.remoteKey.replace(/^releases\//,"").replace(/\.tar\.gz$/,"").replace(/\//g,"-")}-${to()}.tar.gz`,s=`${Ft}/${n}`,i=await Ul(e.localPath),o=`${Hn}/${i}.tar.gz`;for(let a of t){if(!a.publicIp)throw Error(`Target ${a.id} has no address for SCP upload`);let c=["set -euo pipefail",`mkdir -p ${Mt(Hn)} ${Mt(Ft)}`,`test -s ${Mt(o)}`,`cp -- ${Mt(o)} ${Mt(s)}`].join(`
|
|
494
496
|
`);try{await this.exec(a.publicIp,c);continue}catch{}let l=`${Hn}/.${i}-${to()}.tmp`;try{await this.transport.scp(a.publicIp,e.localPath,l),await this.exec(a.publicIp,rr(l,o,s))}catch(u){throw await this.exec(a.publicIp,nr(l)).catch(()=>{}),u}}return{artifactRef:s}}async runRemoteDeploy(e){if(e.targets.length===0)return{success:!1,instanceCount:0,perInstance:[],error:"No targets provided"};let t=e.commands.join(`
|
|
495
497
|
`),r=[];for(let s of e.targets){if(!s.publicIp){r.push({instanceId:s.id,status:"Failed",error:"Missing address"});continue}try{let i=await this.exec(s.publicIp,t);qe(i),r.push({instanceId:s.id,status:"Success",output:i})}catch(i){r.push({instanceId:s.id,status:"Failed",error:i.message})}}let n=r.every((s)=>s.status==="Success");return{success:n,instanceCount:e.targets.length,perInstance:r,error:n?void 0:Me(r,"One or more SSH deploy commands failed")}}targetHost(){return this.host.host}ensureHostKey(e=this.host.host){return this.transport.ensureHostKey(e)}async preflight(e=this.host.host){let t=await this.transport.exec(e,Ln,{sudo:!1}),r=kn(t);return{facts:r,findings:Nn(r,{user:this.host.user,profile:this.settings.profile})}}async provisionComputeInfrastructure(e){let{config:t,environment:r}=e;for(let f of no(t,this.settings))console.warn(`[ts-cloud] ${f}`);let n=this.host,s=T(t,r),i=await Ge(s,this.cwd),o=process.env[ro]==="1"&&i?.provider==="ssh"&&i.host===n.host&&i.bootstrapVersion===Ne,a=await this.ensureHostKey(n.host);if(a.pinnedNow)console.warn(`[ts-cloud] pinned ${n.host} host key ${a.fingerprint}`);await this.waitForSshReady(n.host);let{facts:c,findings:l}=await this.preflight(n.host);if(Rr(l))throw Error(`Preflight failed for ${n.host}; refusing to bootstrap it.
|
|
496
|
-
${pn(l)}`);for(let f of l)console.warn(`[ts-cloud] ${f.severity}: ${f.message}`);if(c.cloudInitPresent&&/running/i.test(c.cloudInitStatus??""))await this.waitForCloudInit(n.host);let u=c.lanIp?.trim()||(
|
|
497
|
-
${s}`);if(/status:\s*(?:done|degraded)/.test(s))return}catch(s){if(s instanceof Error&&/cloud-init reported an error/.test(s.message))throw s}await this.sleep(t)}throw Error(`Timed out waiting for cloud-init to finish on ${e} after ${r}ms`)}}function O(e){let t=e.provider??U(e.config);if(e.config.cloud?.residency&&(ur()||t!=="hetzner"))throw Error("Residency policy requires a Hetzner host with live provider evidence");if(ur())return new lr;switch(e.provider??U(e.config)){case"aws":return new qt({region:e.config.project.region});case"hetzner":return new ke({apiToken:e.config.hetzner?.apiToken,allowStateOnly:Boolean(e.config.cloud?.attachTo),sshPrivateKeyPath:e.config.hetzner?.sshPrivateKeyPath,sshPublicKeyPath:e.config.hetzner?.sshPublicKeyPath,sshUser:e.config.hetzner?.sshUser,location:e.config.hetzner?.location,residency:e.config.cloud?.residency});case"ssh":return new Ir({hosts:e.config.ssh?.hosts,hostKey:e.config.ssh?.hostKey,sudo:e.config.ssh?.sudo,profile:e.config.ssh?.profile,publicIp:e.config.ssh?.publicIp,lan:e.config.ssh?.lan});case"fly":throw Error("Fly.io has no box to provision or reach over SSH: deploy it with deployToFly() from @stacksjs/ts-cloud.");default:throw Error(`Unknown cloud provider: ${e.provider??U(e.config)}`)}}class so{drivers=new Map;getDriver(e,t){let r=t??U(e),n=`${r}:${e.project.slug}:${e.project.region||"default"}:${JSON.stringify(e.cloud?.residency||null)}`,s=this.drivers.get(n);if(s)return s;let i=O({config:e,provider:r});return this.drivers.set(n,i),i}}var
|
|
498
|
-
`)||!/^(?:ssh|ecdsa|sk)-/.test(t))throw Error("publicKey must be one public key line, e.g. the contents of ~/.ssh/id_ed25519.pub.");if(e.wifi){if(!e.wifi.ssid)throw Error("wifi.ssid is required.");if(!e.wifi.passphrase)throw Error("wifi.passphrase is required.");if(!
|
|
498
|
+
${pn(l)}`);for(let f of l)console.warn(`[ts-cloud] ${f.severity}: ${f.message}`);if(c.cloudInitPresent&&/running/i.test(c.cloudInitStatus??""))await this.waitForCloudInit(n.host);let u=c.lanIp?.trim()||(Nl(n.host)!==0?n.host:void 0),d=i?.provider==="ssh"?i.bootstrappedAt:void 0;if(!o){let f=wr({config:t,environment:r,profile:this.settings.profile,facts:c,sudoUser:n.user!=="root"?n.user:void 0,lan:this.settings.lan,lanIp:u});qe(await this.exec(n.host,f)),d=new Date().toISOString()}let m={provider:"ssh",stackName:s,host:n.host,sshUser:n.user,sshPort:n.port,hostKeyFingerprint:a.fingerprint||void 0,publicIp:this.settings.publicIp==="auto"?void 0:this.settings.publicIp,lanIp:c.lanIp||void 0,deployStoragePath:Ft,profile:this.settings.profile,bootstrapVersion:Ne,bootstrappedAt:d};await mt(s,m,this.cwd);let h=await this.getComputeOutputs();if(ee(t.infrastructure?.compute)&&Br(this.settings.lan)==="local-ca")h.lanCaCertPath=Gt(),console.warn(`[ts-cloud] LAN certificate authority on ${n.host} at ${h.lanCaCertPath}. Fetch it with: scp ${n.user}@${n.host}:${h.lanCaCertPath} ./rpx-root-ca.crt`);return h}async destroyCompute(e){let t=T(e.config,e.environment),r=this.host;return console.warn(`[ts-cloud] forgot ${r.host} for ${t}; nothing was removed on the host (stop the systemd units and remove /var/ts-cloud and /etc/rpx by hand if you want it clean).`),await mt(t,{provider:"ssh",stackName:t,host:r.host,sshUser:r.user,sshPort:r.port},this.cwd).catch(()=>{}),{destroyed:[`local state for ${t}`]}}async sleep(e){await new Promise((t)=>setTimeout(t,e))}async waitForSshReady(e){let{sshIntervalMs:t,sshTimeoutMs:r}=this.bootWait,n=Date.now();do{if(await this.transport.probe(e))return;await this.sleep(t)}while(Date.now()-n<r);throw Error(`Timed out waiting for SSH on ${e} after ${r}ms`)}async waitForCloudInit(e){let{cloudInitIntervalMs:t,cloudInitTimeoutMs:r}=this.bootWait,n=Date.now();while(Date.now()-n<r){try{let s=await this.transport.exec(e,"if command -v cloud-init >/dev/null 2>&1; then cloud-init status --long 2>/dev/null || true; else echo 'status: done'; fi",{sudo:!1});if(/status:\s*error/.test(s))throw Error(`cloud-init reported an error on ${e}:
|
|
499
|
+
${s}`);if(/status:\s*(?:done|degraded)/.test(s))return}catch(s){if(s instanceof Error&&/cloud-init reported an error/.test(s.message))throw s}await this.sleep(t)}throw Error(`Timed out waiting for cloud-init to finish on ${e} after ${r}ms`)}}function O(e){let t=e.provider??U(e.config);if(e.config.cloud?.residency&&(ur()||t!=="hetzner"))throw Error("Residency policy requires a Hetzner host with live provider evidence");if(ur())return new lr;switch(e.provider??U(e.config)){case"aws":return new qt({region:e.config.project.region});case"hetzner":return new ke({apiToken:e.config.hetzner?.apiToken,allowStateOnly:Boolean(e.config.cloud?.attachTo),sshPrivateKeyPath:e.config.hetzner?.sshPrivateKeyPath,sshPublicKeyPath:e.config.hetzner?.sshPublicKeyPath,sshUser:e.config.hetzner?.sshUser,location:e.config.hetzner?.location,residency:e.config.cloud?.residency});case"ssh":return new Ir({hosts:e.config.ssh?.hosts,hostKey:e.config.ssh?.hostKey,sudo:e.config.ssh?.sudo,profile:e.config.ssh?.profile,publicIp:e.config.ssh?.publicIp,lan:e.config.ssh?.lan});case"fly":throw Error("Fly.io has no box to provision or reach over SSH: deploy it with deployToFly() from @stacksjs/ts-cloud.");default:throw Error(`Unknown cloud provider: ${e.provider??U(e.config)}`)}}class so{drivers=new Map;getDriver(e,t){let r=t??U(e),n=`${r}:${e.project.slug}:${e.project.region||"default"}:${JSON.stringify(e.cloud?.residency||null)}`,s=this.drivers.get(n);if(s)return s;let i=O({config:e,provider:r});return this.drivers.set(n,i),i}}var Of=new so;var io="/var/lib/ts-cloud/bootstrap.sh",bo={"raspberry-pi-os":"bootfs",ubuntu:"system-boot"},Ml=/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/,zl=/^[a-z_][a-z0-9_-]{0,31}$/,Fl=/^[A-Z]{2}$/;function ve(e){return JSON.stringify(e)}function Bl(e){if(!Ml.test(e.hostname))throw Error(`Invalid hostname '${e.hostname}': use lowercase letters, digits and hyphens.`);if(!zl.test(e.user))throw Error(`Invalid user '${e.user}': use a lowercase Unix user name.`);let t=e.publicKey.trim();if(!t||t.includes(`
|
|
500
|
+
`)||!/^(?:ssh|ecdsa|sk)-/.test(t))throw Error("publicKey must be one public key line, e.g. the contents of ~/.ssh/id_ed25519.pub.");if(e.wifi){if(!e.wifi.ssid)throw Error("wifi.ssid is required.");if(!e.wifi.passphrase)throw Error("wifi.passphrase is required.");if(!Fl.test(e.wifi.country))throw Error(`wifi.country must be a two-letter regulatory domain such as DE or US, not '${e.wifi.country}'.`)}}function Kl(e,t){let r=[`hostname: ${ve(e.hostname)}`,"manage_etc_hosts: true","users:",` - name: ${ve(e.user)}`," ssh_authorized_keys:",` - ${ve(e.publicKey.trim())}`,` sudo: ${ve("ALL=(ALL) NOPASSWD:ALL")}`," shell: /bin/bash"," lock_passwd: true","ssh_pwauth: false"];if(e.timezone)r.push(`timezone: ${ve(e.timezone)}`);if(e.locale)r.push(`locale: ${ve(e.locale)}`);if(r.push("package_update: true"),t==="raspberry-pi-os")r.push("rpi:"," enable_ssh: true"," interfaces:"," spi: false"," i2c: false"," onewire: false"," remote_gpio: false"," serial:"," console: false"," hardware: false");return`${r.join(`
|
|
499
501
|
`)}
|
|
500
|
-
`}function
|
|
502
|
+
`}function jl(e){let t=["version: 2","ethernets:"," eth0:"," match:",` name: ${ve("e*")}`," dhcp4: true"," dhcp6: true"," optional: true"];if(e.wifi)t.push("wifis:"," wlan0:"," dhcp4: true"," dhcp6: true"," optional: true",` regulatory-domain: ${ve(e.wifi.country)}`," access-points:",` ${ve(e.wifi.ssid)}:`,` password: ${ve(e.wifi.passphrase)}`);return`${t.join(`
|
|
501
503
|
`)}
|
|
502
|
-
`}function
|
|
504
|
+
`}function Gl(e){return[`instance-id: ${ve(`ts-cloud-${e.hostname}`)}`,`local-hostname: ${ve(e.hostname)}`].join(`
|
|
503
505
|
`)+`
|
|
504
|
-
`}function
|
|
505
|
-
`)}function So(e,t,r){
|
|
506
|
+
`}function Wl(e,t){return[`Copy user-data, network-config and meta-data onto the boot partition of the flashed card (the volume named '${bo[t]}'), replacing the files already there.`,"Eject the card, put it in the board and power it on.","The first boot takes a few minutes: cloud-init creates the user, waits for the clock to sync, then runs the ts-cloud bootstrap (packages, runtime, gateway).",`Then, from this machine: cloud ssh:preflight ${e.hostname}.local --user ${e.user}`,"When the preflight is clean, cloud deploy adopts the host and ships the sites."].join(`
|
|
507
|
+
`)}function So(e,t,r){Bl(e);let n={scriptPath:io,permissions:"0700",before:Kl(e,r.os)};return{files:{"user-data":Ce(t,n),"network-config":jl(e),"meta-data":Gl(e)},instructions:Wl(e,r.os)}}function dr(e){return e.split("=",1)[0]??e}function oo(e){return e.replace(/@[^.]+(?=\.service$)/,"")}function Vc(e,t){let r=new Set(t.runningServices.map(oo)),n=new Set(t.routeFragments.map(dr)),s=new Set(t.releaseLinks.map(dr)),i=new Set(t.routeIds),o=new Set(t.dataCatalog);return{stoppedServices:e.runningServices.filter((a)=>!r.has(oo(a))),changedRouteFragments:e.routeFragments.filter((a)=>!n.has(dr(a))),missingRouteIds:e.routeIds.filter((a)=>!i.has(a)),changedReleaseLinks:e.releaseLinks.filter((a)=>!s.has(dr(a))),missingData:e.dataCatalog.filter((a)=>!o.has(a))}}function ao(e){return e.infrastructure?.compute??{}}function vo(e){let t=ao(e),r=t.monitoring;return{firewallPorts:[...new Set([22,80,443,...t.firewall?.allowedPorts??[]])].filter((s)=>Number.isInteger(s)&&s>=1&&s<=65535).sort((s,i)=>s-i),monitoring:r!==!1&&(typeof r!=="object"||r.enabled!==!1),autoUpdates:t.autoUpdates!==!1,swapGb:Math.max(0,Math.floor(t.swapGb??2)),sshPasswordAuthentication:!1,sshMaxStartups:"100:30:200",journalMaxUse:"256M",journalRetention:"14day"}}function Vl(e){return["install -d -m 0755 /etc/ssh/sshd_config.d /etc/systemd/journald.conf.d /etc/sysctl.d /etc/fail2ban/jail.d","cat > /etc/ssh/sshd_config.d/99-ts-cloud-hardening.conf <<'TS_CLOUD_SSH_EOF'","# Managed by ts-cloud host optimization.","PasswordAuthentication no","KbdInteractiveAuthentication no","PermitRootLogin prohibit-password","MaxAuthTries 4","LoginGraceTime 30",`MaxStartups ${e.sshMaxStartups}`,"TS_CLOUD_SSH_EOF","sshd -t","systemctl reload ssh","cat > /etc/systemd/journald.conf.d/99-ts-cloud-retention.conf <<'TS_CLOUD_JOURNAL_EOF'","[Journal]",`SystemMaxUse=${e.journalMaxUse}`,`MaxRetentionSec=${e.journalRetention}`,"Compress=yes","TS_CLOUD_JOURNAL_EOF","systemctl restart systemd-journald","journalctl --vacuum-size=256M --vacuum-time=14d >/dev/null","cat > /etc/sysctl.d/99-ts-cloud-production.conf <<'TS_CLOUD_SYSCTL_EOF'","# Managed by ts-cloud host optimization.","vm.swappiness=10","vm.dirty_background_ratio=5","vm.dirty_ratio=15","net.core.somaxconn=4096","net.ipv4.tcp_max_syn_backlog=4096","net.ipv4.tcp_fin_timeout=30","net.ipv4.tcp_keepalive_time=300","net.ipv4.tcp_keepalive_intvl=30","net.ipv4.tcp_keepalive_probes=5","fs.inotify.max_user_watches=524288","TS_CLOUD_SYSCTL_EOF","sysctl --system >/dev/null","export DEBIAN_FRONTEND=noninteractive","command -v fail2ban-client >/dev/null 2>&1 || apt-get install -y fail2ban","cat > /etc/fail2ban/jail.d/99-ts-cloud-sshd.conf <<'TS_CLOUD_FAIL2BAN_EOF'","[sshd]","enabled = true","backend = systemd","bantime = 1h","findtime = 10m","maxretry = 5","TS_CLOUD_FAIL2BAN_EOF","systemctl enable --now fail2ban","systemctl restart fail2ban"]}function ql(e){if(e===0)return[];return["if ! swapon --show=NAME --noheadings 2>/dev/null | grep -qx '/swapfile'; then",` if [ ! -f /swapfile ]; then fallocate -l ${e}G /swapfile || dd if=/dev/zero of=/swapfile bs=1M count=${e*1024} status=none; fi`," chmod 600 /swapfile"," mkswap /swapfile >/dev/null"," swapon /swapfile","fi","grep -qE '^/swapfile\\s' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab"]}function co(e){let t=ao(e),r=vo(e),n=t.monitoring??!0,s=t.firewall??{enabled:!0};return["set -euo pipefail",...ee(t)?["systemctl disable --now nginx.service bun-gateway.service ts-cloud-nginx.service 2>/dev/null || true","systemctl reset-failed nginx.service bun-gateway.service ts-cloud-nginx.service 2>/dev/null || true"]:[],...ql(r.swapGb),...Ee(r.autoUpdates),...Pe(n),...Vl(r),...Wt(s),"systemctl daemon-reload","systemctl reset-failed"]}var Xl=String.raw`
|
|
506
508
|
const text = (command) => {
|
|
507
509
|
const result = Bun.spawnSync(['bash', '-lc', command], { stdout: 'pipe', stderr: 'pipe' })
|
|
508
510
|
return result.stdout.toString().trim()
|
|
@@ -533,35 +535,35 @@ console.log(JSON.stringify({
|
|
|
533
535
|
failedUnits: lines("systemctl --failed --type=service --no-legend --plain | awk '{print $1}'"),
|
|
534
536
|
publicTcpPorts: [...new Set(publicTcpPorts)].sort((a, b) => a - b),
|
|
535
537
|
}))
|
|
536
|
-
`;function
|
|
537
|
-
`),n=`printf %s ${Buffer.from(r).toString("base64")} | base64 -d | bash`;await _e(t.host,n,t)}async function xs(e){let t=await _e(e.host,Xl
|
|
538
|
-
`,"utf8"),await lo(r,t)}async function $s(e,t=1800000){let r=wo(e);await mr(Le(),{recursive:!0});try{await mr(r)}catch(n){if(Date.now()-(await
|
|
539
|
-
`,"utf8"),async()=>{await
|
|
538
|
+
`;function Yl(e){return`printf %s ${Buffer.from(e).toString("base64")} | base64 -d | bun -`}async function Ns(e,t){let r=co(e).join(`
|
|
539
|
+
`),n=`printf %s ${Buffer.from(r).toString("base64")} | base64 -d | bash`;await _e(t.host,n,t)}async function xs(e){let t=await _e(e.host,Yl(Xl),e);return JSON.parse(t)}function _s(e,t){let r=[],n=new Set(t.firewallPorts),s=e.firewallPorts.filter((o)=>!n.has(o)),i=t.firewallPorts.filter((o)=>!e.firewallPorts.includes(o));if(!t.firewallActive)r.push("UFW is not active");if(s.length>0)r.push(`UFW is missing ports: ${s.join(", ")}`);if(i.length>0)r.push(`UFW has unexpected ports: ${i.join(", ")}`);if(t.passwordAuthentication)r.push("SSH password authentication remains enabled");if(t.rootPasswordLogin)r.push("SSH root password login remains enabled");if(!t.fail2banActive)r.push("fail2ban is not active");if(e.autoUpdates&&!t.unattendedUpgradesActive)r.push("unattended upgrades are not active");if(e.monitoring&&!t.metricsTimerActive)r.push("metrics timer is not active");if(e.monitoring&&!t.metricsSnapshotFresh)r.push("metrics snapshot is stale");if(e.swapGb>0&&t.swapBytes<e.swapGb*1073741824*0.9)r.push("configured swap is missing");if(t.failedUnits.length>0)r.push(`failed services: ${t.failedUnits.join(", ")}`);return r}import{mkdir as mr,readFile as Jl,rename as lo,rmdir as Zl,stat as Ql,unlink as eu,writeFile as uo}from"node:fs/promises";import{join as fr}from"node:path";function Un(e){return fr(Le(),`${e}-resize.json`)}function wo(e){return fr(Le(),`${e}-resize.lock`)}async function Os(e){try{return JSON.parse(await Jl(Un(e),"utf8"))}catch{return null}}async function Ct(e){let t=Un(e.stackName);await mr(Le(),{recursive:!0});let r=`${t}.${process.pid}.tmp`;await uo(r,`${JSON.stringify(e,null,2)}
|
|
540
|
+
`,"utf8"),await lo(r,t)}async function $s(e,t=1800000){let r=wo(e);await mr(Le(),{recursive:!0});try{await mr(r)}catch(n){if(Date.now()-(await Ql(r)).mtimeMs<=t)throw Error(`Another resize process holds ${r}.`);await lo(r,`${r}.stale-${Date.now()}`),await mr(r)}return await uo(fr(r,"owner.json"),`${JSON.stringify({pid:process.pid,acquiredAt:new Date().toISOString()},null,2)}
|
|
541
|
+
`,"utf8"),async()=>{await eu(fr(r,"owner.json")).catch(()=>{}),await Zl(r).catch(()=>{})}}async function Mn(e,t){let r=je(t.publicKey),n=(await e.listSshKeys()).find((i)=>je(i.public_key)===r);if(n)return{id:n.id,name:n.name,created:!1};let s=await e.createSshKey({name:t.name,publicKey:t.publicKey.trim(),labels:t.labels});return{id:s.id,name:s.name,created:!0}}async function zn(e,t){let r=(await e.listFirewalls()).find((s)=>s.name===t.name);if(r)return await e.setFirewallRules(r.id,t.rules),{id:r.id,name:r.name,created:!1};let{firewall:n}=await e.createFirewall({name:t.name,rules:t.rules,labels:t.labels});return{id:n.id,name:n.name,created:!0}}async function Fn(e,t){let{waitForRunning:r=!0,...n}=t,s=(await e.listServers()).find((o)=>o.name===t.name),i=!s;if(!s)s=(await e.createServer(n)).server;if(r)s=await e.waitForServerRunning(s.id);return{server:s,created:i}}function Bn(e){let t=e.public_net.ipv4?.ip;if(!t)throw Error(`Hetzner server "${e.name}" (#${e.id}) has no public IPv4`);return t}function $o(e){let t=e.sshPublicKey.trim(),r=["#cloud-config","disable_root: false","ssh_authorized_keys:",` - ${t}`];if(e.bootstrapScript){let s=e.bootstrapScript.split(`
|
|
540
542
|
`).map((i)=>` ${i}`).join(`
|
|
541
543
|
`);r.push("write_files:"," - path: /var/lib/cloud/box-bootstrap.sh"," permissions: '0755'"," owner: root:root"," content: |",s)}if(r.push("runcmd:",` - mkdir -p /root/.ssh && grep -qF "${t}" /root/.ssh/authorized_keys 2>/dev/null || echo "${t}" >> /root/.ssh/authorized_keys`),e.bootstrapScript)r.push(" - [ bash, /var/lib/cloud/box-bootstrap.sh ]");return`${r.join(`
|
|
542
544
|
`)}
|
|
543
|
-
`}function
|
|
544
|
-
`)}function Lo(e){let t=Ao(e),r=e.packages??[];return Object.entries(
|
|
545
|
+
`}function tu(e=[]){let t=[{direction:"in",protocol:"tcp",port:"22",source_ips:["0.0.0.0/0","::/0"],description:"SSH"}];for(let r of e)t.push({direction:"in",protocol:r.protocol,...r.protocol==="icmp"?{}:{port:String(r.port)},source_ips:["0.0.0.0/0","::/0"]});return t}class gr{client;provider="hetzner";constructor(e){this.client=e}async ensureBox(e){let t=await Mn(this.client,{name:`${e.name}-key`,publicKey:e.sshPublicKey}),r=await zn(this.client,{name:`${e.name}-fw`,rules:tu(e.ports)}),{server:n,created:s}=await Fn(this.client,{name:e.name,serverType:e.size,image:e.image??"ubuntu-24.04",location:e.location,sshKeys:[t.id],firewalls:[{firewall:r.id}],userData:$o(e),labels:e.labels});if(!s){if(!((await this.client.listFirewalls()).find((a)=>a.id===r.id)?.applied_to??[]).some((a)=>a.type==="server"&&a.server?.id===n.id)){let a=await this.client.applyFirewallToResources(r.id,[{type:"server",server:n.id}]);for(let c of a)await this.client.waitForAction(c.id).catch(()=>{})}}return{provider:"hetzner",id:String(n.id),name:n.name,publicIp:Bn(n),created:s}}async destroyBox(e){let t=[],r=(await this.client.listServers()).find((s)=>s.name===e);if(r){let s=await this.client.deleteServer(r.id);await this.client.waitForAction(s.id).catch(()=>{}),t.push(`server ${e} (#${r.id})`)}let n=(await this.client.listFirewalls()).find((s)=>s.name===`${e}-fw`);if(n)for(let s=0;s<10;s++)try{await this.client.deleteFirewall(n.id),t.push(`firewall ${e}-fw (#${n.id})`);break}catch{if(s===9)break;await new Promise((i)=>setTimeout(i,3000))}return{destroyed:t}}}var hr="/opt/pantry/cli",Po=`${R}/pantry/.bin`,ru={"google.com/chrome":{profile:"ts-cloud-pantry-chrome",binary:`${R}/pantry/google.com/chrome/*/lib/chrome-for-testing/chrome`}};function Ro(e){if(!e.startsWith("/")||/[^A-Za-z0-9_./*-]/.test(e)||e.includes("**")||e.split("/").slice(1).some((t)=>!t||t==="."||t===".."))throw Error("Sandbox paths must be absolute without traversal or AppArmor control syntax")}function Ao(e){let t=[...new Set(e.sandboxedPackageProjects??[])].sort();if(t.length>16)throw Error("At most 16 sandboxed package projects may be declared");for(let r of t){Ro(r);let n=r.indexOf("*"),s=r.slice(r.lastIndexOf("/")+1);if(r.length>240||n!==-1&&(n!==r.length-1||!/^[A-Za-z0-9_][A-Za-z0-9_.-]+-\*$/.test(s)))throw Error("Sandbox project wildcards must follow a named final directory prefix")}if(t.length&&!(e.packages??[]).some((r)=>r==="google.com/chrome"||r.startsWith("google.com/chrome@")))throw Error("Sandboxed projects require the Chrome package to be explicitly declared");return t}function nu(e,t,r=[]){if(bt("sandbox profile",e),/[^A-Za-z0-9_.-]/.test(e))throw Error("Invalid sandbox profile name");let n=[...new Set([t,...r])];for(let i of n)Ro(i);let s=n.length===1?n[0]:`/{${n.map((i)=>i.slice(1)).join(",")}}`;return["# Written by ts-cloud: lets this pantry package create user namespaces for its","# sandbox, like Ubuntu's own profile for /opt/google/chrome/chrome.","abi <abi/4.0>,","include <tunables/global>","",`profile ${e} ${s} flags=(unconfined) {`," userns,","",` include if exists <local/${e}>`,"}",""].join(`
|
|
546
|
+
`)}function Lo(e){let t=Ao(e),r=e.packages??[];return Object.entries(ru).filter(([n])=>r.some((s)=>s===n||s.startsWith(`${n}@`))).map(([,{profile:n,binary:s}])=>({name:n,content:nu(n,s,t.map((i)=>`${i}/pantry/google.com/chrome/*/lib/chrome-for-testing/chrome`))}))}function Uo(e){let t=Lo(e);if(!t.length)return`set -euo pipefail
|
|
545
547
|
# No declared sandboxed package profiles.
|
|
546
548
|
`;return["set -euo pipefail","PROFILE_DIR=/etc/apparmor.d","PROFILE_STATE=/sys/kernel/security/apparmor/profiles",'[ "$(id -u)" = 0 ] || { echo "Sandbox policy convergence requires root" >&2; exit 1; }','command -v apparmor_parser >/dev/null || { echo "AppArmor parser is required" >&2; exit 1; }','[ -d "$PROFILE_DIR" ] && [ ! -L "$PROFILE_DIR" ] || { echo "AppArmor policy directory must be a real directory" >&2; exit 1; }','[ "$(stat -c %u "$PROFILE_DIR")" = 0 ] || { echo "AppArmor policy directory must be root-owned" >&2; exit 1; }','MODE=$(stat -c %a "$PROFILE_DIR")','(( (8#$MODE & 022) == 0 )) || { echo "AppArmor policy directory must not be group/other writable" >&2; exit 1; }','WORK=$(mktemp -d "$PROFILE_DIR/.ts-cloud-sandbox.XXXXXX")',"KEEP_BACKUP=0",`trap 'if [ "$KEEP_BACKUP" = 0 ]; then rm -rf -- "$WORK"; fi' EXIT`,...t.flatMap(({name:r,content:n})=>[`TARGET="$PROFILE_DIR/${r}"`,'if [ -L "$TARGET" ] || { [ -e "$TARGET" ] && [ ! -f "$TARGET" ]; }; then echo "Refusing non-regular sandbox policy" >&2; exit 1; fi','if [ -f "$TARGET" ] && [ "$(stat -c %u:%g "$TARGET")" != 0:0 ]; then echo "Sandbox policy must be root-owned" >&2; exit 1; fi','if [ -f "$TARGET" ]; then MODE=$(stat -c %a "$TARGET"); (( (8#$MODE & 022) == 0 )) || { echo "Sandbox policy must not be group/other writable" >&2; exit 1; }; fi','if [ ! -f "$TARGET" ]; then',' [ -r "$PROFILE_STATE" ] || { echo "Cannot inspect existing sandbox policy state" >&2; exit 1; }',' cat -- "$PROFILE_STATE" > "$WORK/profile-state"',' while IFS= read -r LOADED || [ -n "$LOADED" ]; do',' case "$LOADED" in',` "${r} ("*) echo "Refusing to replace an orphaned loaded policy without a recovery definition" >&2; exit 1 ;;`," esac",' done < "$WORK/profile-state"',"fi",`printf %s ${yt(yr(n))} | base64 -d > "$WORK/candidate"`,'apparmor_parser -Q -K "$WORK/candidate"','if [ -f "$TARGET" ] && cmp -s "$WORK/candidate" "$TARGET"; then',' apparmor_parser -r -K "$TARGET"',` echo "${$t} sandbox profile ${r} reconciled"`,"else"," HAD_PREVIOUS=0"," PREVIOUS_MODE=644",' if [ -f "$TARGET" ]; then cp -p -- "$TARGET" "$WORK/previous"; PREVIOUS_MODE=$(stat -c %a "$TARGET"); HAD_PREVIOUS=1; fi'," KEEP_BACKUP=1",' if install -m 644 -o root -g root "$WORK/candidate" "$WORK/ready" && mv -f -- "$WORK/ready" "$TARGET" && apparmor_parser -r -K "$TARGET"; then'," KEEP_BACKUP=0",` echo "${$t} sandbox profile ${r} converged"`," else"," KEEP_BACKUP=1",' if [ "$HAD_PREVIOUS" = 1 ]; then',' if install -m "$PREVIOUS_MODE" -o root -g root "$WORK/previous" "$WORK/restore" && mv -f -- "$WORK/restore" "$TARGET" && apparmor_parser -r -K "$TARGET"; then KEEP_BACKUP=0; fi'," else",' if [ ! -e "$TARGET" ] || apparmor_parser -R -K "$TARGET"; then rm -f -- "$TARGET"; KEEP_BACKUP=0; fi'," fi",' echo "Sandbox reload failed; retained recovery files if rollback failed: $WORK" >&2'," exit 1"," fi","fi"]),""].join(`
|
|
547
|
-
`)}var $t="ts-cloud:",
|
|
549
|
+
`)}var $t="ts-cloud:",su=["/usr/local/bin","/usr/bin","/bin"];function yt(e){return`'${e.split("'").join("'\\''")}'`}function yr(e){return Buffer.from(e,"utf8").toString("base64")}function bt(e,t){if(!/^[a-z_][a-z0-9_.-]*$/i.test(t))throw Error(`Box ${e} "${t}" may only contain letters, digits, '.', '_' and '-'`)}function Mo(e){return Object.entries(e).map(([t,r])=>{if(!/^[A-Z_][A-Z0-9_]*$/i.test(t))throw Error(`Environment variable name "${t}" is not valid`);let n=String(r).replace(/\\/g,"\\\\").replace(/"/g,"\\\"").replace(/\n/g,"\\n");return`${t}="${n}"`}).join(`
|
|
548
550
|
`).concat(`
|
|
549
|
-
`)}function Fo(e,t,r){let n=r.userUnit?"%h":"/root",s=["# Written by ts-cloud from the box declaration; edits here are overwritten.","[Unit]",`Description=${t.description??e}`,"After=network-online.target","","[Service]","Type=simple",`WorkingDirectory=${t.workingDirectory??n}`];if(t.environmentFile)s.push(`EnvironmentFile=${t.environmentFile}`);if(t.environment&&Object.keys(t.environment).length>0)s.push(`EnvironmentFile=${n}/.config/ts-cloud/${e}.env`);return s.push(`Environment=PATH=${[...t.path??[],...r.pantryOnPath===!1?[]:[Po],...
|
|
551
|
+
`)}function Fo(e,t,r){let n=r.userUnit?"%h":"/root",s=["# Written by ts-cloud from the box declaration; edits here are overwritten.","[Unit]",`Description=${t.description??e}`,"After=network-online.target","","[Service]","Type=simple",`WorkingDirectory=${t.workingDirectory??n}`];if(t.environmentFile)s.push(`EnvironmentFile=${t.environmentFile}`);if(t.environment&&Object.keys(t.environment).length>0)s.push(`EnvironmentFile=${n}/.config/ts-cloud/${e}.env`);return s.push(`Environment=PATH=${[...t.path??[],...r.pantryOnPath===!1?[]:[Po],...su].join(":")}`),s.push(`ExecStart=${t.exec}`),s.push(`Restart=${t.restart??"always"}`),s.push(`RestartSec=${t.restartSec??10}`),s.push("","[Install]",`WantedBy=${r.userUnit?"default.target":"multi-user.target"}`,""),s.join(`
|
|
550
552
|
`)}function Bo(e){Ao(e);let t=e.user?.name;if(t)bt("user",t);for(let c of e.user?.groups??[])bt("group",c);let r=Object.entries(e.services??{});for(let[c]of r)bt("service",c);let n=e.pantryVersion,s=["set -euo pipefail","export DEBIAN_FRONTEND=noninteractive",`say() { echo "${$t} $*"; }`,"write_if_changed() {",' local path="$1" mode="$2" owner="$3" tmp'," tmp=$(mktemp)",' printf %s "$4" | base64 -d > "$tmp"',' if [ -f "$path" ] && cmp -s "$tmp" "$path"; then rm -f "$tmp"; return 1; fi',' install -D -m "$mode" -o "${owner%%:*}" -g "${owner##*:}" "$tmp" "$path"',' rm -f "$tmp"',"}",`PANTRY="${hr}/pantry"`,n?`if [ ! -x "$PANTRY" ] || ! "$PANTRY" --version 2>/dev/null | grep -q "pantry ${n.replace(/[^0-9A-Za-z.+-]/g,"")}\\b"; then`:'if [ ! -x "$PANTRY" ]; then'," (",...Tt({version:n,installDir:hr}).map((c)=>` ${c}`)," )",' say "pantry $("$PANTRY" --version 2>/dev/null | head -1)"',"fi",`mkdir -p ${R}`],i=[...new Set(e.packages??[])];if(i.length>0){let c=Math.round((e.minFreeDiskGb??10)*1024*1024),l=`${R}/pantry/.ts-cloud-packages`,u=`${[...i].sort().join(`
|
|
551
553
|
`)}
|
|
552
554
|
`;if(s.push(`WANTED_PACKAGES=${yt(yr(u))}`,`if [ -f ${l} ] && [ "$(base64 -w0 < ${l})" = "$WANTED_PACKAGES" ]; then MISSING=0; else MISSING=1; fi`),s.push('if [ "$MISSING" = 1 ]; then'," FREE_KB=$(df -Pk / | awk 'NR==2 {print $4}')",` if [ "$FREE_KB" -lt ${c} ]; then`,` say "not installing packages: $((FREE_KB / 1048576)) GB free, the box declares at least ${e.minFreeDiskGb??10} GB"`," exit 3"," fi",` PANTRY_SERVICE_SCOPE=system PATH="${hr}:$PATH" sh -c ${yt(`cd ${R} && pantry install ${i.map(yt).join(" ")}`)}`,` chmod -R a+rX ${R}/pantry 2>/dev/null || true`,` printf %s "$WANTED_PACKAGES" | base64 -d > ${l}`,` say "packages ${i.join(" ")}"`,"fi"),Lo(e).length>0)s.push("if command -v apparmor_parser >/dev/null 2>&1 && [ -d /etc/apparmor.d ]; then"," ("),s.push(...Uo(e).trimEnd().split(`
|
|
553
|
-
`).map((d)=>` ${d}`)),s.push(" )","fi")}if(t)s.push(`if ! id -u ${t} >/dev/null 2>&1; then useradd --create-home --shell /bin/bash ${t}; say "created user ${t}"; fi`,...(e.user?.groups??[]).map((c)=>`if getent group ${c} >/dev/null && ! id -nG ${t} | tr ' ' '\\n' | grep -qx ${c}; then usermod -aG ${c} ${t}; say "added ${t} to ${c}"; fi`),`if [ "$(loginctl show-user ${t} -p Linger --value 2>/dev/null)" != yes ]; then loginctl enable-linger ${t}; say "enabled linger for ${t}"; fi`);s.push(...jo(e));let o=t?`${t}:${t}`:"root:root",a=t?"$SVC_HOME/.config/systemd/user":"/etc/systemd/system";if(r.length>0)s.push("RELOAD=0");for(let[c,l]of r){let u=Fo(c,l,{userUnit:Boolean(t),pantryOnPath:i.length>0});s.push(`CHANGED_${c.replace(/[^A-Za-z0-9_]/g,"_")}=0`);let d=`CHANGED_${c.replace(/[^A-Za-z0-9_]/g,"_")}`;if(l.environment&&Object.keys(l.environment).length>0)s.push(`if write_if_changed "$SVC_HOME/.config/ts-cloud/${c}.env" 600 ${o} ${yt(yr(Mo(l.environment)))}; then ${d}=1; fi`);s.push(`if write_if_changed "${a}/${c}.service" 644 ${o} ${yt(yr(u))}; then ${d}=1; RELOAD=1; fi`)}if(r.length>0)s.push('if [ "$RELOAD" = 1 ]; then svc daemon-reload; fi');for(let[c,l]of r){let u=`CHANGED_${c.replace(/[^A-Za-z0-9_]/g,"_")}`;s.push(`svc enable ${c}.service >/dev/null 2>&1 || svc enable ${c}.service`,`if ! svc is-active --quiet ${c}.service; then svc start ${c}.service; say "started ${c}"`,l.restartOnChange?`elif [ "$${u}" = 1 ]; then svc restart ${c}.service; say "restarted ${c}"`:
|
|
555
|
+
`).map((d)=>` ${d}`)),s.push(" )","fi")}if(t)s.push(`if ! id -u ${t} >/dev/null 2>&1; then useradd --create-home --shell /bin/bash ${t}; say "created user ${t}"; fi`,...(e.user?.groups??[]).map((c)=>`if getent group ${c} >/dev/null && ! id -nG ${t} | tr ' ' '\\n' | grep -qx ${c}; then usermod -aG ${c} ${t}; say "added ${t} to ${c}"; fi`),`if [ "$(loginctl show-user ${t} -p Linger --value 2>/dev/null)" != yes ]; then loginctl enable-linger ${t}; say "enabled linger for ${t}"; fi`);s.push(...jo(e));let o=t?`${t}:${t}`:"root:root",a=t?"$SVC_HOME/.config/systemd/user":"/etc/systemd/system";if(r.length>0)s.push("RELOAD=0");for(let[c,l]of r){let u=Fo(c,l,{userUnit:Boolean(t),pantryOnPath:i.length>0});s.push(`CHANGED_${c.replace(/[^A-Za-z0-9_]/g,"_")}=0`);let d=`CHANGED_${c.replace(/[^A-Za-z0-9_]/g,"_")}`;if(l.environment&&Object.keys(l.environment).length>0)s.push(`if write_if_changed "$SVC_HOME/.config/ts-cloud/${c}.env" 600 ${o} ${yt(yr(Mo(l.environment)))}; then ${d}=1; fi`);s.push(`if write_if_changed "${a}/${c}.service" 644 ${o} ${yt(yr(u))}; then ${d}=1; RELOAD=1; fi`)}if(r.length>0)s.push('if [ "$RELOAD" = 1 ]; then svc daemon-reload; fi');for(let[c,l]of r){let u=`CHANGED_${c.replace(/[^A-Za-z0-9_]/g,"_")}`;s.push(`svc enable ${c}.service >/dev/null 2>&1 || svc enable ${c}.service`,`if ! svc is-active --quiet ${c}.service; then svc start ${c}.service; say "started ${c}"`,l.restartOnChange?`elif [ "$${u}" = 1 ]; then svc restart ${c}.service; say "restarted ${c}"`:iu(l)?`elif [ "$${u}" = 1 ]; then svc kill --kill-who=main -s ${Ko(c,l)} ${c}.service; say "changed ${c}; it restarts on the new unit once its current work is done"`:`elif [ "$${u}" = 1 ]; then say "changed ${c}; restart it to apply"`,"fi")}return s.push('say "converged"'),`${s.join(`
|
|
554
556
|
`)}
|
|
555
|
-
`}function Ko(e,t){let r=String(t.drainSignal).replace(/^SIG/i,"").toUpperCase();if(!/^[A-Z0-9]+$/.test(r))throw Error(`Box service "${e}" has an invalid drainSignal`);return`SIG${r}`}function
|
|
557
|
+
`}function Ko(e,t){let r=String(t.drainSignal).replace(/^SIG/i,"").toUpperCase();if(!/^[A-Z0-9]+$/.test(r))throw Error(`Box service "${e}" has an invalid drainSignal`);return`SIG${r}`}function iu(e){return Boolean(e.drainSignal)&&(e.restart??"always")==="always"}function jo(e){let t=e.user?.name;if(!t)return["SVC_HOME=/root",'svc() { systemctl "$@"; }'];return[`SVC_UID=$(id -u ${t})`,`SVC_HOME=$(getent passwd ${t} | cut -d: -f6)`,`svc() { sudo -u ${t} XDG_RUNTIME_DIR="/run/user/$SVC_UID" systemctl --user "$@"; }`,'for _ in $(seq 1 30); do [ -S "/run/user/$SVC_UID/bus" ] && break; sleep 1; done']}function ou(e){if(e.user?.name)bt("user",e.user.name);let t=Object.entries(e.services??{}).filter(([,n])=>n.drainSignal),r=["set -euo pipefail",`say() { echo "${$t} $*"; }`,...jo(e)];for(let[n,s]of t){bt("service",n);let i=Ko(n,s).slice(3),o=s.drainTimeoutSec??3600;r.push(`if svc is-active --quiet ${n}.service; then`,` PID=$(svc show -p MainPID --value ${n}.service)`,` svc kill --kill-who=main -s SIG${i} ${n}.service`,` say "draining ${n} (SIG${i})"`," WAITED=0",' while [ "$PID" != 0 ] && kill -0 "$PID" 2>/dev/null; do',` if [ "$WAITED" -ge ${o} ]; then say "${n} still busy after ${o}s; not powering off"; exit 4; fi`," sleep 5; WAITED=$((WAITED + 5))"," done",` svc stop ${n}.service`,` say "drained ${n} after \${WAITED}s"`,"fi")}return r.push('say "drained"'),`${r.join(`
|
|
556
558
|
`)}
|
|
557
559
|
`}function Go(e,t){if(!e.resize||t.alreadyComplete)return"none";if(t.capacityAvailable||e.resize.attemptUnavailable)return"resize";if(e.resize.whenUnavailable==="keep-current")return"unavailable";throw Error(`${t.targetType.name} is out of stock in ${t.location}; set resize.attemptUnavailable to ask anyway, or whenUnavailable: 'keep-current' to wait`)}function Kn(e){return e.split(`
|
|
558
|
-
`).filter((t)=>t.startsWith($t)).map((t)=>t.slice($t.length).trim())}async function Co(e){let{box:t}=e;if(e.dryRun)return
|
|
560
|
+
`).filter((t)=>t.startsWith($t)).map((t)=>t.slice($t.length).trim())}async function Co(e){let{box:t}=e;if(e.dryRun)return au(e);let r=await new gr(e.client).ensureBox({name:t.name,size:t.size,image:t.image,location:t.location,ports:t.ports,sshPublicKey:e.sshPublicKey,labels:{"ts-cloud/role":"box","ts-cloud/managed-by":"ts-cloud",...t.labels}});await at(r.publicIp,{identityFile:e.identityFile});let n=new ft({user:"root",identityFile:e.identityFile,hostKey:{policy:"pin",knownHostsFile:e.knownHostsFile}}),s=[],i=await n.ensureHostKey(r.publicIp);if(i.pinnedNow)s.push(`pinned host key ${i.fingerprint}`);if(t.resize&&!r.created){let a=await rt(e.client,Number(r.id),t.size,t.resize.upgradeDisk!==!1),c=Go(t,a);if(c==="unavailable")s.push(`${a.targetType.name} is out of stock in ${a.location}; keeping ${a.currentType.name}`);if(c==="resize"){let l=await Tr({client:e.client,serverId:Number(r.id),targetType:a.targetType.name,upgradeDisk:a.upgradeDisk,attemptUnavailable:t.resize.attemptUnavailable,hooks:{preflight:async()=>{s.push(...Kn(await n.exec(r.publicIp,ou(t))))},onPhase:(u)=>e.onPhase?.(u)}});if(l.status==="completed")s.push(`resized ${a.currentType.name} to ${a.targetType.name}`);else if(l.status==="recovered")s.push(`could not resize to ${a.targetType.name} (${l.error}); back on ${a.currentType.name}`);else if(l.status==="waiting-capacity")s.push(`${a.targetType.name} is out of stock in ${a.location}; keeping ${a.currentType.name}`);await at(r.publicIp,{identityFile:e.identityFile})}}let o=await n.exec(r.publicIp,Bo(t));return{box:r,report:[...s,...Kn(o)]}}async function au(e){let{box:t}=e,r=(await e.client.listServers()).find((i)=>i.name===t.name);if(!r)return{box:{provider:"hetzner",id:"",name:t.name,publicIp:"",created:!1},report:[`would create ${t.name} (${t.size})`,"would converge"]};let n={provider:"hetzner",id:String(r.id),name:r.name,publicIp:r.public_net?.ipv4?.ip??"",created:!1},s=[];if(t.resize){let i=await rt(e.client,r.id,t.size,t.resize.upgradeDisk!==!1),o=Go(t,i);if(o==="resize")s.push(`would resize ${i.currentType.name} to ${i.targetType.name}${i.capacityAvailable?"":" (out of stock; asking placement anyway)"}, draining ${Object.entries(t.services??{}).filter(([,a])=>a.drainSignal).map(([a])=>a).join(", ")||"nothing"} first`);else if(o==="unavailable")s.push(`${i.targetType.name} is out of stock in ${i.location}; would keep ${i.currentType.name}`)}else if(r.server_type.name!==t.size)s.push(`runs ${r.server_type.name}, declared ${t.size}; set resize to change it`);return s.push("would converge"),{box:n,report:s}}import{copyFileSync as Au}from"node:fs";import{tmpdir as Lu}from"node:os";import{join as ku}from"node:path";var cu=`${oe}/project`;function Wo(e,t){let r=t.ownerSlug.trim(),n=t.selfSlug?.trim(),s={total:e.length,owner:[],self:[],others:new Map,unmanaged:[]};for(let i of e){let o=i.labels?.[cu]?.trim();if(!o){s.unmanaged.push(i.name);continue}if(o===r){s.owner.push(i.name);continue}if(n&&o===n){s.self.push(i.name);continue}let a=s.others.get(o);if(a)a.push(i.name);else s.others.set(o,[i.name])}return s}function jn(e){let t=e.unmanaged.length;for(let r of e.others.values())t+=r.length;return t}function Vo(e,t){let r=jn(e),n=[];if(n.push(`Attaching to '${t.ownerSlug}' shares one provider project, so this deploy's credential can write to all ${e.total} server(s) it can see.`),r===0)return n.push("Nothing outside the two projects being joined is reachable with it."),n;n.push(`${r} of them belong to neither project:`);for(let s of[...e.others.keys()].sort())n.push(` ${s}: ${[...e.others.get(s)].sort().join(", ")}`);if(e.unmanaged.length>0)n.push(` not managed by ts-cloud: ${[...e.unmanaged].sort().join(", ")}`);return n.push("A compromised CI run or a mistargeted teardown in this project now reaches those. Keep the app in its own provider project instead if that is not acceptable, which rules out attaching."),n}var lu=new Set(["laravel","php","statamic","wordpress"]);function mn(e){if(e.framework)return e.framework;if(e.type&&lu.has(e.type))return"laravel";return"stacks"}var qo=`eval "$(cd ${R} && pantry env 2>/dev/null)"`,uu="/usr/local/bin/bun",Sr={PATH:"/usr/local/bin:/usr/bin:/bin",BUN_INSTALL:"/opt/ts-cloud/bun",APP_ENV:"production",NODE_ENV:"production"};function Gn(e){if(e.backgroundBun===void 0)return uu;return el(e.current,e.backgroundBun)}function pu(e){let t=e.queue??"default",r=[e.sleep,e.tries,e.timeout,e.processes,e.stopWaitSecs];if(typeof t!=="string"||!/^[a-zA-Z0-9_.:,{}/@+-]+$/.test(t||"default")||r.some((n)=>n!==void 0&&(typeof n!=="number"||!Number.isFinite(n))))throw Error("Stacks background queue options require safe queue names and finite numeric values")}var du="node_modules/@stacksjs/ts-cloud/dist/bin/stacks-scheduler.js",mu="node_modules/@stacksjs/ts-cloud/dist/bin/stacks-queue-worker.js",Xo={id:"stacks",wrapExec:(e)=>e,execEnv:Sr,executionEnvironment:(e)=>{let t=Gn(e);return e.backgroundBun===void 0?Sr:{...Sr,PATH:`${t.slice(0,t.lastIndexOf("/"))}:${Sr.PATH}`,TS_CLOUD_BACKGROUND_BUN:e.backgroundBun}},schedulerMode:"daemon",schedulerCommand:(e)=>`${Gn(e)} ${du}`,queueWorkerCommand:(e,t)=>{if(t.backgroundBun!==void 0)pu(e);let{current:r}=t,n=[`--queue=${e.queue||"default"}`,`--sleep=${e.sleep??3}`,`--tries=${e.tries??3}`,`--timeout=${e.timeout??60}`];return`${Gn(t)} ${r}/${mu} ${n.join(" ")}`}},fu={id:"laravel",wrapExec:(e)=>`/bin/sh -lc '${qo}; exec ${e}'`,schedulerMode:"cron",schedulerCommand:({current:e})=>`cd ${e} && ${qo} && php artisan schedule:run`,queueWorkerCommand:(e,{current:t})=>{let r=`${t}/artisan`;if(e.horizon)return`php ${r} horizon`;let n=[e.connection||"default",`--queue=${e.queue||"default"}`,`--sleep=${e.sleep??3}`,`--tries=${e.tries??3}`,`--timeout=${e.timeout??60}`,`--memory=${e.memory??128}`];if(e.maxJobs)n.push(`--max-jobs=${e.maxJobs}`);if(e.maxTime)n.push(`--max-time=${e.maxTime}`);return`php ${r} queue:work ${n.join(" ")}`}},gu={stacks:Xo,laravel:fu};function Yo(e){return gu[e]??Xo}function Zo(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function hu(e){return`'${e.split("'").join("'\\''")}'`}function Qo(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"unnamed"}function ea(e,t){return`for unit in $(ls /etc/systemd/system/ 2>/dev/null | grep -E '^${Zo(e)}-${Zo(t)}-((queue|daemon)-.*|scheduler)\\.service$' || true); do`}function qn(e){return[ea(e.slug,e.siteName),' systemctl stop "$unit"',"done"]}function yu(e,t,r){return`${e}-${t}-queue-${r}`}function Su(e,t,r,n){return`${e}-${t}-daemon-${r.name?Qo(r.name):Qo(r.command).slice(0,32)||String(n)}`}function Wn(e){let t=["[Unit]",`Description=${e.description}`,"After=network.target","","[Service]","Type=simple",`WorkingDirectory=${e.workingDir}`,...Object.entries(e.environment??{}).map(([r,n])=>`Environment="${r}=${n}"`),...e.environmentFile?[`EnvironmentFile=-${e.environmentFile}`]:[],`ExecStart=${e.execStart}`,`Restart=${e.restart||"always"}`,"RestartSec=5","MemoryAccounting=true",`MemoryHigh=${e.memoryHigh??"2G"}`,...e.memoryMax?[`MemoryMax=${e.memoryMax}`]:[]];if(e.isolatedUser){if(!e.isolationRoot||!e.isolationSlug)throw Error("Runtime isolation requires its project directory");t.push(...xt(e.isolatedUser,e.isolationRoot,e.isolationSlug))}else if(e.user)t.push(`User=${e.user}`);if(typeof e.stopWaitSecs==="number")t.push(`TimeoutStopSec=${e.stopWaitSecs}`);return t.push("","[Install]","WantedBy=multi-user.target"),`${t.join(`
|
|
559
561
|
`)}
|
|
560
|
-
`}function Vn(e,t){return[`cat > /etc/systemd/system/${e}.service <<'TS_CLOUD_UNIT_EOF'`,t.replace(/\n$/,""),"TS_CLOUD_UNIT_EOF"]}function
|
|
561
|
-
`.replace(/%/g,"\\%");m.push(`cat > ${D} <<'TS_CLOUD_CRON_EOF'`,H.replace(/\n$/,""),"TS_CLOUD_CRON_EOF",`chmod 644 ${D}`)}else m.push(`rm -f ${D}`);return m}function ta(e){return!!(e.queues?.length||e.daemons?.length||e.scheduler)}function Yn(e){if(e.ssl?.provider)return e.ssl.provider;return e.domain?"letsencrypt":"none"}var ra={cloudflare:{pkg:"python3-certbot-dns-cloudflare",plugin:"dns-cloudflare"},route53:{pkg:"python3-certbot-dns-route53",plugin:"dns-route53"},digitalocean:{pkg:"python3-certbot-dns-digitalocean",plugin:"dns-digitalocean"},google:{pkg:"python3-certbot-dns-google",plugin:"dns-google"}};function na(e){return`/etc/letsencrypt/ts-cloud-${e}.ini`}function
|
|
562
|
-
`);return[`cat > ${t} <<'TS_CLOUD_DNSCREDS_EOF'`,r,"TS_CLOUD_DNSCREDS_EOF",`chmod 600 ${t}`]}function
|
|
563
|
-
`)){let s=n.indexOf(Bt);if(s===-1)continue;let[i,o]=n.slice(s+Bt.length).trim().split(":");if(i)r.set(i,o)}return t.filter((n)=>r.get(n)==="missing")}function fa(e,t,r){let n=r?` on ${r}`:"",s=e.map((o)=>`managedServices.${o}`).join(", "),i=e.length===1?`no ${e[0]}`:`none of ${e.join(", ")}`;return`This project declares ${s}, but '${t}' has ${i}${n}. Attach mode does not provision services on the owner's box, so nothing will install ${e.length===1?"it":"them"}. Point the app at an external service, or ask the owner of '${t}' to add ${e.length===1?"it":"them"} to their own config and re-provision.`}var ha="/var/lib/pantry/php-fpm/pool.d",
|
|
564
|
-
`)}function Sa(e){let t=ya(e.siteName),r=_r(e.siteName),n=`${ha}/${r}.conf`;return[`getent group ${t} >/dev/null || groupadd --system ${t}`,`id -u ${t} >/dev/null 2>&1 || useradd --system --no-create-home --shell /usr/sbin/nologin -g ${t} ${t}`,`usermod -aG ${t} www-data 2>/dev/null || true`,`chown -R ${t}:${t} ${e.appBase} || true`,`chmod -R g+rX ${e.appBase} || true`,`chmod 600 ${e.appBase}/shared/.env 2>/dev/null || true`,`mkdir -p ${ha}`,`cat > ${n} <<'TS_CLOUD_FPMPOOL_EOF'`,
|
|
562
|
+
`}function Vn(e,t){return[`cat > /etc/systemd/system/${e}.service <<'TS_CLOUD_UNIT_EOF'`,t.replace(/\n$/,""),"TS_CLOUD_UNIT_EOF"]}function vu(e,t){return`/etc/cron.d/${e}-${t}-scheduler`}function Xn(e){let t=e.site.isolation&&!["laravel","php","statamic","wordpress"].includes(e.site.type||"")?st(e.slug):void 0,{slug:r,siteName:n,site:s}=e,i=e.appBase??`/var/www/${n}`,o=`${i}/current`,a=Yo(mn(s)),c=a.id==="stacks"&&s.backgroundBun!==void 0;if(c&&(!e.releaseId||!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(e.releaseId)))throw Error("Stacks backgroundBun requires a safe physical releaseId");let l=c?`${i}/releases/${e.releaseId}`:o,u={current:l,backgroundBun:s.backgroundBun},d=a.executionEnvironment?.(u)??a.execEnv,m=[],h=[];(s.queues||[]).forEach((E,M)=>{let H=a.wrapExec(a.queueWorkerCommand(E,u)),F=Math.max(1,E.processes??1);for(let X=0;X<F;X++){let V=yu(r,n,h.length);h.push(V),m.push(...Vn(V,Wn({description:`${n} queue worker ${M}.${X} (managed by ts-cloud)`,workingDir:l,execStart:H,environment:d,isolatedUser:t,isolationRoot:i,isolationSlug:r,stopWaitSecs:E.stopWaitSecs??90})))}}),(s.daemons||[]).forEach((E,M)=>{if(t&&E.user&&E.user!==t)throw Error("An isolated runtime daemon cannot change its project identity");let H=Math.max(1,E.processes??1),F=E.directory||o,X=E.command.trim().replace(/^\.\//,()=>`${F.replace(/\/+$/,"")}/`);for(let V=0;V<H;V++){let Y=`${Su(r,n,E,M)}-${V}`;h.push(Y),m.push(...Vn(Y,Wn({description:`${n} daemon ${E.name||E.command} (managed by ts-cloud)`,workingDir:F,execStart:a.wrapExec(X),environment:a.execEnv,environmentFile:`${o}/.env`,restart:E.restart,user:E.user,isolatedUser:t,isolationRoot:i,isolationSlug:r})))}});let x=s.scheduler,B=x===!0||typeof x==="object"&&x!==null,L=`${r}-${n}-scheduler`;if(B&&a.schedulerMode==="daemon"){h.push(L);let E=typeof x==="object"&&x!==null?x:{};m.push(...Vn(L,Wn({description:`${n} scheduler (managed by ts-cloud)`,workingDir:l,execStart:a.wrapExec(a.schedulerCommand(u)),environment:d,isolatedUser:t,isolationRoot:i,isolationSlug:r,memoryHigh:E.memoryHigh,memoryMax:E.memoryMax})),...tr(`${L}.service`,{auto:!1,high:Boolean(E.memoryHigh),max:Boolean(E.memoryMax)}))}let N=h.map((E)=>`${E}.service`).join(" ");m.push("systemctl daemon-reload",`TS_CLOUD_DESIRED="${N}"`,ea(r,n),' case " $TS_CLOUD_DESIRED " in',' *" $unit "*) ;;',' *) systemctl stop "$unit"; systemctl disable "$unit"; rm -f "/etc/systemd/system/$unit" ;;'," esac","done","systemctl daemon-reload");for(let E of h)m.push(`systemctl enable ${E}.service`,`systemctl restart ${E}.service`);let D=vu(r,n);if(B&&a.schedulerMode==="cron"){let E=typeof x==="object"&&x!==null?x:void 0,M=`${a.schedulerCommand(u)} >> /dev/null 2>&1`;if(E?.heartbeatUrl){let F=E.heartbeatMethod||"GET",X=F==="GET"?"":`-X ${F} `;M+=` && curl -fsS -m 10 ${X}${hu(E.heartbeatUrl)} >/dev/null 2>&1`}let H=`* * * * * root ${M}
|
|
563
|
+
`.replace(/%/g,"\\%");m.push(`cat > ${D} <<'TS_CLOUD_CRON_EOF'`,H.replace(/\n$/,""),"TS_CLOUD_CRON_EOF",`chmod 644 ${D}`)}else m.push(`rm -f ${D}`);return m}function ta(e){return!!(e.queues?.length||e.daemons?.length||e.scheduler)}function Yn(e){if(e.ssl?.provider)return e.ssl.provider;return e.domain?"letsencrypt":"none"}var ra={cloudflare:{pkg:"python3-certbot-dns-cloudflare",plugin:"dns-cloudflare"},route53:{pkg:"python3-certbot-dns-route53",plugin:"dns-route53"},digitalocean:{pkg:"python3-certbot-dns-digitalocean",plugin:"dns-digitalocean"},google:{pkg:"python3-certbot-dns-google",plugin:"dns-google"}};function na(e){return`/etc/letsencrypt/ts-cloud-${e}.ini`}function bu(e){let t=e?ra[e.provider]:void 0,r=["certbot","python3-certbot-nginx",...t?[t.pkg]:[]].join(" ");return["export DEBIAN_FRONTEND=noninteractive",t?`apt-get update -y && apt-get install -y ${r}`:`command -v certbot >/dev/null 2>&1 || { apt-get update -y && apt-get install -y ${r}; }`,"mkdir -p /etc/letsencrypt/renewal-hooks/deploy","cat > /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh <<'TS_CLOUD_HOOK_EOF'","#!/bin/sh","systemctl reload ts-cloud-nginx 2>/dev/null || systemctl reload nginx 2>/dev/null || true","TS_CLOUD_HOOK_EOF","chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh","systemctl enable certbot.timer 2>/dev/null || true","systemctl start certbot.timer 2>/dev/null || true"]}function wu(e){if(!e.credentials||Object.keys(e.credentials).length===0)return[];let t=na(e.provider),r=Object.entries(e.credentials).map(([n,s])=>`${n} = ${s}`).join(`
|
|
564
|
+
`);return[`cat > ${t} <<'TS_CLOUD_DNSCREDS_EOF'`,r,"TS_CLOUD_DNSCREDS_EOF",`chmod 600 ${t}`]}function $u(e){let t=e.dns,r=e.wildcard?[`*.${e.domain}`,e.domain]:[e.domain,...e.aliases||[]].filter(Boolean),n=["certbot","--non-interactive","--agree-tos","--keep-until-expiring"];if(t){let s=ra[t.provider].plugin;if(n.push(`--${s}`),t.provider!=="route53"&&t.credentials)n.push(`--${s}-credentials ${na(t.provider)}`);if(typeof t.propagationSeconds==="number"&&t.provider!=="route53")n.push(`--${s}-propagation-seconds ${t.propagationSeconds}`);n.push("certonly")}else n.push("--nginx",`--nginx-ctl ${xe}`,e.redirect===!1?"--no-redirect":"--redirect");if(e.email)n.push(`-m ${e.email}`);else n.push("--register-unsafely-without-email");for(let s of r)n.push(`-d ${s}`);return[n.join(" ")]}function Jn(e){if(Yn(e)!=="letsencrypt"||!e.domain)return[];let t=e.ssl,r=t?.dns,n=t?.wildcard===!0;if(n&&!r)return[];return[...bu(r),...r?wu(r):[],...$u({domain:e.domain,aliases:e.aliases,email:t?.email,wildcard:n,dns:r})]}function sa(e){let{repository:t,releaseDir:r,commit:n}=e,s=t.branch||"main",i=t.url,o=[`rm -rf ${r}`,`mkdir -p ${r}`];if(t.strategy==="tag")if(t.tag)o.push(`git clone -q --depth 1 --branch ${be(t.tag)} ${be(i)} ${r}`),o.push(`printf '%s' ${be(t.tag)} > ${r}/.ts-cloud-tag`);else{let a=t.tagPattern||"v*";o.push(`TS_CLOUD_TAG="$(git ls-remote --tags --refs --sort=-v:refname ${be(i)} ${be(`refs/tags/${a}`)} | head -n1 | sed 's#.*refs/tags/##')"`,`test -n "$TS_CLOUD_TAG" || { echo "no tags matching" ${be(a)} "found in" ${be(i)} >&2; exit 1; }`,`git clone -q --depth 1 --branch "$TS_CLOUD_TAG" ${be(i)} ${r}`,`printf '%s' "$TS_CLOUD_TAG" > ${r}/.ts-cloud-tag`)}else if(n)o.push(`git -C ${r} init -q`,`git -C ${r} remote add origin ${be(i)}`,`git -C ${r} fetch -q --depth 1 origin ${be(n)}`,`git -C ${r} checkout -q FETCH_HEAD`);else o.push(`git clone -q --depth 1 --branch ${be(s)} ${be(i)} ${r}`);return o.push(`git -C ${r} rev-parse HEAD > ${r}/.ts-cloud-sha`),o}function be(e){return`'${e.split("'").join("'\\''")}'`}var vr="$CREATE_RELEASE",br="$ACTIVATE_RELEASE",aa="$RESTART_QUEUES",ia="composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev",_u=["php artisan migrate --force","php artisan config:cache","php artisan route:cache","php artisan view:cache","php artisan event:cache","php artisan storage:link"];function Tu(e){switch(e){case"laravel":case"statamic":return[vr,ia,..._u,br,aa];case"php":case"wordpress":return[vr,`${ia} || true`,br];case"static":case"spa":return[vr,br]}}function Cu(e,t){return e.replace(/^php\s+/,`${t} `).replace(/(\s)php\s+artisan\s+/g,`$1${t} artisan `)}function Pu(e,t){let r=Jt(t);return[`cat > ${e} <<'TS_CLOUD_ENV_EOF'`,r,"TS_CLOUD_ENV_EOF",`chmod 600 ${e}`]}function oa(e,t,r){return[`cat > ${e} <<'${r}'`,t.replace(/\n$/,""),r,`chmod 600 ${e}`]}function Ru(e,t){if(!t)return[];let r=[];if(t.composerAuth){let n=typeof t.composerAuth==="string"?t.composerAuth:JSON.stringify(t.composerAuth,null,2);r.push(...oa(`${e}/auth.json`,n,"TS_CLOUD_AUTHJSON_EOF"))}if(t.npmrc)r.push(...oa(`${e}/.npmrc`,t.npmrc,"TS_CLOUD_NPMRC_EOF"));return r}function ca(e){let t=e.healthCheck?.path;if(!t||!e.domain)return[];let n=`http://127.0.0.1${t.startsWith("/")?t:`/${t}`}`;return[`echo "[ts-cloud] health check ${n} (Host: ${e.domain})"`,"TS_CLOUD_HC_OK=0",`for i in $(seq 1 10); do TS_CLOUD_HC=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: ${e.domain}" -m 10 "${n}" 2>/dev/null || echo 000); case "$TS_CLOUD_HC" in 2*|3*) TS_CLOUD_HC_OK=1; break ;; esac; sleep 3; done`,'[ "$TS_CLOUD_HC_OK" = 1 ] && echo "health check passed ($TS_CLOUD_HC)" || { echo "health check FAILED (last=$TS_CLOUD_HC)" >&2; exit 1; }']}function la(e){let{siteName:t,site:r,releaseId:n,commit:s}=e;if(!r.repository?.url)throw Error(`Site '${t}' is a PHP/git site but has no repository.url to clone`);let i=e.appBase??`/var/www/${t}`,o="php",a=Ht(i,n),c=r.sharedPaths??No,l=[...c,...Zt(r.env,c)],u=r.keepReleases??_i,d=r.deployScript?.length?r.deployScript:Tu(r.type??"laravel"),m=["set -euo pipefail",'export HOME="${HOME:-/root}"','export COMPOSER_HOME="${COMPOSER_HOME:-/root/.composer}"',"export COMPOSER_ALLOW_SUPERUSER=1",Ve()];if(m.push(...Oo(i,{releaseId:n,commit:s,branch:r.repository.branch,keepLogs:u})),m.push(...$i(a,l)),r.env&&Object.keys(r.env).length>0)m.push(...Pu(`${a.shared}/.env`,r.env));for(let h of d){let f=h.trim();if(f===vr)m.push(...sa({repository:r.repository,releaseDir:a.release,commit:s})),m.push(..._o(a,l)),m.push(`cd ${a.release}`),m.push(...Ru(a.release,r.credentials)),m.push(`chown -R www-data:www-data ${a.shared}/storage 2>/dev/null || true`,`[ -d ${a.release}/bootstrap/cache ] && chown -R www-data:www-data ${a.release}/bootstrap/cache 2>/dev/null || true`,`chmod -R ug+rwX ${a.shared}/storage 2>/dev/null || true`);else if(f===br)m.push(...hn(a)),m.push(...yn(a,u)),m.push(`(cd ${R} && pantry restart php-fpm) 2>/dev/null || true`);else if(f===aa)m.push(`${o} artisan queue:restart || true`);else m.push(Cu(h,o))}return m}var $r={mysql:{port:3306,binaries:["mysqld","mysql"]},mariadb:{port:3306,binaries:["mariadbd","mariadb","mysqld"]},postgres:{port:5432,binaries:["postgres","psql"]},redis:{port:6379,binaries:["redis-server","redis-cli"]},memcached:{port:11211,binaries:["memcached"]},meilisearch:{port:7700,binaries:["meilisearch"]},vitess:{port:15306,binaries:["vtgate"]}},Bt="ts-cloud-service:";function Eu(e){return e===!0||typeof e==="object"&&e!=null}function ua(e){if(!e)return[];return Object.keys($r).filter((t)=>Eu(e[t]))}function da(e){if(e.length===0)return[];return[Ve(),"ts_cloud_port_open() {",' if command -v ss >/dev/null 2>&1; then ss -ltn 2>/dev/null | grep -qE "[:.]$1[[:space:]]" && return 0; fi',' if command -v netstat >/dev/null 2>&1; then netstat -ltn 2>/dev/null | grep -qE "[:.]$1[[:space:]]" && return 0; fi'," (exec 3<>/dev/tcp/127.0.0.1/$1) 2>/dev/null && { exec 3<&- 3>&-; return 0; }"," return 1","}","ts_cloud_probe() {",' TS_CLOUD_SVC="$1"; TS_CLOUD_PORT="$2"; shift 2',' for TS_CLOUD_BIN in "$@"; do',` if command -v "$TS_CLOUD_BIN" >/dev/null 2>&1; then echo "${Bt}$TS_CLOUD_SVC:present"; return 0; fi`," done",` if ts_cloud_port_open "$TS_CLOUD_PORT"; then echo "${Bt}$TS_CLOUD_SVC:present"; return 0; fi`,` echo "${Bt}$TS_CLOUD_SVC:missing"`," return 0","}",...e.filter((t)=>$r[t]).map((t)=>`ts_cloud_probe ${t} ${$r[t].port} ${$r[t].binaries.join(" ")}`),"exit 0"]}function ma(e,t){if(!e)return[];let r=new Map;for(let n of e.split(`
|
|
565
|
+
`)){let s=n.indexOf(Bt);if(s===-1)continue;let[i,o]=n.slice(s+Bt.length).trim().split(":");if(i)r.set(i,o)}return t.filter((n)=>r.get(n)==="missing")}function fa(e,t,r){let n=r?` on ${r}`:"",s=e.map((o)=>`managedServices.${o}`).join(", "),i=e.length===1?`no ${e[0]}`:`none of ${e.join(", ")}`;return`This project declares ${s}, but '${t}' has ${i}${n}. Attach mode does not provision services on the owner's box, so nothing will install ${e.length===1?"it":"them"}. Point the app at an external service, or ask the owner of '${t}' to add ${e.length===1?"it":"them"} to their own config and re-provision.`}var ha="/var/lib/pantry/php-fpm/pool.d",xu=9100,Ou=400;function _r(e){return(e.toLowerCase().replace(/[^a-z0-9]+/g,"_").replace(/^_+|_+$/g,"")||"site").slice(0,28)}function ya(e){return`web_${_r(e)}`}function Du(e){let t=_r(e),r=0;for(let n=0;n<t.length;n++)r=r*31+t.charCodeAt(n)>>>0;return xu+r%Ou}function Zn(e){return`127.0.0.1:${Du(e)}`}function Iu(e){let t=_r(e.siteName),r=ya(e.siteName),n=e.maxChildren??10;return[`; ts-cloud-managed pool for ${e.siteName} (site isolation)`,`[${t}]`,`user = ${r}`,`group = ${r}`,`listen = ${Zn(e.siteName)}`,"pm = dynamic",`pm.max_children = ${n}`,"pm.start_servers = 2","pm.min_spare_servers = 1","pm.max_spare_servers = 3","pm.max_requests = 500","catch_workers_output = yes",`php_admin_value[open_basedir] = ${e.appBase}:/tmp`,""].join(`
|
|
566
|
+
`)}function Sa(e){let t=ya(e.siteName),r=_r(e.siteName),n=`${ha}/${r}.conf`;return[`getent group ${t} >/dev/null || groupadd --system ${t}`,`id -u ${t} >/dev/null 2>&1 || useradd --system --no-create-home --shell /usr/sbin/nologin -g ${t} ${t}`,`usermod -aG ${t} www-data 2>/dev/null || true`,`chown -R ${t}:${t} ${e.appBase} || true`,`chmod -R g+rX ${e.appBase} || true`,`chmod 600 ${e.appBase}/shared/.env 2>/dev/null || true`,`mkdir -p ${ha}`,`cat > ${n} <<'TS_CLOUD_FPMPOOL_EOF'`,Iu(e).replace(/\n$/,""),"TS_CLOUD_FPMPOOL_EOF",`(cd ${R} && pantry restart php-fpm) 2>/dev/null || true`]}var Or={info:()=>{},warn:()=>{},error:()=>{},step:()=>{},success:()=>{}};function Nu(e){return`'${e.replace(/'/g,`'"'"'`)}'`}async function ba(e,t,r=Or){let{config:n,environment:s,siteName:i,site:o,slug:a,sha:c,runtime:l,localTarballPath:u}=t,d=`releases/${i}/${c}.tar.gz`,m=T(n,s),h=await e.getComputeOutputs({config:n,environment:s}),f=await e.findComputeTargets({slug:a,environment:s,role:"app",stackName:m});if(f.length===0)return{success:!1,error:e.name==="aws"?`Stack '${m}' has no EC2 instances tagged Project=${a} Environment=${s} Role=app, and storage/cloud/state/${m}.json pins no live instance. For a shared box, record its instanceId there.`:`No Hetzner servers labeled ts-cloud/project=${a} ts-cloud/environment=${s} ts-cloud/role=app, and storage/cloud/state/${m}.json pins no live server. For a shared box, record its serverId there.`};let S=h.servicesPrivateIp?di(h.servicesPrivateIp,A(n)):Kc(A(n)),x=ga(ne(n,{environment:s})),B=n.infrastructure?.compute,L=tn(B?.hostCleanup),N=ee({...B,proxy:le(n,{environment:s})})?{HOST:"127.0.0.1"}:{},D=s==="production"?{APP_ENV:"production",NODE_ENV:"production"}:{},E={...S,...x,...N,...o.env||{},...D},M=Jr(E,o.sharedPaths??[]);if(M.warning)r.warn(`Site '${i}': ${M.warning}`);else if(M.path)r.info(`Site '${i}': sharing '${M.path}' so the SQLite database survives deploys.`);if(ki(o)){let b=o.phpVersion??B?.php?.default??B?.php?.versions?.[0],C=W(a,i),P=Object.keys(S).length>0?{...o,env:E}:o,G=la({siteName:i,site:P,releaseId:c,appBase:C,defaultPhpVersion:b}),K=!ee(B),q=Yn(o)==="custom"&&o.ssl?.certPath&&o.ssl?.keyPath?{certPath:o.ssl.certPath,keyPath:o.ssl.keyPath}:void 0,se=o.isolation?Sa({siteName:i,appBase:C}):[],_=K?Nr({siteName:i,domain:o.domain||i,aliases:o.aliases,type:o.type,appDir:`${C}/current`,webDirectory:o.webDirectory,phpVersion:b,fastcgiPass:o.isolation?Zn(i):void 0,redirects:o.redirects,ssl:q,auth:o.auth&&o.auth.enabled!==!1&&o.auth.password?{username:o.auth.username||"admin",password:o.auth.password,realm:o.auth.realm}:void 0,serverSnippet:kr(o.nginx,B?.nginxTemplates),clientMaxBodySize:o.nginx?.clientMaxBodySize,hsts:o.ssl?.hsts,tlsProtocols:o.ssl?.tlsProtocols,security:o.security}):[],z=K?Jn(o):[],ue=ta(o)?Xn({slug:a,siteName:i,site:o,phpVersion:b,appBase:C}):[],re=K?ca(o):[];r.step(`Deploying PHP site '${i}' to ${f.length} target(s)...`);let fe=await e.runRemoteDeploy({targets:f,commands:[...Ks(C,a),...Yt(C,a),...G,...se,..._,...z,...ue,...re,...L],comment:`ts-cloud deploy ${a}/${i}@${c}`,tags:{Project:a,Environment:s,Role:"app"}}),_t=ls(n.notifications,o.notifications);if(!fe.success)return await va(e,f,L,r),await Ur(_t,"deploy-failed",`❌ Deploy of ${a}/${i}@${c} failed: ${fe.error||"unknown error"}`),{success:!1,error:fe.error||"Remote PHP deploy failed",instanceCount:fe.instanceCount,perInstance:fe.perInstance};let Aa=o.domain?` → https://${o.domain}`:"";return await Ur(_t,"deploy",`✅ Deployed ${a}/${i}@${c}${Aa}`),{success:!0,instanceCount:fe.instanceCount,perInstance:fe.perInstance}}if(!u)return{success:!1,error:`Site '${i}' requires a release tarball but none was provided`};let H=await e.uploadRelease({config:n,environment:s,localPath:u,remoteKey:d,targets:f}),F=It(a,i,c),X=e.name==="aws"?fn(h.deployBucketName,d,n.project.region||"us-east-1",F):gn(H.artifactRef,F),V=w(o),Y=W(a,i),te=Xn({slug:a,siteName:i,site:o,appBase:Y,releaseId:c}),Z=V==="server-static"?an({siteName:i,slug:a,appDir:Y,artifactFetch:X,releaseId:c,preStartCommands:o.preStart,postActivateCommands:te,beforeResetCommands:qn({slug:a,siteName:i})}):on({siteName:i,slug:a,appDir:Y,artifactFetch:X,releaseId:c,execStart:sn(o.start,l,`${Y.replace(/\/+$/,"")}/releases/%i`),envEntries:E,isolation:o.isolation,port:o.port,preStartCommands:o.preStart,postActivateCommands:te,beforeResetCommands:qn({slug:a,siteName:i}),sharedPaths:o.sharedPaths,zeroDowntime:o.zeroDowntime!==!1,healthCheckPath:o.healthCheck?.path,liveness:o.liveness,memoryHigh:o.memoryHigh,memoryMax:o.memoryMax,cpuWeight:o.cpuWeight,ioWeight:o.ioWeight,tasksMax:o.tasksMax,stopTimeout:o.stopTimeout}),J=V==="server-static"&&!ee(B)&&!!o.domain,ie=J?Nr({siteName:i,domain:o.domain,aliases:o.aliases,type:o.type==="spa"?"spa":"static",appDir:`${Y}/current`,webDirectory:"",redirects:o.redirects,auth:o.auth&&o.auth.enabled!==!1&&o.auth.password?{username:o.auth.username||"admin",password:o.auth.password,realm:o.auth.realm}:void 0,serverSnippet:kr(o.nginx,B?.nginxTemplates),clientMaxBodySize:o.nginx?.clientMaxBodySize,hsts:o.ssl?.hsts,tlsProtocols:o.ssl?.tlsProtocols,security:o.security}):[],ce=J?Jn(o):[],y=[...Oo(Y,{releaseId:c,commit:c,branch:o.repository?.branch??"main"}),...Ks(Y,a),...Yt(Y,a),...Z,...ie,...ce,...L];r.step(`Deploying to ${f.length} target(s)...`);let k=await e.runRemoteDeploy({targets:f,commands:y,comment:`ts-cloud deploy ${a}/${i}@${c}`,tags:{Project:a,Environment:s,Role:"app"}});if(!k.success)return await va(e,f,L,r),{success:!1,error:k.error||"Remote deploy failed",instanceCount:k.instanceCount,perInstance:k.perInstance};return{success:!0,instanceCount:k.instanceCount,perInstance:k.perInstance}}async function va(e,t,r,n){if(r.length===0)return;try{let s=await e.runRemoteDeploy({targets:t,commands:r,comment:"ts-cloud host cleanup after failed deploy"});if(!s.success)n.warn(`Host cleanup after the failed deploy did not complete: ${s.error||"unknown error"}`)}catch(s){n.warn(`Host cleanup after the failed deploy did not run: ${s.message}`)}}function xr(e){return`'${e.replaceAll("'","'\\''")}'`}function Hu(e,t,r=!1,n=[]){let s=`${e}-dashboard-`,i=t.map((a)=>`${e}-${a}.service`),o=r?[`export TS_CLOUD_DASHBOARD_KEEP_DOMAINS=${xr(JSON.stringify(n))}`,"rm -f /run/ts-cloud-dashboard-routes-changed",`/usr/local/bin/bun --bun -e ${xr(`
|
|
565
567
|
const { chmodSync, existsSync, readdirSync, readFileSync, renameSync, statSync, writeFileSync } = require('node:fs')
|
|
566
568
|
const { basename, join } = require('node:path')
|
|
567
569
|
const root = '/etc/rpx/sites.d'
|
|
@@ -589,15 +591,15 @@ console.log(JSON.stringify({
|
|
|
589
591
|
changed = true
|
|
590
592
|
}
|
|
591
593
|
if (changed) writeFileSync('/run/ts-cloud-dashboard-routes-changed', '')
|
|
592
|
-
`)}`,"if [ -e /run/ts-cloud-dashboard-routes-changed ]; then"," systemctl restart rpx-gateway.service"," rm -f /run/ts-cloud-dashboard-routes-changed","fi"]:[];return["set -euo pipefail",`TS_CLOUD_DASHBOARD_PREFIX=${xr(s)}`,`TS_CLOUD_DASHBOARD_SERVER_OWNER=${r?"1":"0"}`,`TS_CLOUD_DASHBOARD_DESIRED=${xr(` ${i.join(" ")} `)}`,"for TS_CLOUD_UNIT_FILE in /etc/systemd/system/*.service; do",' [ -e "$TS_CLOUD_UNIT_FILE" ] || continue',' TS_CLOUD_UNIT=$(basename "$TS_CLOUD_UNIT_FILE")',' case "$TS_CLOUD_UNIT" in'," *-dashboard-*.service)",' if [ "$TS_CLOUD_DASHBOARD_SERVER_OWNER" != 1 ]; then',' case "$TS_CLOUD_UNIT" in "$TS_CLOUD_DASHBOARD_PREFIX"*.service) ;; *) continue ;; esac'," fi",' case "$TS_CLOUD_DASHBOARD_DESIRED" in',' *" $TS_CLOUD_UNIT "*) ;;'," *)",' systemctl disable --now "$TS_CLOUD_UNIT" 2>/dev/null || true',' rm -f "$TS_CLOUD_UNIT_FILE"'," ;;"," esac"," ;;"," esac","done","systemctl daemon-reload","systemctl reset-failed",...o]}async function
|
|
594
|
+
`)}`,"if [ -e /run/ts-cloud-dashboard-routes-changed ]; then"," systemctl restart rpx-gateway.service"," rm -f /run/ts-cloud-dashboard-routes-changed","fi"]:[];return["set -euo pipefail",`TS_CLOUD_DASHBOARD_PREFIX=${xr(s)}`,`TS_CLOUD_DASHBOARD_SERVER_OWNER=${r?"1":"0"}`,`TS_CLOUD_DASHBOARD_DESIRED=${xr(` ${i.join(" ")} `)}`,"for TS_CLOUD_UNIT_FILE in /etc/systemd/system/*.service; do",' [ -e "$TS_CLOUD_UNIT_FILE" ] || continue',' TS_CLOUD_UNIT=$(basename "$TS_CLOUD_UNIT_FILE")',' case "$TS_CLOUD_UNIT" in'," *-dashboard-*.service)",' if [ "$TS_CLOUD_DASHBOARD_SERVER_OWNER" != 1 ]; then',' case "$TS_CLOUD_UNIT" in "$TS_CLOUD_DASHBOARD_PREFIX"*.service) ;; *) continue ;; esac'," fi",' case "$TS_CLOUD_DASHBOARD_DESIRED" in',' *" $TS_CLOUD_UNIT "*) ;;'," *)",' systemctl disable --now "$TS_CLOUD_UNIT" 2>/dev/null || true',' rm -f "$TS_CLOUD_UNIT_FILE"'," ;;"," esac"," ;;"," esac","done","systemctl daemon-reload","systemctl reset-failed",...o]}async function Uu(e,t,r,n){let{config:s,environment:i}=t,o=s.project.slug,a=T(s,i),c=await e.findComputeTargets({slug:o,environment:i,role:"app",stackName:a});if(c.length===0)return!0;let l=await e.runRemoteDeploy({targets:c,commands:Hu(o,r,!s.cloud?.attachTo,r.map((u)=>s.sites?.[u]?.domain).filter((u)=>typeof u==="string")),comment:`ts-cloud reconcile management dashboard ${o}`,tags:{Project:o,Environment:i,Role:"app"}});if(!l.success)return n.error(`Management dashboard reconciliation failed: ${l.error||"unknown error"}`),!1;return!0}async function Mu(e,t,r){let{config:n}=t,s=n.cloud?.attachTo;if(!s||!e.listReachableResources)return;let i;try{i=await e.listReachableResources()}catch(c){r.warn(`Could not determine this credential's reach: ${c instanceof Error?c.message:String(c)}`);return}let o=Wo(i,{ownerSlug:s,selfSlug:n.project.slug}),a=jn(o)>0;for(let c of Vo(o,{ownerSlug:s,selfSlug:n.project.slug}))if(a)r.warn(c);else r.info(c)}async function zu(e,t,r){let{config:n,environment:s}=t,i=n.cloud?.attachTo;if(!i)return!0;let o=ua(n.infrastructure?.compute?.managedServices);if(o.length===0)return!0;let a=n.project.slug,c=T(n,s),l=await e.findComputeTargets({slug:a,environment:s,role:"app",stackName:c});if(l.length===0)return!0;let u=await e.runRemoteDeploy({targets:l,commands:da(o),comment:`ts-cloud preflight managed services ${a}`,tags:{Project:a,Environment:s,Role:"app"}});if(!u.success)return r.warn(`Could not check '${i}' for the declared services: ${u.error||"unknown error"}`),!0;let d=new Set;for(let m of u.perInstance)for(let h of ma(m.output,o))d.add(h);if(d.size===0)return!0;return r.error(fa([...d],i,l[0]?.publicIp)),!1}async function Fu(e,t,r){let{config:n,environment:s}=t,i=n.infrastructure?.compute,o=A(n);if(n.cloud?.attachTo&&o?.requireLocalPasswordAuth)throw Error("PostgreSQL host authentication can only be managed by an owned compute installation");if(!n.cloud?.attachTo&&!o?.requireLocalPasswordAuth)return!0;let a=wt(o,i?.managedServices??{});if(a.length===0)return!0;let c=n.project.slug,l=T(n,s),u=await e.findComputeTargets({slug:c,environment:s,role:"app",stackName:l});if(u.length===0)return!0;r.step(`Ensuring database '${o?.name}' + role exist on the shared box...`);let d=await e.runRemoteDeploy({targets:u,commands:a,comment:`ts-cloud ensure database ${c}/${o?.name}`,tags:{Project:c,Environment:s,Role:"app"}});if(!d.success)return r.error(`Ensuring database '${o?.name}' failed: ${d.error||"unknown error"}`),!1;return r.success(`Database '${o?.name}' is ready on ${d.instanceCount} target(s)`),!0}async function To(e){let{config:t,environment:r,driver:n,sha:s,runtime:i,tarballForSite:o,logger:a=Or}=e,c=t.project.slug,l=e.cwd??process.cwd(),u=e.managementDashboard!==!1,d=er(t);if(u)zo(t,{cwd:l,logger:{info:a.info,warn:a.warn}});let m=!u||d?[]:Ho(t);if(u&&e.rpxConfig&&e.rpxConfig!==t&&!er(e.rpxConfig))zo(e.rpxConfig,{cwd:l,logger:{info:()=>{},warn:()=>{}}});if(u&&!await Uu(n,e,Ho(t),a))return!1;let h=null,f=new Map;if(m.length>0)if(h=Ys(t.sites?.[m[0]],{cwd:l,slug:c,sha:s,siteName:m[0],logger:{info:a.info,warn:a.warn}}),!h){a.warn("Management dashboard: no artifact available — skipping dashboard site(s) for this deploy.");for(let N of m)if(t.sites)delete t.sites[N]}else for(let N of m){if(N===m[0]){f.set(N,h);continue}try{let D=ku(Lu(),`${c}-${N}-${s}.tar.gz`);Au(h,D),f.set(N,D)}catch{f.set(N,h)}}let S=t.sites||{},x=Object.entries(S).filter(([N,D])=>{if(!D)return!1;let E=w(D);if(E==="bucket")return a.warn(`Site '${N}' targets a bucket — skipping (handled by the static-site path, not compute).`),!1;if(E==="redirect")return!1;if(E==="proxy")return!1;return!0});if(x.length===0)return Qn(e);if(await Mu(n,e,a),!await zu(n,e,a))return!1;if(!await Fu(n,e,a))return!1;let B=(N)=>f.get(N)??o(N);for(let[N,D]of x){a.step(`Deploying site: ${N}`);let E=ki(D)?void 0:B(N),M=await ba(n,{config:t,environment:r,siteName:N,site:D,slug:c,sha:s,runtime:i,localTarballPath:E},a);if(!M.success){if(a.error(`Deploy of '${N}' failed: ${M.error||"unknown error"}`),M.perInstance)for(let H of M.perInstance)a.error(` ${H.instanceId}: ${H.status}${H.error?` — ${H.error}`:""}`);return!1}if(a.success(`Deployed ${c}/${N}@${s} to ${M.instanceCount} target(s)`),M.perInstance)for(let H of M.perInstance)a.info(` ✓ ${H.instanceId}: ${H.status}`)}if(!await Qn(e))return!1;return!0}async function Bu(e,t,r){let n=ho(e);if(!n||n.tls==="off")return n;let s=await Ge(t,r),i=s?.provider==="ssh"?s.lanIp:void 0;return i?{...n,ip:i}:n}async function Qn(e){let{config:t,environment:r,driver:n,logger:s=Or}=e,i=e.rpxConfig??t,o=le(i,{environment:r})??le(t,{environment:r});if(o?.engine!=="rpx")return!0;let a=i.sites||{},c=t.project.slug,l=T(t,r),u=await n.findComputeTargets({slug:c,environment:r,role:"lb",stackName:l});if(u.length>0){let L=(await n.findComputeTargets({slug:c,environment:r,role:"app",stackName:l})).map((E)=>({privateIp:E.privateIp,publicIp:E.publicIp})),N=Je(a,L,{proxy:o,slug:c});if(N.proxies.length===0)return s.warn("rpx gateway: no server sites with a domain to route — skipping gateway reload."),!0;s.step(`Reloading the load balancer's rpx gateway with ${N.proxies.length} route(s)...`);let D=await n.runRemoteDeploy({targets:u,commands:et({config:N,slug:c,preserveManagementDashboardRoutes:e.managementDashboard===!1}),comment:`ts-cloud rpx gateway reload ${c}`,tags:{Project:c,Environment:r,Role:"lb"}});if(!D.success)return s.error(`rpx gateway reload failed: ${D.error||"unknown error"}`),!1;return s.success(`rpx gateway reloaded on ${D.instanceCount} load balancer(s)`),!0}let d=await Bu(t,l,e.cwd),m=Te(a,{proxy:o,slug:c,lan:d,mail:ne(t,{environment:r})});if(m.proxies.length===0)return s.warn("rpx gateway: no server sites with a domain to route — skipping gateway reload."),!0;let h=await n.findComputeTargets({slug:c,environment:r,role:"app",stackName:l});if(h.length===0)return s.warn("rpx gateway: no compute targets found — skipping gateway reload."),!0;let f=Boolean(t.cloud?.attachTo);if(f&&(o.version||o.tlsxVersion))s.warn(`rpx gateway: ignoring proxy.version/tlsxVersion — this project attaches to '${t.cloud?.attachTo}' and the gateway belongs to that box's owner.`);s.step(`Reloading rpx gateway with ${m.proxies.length} route(s)...`);let S=$e({proxy:o,config:m,slug:c,tenant:f,profile:ms(t),preserveManagementDashboardRoutes:e.managementDashboard===!1}),x=await n.runRemoteDeploy({targets:h,commands:S,comment:`ts-cloud rpx gateway reload ${c}`,tags:{Project:c,Environment:r,Role:"app"}});if(!x.success)return s.error(`rpx gateway reload failed: ${x.error||"unknown error"}`),!1;return s.success(`rpx gateway reloaded on ${x.instanceCount} target(s)`),!0}async function Eo(e){let{config:t,environment:r,driver:n,logger:s=Or}=e,i=e.rpxConfig??t,o=le(i,{environment:r})??le(t,{environment:r});if(o?.engine!=="rpx"||!o.onDemandTls)return!0;let a=Te(i.sites||{},{proxy:o,slug:t.project.slug,mail:ne(t,{environment:r})});if(a.proxies.length===0)return!0;let c=Gr(a);if(c.length===0)return!0;let l=t.project.slug,u=T(t,r),d="lb",m=await n.findComputeTargets({slug:l,environment:r,role:d,stackName:u});if(m.length===0)d="app",m=await n.findComputeTargets({slug:l,environment:r,role:d,stackName:u});if(m.length===0)return s.warn("rpx TLS: no gateway targets found - skipping certificate issuance."),!0;s.step("Issuing rpx TLS certificates after DNS reconciliation...");let h=c.map((S)=>`test -s ${Nu(`${a.productionCerts.certsDir}/${S}.crt`)}`).join(" && "),f=await n.runRemoteDeploy({targets:m,commands:[`systemctl start ${Kr(l)}`,h],comment:`ts-cloud rpx TLS issuance ${l}`,tags:{Project:l,Environment:r,Role:d}});if(!f.success)return s.error(`rpx TLS issuance failed: ${f.error||"unknown error"}`),!1;return s.success(`rpx TLS certificates reconciled on ${f.instanceCount} gateway target(s)`),!0}import{existsSync as Yu,readFileSync as Ju}from"node:fs";import{createHash as Ku}from"node:crypto";function zc(e){let t=es(e.name),r=ts(e.publicKey),n=Gu([...e.existingKeys??[],{name:t,publicKey:r}]);return $a(e.configText,n)}function Hc(e){let t=es(e.name),r=(e.existingKeys??[]).filter((n)=>n.name!==t);if((e.existingKeys??[]).length===r.length)throw Error(`SSH key '${t}' does not exist in compute.sshKeys`);return $a(e.configText,r)}function zt(e=[]){return e.map((t)=>({name:t.name,publicKey:t.publicKey,type:Wu(t.publicKey),fingerprint:Vu(t.publicKey),added:"configured"}))}function $a(e,t){let r=qu(e,"compute"),n=ju(t),s=Xu(e,r.start,r.end,"sshKeys");if(s){let c=s.propertyStart;while(c>0&&e[c-1]!==`
|
|
593
595
|
`&&/\s/.test(e[c-1]))c--;return`${e.slice(0,c)}${n}${e.slice(s.end+1)}`}let i=e.slice(0,r.end).trimEnd(),o=e.slice(r.end),a=i.endsWith(",")||i.endsWith("{")?"":",";return`${i}${a}
|
|
594
596
|
${n},
|
|
595
|
-
${o}`}function
|
|
597
|
+
${o}`}function ju(e){if(e.length===0)return" sshKeys: []";return` sshKeys: [
|
|
596
598
|
${e.map((r)=>[" {",` name: '${wa(r.name)}',`,` publicKey: '${wa(ts(r.publicKey))}',`," },"].join(`
|
|
597
599
|
`)).join(`
|
|
598
600
|
`)}
|
|
599
|
-
]`}function
|
|
600
|
-
`)a=!1;continue}if(c){if(u==="*"&&d==="/")c=!1,l++;continue}if(i){if(o){o=!1;continue}if(u==="\\"){o=!0;continue}if(u===i)i=void 0;continue}if(u==="/"&&d==="/"){a=!0,l++;continue}if(u==="/"&&d==="*"){c=!0,l++;continue}if(u==='"'||u==="'"||u==="`"){i=u;continue}if(u===r)s++;if(u===n){if(s--,s===0)return l}}throw Error(`Could not find matching ${n} in cloud config.`)}var
|
|
601
|
+
]`}function Gu(e){let t=new Map;for(let r of e){let n=es(r.name);if(t.has(n))throw Error(`SSH key '${n}' already exists in compute.sshKeys`);t.set(n,{name:n,publicKey:ts(r.publicKey)})}return[...t.values()]}function es(e){let t=e.trim();if(!/^[\w@.+-]{2,80}$/.test(t))throw Error("SSH key name must be 2-80 characters and use letters, numbers, @, ., +, _, or -.");return t}function ts(e){let t=e.trim().replace(/\s+/g," "),[r,n]=t.split(" ");if(!/^(?:ssh-(?:ed25519|rsa)|ecdsa-sha2-nistp(?:256|384|521))$/.test(r??"")||!n)throw Error("Public key must be an OpenSSH public key, e.g. ssh-ed25519 AAAA... name@host.");return t}function Wu(e){return e.trim().split(/\s+/)[0]||"ssh"}function Vu(e){let t=e.trim().split(/\s+/)[1];if(!t)return"-";try{return`SHA256:${Ku("sha256").update(Buffer.from(t,"base64")).digest("base64").replace(/=+$/,"")}`}catch{return"-"}}function wa(e){return e.replace(/\\/g,"\\\\").replaceAll(String.fromCharCode(39),"\\'")}function qu(e,t){let r=new RegExp(`\\b${t}\\s*:\\s*{`,"g").exec(e);if(!r)throw Error(`Could not find ${t}: { ... } in cloud config.`);let n=e.indexOf("{",r.index);return{propertyStart:r.index,start:n,end:Ii(e,n,"{","}")}}function Xu(e,t,r,n){let s=e.slice(t+1,r),i=new RegExp(`\\b${n}\\s*:\\s*\\[`,"g").exec(s);if(!i)return null;let o=t+1+i.index,a=e.indexOf("[",o);return{propertyStart:o,start:a,end:Ii(e,a,"[","]")}}function Ii(e,t,r,n){let s=0,i,o=!1,a=!1,c=!1;for(let l=t;l<e.length;l++){let u=e[l],d=e[l+1];if(a){if(u===`
|
|
602
|
+
`)a=!1;continue}if(c){if(u==="*"&&d==="/")c=!1,l++;continue}if(i){if(o){o=!1;continue}if(u==="\\"){o=!0;continue}if(u===i)i=void 0;continue}if(u==="/"&&d==="/"){a=!0,l++;continue}if(u==="/"&&d==="*"){c=!0,l++;continue}if(u==='"'||u==="'"||u==="`"){i=u;continue}if(u===r)s++;if(u===n){if(s--,s===0)return l}}throw Error(`Could not find matching ${n} in cloud config.`)}var Zu=["rpx-gateway","nginx","php8.3-fpm","php8.2-fpm","mysql","mariadb","postgresql","redis","redis-server","meilisearch"],de="-";function Qu(e){let t=String.raw`
|
|
601
603
|
import { X509Certificate } from 'node:crypto'
|
|
602
604
|
import { existsSync, readdirSync, readFileSync, readlinkSync, statSync } from 'node:fs'
|
|
603
605
|
import { basename, join } from 'node:path'
|
|
@@ -743,7 +745,7 @@ if (runningUnits.length > 0) {
|
|
|
743
745
|
].join('='))
|
|
744
746
|
}
|
|
745
747
|
}
|
|
746
|
-
`;return`echo '${Buffer.from(t).toString("base64")}' | base64 -d | bun -`}function
|
|
748
|
+
`;return`echo '${Buffer.from(t).toString("base64")}' | base64 -d | bun -`}function ep(e={}){let t=["set +e",'echo "CPUS=$(nproc 2>/dev/null || echo 1)"',`read TS_TOTAL_1 TS_IDLE_1 <<EOF
|
|
747
749
|
$(awk '/^cpu /{idle=$5+$6; total=0; for(i=2;i<=NF;i++) total+=$i; print total, idle; exit}' /proc/stat 2>/dev/null)
|
|
748
750
|
EOF`,"sleep 0.2",`read TS_TOTAL_2 TS_IDLE_2 <<EOF
|
|
749
751
|
$(awk '/^cpu /{idle=$5+$6; total=0; for(i=2;i<=NF;i++) total+=$i; print total, idle; exit}' /proc/stat 2>/dev/null)
|
|
@@ -751,9 +753,9 @@ EOF`,`echo "CPUUSED=$(awk -v t1="$TS_TOTAL_1" -v i1="$TS_IDLE_1" -v t2="$TS_TOTA
|
|
|
751
753
|
$(free -m 2>/dev/null | awk '/^Mem:/{total=$2; used=$3; available=$7; cache=$6} /^Swap:/{print total, used, available, cache, $2, $3}')
|
|
752
754
|
EOF`,'echo "MEMTOTAL=${MEMTOTAL:-0}"','echo "MEMUSED=${MEMUSED:-0}"','echo "MEMAVAILABLE=${MEMAVAILABLE:-0}"','echo "MEMCACHE=${MEMCACHE:-0}"','echo "SWAPTOTAL=${SWAPTOTAL:-0}"','echo "SWAPUSED=${SWAPUSED:-0}"',`read DISKTOTG DISKUSEDG DISKPCT <<EOF
|
|
753
755
|
$(df -BG -P / 2>/dev/null | awk 'NR==2{gsub("G","",$2); gsub("G","",$3); gsub("%","",$5); print $2, $3, $5}')
|
|
754
|
-
EOF`,'echo "DISKPCT=${DISKPCT:-0}"','echo "DISKUSEDG=${DISKUSEDG:-0}"','echo "DISKTOTG=${DISKTOTG:-0}"','if [ -r /var/lib/ts-cloud/compute-storage-status.json ]; then echo "STORAGE=$(base64 -w0 /var/lib/ts-cloud/compute-storage-status.json)"; fi','if [ -r /var/lib/ts-cloud/metrics.json ]; then echo "COLLECTOR=$(base64 -w0 /var/lib/ts-cloud/metrics.json)"; fi',`echo "INODEPCT=$(df -Pi / 2>/dev/null | awk 'NR==2{gsub("%","",$5);print $5}')"`,`echo "NETRX=$(awk -F'[: ]+' 'NR>2 && $2!="lo"{sum+=$3} END{print sum+0}' /proc/net/dev 2>/dev/null)"`,`echo "NETTX=$(awk -F'[: ]+' 'NR>2 && $2!="lo"{sum+=$11} END{print sum+0}' /proc/net/dev 2>/dev/null)"`,". /var/lib/ts-cloud/bandwidth-state 2>/dev/null || true",'echo "NETRXRATE=${RX_RATE:-0}"','echo "NETTXRATE=${TX_RATE:-0}"','echo "NETRXTODAY=${DAY_RX:-0}"','echo "NETTXTODAY=${DAY_TX:-0}"','echo "NETRXMONTH=${MONTH_RX:-0}"','echo "NETTXMONTH=${MONTH_TX:-0}"','echo "NETMONTHKEY=${MONTH_KEY:-}"',`echo "PROCESSES=$(ps -e --no-headers 2>/dev/null | awk 'END{print NR+0}')"`,`echo "UPTIME=$(uptime -p 2>/dev/null | sed 's/^up //' || echo unknown)"`,`echo "UPTIME_SECONDS=$(cut -d' ' -f1 /proc/uptime 2>/dev/null || echo 0)"`,'echo "OS=$(. /etc/os-release 2>/dev/null; echo "$PRETTY_NAME")"'];if(e.includeServices!==!1)t.push(`for s in ${
|
|
755
|
-
`)){let n=r.trim();if(n.startsWith("DISCOVERED_SITE="))try{let s=JSON.parse(Buffer.from(n.slice(16),"base64").toString("utf8"));if(s?.domain&&s?.route)t.discoveredSites.push(s)}catch{}else if(n.startsWith("SVC=")){let[,s,i,o,a,c]=/^SVC=([^=]+)=([^=]+)(?:=([^=]*)(?:=([^=]*)(?:=(.*))?)?)?$/.exec(n)??[];if(s)t.services.push({name:s,status:i,memBytes:Number(o||0),enabled:a||de,since:c||de})}else{let s=n.indexOf("=");if(s>0)t[n.slice(0,s)]=n.slice(s+1)}}return t}function Ta(e){let t=e.infrastructure?.compute,r=[];if(t?.webServer==="rpx"||t?.proxy?.engine==="rpx")r.push({name:"rpx-gateway",status:"configured"});else if(t?.webServer==="nginx"||t?.webServer==null)r.push({name:"ts-cloud-nginx",status:"configured"});let n=t?.php?.versions??(t?.runtime==="php"?["8.3"]:[]);for(let o of n)r.push({name:`php${o}-fpm`,status:"configured"});let s=t?.managedServices??{},i=[["mysql","mysql"],["mariadb","mariadb"],["postgres","postgresql"],["redis","redis"],["memcached","memcached"],["meilisearch","meilisearch"]];for(let[o,a]of i)if(s[o])r.push({name:a,status:"configured"});return r}function
|
|
756
|
-
`)){let n=r.trim();if(!n.startsWith("LOG="))continue;let s=n.indexOf("\t");if(s<0)continue;let i=n.slice(4,s),o=n.slice(s+1),a=/^(\d{4}-\d{2}-\d{2}T[^\s]+)\s+(\S+)\s+(.*)$/.exec(o),c=a?.[1]??"",l=a?.[2]??"",u=(a?.[3]??o).trim();if(!u)continue;t.push({source:i,timestamp:c,when:c?ns(c):de,host:l,message:u,level:xa(u)})}return t.sort((r,n)=>String(n.timestamp).localeCompare(String(r.timestamp))).slice(0,600)}function Oa(e){if(/^(?:127\.|localhost:|\[?::1\]?)/.test(e))return"loopback";if(/^(?:10\.|192\.168\.|172\.(?:1[6-9]|2\d|3[01])\.|169\.254\.|\[?f[cd][0-9a-f:])/i.test(e))return"private";return"public"}function
|
|
757
|
-
`)){let c=a.trim();if(c.startsWith("PORT=")){let[l="",u="",d=""]=c.slice(5).split("\t");if(l&&u)t.push({proto:l,listen:u,processName:d||de,exposure:Oa(u),tone:
|
|
758
|
-
`)){let s=n.trim();if(!s.startsWith("DEPLOY="))continue;let i=s.split("\t"),o=i[0]?.replace(/^DEPLOY=/,"")??"",[a,c,l,u,d]=i.slice(1);if(!o||!a||!c)continue;let m=t[o]??{},h=w(m),f=(l||c).slice(0,7);r.push({sha:f,release:c,commit:l||c,site:o,branch:m.branch??(h==="server-static"?"build artifact":"main"),status:u||"unknown",when:ns(a),timestamp:a,took:"-",by:"ts-cloud",rc:d?.replace(/^rc=/,"")??"",steps:h==="server-static"?["upload artifact","publish static files"]:["upload artifact","restart service"]})}return r.sort((n,s)=>String(s.timestamp).localeCompare(String(n.timestamp)))}function
|
|
756
|
+
EOF`,'echo "DISKPCT=${DISKPCT:-0}"','echo "DISKUSEDG=${DISKUSEDG:-0}"','echo "DISKTOTG=${DISKTOTG:-0}"','if [ -r /var/lib/ts-cloud/compute-storage-status.json ]; then echo "STORAGE=$(base64 -w0 /var/lib/ts-cloud/compute-storage-status.json)"; fi','if [ -r /var/lib/ts-cloud/metrics.json ]; then echo "COLLECTOR=$(base64 -w0 /var/lib/ts-cloud/metrics.json)"; fi',`echo "INODEPCT=$(df -Pi / 2>/dev/null | awk 'NR==2{gsub("%","",$5);print $5}')"`,`echo "NETRX=$(awk -F'[: ]+' 'NR>2 && $2!="lo"{sum+=$3} END{print sum+0}' /proc/net/dev 2>/dev/null)"`,`echo "NETTX=$(awk -F'[: ]+' 'NR>2 && $2!="lo"{sum+=$11} END{print sum+0}' /proc/net/dev 2>/dev/null)"`,". /var/lib/ts-cloud/bandwidth-state 2>/dev/null || true",'echo "NETRXRATE=${RX_RATE:-0}"','echo "NETTXRATE=${TX_RATE:-0}"','echo "NETRXTODAY=${DAY_RX:-0}"','echo "NETTXTODAY=${DAY_TX:-0}"','echo "NETRXMONTH=${MONTH_RX:-0}"','echo "NETTXMONTH=${MONTH_TX:-0}"','echo "NETMONTHKEY=${MONTH_KEY:-}"',`echo "PROCESSES=$(ps -e --no-headers 2>/dev/null | awk 'END{print NR+0}')"`,`echo "UPTIME=$(uptime -p 2>/dev/null | sed 's/^up //' || echo unknown)"`,`echo "UPTIME_SECONDS=$(cut -d' ' -f1 /proc/uptime 2>/dev/null || echo 0)"`,'echo "OS=$(. /etc/os-release 2>/dev/null; echo "$PRETTY_NAME")"'];if(e.includeServices!==!1)t.push(`for s in ${Zu.join(" ")}; do st=$(systemctl is-active "$s" 2>/dev/null); if [ -n "$st" ] && [ "$st" != "inactive" ] && [ "$st" != "unknown" ]; then mem=$(systemctl show "$s" -p MemoryCurrent --value 2>/dev/null); en=$(systemctl is-enabled "$s" 2>/dev/null); since=$(systemctl show "$s" -p ActiveEnterTimestamp --value 2>/dev/null); echo "SVC=$s=$st=$mem=$en=$since"; fi; done`);if(e.includeSites!==!1)t.push(Qu(e.projectFilter));return t.push("true"),t}function tp(e){let t={services:[],discoveredSites:[]};for(let r of e.split(`
|
|
757
|
+
`)){let n=r.trim();if(n.startsWith("DISCOVERED_SITE="))try{let s=JSON.parse(Buffer.from(n.slice(16),"base64").toString("utf8"));if(s?.domain&&s?.route)t.discoveredSites.push(s)}catch{}else if(n.startsWith("SVC=")){let[,s,i,o,a,c]=/^SVC=([^=]+)=([^=]+)(?:=([^=]*)(?:=([^=]*)(?:=(.*))?)?)?$/.exec(n)??[];if(s)t.services.push({name:s,status:i,memBytes:Number(o||0),enabled:a||de,since:c||de})}else{let s=n.indexOf("=");if(s>0)t[n.slice(0,s)]=n.slice(s+1)}}return t}function Ta(e){let t=e.infrastructure?.compute,r=[];if(t?.webServer==="rpx"||t?.proxy?.engine==="rpx")r.push({name:"rpx-gateway",status:"configured"});else if(t?.webServer==="nginx"||t?.webServer==null)r.push({name:"ts-cloud-nginx",status:"configured"});let n=t?.php?.versions??(t?.runtime==="php"?["8.3"]:[]);for(let o of n)r.push({name:`php${o}-fpm`,status:"configured"});let s=t?.managedServices??{},i=[["mysql","mysql"],["mariadb","mariadb"],["postgres","postgresql"],["redis","redis"],["memcached","memcached"],["meilisearch","meilisearch"]];for(let[o,a]of i)if(s[o])r.push({name:a,status:"configured"});return r}function rp(e){let t=e.infrastructure?.compute?.backups,r=!!t?.enabled,n=t?.schedule??"0 2 * * *";return{enabled:r,schedule:n,destination:r?t?.bucket??"local":"off",retention:r?t?.retentionCount??5:0,last:r?"pending first run":"not configured",size:r?de:"0 MB"}}function Ca(e){return e.infrastructure?.compute?.provider??e.cloud?.provider??e.provider??"aws"}function np(e){if(Ca(e)==="hetzner")return _n(e);return e.project.region??"us-east-1"}function sp(e){let t=e.infrastructure?.compute?.monitoring,r=Number(typeof t==="object"?t?.alerts?.bandwidthTb??0:0);return Number.isFinite(r)&&r>0?Math.round(r*1000000000000):0}function ip(e,t){let r=dt(T(e,t));if(!Yu(r))return null;try{return JSON.parse(Ju(r,"utf8"))}catch{return null}}function op(e){let t=e.sites??{},r=[];for(let[n,s]of Object.entries(t)){let i=s.queues??s.workers;if(Array.isArray(i)){for(let o of i)if(typeof o==="string")r.push({name:`${n}:${o}`,site:n,processes:1,status:"configured"});else if(o?.name)r.push({name:`${n}:${o.name}`,site:n,processes:o.processes??1,status:"configured"})}}return r}function Pa(e){if(!e||e==="")return"/";return e.startsWith("/")?e:`/${e}`}function ap(e){let t=Pa(e.path);if(!e.domain)return{route:"internal"};return{route:t==="/"?e.domain:`${e.domain}${t}`,href:`https://${e.domain}${t==="/"?"":t}`}}function _a(e,t){let r=w(t);if(r==="redirect")return"redirect";if(r==="proxy")return"proxy";let n=String(t.build??"").toLowerCase(),s=String(t.start??"").toLowerCase();if(e==="main"||s.includes("buddy/src/cli.ts serve"))return"stacks";if(e==="api"||s.includes("/serve/api"))return"api";if(n.includes("buildblog"))return"bunpress blog";if(n.includes("bunpress"))return"bunpress";if(n.includes("site:build")||n.includes("stx"))return"stx static";if(r==="server-static")return t.spa?"spa":"static";if(r==="server-app")return"app";if(r==="server-php")return t.type??"php";return"bucket"}function cp(e){let t=w(e);if(t==="redirect"||t==="proxy")return"—";let r=`${e.start??""} ${e.build??""}`.toLowerCase();if(t==="server-static")return r.includes("bunpress")||r.includes("bun ")||r.includes("bunx ")?"static/bun":"static";if(t==="server-app")return r.includes("bun")?"bun":"node";if(t==="server-php")return`php ${e.php??e.phpVersion??"8.3"}`;return"static"}function lp(e){let t=w(e);if(t==="redirect")return"redirect";if(t==="proxy")return"proxy";if(t==="server-app")return"service";if(t==="server-static")return"server static";if(t==="server-php")return"php release";return"bucket"}function up(e){let t=new Map;return Object.entries(e.sites??{}).map(([r,n])=>{let s=Pa(n.path),{route:i,href:o}=ap(n),a=n.domain?`${n.domain}${s}`:"",c=a?t.get(a):void 0;if(a&&!c)t.set(a,r);return{name:r,route:i,href:o,domain:n.domain??"internal",path:s,kind:_a(r,n),type:_a(r,n),runtime:cp(n),deploy:lp(n),tls:n.domain?n.ssl===!1?"http":"https":"loopback",ssl:n.domain?n.ssl!==!1:!1,status:c?"shadowed":"live",shadowedBy:c,lastDeploy:de}})}function pp(e,t,r){let n=new Map(e.map((s)=>[String(s.route),{...s,project:r,managedBy:"owner"}]));for(let s of t){let i=n.get(s.route),o=i?.name??`${s.project}:${s.name}`;n.set(s.route,{...i,...s,name:o,kind:i?.kind??s.kind,type:i?.type??s.kind,runtime:i?.runtime??s.runtime,deploy:i?.deploy??s.deploy,root:i?.root??s.root,branch:i?.branch??s.branch,managedBy:s.project===r?"owner":"attached"})}return[...n.values()].sort((s,i)=>{if(s.managedBy!==i.managedBy)return s.managedBy==="owner"?-1:1;return String(s.route).localeCompare(String(i.route))})}function rs(e){return`'${e.replaceAll("'","'\\''")}'`}function Ra(e){return e.replace(/[\\&/]/g,"\\$&")}function dp(e,t){if(e.length===0)return["true"];return["set +e",...e.map((r)=>{let n=rs(`${W(t,r)}/.ts-cloud/deploy-history.log`),s=Ra(r);return`TS_CLOUD_HISTORY=${n}; { [ -f "$TS_CLOUD_HISTORY" ] && tail -n 24 "$TS_CLOUD_HISTORY" | sed "s/^/DEPLOY=${s}\\t/"; } || true`}),"true"]}function Ea(e){return[...new Set(Object.values(e.sites??{}).map((t)=>t.domain).filter((t)=>typeof t==="string"&&t.length>0))]}function mp(e){return["set +e",'if command -v ss >/dev/null 2>&1; then ss -H -lntup 2>/dev/null | while read -r proto _state _recv _send local _peer rest; do [ -n "$proto" ] && [ -n "$local" ] && printf "PORT=%s\\t%s\\t%s\\n" "$proto" "$local" "$rest"; done; fi','if command -v ufw >/dev/null 2>&1; then ufw status numbered 2>/dev/null | sed "s/^/FIREWALL=/"; else echo "FIREWALL=ufw unavailable"; fi','journalctl _COMM=sshd --no-pager -n 30 -o short-iso 2>/dev/null | sed "s/^/AUTH=/" || true',...Ea(e).map((r)=>`TS_CLOUD_DOMAIN=${rs(r)}; TS_CLOUD_EXPIRY=$(echo | timeout 8 openssl s_client -servername "$TS_CLOUD_DOMAIN" -connect "$TS_CLOUD_DOMAIN:443" 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2-); [ -n "$TS_CLOUD_EXPIRY" ] && printf "CERT=%s\\t%s\\n" "$TS_CLOUD_DOMAIN" "$TS_CLOUD_EXPIRY" || printf "CERT=%s\\tunavailable\\n" "$TS_CLOUD_DOMAIN"`),"true"]}function fp(e){let t=new Map,r=(i,o)=>{if(i&&o&&!t.has(o))t.set(o,{pattern:i,label:o})},n=e.infrastructure?.compute,s=e.project.slug;if(n?.webServer==="rpx"||n?.proxy?.engine==="rpx")r("rpx-gateway","rpx-gateway");else r("nginx","nginx");for(let i of Ta(e))r(i.name,i.name);for(let[i,o]of Object.entries(e.sites??{})){if(w(o)!=="server-app")continue;if(r(`${s}-${i}`,`${s}-${i}`),Array.isArray(o.queues??o.workers)&&(o.queues??o.workers).length)r(`${s}-${i}-queue-*`,`${s}-${i}-queues`);if(Array.isArray(o.daemons)&&o.daemons.length)r(`${s}-${i}-daemon-*`,`${s}-${i}-daemons`)}return[...t.values()]}function gp(e){let t=fp(e);if(t.length===0)return["true"];return["set +e",...t.map((r)=>{let n=rs(r.pattern),s=Ra(r.label);return`journalctl -u ${n} --no-pager -n 150 -o short-iso 2>/dev/null | sed "s/^/LOG=${s}\\t/" || true`}),'[ -f /var/log/ts-cloud-backup.log ] && tail -n 60 /var/log/ts-cloud-backup.log 2>/dev/null | sed "s/^/LOG=backups\\t/" || true',"true"]}function ns(e){let t=new Date(e);if(Number.isNaN(t.getTime()))return e||de;let r=Math.max(0,Math.round((Date.now()-t.getTime())/1000));if(r<60)return`${r}s ago`;let n=Math.round(r/60);if(n<60)return`${n}m ago`;let s=Math.round(n/60);if(s<48)return`${s}h ago`;return`${Math.round(s/24)}d ago`}function xa(e){if(/(?:error|failed|panic|fatal|exception|denied|unhealthy)/i.test(e))return"error";if(/(?:warn|warning|retry|restart|deprecated|timeout)/i.test(e))return"warn";return"info"}function hp(e){let t=[];for(let r of e.split(`
|
|
758
|
+
`)){let n=r.trim();if(!n.startsWith("LOG="))continue;let s=n.indexOf("\t");if(s<0)continue;let i=n.slice(4,s),o=n.slice(s+1),a=/^(\d{4}-\d{2}-\d{2}T[^\s]+)\s+(\S+)\s+(.*)$/.exec(o),c=a?.[1]??"",l=a?.[2]??"",u=(a?.[3]??o).trim();if(!u)continue;t.push({source:i,timestamp:c,when:c?ns(c):de,host:l,message:u,level:xa(u)})}return t.sort((r,n)=>String(n.timestamp).localeCompare(String(r.timestamp))).slice(0,600)}function Oa(e){if(/^(?:127\.|localhost:|\[?::1\]?)/.test(e))return"loopback";if(/^(?:10\.|192\.168\.|172\.(?:1[6-9]|2\d|3[01])\.|169\.254\.|\[?f[cd][0-9a-f:])/i.test(e))return"private";return"public"}function yp(e){if(Oa(e)!=="public")return"ok";if(/:(?:22|80|443)$/.test(e)||/^\*:(?:22|80|443)$/.test(e))return"ok";return"warn"}function Sp(e){if(e==null)return"warn";if(e<8)return"bad";if(e<30)return"warn";return"ok"}function vp(e){let t=[],r=[],n=[],s=[];for(let a of e.split(`
|
|
759
|
+
`)){let c=a.trim();if(c.startsWith("PORT=")){let[l="",u="",d=""]=c.slice(5).split("\t");if(l&&u)t.push({proto:l,listen:u,processName:d||de,exposure:Oa(u),tone:yp(u)})}else if(c.startsWith("FIREWALL=")){let l=c.slice(9).trim();if(l)r.push(l)}else if(c.startsWith("AUTH=")){let l=c.slice(5),u=/^(\d{4}-\d{2}-\d{2}T[^\s]+)\s+(\S+)\s+(.*)$/.exec(l),d=(u?.[3]??l).trim();if(d)n.push({timestamp:u?.[1]??"",when:u?.[1]?ns(u[1]):de,host:u?.[2]??"",message:d,level:xa(d)})}else if(c.startsWith("CERT=")){let[l="",u=""]=c.slice(5).split("\t");if(!l)continue;let d=new Date(u),m=Number.isNaN(d.getTime())?null:Math.ceil((d.getTime()-Date.now())/86400000);s.push({domain:l,expires:Number.isNaN(d.getTime())?u||de:d.toISOString().slice(0,10),daysRemaining:m,status:Sp(m)})}}let i=r.some((a)=>/Status:\s*active/i.test(a)),o=r.some((a)=>/unavailable/i.test(a));return{ports:t.slice(0,80),firewall:{status:i?"active":o?"unavailable":"inactive",summary:i?"ufw active":o?"ufw unavailable":"ufw inactive or not configured",rules:r.filter((a)=>a&&!/^Status:/i.test(a)).slice(0,60)},tlsCertificates:s,authEvents:n.sort((a,c)=>String(c.timestamp).localeCompare(String(a.timestamp))).slice(0,30)}}function ss(e){let r=e.infrastructure?.compute?.firewall??{},n=[...new Set([22,80,443,...r.allowedPorts??[]])],s=Ea(e);return{ports:n.map((i)=>({proto:"tcp",listen:`0.0.0.0:${i}`,processName:"configured firewall",exposure:"public",tone:[22,80,443].includes(Number(i))?"ok":"warn"})),firewall:{status:r.enabled===!1?"disabled":"configured",summary:r.enabled===!1?"host firewall disabled in config":"host firewall configured declaratively",rules:n.map((i)=>`ALLOW ${i}/tcp`)},tlsCertificates:s.map((i)=>({domain:i,expires:de,daysRemaining:null,status:"warn"})),authEvents:[]}}function Da(e,t){let r=t.security??ss(e),n=!!t._serverReachable&&!t.metricsUnavailable,s=t.sites??[],i=s.filter((l)=>l.status==="shadowed"),o=(t.services??[]).filter((l)=>["failed","stopped"].includes(l.status)),a=(r.tlsCertificates??[]).filter((l)=>l.status!=="ok"),c=t.sshKeys??[];return[{name:"Live server probe",status:n?"pass":"warn",detail:n?"Metrics and remote checks are coming from the compute box.":"The dashboard is rendering config/state data until the box probe succeeds."},{name:"Managed services",status:o.length?"fail":"pass",detail:o.length?`${o.length} service(s) need attention.`:`${(t.services??[]).length} service(s) reported healthy or configured.`},{name:"Route conflicts",status:i.length?"warn":"pass",detail:i.length?`${i.map((l)=>l.name).join(", ")} is shadowed by an earlier route.`:`${s.length} site route(s) are unshadowed.`},{name:"SSH access",status:c.length?"pass":"warn",detail:c.length?`${c.length} declarative authorized key(s) configured.`:"No declarative SSH keys are configured."},{name:"Firewall",status:["active","configured"].includes(r.firewall?.status)?"pass":"warn",detail:r.firewall?.summary??"Firewall status unavailable."},{name:"TLS certificates",status:a.length?"warn":"pass",detail:a.length?`${a.length} certificate(s) need renewal visibility.`:`${(r.tlsCertificates??[]).length} certificate(s) look healthy.`}]}function Ia(e){let t=[];for(let r of e.serverDeploymentsDetail??e.serverDeployments??[])t.push({type:"deploy",tone:r.status==="failed"?"bad":"ok",title:`${r.site} deployed ${r.sha}`,detail:`${r.branch} · ${r.status}`,when:r.when,timestamp:r.timestamp});for(let r of e.serverLogs??[])if(r.level==="error"||r.level==="warn")t.push({type:"log",tone:r.level==="error"?"bad":"warn",title:`${r.source} ${r.level}`,detail:r.message,when:r.when,timestamp:r.timestamp});for(let r of e.sshKeys??[])t.push({type:"ssh",tone:"ok",title:`${r.name} authorized`,detail:r.fingerprint,when:r.added??de,timestamp:""});return t.sort((r,n)=>String(n.timestamp).localeCompare(String(r.timestamp))).slice(0,80)}function bp(e,t={}){let r=[];for(let n of e.split(`
|
|
760
|
+
`)){let s=n.trim();if(!s.startsWith("DEPLOY="))continue;let i=s.split("\t"),o=i[0]?.replace(/^DEPLOY=/,"")??"",[a,c,l,u,d]=i.slice(1);if(!o||!a||!c)continue;let m=t[o]??{},h=w(m),f=(l||c).slice(0,7);r.push({sha:f,release:c,commit:l||c,site:o,branch:m.branch??(h==="server-static"?"build artifact":"main"),status:u||"unknown",when:ns(a),timestamp:a,took:"-",by:"ts-cloud",rc:d?.replace(/^rc=/,"")??"",steps:h==="server-static"?["upload artifact","publish static files"]:["upload artifact","restart service"]})}return r.sort((n,s)=>String(s.timestamp).localeCompare(String(n.timestamp)))}function wp(e,t){let r=ip(e,t),n=Ta(e),s=up(e),i=Math.max(1,Number(e.infrastructure?.compute?.disk?.size??1)),o={server:{name:r?.serverName??`${e.project.slug}-${t}-app`,provider:r?.provider??Ca(e),region:np(e),ip:r?.publicIp??de,os:"Linux",uptime:de,probeStatus:"unavailable"},systemMetrics:{load:0,load5:0,load15:0,cpuUsedPct:0,cpus:Math.max(1,Number(e.infrastructure?.compute?.instances??1)),memUsedMb:0,memTotalMb:0,memAvailableMb:0,memCacheMb:0,swapUsedMb:0,swapTotalMb:0,diskUsedPct:0,diskUsedGb:0,diskTotalGb:i,inodeUsedPct:0,networkRxBytes:0,networkTxBytes:0,networkRxBytesPerSec:0,networkTxBytesPerSec:0,networkRxBytesToday:0,networkTxBytesToday:0,networkRxBytesMonth:0,networkTxBytesMonth:0,networkMonthKey:"",processes:0,uptimeSeconds:0},metricsUnavailable:!0,bandwidthBudgetBytes:sp(e),services:n,servicesDetail:n.map((a)=>({...a,since:de,memMb:0,auto:!0})),backup:rp(e),backupHistory:[],workers:op(e),serverScheduler:{enabled:!1,lastRun:"not configured"},serverDeployments:[],serverDeploymentsDetail:[],deploymentsEmptyReason:"No deployment history has been recorded yet. Future server deploys will write /var/www/<site>/.ts-cloud/deploy-history.log.",serverLogs:[],serverLogsEmptyReason:"Live server logs are unavailable until the dashboard can reach the compute box.",security:ss(e),securityEmptyReason:"Live security checks are unavailable until the dashboard can reach the compute box.",sites:s,sitesDetail:s.map((a)=>{let c=e.sites?.[a.name]??{},l=w(c);return{...a,root:l==="server-static"?W(e.project.slug,a.name):`${W(e.project.slug,a.name)}/current`,branch:l==="server-static"?"build artifact":"main",build:c.build,php:c.php??c.phpVersion,aliases:Array.isArray(c.aliases)?c.aliases:[],redirects:c.redirects&&typeof c.redirects==="object"?c.redirects:{},envKeys:Object.keys(c.env??c.environment??{})}}),siteHealth:[],sshKeys:zt(e.infrastructure?.compute?.sshKeys??[]),_serverReachable:!1,_metricsStatus:"unavailable"};return o.diagnostics=Da(e,o),o.activity=Ia(o),o.topology=bn(o,{project:e.project.name,environment:t}),o}async function Pi(e,t,r={}){if(!e.infrastructure?.compute)return null;let n=null;try{n=O({config:e})}catch{n=null}let s=null,i=0,o=[];if(n)try{if(o=await n.findComputeTargets({slug:e.project.slug,environment:t,role:"app"}),i=o.length,o.length){let d=(await n.runRemoteDeploy({targets:[o[0]],commands:ep({includeServices:!r.telemetryOnly,includeSites:r.includeSiteHealth!==!1,projectFilter:e.cloud?.attachTo?e.project.slug:void 0}),comment:`ts-cloud dashboard:build ${e.project.slug}`,tags:{Project:e.project.slug,Environment:t,Role:"app"}})).perInstance?.[0]?.output;if(d)s=tp(d)}}catch{}let a=wp(e,t),c=(u,d=0)=>{let m=Number(u);return Number.isFinite(m)?m:d};if(a.server.os=s?.OS||a.server.os,a.server.uptime=s?.UPTIME||a.server.uptime,s){a.server.probeStatus="live",a.metricsUnavailable=!1,a._metricsStatus="live";try{a.storage=JSON.parse(Buffer.from(s.STORAGE||"","base64").toString("utf8"))}catch{a.storage=null}let u=[];try{u=JSON.parse(Buffer.from(s.COLLECTOR||"","base64").toString("utf8")).disks??[]}catch{}if(a.systemMetrics={disks:u,load:c(s.LOAD),load5:c(s.LOAD5),load15:c(s.LOAD15),cpuUsedPct:c(s.CPUUSED),cpus:c(s.CPUS,1),memUsedMb:c(s.MEMUSED),memTotalMb:c(s.MEMTOTAL,1),memAvailableMb:c(s.MEMAVAILABLE),memCacheMb:c(s.MEMCACHE),swapUsedMb:c(s.SWAPUSED),swapTotalMb:c(s.SWAPTOTAL),diskUsedPct:c(s.DISKPCT),diskUsedGb:c(s.DISKUSEDG),diskTotalGb:c(s.DISKTOTG,1),inodeUsedPct:c(s.INODEPCT),networkRxBytes:c(s.NETRX),networkTxBytes:c(s.NETTX),networkRxBytesPerSec:c(s.NETRXRATE),networkTxBytesPerSec:c(s.NETTXRATE),networkRxBytesToday:c(s.NETRXTODAY),networkTxBytesToday:c(s.NETTXTODAY),networkRxBytesMonth:c(s.NETRXMONTH),networkTxBytesMonth:c(s.NETTXMONTH),networkMonthKey:s.NETMONTHKEY||"",processes:c(s.PROCESSES),uptimeSeconds:c(s.UPTIME_SECONDS)},s.services.length){let d=[...new Map(s.services.map((m)=>[m.name,m])).values()];a.services=d.map((m)=>({name:m.name,status:m.status==="active"?"running":m.status})),a.servicesDetail=d.map((m)=>({name:m.name,status:m.status==="active"?"running":m.status,since:m.since&&m.since!==de?m.since:a.server.uptime,memMb:Math.max(0,Math.round(Number(m.memBytes??0)/1024/1024)),auto:m.enabled?!["disabled","static","masked"].includes(m.enabled):!0}))}if(s.discoveredSites.length)a.sitesDetail=pp(a.sitesDetail,s.discoveredSites,e.project.slug),a.sites=a.sitesDetail.map((d)=>({name:d.name,project:d.project,managedBy:d.managedBy,route:d.route,href:d.href,domain:d.domain,path:d.path,kind:d.kind,type:d.type,runtime:d.runtime,deploy:d.deploy,tls:d.tls,status:d.status,httpStatus:d.httpStatus,responseMs:d.responseMs,tlsDaysRemaining:d.tlsDaysRemaining,checkedAt:d.checkedAt})),a.siteHealth=a.sitesDetail.map((d)=>({name:d.name,project:d.project,route:d.route,status:d.status,httpStatus:d.httpStatus,responseMs:d.responseMs,tlsDaysRemaining:d.tlsDaysRemaining,checkedAt:d.checkedAt}))}if(n&&o.length&&!r.telemetryOnly)await Promise.all([(async()=>{try{let u=Object.keys(e.sites??{}),m=(await n.runRemoteDeploy({targets:[o[0]],commands:dp(u,e.project.slug),comment:`ts-cloud dashboard:deploy-history ${e.project.slug}`,tags:{Project:e.project.slug,Environment:t,Role:"app"}})).perInstance?.[0]?.output??"",h=bp(m,e.sites);a.serverDeployments=h.slice(0,5),a.serverDeploymentsDetail=h.slice(0,50),a.deploymentsEmptyReason=h.length?void 0:"No deployment history was found on the server yet. Deploy again to populate this timeline."}catch{a.deploymentsEmptyReason="Deployment history could not be read from the server."}})(),(async()=>{try{let d=(await n.runRemoteDeploy({targets:[o[0]],commands:gp(e),comment:`ts-cloud dashboard:server-logs ${e.project.slug}`,tags:{Project:e.project.slug,Environment:t,Role:"app"}})).perInstance?.[0]?.output??"",m=hp(d);a.serverLogs=m,a.serverLogsEmptyReason=m.length?void 0:"No recent journal entries were found for the managed server units."}catch{a.serverLogsEmptyReason="Server logs could not be read from the box."}})(),(async()=>{try{let d=(await n.runRemoteDeploy({targets:[o[0]],commands:mp(e),comment:`ts-cloud dashboard:security ${e.project.slug}`,tags:{Project:e.project.slug,Environment:t,Role:"app"}})).perInstance?.[0]?.output??"",m=vp(d),h=ss(e);if(!m.ports.length)m.ports=h.ports;if(!m.tlsCertificates.length)m.tlsCertificates=h.tlsCertificates;a.security=m,a.securityEmptyReason=void 0}catch{a.securityEmptyReason="Server security checks could not be read from the box."}})()]);else if(n&&i===0)a.deploymentsEmptyReason="No app server target was found for this environment.";let l=e.infrastructure?.compute?.sshKeys??e.infrastructure?.ssh?.keys??e.infrastructure?.sshKeys;if(Array.isArray(l)&&l.length)a.sshKeys=zt(l);return a._serverReachable=i>0&&!!s,a.diagnostics=Da(e,a),a.activity=Ia(a),a.topology=bn(a,{project:e.project.name,environment:t}),a}
|
|
759
761
|
export{Ze,ks,me,po,Ei,mo,go,cn,fo,ho,Te,ee,et,_e,ln,St,Oe,Cr,dn,tt,rt,Tr,un,wi,Ai,ge,vt,Er,yo,Ri,wr,Ar,Rr,pn,Ir,O,bo,So,Vc,vo,Ns,xs,_s,Os,Ct,$s,Co,mn,To,Eo,zc,Hc,zt,Ii,Pi};
|