Skip to content

Commit

Permalink
feat(deployment): run remote pre-hooks in separate containers
Browse files Browse the repository at this point in the history
  • Loading branch information
yarlson committed Jan 21, 2025
1 parent 6bd17c7 commit 4641d37
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type ServiceHealthCheck struct {
type Container struct {
HealthCheck *ContainerHealthCheck `yaml:"health_check"`
ULimits []ULimit `yaml:"ulimits"`
RunOnce bool `yaml:"run_once"`
}

type ULimit struct {
Expand Down
10 changes: 9 additions & 1 deletion pkg/deployment/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@ func (d *Deployment) startContainer(container string) error {
func (d *Deployment) createContainer(project string, service *config.Service, suffix string) error {
container := containerName(project, service.Name, suffix)

args := []string{"run", "-d", "--name", container, "--network", project, "--network-alias", service.Name + suffix, "--restart", "unless-stopped"}
args := []string{"run"}

if service.Container != nil && service.Container.RunOnce {
args = append(args, "--rm")
} else {
args = append(args, "--detach")
}

args = append(args, []string{"--name", container, "--network", project, "--network-alias", service.Name + suffix, "--restart", "unless-stopped"}...)

for _, value := range service.Env {
args = append(args, "-e", value)
Expand Down
22 changes: 19 additions & 3 deletions pkg/deployment/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (d *Deployment) installService(project string, service *config.Service) err
return fmt.Errorf("install failed for %s: container is unhealthy: %w", container, err)
}

err := d.processPreHooks(service, container)
err := d.processPreHooks(project, service)
if err != nil {
return err
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func (d *Deployment) updateService(project string, service *config.Service) erro
return fmt.Errorf("update failed for %s: new container is unhealthy: %w", container, err)
}

err := d.processPreHooks(service, container+newContainerSuffix)
err := d.processPreHooks(project, service)
if err != nil {
return err
}
Expand All @@ -155,7 +155,7 @@ func (d *Deployment) updateService(project string, service *config.Service) erro
return nil
}

func (d *Deployment) processPreHooks(service *config.Service, container string) error {
func (d *Deployment) processPreHooks(project string, service *config.Service) error {
if service.Hooks == nil || service.Hooks.Pre == nil {
return nil
}
Expand All @@ -165,7 +165,23 @@ func (d *Deployment) processPreHooks(service *config.Service, container string)
return fmt.Errorf("local pre-hook failed: %w", err)
}
}

if service.Hooks.Pre.Remote != "" {
runService := &config.Service{
Name: service.Name,
Image: service.Image,
Volumes: service.Volumes,
Env: service.Env,
Entrypoint: service.Entrypoint,
Command: service.Hooks.Pre.Remote,
Container: &config.Container{RunOnce: true},
}
err := d.createContainer(project, runService, "run")
if err != nil {
return err
}

container := containerName(project, service.Name, "run")
if err := d.runRemoteHook(context.Background(), container, service.Hooks.Pre.Remote); err != nil {
return fmt.Errorf("remote pre-hook failed: %w", err)
}
Expand Down

0 comments on commit 4641d37

Please sign in to comment.