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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"regexp"
"strings"
"time"
"github.com/bdw/ship/internal/detect"
"github.com/bdw/ship/internal/output"
"github.com/bdw/ship/internal/state"
)
// deployV2 implements the new agent-first deploy interface.
// Usage: ship [PATH] [FLAGS]
// PATH defaults to "." if not provided.
func deployV2(path string, opts deployV2Options) {
start := time.Now()
// Validate name if provided
if opts.Name != "" {
if err := validateName(opts.Name); err != nil {
output.PrintAndExit(err)
}
}
// Parse TTL if provided
var ttlDuration time.Duration
if opts.TTL != "" {
var err error
ttlDuration, err = parseTTL(opts.TTL)
if err != nil {
output.PrintAndExit(output.Err(output.ErrInvalidTTL, err.Error()))
}
}
// Get host configuration
st, err := state.Load()
if err != nil {
output.PrintAndExit(output.Err(output.ErrHostNotConfigured, "failed to load state: "+err.Error()))
}
hostName := opts.Host
if hostName == "" {
hostName = st.DefaultHost
}
if hostName == "" {
output.PrintAndExit(output.Err(output.ErrHostNotConfigured, "no host specified and no default host configured. Run: ship host init"))
}
hostConfig := st.GetHost(hostName)
if hostConfig.BaseDomain == "" {
output.PrintAndExit(output.Err(output.ErrHostNotConfigured, fmt.Sprintf("host %q has no base domain configured. Run: ship host init", hostName)))
}
// Auto-detect project type
result := detect.Detect(path)
if result.Error != nil {
output.PrintAndExit(result.Error)
}
// Generate name if not provided
name := opts.Name
if name == "" {
name = generateName()
}
// Build URL
url := fmt.Sprintf("https://%s.%s", name, hostConfig.BaseDomain)
// Build deploy context
ctx := &deployContext{
SSHHost: hostName,
HostConfig: hostConfig,
Name: name,
Path: result.Path,
URL: url,
Opts: opts,
}
// Deploy based on type
var deployErr *output.ErrorResponse
switch result.Type {
case detect.TypeStatic:
deployErr = deployStaticV2(ctx)
case detect.TypeDocker:
deployErr = deployDockerV2(ctx)
case detect.TypeBinary:
deployErr = deployBinaryV2(ctx)
}
if deployErr != nil {
deployErr.Name = name
deployErr.URL = url
output.PrintAndExit(deployErr)
}
// Set TTL if specified
if ttlDuration > 0 {
if err := setTTLV2(ctx, ttlDuration); err != nil {
// Non-fatal, deploy succeeded
// TODO: log warning
}
}
// Health check
var healthResult *output.HealthResult
if opts.Health != "" || result.Type == detect.TypeStatic {
endpoint := opts.Health
if endpoint == "" {
endpoint = "/"
}
healthResult, deployErr = runHealthCheck(url, endpoint)
if deployErr != nil {
deployErr.Name = name
deployErr.URL = url
output.PrintAndExit(deployErr)
}
}
// Build response
resp := &output.DeployResponse{
Status: "ok",
Name: name,
URL: url,
Type: string(result.Type),
TookMs: time.Since(start).Milliseconds(),
Health: healthResult,
}
if ttlDuration > 0 {
resp.Expires = time.Now().Add(ttlDuration).UTC().Format(time.RFC3339)
}
output.PrintAndExit(resp)
}
type deployV2Options struct {
Name string
Host string
Health string
TTL string
Env []string
EnvFile string
Pretty bool
}
// deployContext holds all info needed for a deploy
type deployContext struct {
SSHHost string // SSH connection string (config alias or user@host)
HostConfig *state.Host // Host configuration
Name string // Deploy name
Path string // Local path to deploy
URL string // Full URL after deploy
Opts deployV2Options
}
// validateName checks if name matches allowed pattern
func validateName(name string) *output.ErrorResponse {
// Must be lowercase alphanumeric with hyphens, 1-63 chars
pattern := regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$|^[a-z0-9]$`)
if !pattern.MatchString(name) {
return output.Err(output.ErrInvalidName,
"name must be lowercase alphanumeric with hyphens, 1-63 characters")
}
return nil
}
// generateName creates a random deploy name
func generateName() string {
bytes := make([]byte, 3)
rand.Read(bytes)
return "ship-" + hex.EncodeToString(bytes)
}
// parseTTL converts duration strings like "1h", "7d" to time.Duration
func parseTTL(s string) (time.Duration, error) {
s = strings.TrimSpace(s)
if s == "" {
return 0, nil
}
// Handle days specially (not supported by time.ParseDuration)
if strings.HasSuffix(s, "d") {
days := strings.TrimSuffix(s, "d")
var d int
_, err := fmt.Sscanf(days, "%d", &d)
if err != nil {
return 0, fmt.Errorf("invalid TTL: %s", s)
}
return time.Duration(d) * 24 * time.Hour, nil
}
d, err := time.ParseDuration(s)
if err != nil {
return 0, fmt.Errorf("invalid TTL: %s", s)
}
return d, nil
}
// Deploy implementations are in deploy_impl_v2.go
|