Skip to content

Commit

Permalink
feat: #17 add support for the release command
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Jul 4, 2024
1 parent 7a9b6fe commit 6f1f083
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
61 changes: 58 additions & 3 deletions internal/app/ptah-agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/pkg/errors"
t "github.com/ptah-sh/ptah-agent/internal/pkg/ptah-client"
"strings"
)

func (e *taskExecutor) createDockerService(ctx context.Context, req *t.CreateServiceReq) (*t.CreateServiceRes, error) {
var res t.CreateServiceRes

spec, err := e.prepareServicePayload(ctx, req.SwarmServiceSpec, req.SecretVars)
spec, err := e.prepareServicePayload(ctx, req.ServicePayload, req.SecretVars)
if err != nil {
return nil, errors.Wrapf(err, "create docker service")
}
Expand All @@ -32,7 +33,7 @@ func (e *taskExecutor) createDockerService(ctx context.Context, req *t.CreateSer
func (e *taskExecutor) updateDockerService(ctx context.Context, req *t.UpdateServiceReq) (*t.UpdateServiceRes, error) {
var res t.UpdateServiceRes

spec, err := e.prepareServicePayload(ctx, req.SwarmServiceSpec, req.SecretVars)
spec, err := e.prepareServicePayload(ctx, req.ServicePayload, req.SecretVars)
if err != nil {
return nil, errors.Wrapf(err, "update docker service")
}
Expand Down Expand Up @@ -65,7 +66,9 @@ func (e *taskExecutor) updateDockerService(ctx context.Context, req *t.UpdateSer
return &res, nil
}

func (e *taskExecutor) prepareServicePayload(ctx context.Context, spec swarm.ServiceSpec, secretVars t.SecretVars) (*swarm.ServiceSpec, error) {
func (e *taskExecutor) prepareServicePayload(ctx context.Context, servicePayload t.ServicePayload, secretVars t.SecretVars) (*swarm.ServiceSpec, error) {
spec := servicePayload.SwarmServiceSpec

if secretVars.ConfigName != "" {
newVars := make(map[string]string)

Expand Down Expand Up @@ -123,6 +126,58 @@ func (e *taskExecutor) prepareServicePayload(ctx context.Context, spec swarm.Ser
config.ConfigID = cfg.ID
}

if servicePayload.ReleaseCommand.Command != "" {
image, _, err := e.docker.ImageInspectWithRaw(ctx, spec.TaskTemplate.ContainerSpec.Image)
if err != nil {
return nil, errors.Wrapf(err, "get image %s", spec.TaskTemplate.ContainerSpec.Image)
}

entrypoint := strings.Join(image.Config.Entrypoint, " ")
command := strings.Join(image.Config.Cmd, " ")

originalEntrypoint := entrypoint + " " + command

script := []string{
"#!/bin/sh",
"set -e",
"echo 'Starting release command'",
servicePayload.ReleaseCommand.Command,
"echo 'Release command finished'",
"echo 'Starting original entrypoint'",
originalEntrypoint,
}

config := swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: servicePayload.ReleaseCommand.ConfigName,
Labels: servicePayload.ReleaseCommand.ConfigLabels,
},
Data: []byte(strings.Join(script, "\n")),
}

config.Labels["kind"] = "entrypoint"

resp, err := e.docker.ConfigCreate(ctx, config)
if err != nil {
return nil, errors.Wrapf(err, "create config %s", servicePayload.ReleaseCommand.ConfigName)
}

spec.TaskTemplate.ContainerSpec.Configs = append(spec.TaskTemplate.ContainerSpec.Configs, &swarm.ConfigReference{
File: &swarm.ConfigReferenceFileTarget{
Name: "/ptah/entrypoint.sh",
UID: "0",
GID: "0",
Mode: 0644,
},
ConfigID: resp.ID,
ConfigName: servicePayload.ReleaseCommand.ConfigName,
})

spec.TaskTemplate.ContainerSpec.Command = []string{
"sh", "/ptah/entrypoint.sh",
}
}

for _, secret := range spec.TaskTemplate.ContainerSpec.Secrets {
secrets, err := e.docker.SecretList(ctx, types.SecretListOptions{
Filters: filters.NewArgs(
Expand Down
7 changes: 6 additions & 1 deletion internal/pkg/ptah-client/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ type CreateSecretRes struct {
}

type ServicePayload struct {
AuthConfigName string
AuthConfigName string
ReleaseCommand struct {
ConfigName string
ConfigLabels map[string]string
Command string
}
SwarmServiceSpec swarm.ServiceSpec
}

Expand Down

0 comments on commit 6f1f083

Please sign in to comment.