From b976b147e2e5e34b940c69fee7d7c121e12cd9a8 Mon Sep 17 00:00:00 2001 From: Clawd Date: Tue, 17 Feb 2026 08:09:34 -0800 Subject: Preserve existing Caddyfiles on redeploy Don't overwrite Caddyfile if it already exists. This preserves manual customizations (NIP-05 routes, custom headers, etc.). First deploy generates Caddyfile, subsequent deploys leave it alone. --- cmd/ship/deploy_impl_v2.go | 78 +++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 35 deletions(-) (limited to 'cmd/ship') diff --git a/cmd/ship/deploy_impl_v2.go b/cmd/ship/deploy_impl_v2.go index 9ff674e..5b68dc3 100644 --- a/cmd/ship/deploy_impl_v2.go +++ b/cmd/ship/deploy_impl_v2.go @@ -46,20 +46,22 @@ func deployStaticV2(ctx *deployContext) *output.ErrorResponse { // Non-fatal, continue } - // Generate Caddyfile - caddyfile, err := templates.StaticCaddy(map[string]string{ - "Domain": ctx.URL[8:], // Strip https:// - "RootDir": remotePath, - "Name": name, - }) - if err != nil { - return output.Err(output.ErrCaddyFailed, "failed to generate Caddyfile: "+err.Error()) - } - - // Upload Caddyfile + // Generate Caddyfile only if it doesn't exist (preserve manual edits) caddyPath := fmt.Sprintf("/etc/caddy/sites-enabled/%s.caddy", name) - if err := client.WriteSudoFile(caddyPath, caddyfile); err != nil { - return output.Err(output.ErrCaddyFailed, "failed to write Caddyfile: "+err.Error()) + caddyExists, _ := client.Run(fmt.Sprintf("test -f %s && echo exists", caddyPath)) + if strings.TrimSpace(caddyExists) != "exists" { + caddyfile, err := templates.StaticCaddy(map[string]string{ + "Domain": ctx.URL[8:], // Strip https:// + "RootDir": remotePath, + "Name": name, + }) + if err != nil { + return output.Err(output.ErrCaddyFailed, "failed to generate Caddyfile: "+err.Error()) + } + + if err := client.WriteSudoFile(caddyPath, caddyfile); err != nil { + return output.Err(output.ErrCaddyFailed, "failed to write Caddyfile: "+err.Error()) + } } // Reload Caddy @@ -150,18 +152,21 @@ func deployDockerV2(ctx *deployContext) *output.ErrorResponse { return output.Err(output.ErrServiceFailed, "failed to write systemd unit: "+err.Error()) } - // Generate Caddyfile - caddyfile, err := templates.AppCaddy(map[string]string{ - "Domain": ctx.URL[8:], // Strip https:// - "Port": strconv.Itoa(port), - }) - if err != nil { - return output.Err(output.ErrCaddyFailed, "failed to generate Caddyfile: "+err.Error()) - } - + // Generate Caddyfile only if it doesn't exist (preserve manual edits) caddyPath := fmt.Sprintf("/etc/caddy/sites-enabled/%s.caddy", name) - if err := client.WriteSudoFile(caddyPath, caddyfile); err != nil { - return output.Err(output.ErrCaddyFailed, "failed to write Caddyfile: "+err.Error()) + caddyExists, _ := client.Run(fmt.Sprintf("test -f %s && echo exists", caddyPath)) + if strings.TrimSpace(caddyExists) != "exists" { + caddyfile, err := templates.AppCaddy(map[string]string{ + "Domain": ctx.URL[8:], // Strip https:// + "Port": strconv.Itoa(port), + }) + if err != nil { + return output.Err(output.ErrCaddyFailed, "failed to generate Caddyfile: "+err.Error()) + } + + if err := client.WriteSudoFile(caddyPath, caddyfile); err != nil { + return output.Err(output.ErrCaddyFailed, "failed to write Caddyfile: "+err.Error()) + } } // Reload systemd and start service @@ -255,18 +260,21 @@ func deployBinaryV2(ctx *deployContext) *output.ErrorResponse { return output.Err(output.ErrServiceFailed, "failed to write systemd unit: "+err.Error()) } - // Generate Caddyfile - caddyfile, err := templates.AppCaddy(map[string]string{ - "Domain": ctx.URL[8:], // Strip https:// - "Port": strconv.Itoa(port), - }) - if err != nil { - return output.Err(output.ErrCaddyFailed, "failed to generate Caddyfile: "+err.Error()) - } - + // Generate Caddyfile only if it doesn't exist (preserve manual edits) caddyPath := fmt.Sprintf("/etc/caddy/sites-enabled/%s.caddy", name) - if err := client.WriteSudoFile(caddyPath, caddyfile); err != nil { - return output.Err(output.ErrCaddyFailed, "failed to write Caddyfile: "+err.Error()) + caddyExists, _ := client.Run(fmt.Sprintf("test -f %s && echo exists", caddyPath)) + if strings.TrimSpace(caddyExists) != "exists" { + caddyfile, err := templates.AppCaddy(map[string]string{ + "Domain": ctx.URL[8:], // Strip https:// + "Port": strconv.Itoa(port), + }) + if err != nil { + return output.Err(output.ErrCaddyFailed, "failed to generate Caddyfile: "+err.Error()) + } + + if err := client.WriteSudoFile(caddyPath, caddyfile); err != nil { + return output.Err(output.ErrCaddyFailed, "failed to write Caddyfile: "+err.Error()) + } } // Reload systemd and start service -- cgit v1.2.3