summaryrefslogtreecommitdiffstats
path: root/cmd/ship/deploy_cmd.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/ship/deploy_cmd.go')
-rw-r--r--cmd/ship/deploy_cmd.go141
1 files changed, 0 insertions, 141 deletions
diff --git a/cmd/ship/deploy_cmd.go b/cmd/ship/deploy_cmd.go
deleted file mode 100644
index ba45c4f..0000000
--- a/cmd/ship/deploy_cmd.go
+++ /dev/null
@@ -1,141 +0,0 @@
1package main
2
3import (
4 "fmt"
5
6 "github.com/bdw/ship/internal/ssh"
7 "github.com/bdw/ship/internal/state"
8 "github.com/spf13/cobra"
9)
10
11var deployGitCmd = &cobra.Command{
12 Use: "deploy <name>",
13 Short: "Manually rebuild and deploy a git-deployed app",
14 Long: `Trigger a manual rebuild from the latest code in the git repo.
15
16This runs the same steps as the post-receive hook: checkout code,
17install .ship/ configs, docker build (for apps), and restart.
18
19Examples:
20 ship deploy myapp`,
21 Args: cobra.ExactArgs(1),
22 RunE: runDeployGit,
23}
24
25func runDeployGit(cmd *cobra.Command, args []string) error {
26 name := args[0]
27 if err := validateName(name); err != nil {
28 return err
29 }
30
31 st, err := state.Load()
32 if err != nil {
33 return fmt.Errorf("error loading state: %w", err)
34 }
35
36 host := hostFlag
37 if host == "" {
38 host = st.GetDefaultHost()
39 }
40 if host == "" {
41 return fmt.Errorf("--host is required")
42 }
43
44 app, err := st.GetApp(host, name)
45 if err != nil {
46 return err
47 }
48
49 if app.Type != "git-app" && app.Type != "git-static" {
50 return fmt.Errorf("%s is not a git-deployed app (type: %s)", name, app.Type)
51 }
52
53 fmt.Printf("Deploying %s...\n", name)
54
55 client, err := ssh.Connect(host)
56 if err != nil {
57 return fmt.Errorf("error connecting to VPS: %w", err)
58 }
59 defer client.Close()
60
61 if app.Type == "git-app" {
62 if err := deployGitApp(client, name); err != nil {
63 return err
64 }
65 } else {
66 if err := deployGitStatic(client, name); err != nil {
67 return err
68 }
69 }
70
71 fmt.Println("\nDeploy complete!")
72 return nil
73}
74
75func deployGitApp(client *ssh.Client, name string) error {
76 repo := fmt.Sprintf("/srv/git/%s.git", name)
77 src := fmt.Sprintf("/var/lib/%s/src", name)
78
79 fmt.Println("-> Checking out code...")
80 if _, err := client.RunSudo(fmt.Sprintf("git --work-tree=%s --git-dir=%s checkout -f main", src, repo)); err != nil {
81 return fmt.Errorf("error checking out code: %w", err)
82 }
83
84 // Install deployment config from repo
85 serviceSrc := fmt.Sprintf("%s/.ship/service", src)
86 serviceDst := fmt.Sprintf("/etc/systemd/system/%s.service", name)
87 fmt.Println("-> Installing systemd unit...")
88 if _, err := client.RunSudo(fmt.Sprintf("cp %s %s", serviceSrc, serviceDst)); err != nil {
89 fmt.Printf(" Warning: no .ship/service found, skipping\n")
90 } else {
91 if _, err := client.RunSudo("systemctl daemon-reload"); err != nil {
92 return fmt.Errorf("error reloading systemd: %w", err)
93 }
94 }
95
96 caddySrc := fmt.Sprintf("%s/.ship/Caddyfile", src)
97 caddyDst := fmt.Sprintf("/etc/caddy/sites-enabled/%s.caddy", name)
98 fmt.Println("-> Installing Caddy config...")
99 if _, err := client.RunSudo(fmt.Sprintf("cp %s %s", caddySrc, caddyDst)); err != nil {
100 fmt.Printf(" Warning: no .ship/Caddyfile found, skipping\n")
101 } else {
102 if _, err := client.RunSudo("systemctl reload caddy"); err != nil {
103 return fmt.Errorf("error reloading Caddy: %w", err)
104 }
105 }
106
107 fmt.Println("-> Building Docker image...")
108 if err := client.RunSudoStream(fmt.Sprintf("docker build -t %s:latest %s", name, src)); err != nil {
109 return fmt.Errorf("error building Docker image: %w", err)
110 }
111
112 fmt.Println("-> Restarting service...")
113 if _, err := client.RunSudo(fmt.Sprintf("systemctl restart %s", name)); err != nil {
114 return fmt.Errorf("error restarting service: %w", err)
115 }
116
117 return nil
118}
119
120func deployGitStatic(client *ssh.Client, name string) error {
121 repo := fmt.Sprintf("/srv/git/%s.git", name)
122 webroot := fmt.Sprintf("/var/www/%s", name)
123
124 fmt.Println("-> Deploying static site...")
125 if _, err := client.RunSudo(fmt.Sprintf("git --work-tree=%s --git-dir=%s checkout -f main", webroot, repo)); err != nil {
126 return fmt.Errorf("error checking out code: %w", err)
127 }
128
129 caddySrc := fmt.Sprintf("%s/.ship/Caddyfile", webroot)
130 caddyDst := fmt.Sprintf("/etc/caddy/sites-enabled/%s.caddy", name)
131 fmt.Println("-> Installing Caddy config...")
132 if _, err := client.RunSudo(fmt.Sprintf("cp %s %s", caddySrc, caddyDst)); err != nil {
133 fmt.Printf(" Warning: no .ship/Caddyfile found, skipping\n")
134 } else {
135 if _, err := client.RunSudo("systemctl reload caddy"); err != nil {
136 return fmt.Errorf("error reloading Caddy: %w", err)
137 }
138 }
139
140 return nil
141}