diff options
| author | bndw <ben@bdw.to> | 2026-02-08 12:32:59 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-08 12:32:59 -0800 |
| commit | a8ad8e934d15d2bf84f942414a89af1d2691adbc (patch) | |
| tree | 82e6765c9d35968b27ac7ee17f5c201a421dc1d3 /cmd/ship/init.go | |
| parent | af109c04a3edd4dcd4e7b16242052442fb4a3b24 (diff) | |
Add git-centric deployment with Docker builds and vanity imports
New deployment model where projects start with a git remote on the VPS.
Pushing to the remote triggers automatic docker build and deploy via
post-receive hooks. The base domain serves Go vanity imports and git
HTTPS cloning via Caddy + fcgiwrap.
- Add `ship init <name>` command to create bare repos and .ship/ config
- Add `ship deploy <name>` command for manual rebuilds
- Extend `ship host init --base-domain` to set up Docker, git user,
fcgiwrap, sudoers, and vanity import infrastructure
- Add git-app and git-static types alongside existing app and static
- Update remove, status, logs, restart, list, and config-update to
handle new types
Diffstat (limited to 'cmd/ship/init.go')
| -rw-r--r-- | cmd/ship/init.go | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/cmd/ship/init.go b/cmd/ship/init.go new file mode 100644 index 0000000..167347d --- /dev/null +++ b/cmd/ship/init.go | |||
| @@ -0,0 +1,247 @@ | |||
| 1 | package main | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "os" | ||
| 6 | "path/filepath" | ||
| 7 | "strconv" | ||
| 8 | |||
| 9 | "github.com/bdw/ship/internal/ssh" | ||
| 10 | "github.com/bdw/ship/internal/state" | ||
| 11 | "github.com/bdw/ship/internal/templates" | ||
| 12 | "github.com/spf13/cobra" | ||
| 13 | ) | ||
| 14 | |||
| 15 | var initCmd = &cobra.Command{ | ||
| 16 | Use: "init <name>", | ||
| 17 | Short: "Initialize a git-deployed project", | ||
| 18 | Long: `Create a bare git repo on the VPS and generate local .ship/ config files. | ||
| 19 | |||
| 20 | Pushing to the remote triggers an automatic docker build and deploy (for apps) | ||
| 21 | or a static file checkout (for static sites). | ||
| 22 | |||
| 23 | Examples: | ||
| 24 | # Initialize an app (Docker-based) | ||
| 25 | ship init myapp | ||
| 26 | |||
| 27 | # Initialize with a custom domain | ||
| 28 | ship init myapp --domain custom.example.com | ||
| 29 | |||
| 30 | # Initialize a static site | ||
| 31 | ship init mysite --static`, | ||
| 32 | Args: cobra.ExactArgs(1), | ||
| 33 | RunE: runInit, | ||
| 34 | } | ||
| 35 | |||
| 36 | func init() { | ||
| 37 | initCmd.Flags().Bool("static", false, "Initialize as static site") | ||
| 38 | initCmd.Flags().String("domain", "", "Custom domain (default: name.basedomain)") | ||
| 39 | } | ||
| 40 | |||
| 41 | func runInit(cmd *cobra.Command, args []string) error { | ||
| 42 | name := args[0] | ||
| 43 | static, _ := cmd.Flags().GetBool("static") | ||
| 44 | domain, _ := cmd.Flags().GetString("domain") | ||
| 45 | |||
| 46 | st, err := state.Load() | ||
| 47 | if err != nil { | ||
| 48 | return fmt.Errorf("error loading state: %w", err) | ||
| 49 | } | ||
| 50 | |||
| 51 | host := hostFlag | ||
| 52 | if host == "" { | ||
| 53 | host = st.GetDefaultHost() | ||
| 54 | } | ||
| 55 | if host == "" { | ||
| 56 | return fmt.Errorf("--host is required") | ||
| 57 | } | ||
| 58 | |||
| 59 | hostState := st.GetHost(host) | ||
| 60 | if !hostState.GitSetup { | ||
| 61 | return fmt.Errorf("git deployment not set up on %s (run 'ship host init --base-domain example.com' first)", host) | ||
| 62 | } | ||
| 63 | |||
| 64 | // Check if app already exists | ||
| 65 | if _, err := st.GetApp(host, name); err == nil { | ||
| 66 | return fmt.Errorf("app %s already exists", name) | ||
| 67 | } | ||
| 68 | |||
| 69 | // Resolve domain | ||
| 70 | if domain == "" && hostState.BaseDomain != "" { | ||
| 71 | domain = name + "." + hostState.BaseDomain | ||
| 72 | } | ||
| 73 | if domain == "" { | ||
| 74 | return fmt.Errorf("--domain required (or configure base domain)") | ||
| 75 | } | ||
| 76 | |||
| 77 | appType := "git-app" | ||
| 78 | if static { | ||
| 79 | appType = "git-static" | ||
| 80 | } | ||
| 81 | |||
| 82 | // Allocate port for apps | ||
| 83 | port := 0 | ||
| 84 | if !static { | ||
| 85 | port = st.AllocatePort(host) | ||
| 86 | } | ||
| 87 | |||
| 88 | fmt.Printf("Initializing %s: %s\n", appType, name) | ||
| 89 | |||
| 90 | client, err := ssh.Connect(host) | ||
| 91 | if err != nil { | ||
| 92 | return fmt.Errorf("error connecting to VPS: %w", err) | ||
| 93 | } | ||
| 94 | defer client.Close() | ||
| 95 | |||
| 96 | // Create bare repo | ||
| 97 | fmt.Println("-> Creating bare git repo...") | ||
| 98 | repo := fmt.Sprintf("/srv/git/%s.git", name) | ||
| 99 | if _, err := client.RunSudo(fmt.Sprintf("sudo -u git git init --bare %s", repo)); err != nil { | ||
| 100 | return fmt.Errorf("error creating bare repo: %w", err) | ||
| 101 | } | ||
| 102 | |||
| 103 | if static { | ||
| 104 | // Create web root | ||
| 105 | fmt.Println("-> Creating web root...") | ||
| 106 | if _, err := client.RunSudo(fmt.Sprintf("mkdir -p /var/www/%s", name)); err != nil { | ||
| 107 | return fmt.Errorf("error creating web root: %w", err) | ||
| 108 | } | ||
| 109 | if _, err := client.RunSudo(fmt.Sprintf("chown git:git /var/www/%s", name)); err != nil { | ||
| 110 | return fmt.Errorf("error setting web root ownership: %w", err) | ||
| 111 | } | ||
| 112 | |||
| 113 | // Write post-receive hook | ||
| 114 | fmt.Println("-> Writing post-receive hook...") | ||
| 115 | hookContent, err := templates.PostReceiveHookStatic(map[string]string{ | ||
| 116 | "Name": name, | ||
| 117 | }) | ||
| 118 | if err != nil { | ||
| 119 | return fmt.Errorf("error generating hook: %w", err) | ||
| 120 | } | ||
| 121 | hookPath := fmt.Sprintf("%s/hooks/post-receive", repo) | ||
| 122 | if err := client.WriteSudoFile(hookPath, hookContent); err != nil { | ||
| 123 | return fmt.Errorf("error writing hook: %w", err) | ||
| 124 | } | ||
| 125 | if _, err := client.RunSudo(fmt.Sprintf("chmod +x %s", hookPath)); err != nil { | ||
| 126 | return fmt.Errorf("error making hook executable: %w", err) | ||
| 127 | } | ||
| 128 | if _, err := client.RunSudo(fmt.Sprintf("chown git:git %s", hookPath)); err != nil { | ||
| 129 | return fmt.Errorf("error setting hook ownership: %w", err) | ||
| 130 | } | ||
| 131 | } else { | ||
| 132 | // Create directories for app | ||
| 133 | fmt.Println("-> Creating app directories...") | ||
| 134 | dirs := []string{ | ||
| 135 | fmt.Sprintf("/var/lib/%s/data", name), | ||
| 136 | fmt.Sprintf("/var/lib/%s/src", name), | ||
| 137 | } | ||
| 138 | for _, dir := range dirs { | ||
| 139 | if _, err := client.RunSudo(fmt.Sprintf("mkdir -p %s", dir)); err != nil { | ||
| 140 | return fmt.Errorf("error creating directory %s: %w", dir, err) | ||
| 141 | } | ||
| 142 | } | ||
| 143 | if _, err := client.RunSudo(fmt.Sprintf("chown -R git:git /var/lib/%s", name)); err != nil { | ||
| 144 | return fmt.Errorf("error setting directory ownership: %w", err) | ||
| 145 | } | ||
| 146 | |||
| 147 | // Create env file | ||
| 148 | fmt.Println("-> Creating environment file...") | ||
| 149 | envContent := fmt.Sprintf("PORT=%d\nDATA_DIR=/data\n", port) | ||
| 150 | envPath := fmt.Sprintf("/etc/ship/env/%s.env", name) | ||
| 151 | if err := client.WriteSudoFile(envPath, envContent); err != nil { | ||
| 152 | return fmt.Errorf("error creating env file: %w", err) | ||
| 153 | } | ||
| 154 | |||
| 155 | // Write post-receive hook | ||
| 156 | fmt.Println("-> Writing post-receive hook...") | ||
| 157 | hookContent, err := templates.PostReceiveHook(map[string]string{ | ||
| 158 | "Name": name, | ||
| 159 | }) | ||
| 160 | if err != nil { | ||
| 161 | return fmt.Errorf("error generating hook: %w", err) | ||
| 162 | } | ||
| 163 | hookPath := fmt.Sprintf("%s/hooks/post-receive", repo) | ||
| 164 | if err := client.WriteSudoFile(hookPath, hookContent); err != nil { | ||
| 165 | return fmt.Errorf("error writing hook: %w", err) | ||
| 166 | } | ||
| 167 | if _, err := client.RunSudo(fmt.Sprintf("chmod +x %s", hookPath)); err != nil { | ||
| 168 | return fmt.Errorf("error making hook executable: %w", err) | ||
| 169 | } | ||
| 170 | if _, err := client.RunSudo(fmt.Sprintf("chown git:git %s", hookPath)); err != nil { | ||
| 171 | return fmt.Errorf("error setting hook ownership: %w", err) | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | // Save state | ||
| 176 | st.AddApp(host, name, &state.App{ | ||
| 177 | Type: appType, | ||
| 178 | Domain: domain, | ||
| 179 | Port: port, | ||
| 180 | Repo: repo, | ||
| 181 | }) | ||
| 182 | if err := st.Save(); err != nil { | ||
| 183 | return fmt.Errorf("error saving state: %w", err) | ||
| 184 | } | ||
| 185 | |||
| 186 | // Generate local .ship/ files | ||
| 187 | fmt.Println("-> Generating local .ship/ config...") | ||
| 188 | if err := os.MkdirAll(".ship", 0755); err != nil { | ||
| 189 | return fmt.Errorf("error creating .ship directory: %w", err) | ||
| 190 | } | ||
| 191 | |||
| 192 | if static { | ||
| 193 | caddyContent, err := templates.DefaultStaticCaddy(map[string]string{ | ||
| 194 | "Domain": domain, | ||
| 195 | "Name": name, | ||
| 196 | }) | ||
| 197 | if err != nil { | ||
| 198 | return fmt.Errorf("error generating Caddyfile: %w", err) | ||
| 199 | } | ||
| 200 | if err := os.WriteFile(filepath.Join(".ship", "Caddyfile"), []byte(caddyContent), 0644); err != nil { | ||
| 201 | return fmt.Errorf("error writing Caddyfile: %w", err) | ||
| 202 | } | ||
| 203 | } else { | ||
| 204 | caddyContent, err := templates.DefaultAppCaddy(map[string]string{ | ||
| 205 | "Domain": domain, | ||
| 206 | "Port": strconv.Itoa(port), | ||
| 207 | }) | ||
| 208 | if err != nil { | ||
| 209 | return fmt.Errorf("error generating Caddyfile: %w", err) | ||
| 210 | } | ||
| 211 | if err := os.WriteFile(filepath.Join(".ship", "Caddyfile"), []byte(caddyContent), 0644); err != nil { | ||
| 212 | return fmt.Errorf("error writing Caddyfile: %w", err) | ||
| 213 | } | ||
| 214 | |||
| 215 | serviceContent, err := templates.DockerService(map[string]string{ | ||
| 216 | "Name": name, | ||
| 217 | "Port": strconv.Itoa(port), | ||
| 218 | }) | ||
| 219 | if err != nil { | ||
| 220 | return fmt.Errorf("error generating service file: %w", err) | ||
| 221 | } | ||
| 222 | if err := os.WriteFile(filepath.Join(".ship", "service"), []byte(serviceContent), 0644); err != nil { | ||
| 223 | return fmt.Errorf("error writing service file: %w", err) | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | // Resolve SSH hostname for git remote URL | ||
| 228 | sshHost := host | ||
| 229 | |||
| 230 | fmt.Printf("\nProject initialized: %s\n", name) | ||
| 231 | fmt.Println("\nGenerated:") | ||
| 232 | fmt.Println(" .ship/Caddyfile — Caddy config (edit to customize routing)") | ||
| 233 | if !static { | ||
| 234 | fmt.Println(" .ship/service — systemd unit (edit to customize resources, ports)") | ||
| 235 | } | ||
| 236 | fmt.Println("\nNext steps:") | ||
| 237 | if static { | ||
| 238 | fmt.Println(" git add .ship/") | ||
| 239 | } else { | ||
| 240 | fmt.Println(" git add .ship/ Dockerfile") | ||
| 241 | } | ||
| 242 | fmt.Println(" git commit -m \"initial deploy\"") | ||
| 243 | fmt.Printf(" git remote add ship git@%s:%s\n", sshHost, repo) | ||
| 244 | fmt.Println(" git push ship main") | ||
| 245 | |||
| 246 | return nil | ||
| 247 | } | ||
