Skip to content

Commit

Permalink
refactor: Improve deployment logging with server-specific output
Browse files Browse the repository at this point in the history
  • Loading branch information
yarlson committed Nov 20, 2024
1 parent cc42aa6 commit ee319d3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
7 changes: 4 additions & 3 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ func deployToServers(cfg *config.Config) error {
if err := deployToServer(cfg.Project.Name, cfg, server); err != nil {
return fmt.Errorf("failed to deploy to server %s: %w", server.Host, err)
}
console.Success(fmt.Sprintf("Successfully deployed to server %s", server.Host))
console.Success(fmt.Sprintf("[%s] Successfully deployed to server", server.Host))
}

return nil
}

func deployToServer(project string, cfg *config.Config, server config.Server) error {
console.Info(fmt.Sprintf("Deploying to server %s...", server.Host))
hostname := server.Host
console.Info(fmt.Sprintf("[%s] Deploying to server...", hostname))

runner, err := connectToServer(server)
if err != nil {
return fmt.Errorf("failed to connect to server: %w", err)
return fmt.Errorf("failed to connect to server %s: %w", hostname, err)
}
defer runner.Close()

Expand Down
49 changes: 29 additions & 20 deletions pkg/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const (
)

type Runner interface {
RunCommand(ctx context.Context, command string, args ...string) (io.ReadCloser, error)
CopyFile(ctx context.Context, from, to string) error
GetHost() string
RunCommand(ctx context.Context, command string, args ...string) (io.ReadCloser, error)
}

type ImageSyncer interface {
Expand Down Expand Up @@ -99,22 +100,24 @@ func (d *Deployment) Deploy(ctx context.Context, project string, cfg *config.Con
}

func (d *Deployment) createVolumes(ctx context.Context, project string, volumes []string, events chan<- console.Event) error {
hostname := d.runner.GetHost()

for _, volume := range volumes {
select {
case <-ctx.Done():
return ctx.Err()
default:
events <- console.Event{
Type: console.EventTypeStart,
Message: fmt.Sprintf("Creating volume %s", volume),
Message: fmt.Sprintf("[%s] Creating volume %s", hostname, volume),
Name: "volumes",
}
if err := d.createVolume(ctx, project, volume); err != nil {
return fmt.Errorf("failed to create volume %s: %w", volume, err)
return fmt.Errorf("failed to create volume %s on host %s: %w", volume, hostname, err)
}
events <- console.Event{
Type: console.EventTypeFinish,
Message: fmt.Sprintf("Volume %s created", volume),
Message: fmt.Sprintf("[%s] Volume %s created", hostname, volume),
Name: "volumes",
}
}
Expand All @@ -125,6 +128,8 @@ func (d *Deployment) createVolumes(ctx context.Context, project string, volumes

// deployDependencies deploys all dependencies concurrently.
func (d *Deployment) deployDependencies(ctx context.Context, project string, dependencies []config.Dependency, events chan<- console.Event) error {
hostname := d.runner.GetHost()

var wg sync.WaitGroup
errChan := make(chan error, len(dependencies))

Expand All @@ -142,23 +147,23 @@ func (d *Deployment) deployDependencies(ctx context.Context, project string, dep

events <- console.Event{
Type: console.EventTypeStart,
Message: fmt.Sprintf("Deploying dependency %s", depName),
Message: fmt.Sprintf("[%s] Deploying dependency %s", hostname, depName),
Name: depName,
}

if err := d.startDependency(project, &dep); err != nil {
events <- console.Event{
Type: console.EventTypeError,
Message: fmt.Sprintf("Failed to deploy dependency %s: %v", depName, err),
Message: fmt.Sprintf("[%s] Failed to deploy dependency %s: %v", hostname, depName, err),
Name: depName,
}
errChan <- fmt.Errorf("failed to deploy dependency %s: %w", depName, err)
errChan <- fmt.Errorf("failed to deploy dependency %s on host %s: %w", depName, hostname, err)
return
}

events <- console.Event{
Type: console.EventTypeFinish,
Message: fmt.Sprintf("Dependency %s deployed", depName),
Message: fmt.Sprintf("[%s] Dependency %s deployed", hostname, depName),
Name: depName,
}
}
Expand All @@ -182,6 +187,8 @@ func (d *Deployment) deployDependencies(ctx context.Context, project string, dep

// deployServices deploys all services concurrently.
func (d *Deployment) deployServices(ctx context.Context, project string, services []config.Service, events chan<- console.Event) error {
hostname := d.runner.GetHost()

var wg sync.WaitGroup
errChan := make(chan error, len(services))

Expand All @@ -199,14 +206,14 @@ func (d *Deployment) deployServices(ctx context.Context, project string, service

events <- console.Event{
Type: console.EventTypeStart,
Message: fmt.Sprintf("Deploying service %s", serviceName),
Message: fmt.Sprintf("[%s] Deploying service %s", hostname, serviceName),
Name: serviceName,
}

if err := d.deployService(project, &service); err != nil {
events <- console.Event{
Type: console.EventTypeError,
Message: fmt.Sprintf("Failed to deploy service %s: %v", serviceName, err),
Message: fmt.Sprintf("[%s] Failed to deploy service %s: %v", hostname, serviceName, err),
Name: serviceName,
}
errChan <- fmt.Errorf("failed to deploy service %s: %w", serviceName, err)
Expand All @@ -215,7 +222,7 @@ func (d *Deployment) deployServices(ctx context.Context, project string, service

events <- console.Event{
Type: console.EventTypeFinish,
Message: fmt.Sprintf("Service %s deployed", serviceName),
Message: fmt.Sprintf("[%s] Service %s successfully deployed", hostname, serviceName),
Name: serviceName,
}
}
Expand All @@ -238,6 +245,8 @@ func (d *Deployment) deployServices(ctx context.Context, project string, service
}

func (d *Deployment) startProxy(ctx context.Context, project string, cfg *config.Config, events chan<- console.Event) error {
hostname := d.runner.GetHost()

projectPath, err := d.prepareProjectFolder(project)
if err != nil {
return fmt.Errorf("failed to prepare project folder: %w", err)
Expand Down Expand Up @@ -279,15 +288,15 @@ func (d *Deployment) startProxy(ctx context.Context, project string, cfg *config
default:
events <- console.Event{
Type: console.EventTypeStart,
Message: fmt.Sprintf("Deploying service %s", service.Name),
Message: fmt.Sprintf("[%s] Deploying service %s", hostname, service.Name),
Name: "proxy",
}
if err := d.deployService(project, service); err != nil {
return fmt.Errorf("failed to deploy service %s: %w", service.Name, err)
return fmt.Errorf("failed to deploy service %s on host %s: %w", service.Name, hostname, err)
}
events <- console.Event{
Type: console.EventTypeFinish,
Message: fmt.Sprintf("Service %s deployed", service.Name),
Message: fmt.Sprintf("[%s] Service %s deployed", hostname, service.Name),
Name: "proxy",
}
}
Expand All @@ -298,15 +307,15 @@ func (d *Deployment) startProxy(ctx context.Context, project string, cfg *config
default:
events <- console.Event{
Type: console.EventTypeStart,
Message: "Reloading Nginx config",
Message: fmt.Sprintf("[%s] Reloading Nginx config", hostname),
Name: "nginx",
}
if err := d.reloadNginxConfig(ctx); err != nil {
return fmt.Errorf("failed to reload nginx config: %w", err)
return fmt.Errorf("failed to reload nginx config on host %s: %w", hostname, err)
}
events <- console.Event{
Type: console.EventTypeFinish,
Message: "Nginx config reloaded",
Message: fmt.Sprintf("[%s] Nginx config reloaded", hostname),
Name: "nginx",
}
}
Expand All @@ -317,15 +326,15 @@ func (d *Deployment) startProxy(ctx context.Context, project string, cfg *config
default:
events <- console.Event{
Type: console.EventTypeStart,
Message: "Deploying cert renewer",
Message: fmt.Sprintf("[%s] Deploying cert renewer", hostname),
Name: "certrenewer",
}
if err := d.deployCertRenewer(project, cfg); err != nil {
return fmt.Errorf("failed to deploy certrenewer service: %w", err)
return fmt.Errorf("failed to deploy certrenewer service on host %s: %w", hostname, err)
}
events <- console.Event{
Type: console.EventTypeFinish,
Message: "Cert renewer deployed",
Message: fmt.Sprintf("[%s] Cert renewer deployed", hostname),
Name: "certrenewer",
}
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/runner/remote/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func (c *Runner) RunCommand(ctx context.Context, command string, args ...string)
return readCloser, nil
}

func (c *Runner) GetHost() string {
fullAddr := c.sshClient.RemoteAddr().String()
addr := strings.Split(fullAddr, ":")
return addr[0]
}

// sshEscapeArg properly escapes a command-line argument for SSH
func sshEscapeArg(arg string) string {
return "'" + strings.Replace(arg, "'", "'\\''", -1) + "'"
Expand Down

0 comments on commit ee319d3

Please sign in to comment.