8 "github.com/hashicorp/consul/api"
11 type supervisor interface {
12 Running(ctx context.Context) (bool, error)
13 Start(ctx context.Context) error
16 func newSupervisor(ctx context.Context, name, cmd string, args ...string) supervisor {
17 if _, err := os.Stat("/run/systemd/system"); err == nil {
31 type supervisedService struct {
37 func (s *supervisedService) Boot(ctx context.Context) error {
42 if _, err := os.Stat(bin); err != nil {
45 sup := newSupervisor(ctx, s.name, bin)
46 if ok, err := sup.Running(ctx); err != nil {
49 if err := sup.Start(ctx); err != nil {
53 if err := consul.Boot(ctx); err != nil {
56 consul, err := api.NewClient(consulCfg)
60 agent := consul.Agent()
61 svcs, err := agent.Services()
65 if svc, ok := svcs[s.name]; ok {
66 log.Printf("%s is registered: %#v", s.name, svc)
69 return agent.ServiceRegister(&api.AgentServiceRegistration{
72 Port: availablePort(),