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