1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/bdw/deploy/internal/ssh"
"github.com/bdw/deploy/internal/state"
)
func runInit(args []string) {
fs := flag.NewFlagSet("init", flag.ExitOnError)
host := fs.String("host", "", "VPS host (SSH config alias or user@host)")
fs.Parse(args)
// Load state
st, err := state.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading state: %v\n", err)
os.Exit(1)
}
// Get host from flag or state default
if *host == "" {
*host = st.GetDefaultHost()
}
if *host == "" {
fmt.Fprintf(os.Stderr, "Error: --host is required\n")
fs.Usage()
os.Exit(1)
}
fmt.Printf("Initializing VPS: %s\n", *host)
// Connect to VPS
client, err := ssh.Connect(*host)
if err != nil {
fmt.Fprintf(os.Stderr, "Error connecting to VPS: %v\n", err)
os.Exit(1)
}
defer client.Close()
// Detect OS
fmt.Println("→ Detecting OS...")
osRelease, err := client.Run("cat /etc/os-release")
if err != nil {
fmt.Fprintf(os.Stderr, "Error detecting OS: %v\n", err)
os.Exit(1)
}
if !strings.Contains(osRelease, "Ubuntu") && !strings.Contains(osRelease, "Debian") {
fmt.Fprintf(os.Stderr, "Error: Unsupported OS (only Ubuntu and Debian are supported)\n")
os.Exit(1)
}
fmt.Println(" ✓ Detected Ubuntu/Debian")
// Check if Caddy is already installed
fmt.Println("→ Checking for Caddy...")
_, err = client.Run("which caddy")
if err == nil {
fmt.Println(" ✓ Caddy already installed")
} else {
// Install Caddy
fmt.Println(" Installing Caddy...")
installCaddy(client)
fmt.Println(" ✓ Caddy installed")
}
// Create Caddyfile
fmt.Println("→ Configuring Caddy...")
caddyfile := `{
email admin@example.com
}
import /etc/caddy/sites-enabled/*
`
if err := client.WriteSudoFile("/etc/caddy/Caddyfile", caddyfile); err != nil {
fmt.Fprintf(os.Stderr, "Error creating Caddyfile: %v\n", err)
os.Exit(1)
}
fmt.Println(" ✓ Caddyfile created")
// Create directories
fmt.Println("→ Creating directories...")
if _, err := client.RunSudo("mkdir -p /etc/deploy/env"); err != nil {
fmt.Fprintf(os.Stderr, "Error creating /etc/deploy/env: %v\n", err)
os.Exit(1)
}
if _, err := client.RunSudo("mkdir -p /etc/caddy/sites-enabled"); err != nil {
fmt.Fprintf(os.Stderr, "Error creating /etc/caddy/sites-enabled: %v\n", err)
os.Exit(1)
}
fmt.Println(" ✓ Directories created")
// Enable and start Caddy
fmt.Println("→ Starting Caddy...")
if _, err := client.RunSudo("systemctl enable caddy"); err != nil {
fmt.Fprintf(os.Stderr, "Error enabling Caddy: %v\n", err)
os.Exit(1)
}
if _, err := client.RunSudo("systemctl restart caddy"); err != nil {
fmt.Fprintf(os.Stderr, "Error starting Caddy: %v\n", err)
os.Exit(1)
}
fmt.Println(" ✓ Caddy started")
// Verify Caddy is running
fmt.Println("→ Verifying installation...")
output, err := client.RunSudo("systemctl is-active caddy")
if err != nil || strings.TrimSpace(output) != "active" {
fmt.Fprintf(os.Stderr, "Warning: Caddy may not be running properly\n")
} else {
fmt.Println(" ✓ Caddy is active")
}
// Update state
st.GetHost(*host) // Ensure host exists in state
if st.GetDefaultHost() == "" {
st.SetDefaultHost(*host)
fmt.Printf(" Set %s as default host\n", *host)
}
if err := st.Save(); err != nil {
fmt.Fprintf(os.Stderr, "Error saving state: %v\n", err)
os.Exit(1)
}
fmt.Println("\n✓ VPS initialized successfully!")
fmt.Println("\nNext steps:")
fmt.Println(" 1. Deploy a Go app:")
fmt.Printf(" deploy deploy --host %s --binary ./myapp --domain api.example.com\n", *host)
fmt.Println(" 2. Deploy a static site:")
fmt.Printf(" deploy deploy --host %s --static --dir ./dist --domain example.com\n", *host)
}
func installCaddy(client *ssh.Client) {
commands := []string{
"apt-get update",
"apt-get install -y debian-keyring debian-archive-keyring apt-transport-https curl",
"curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg",
"curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list",
"apt-get update",
"apt-get install -y caddy",
}
for _, cmd := range commands {
if _, err := client.RunSudo(cmd); err != nil {
fmt.Fprintf(os.Stderr, "Error running: %s\nError: %v\n", cmd, err)
os.Exit(1)
}
}
}
|