-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathdeploy.go
More file actions
78 lines (63 loc) · 1.8 KB
/
deploy.go
File metadata and controls
78 lines (63 loc) · 1.8 KB
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
package command
import (
"context"
"fmt"
"log/slog"
"github.com/spf13/cobra"
"github.com/basecamp/once/internal/docker"
)
type deployCommand struct {
cmd *cobra.Command
host string
}
func newDeployCommand() *deployCommand {
d := &deployCommand{}
d.cmd = &cobra.Command{
Use: "deploy <image>",
Short: "Deploy an application",
Args: cobra.ExactArgs(1),
RunE: WithNamespace(d.run),
}
d.cmd.Flags().StringVar(&d.host, "host", "", "hostname for the application (defaults to <name>.localhost)")
return d
}
// Private
func (d *deployCommand) run(ctx context.Context, ns *docker.Namespace, cmd *cobra.Command, args []string) error {
imageRef := args[0]
if err := ns.Setup(ctx); err != nil {
return fmt.Errorf("%w: %w", docker.ErrSetupFailed, err)
}
baseName := docker.NameFromImageRef(imageRef)
name, err := ns.UniqueName(baseName)
if err != nil {
return fmt.Errorf("generating app name: %w", err)
}
host := d.host
if host == "" {
host = baseName + ".localhost"
}
if ns.HostInUse(host) {
return docker.ErrHostnameInUse
}
app := docker.NewApplication(ns, docker.ApplicationSettings{
Name: name,
Image: imageRef,
Host: host,
AutoUpdate: true,
})
if err := app.Deploy(ctx, printDeployProgress); err != nil {
if cleanupErr := app.Destroy(context.Background(), true); cleanupErr != nil {
slog.Error("Failed to clean up after deploy failure", "app", name, "error", cleanupErr)
}
return fmt.Errorf("%w: %w", docker.ErrDeployFailed, err)
}
fmt.Println("Verifying...")
if err := app.VerifyHTTP(ctx); err != nil {
if cleanupErr := app.Destroy(context.Background(), true); cleanupErr != nil {
slog.Error("Failed to clean up after verification failure", "app", name, "error", cleanupErr)
}
return err
}
fmt.Printf("Deployed %s\n", name)
return nil
}