70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"localenv/utils"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/client"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
const (
|
|
MailhogImageName string = "mailhog/mailhog:latest"
|
|
MailhogServiceName string = "mailhog"
|
|
)
|
|
|
|
type Mailhog struct {
|
|
Service
|
|
}
|
|
|
|
func NewMailhog() Mailhog {
|
|
var nx Mailhog
|
|
|
|
nx.name = fmt.Sprintf("localenv-%s", MailhogServiceName)
|
|
nx.spec = nx.getServiceSpec(
|
|
WithIONetwork(),
|
|
WithRestartPolicy(),
|
|
)
|
|
|
|
return nx
|
|
}
|
|
|
|
func (n *Mailhog) getServiceSpec(opts ...ServiceOption) swarm.ServiceSpec {
|
|
spec := swarm.ServiceSpec{
|
|
Annotations: swarm.Annotations{
|
|
Name: n.name,
|
|
},
|
|
TaskTemplate: swarm.TaskSpec{
|
|
ContainerSpec: &swarm.ContainerSpec{
|
|
Image: MailhogImageName,
|
|
Hostname: MailhogServiceName,
|
|
},
|
|
},
|
|
Mode: swarm.ServiceMode{
|
|
Replicated: &swarm.ReplicatedService{
|
|
Replicas: &SwarmServiceReplicas,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(&spec)
|
|
}
|
|
|
|
return spec
|
|
}
|
|
|
|
func (n *Mailhog) Deploy(ctx context.Context, cli *client.Client) error {
|
|
if err := utils.CreateService(ctx, cli, &n.spec); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := utils.CheckServiceHealthWithRetry(ctx, cli, n.name); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|