|
| 1 | +package notifications |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/docker/docker/api/types" |
| 7 | + "github.com/docker/docker/api/types/container" |
| 8 | + "github.com/docker/docker/api/types/mount" |
| 9 | + "github.com/docker/docker/api/types/network" |
| 10 | + "github.com/docker/docker/client" |
| 11 | + "github.com/icinga/icinga-testing/services" |
| 12 | + "github.com/icinga/icinga-testing/utils" |
| 13 | + "go.uber.org/zap" |
| 14 | + "sync" |
| 15 | + "sync/atomic" |
| 16 | +) |
| 17 | + |
| 18 | +type dockerCreator struct { |
| 19 | + logger *zap.Logger |
| 20 | + dockerClient *client.Client |
| 21 | + dockerNetworkId string |
| 22 | + containerNamePrefix string |
| 23 | + sharedDirPath string |
| 24 | + containerCounter uint32 |
| 25 | + |
| 26 | + runningMutex sync.Mutex |
| 27 | + running map[*dockerInstance]struct{} |
| 28 | +} |
| 29 | + |
| 30 | +var _ Creator = (*dockerCreator)(nil) |
| 31 | + |
| 32 | +func NewDockerCreator( |
| 33 | + logger *zap.Logger, |
| 34 | + dockerClient *client.Client, |
| 35 | + containerNamePrefix string, |
| 36 | + dockerNetworkId string, |
| 37 | + sharedDirPath string, |
| 38 | +) Creator { |
| 39 | + return &dockerCreator{ |
| 40 | + logger: logger.With(zap.Bool("icinga_notifications", true)), |
| 41 | + dockerClient: dockerClient, |
| 42 | + dockerNetworkId: dockerNetworkId, |
| 43 | + containerNamePrefix: containerNamePrefix, |
| 44 | + sharedDirPath: sharedDirPath, |
| 45 | + running: make(map[*dockerInstance]struct{}), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (i *dockerCreator) CreateIcingaNotifications( |
| 50 | + rdb services.RelationalDatabase, |
| 51 | + options ...services.IcingaNotificationsOption, |
| 52 | +) services.IcingaNotificationsBase { |
| 53 | + inst := &dockerInstance{ |
| 54 | + info: info{ |
| 55 | + port: defaultPort, |
| 56 | + rdb: rdb, |
| 57 | + }, |
| 58 | + logger: i.logger, |
| 59 | + icingaNotificationsDockerBinary: i, |
| 60 | + } |
| 61 | + |
| 62 | + idb := &services.IcingaNotifications{IcingaNotificationsBase: inst} |
| 63 | + services.WithIcingaNotificationsDefaultsEnvConfig(inst.info.rdb, ":"+defaultPort)(idb) |
| 64 | + for _, option := range options { |
| 65 | + option(idb) |
| 66 | + } |
| 67 | + |
| 68 | + containerName := fmt.Sprintf("%s-%d", i.containerNamePrefix, atomic.AddUint32(&i.containerCounter, 1)) |
| 69 | + inst.logger = inst.logger.With(zap.String("container-name", containerName)) |
| 70 | + networkName, err := utils.DockerNetworkName(context.Background(), i.dockerClient, i.dockerNetworkId) |
| 71 | + if err != nil { |
| 72 | + panic(err) |
| 73 | + } |
| 74 | + |
| 75 | + dockerImage := utils.GetEnvDefault("ICINGA_TESTING_NOTIFICATIONS_IMAGE", "icinga-notifications:latest") |
| 76 | + err = utils.DockerImagePull(context.Background(), inst.logger, i.dockerClient, dockerImage, false) |
| 77 | + if err != nil { |
| 78 | + panic(err) |
| 79 | + } |
| 80 | + |
| 81 | + cont, err := i.dockerClient.ContainerCreate(context.Background(), &container.Config{ |
| 82 | + Image: dockerImage, |
| 83 | + Env: idb.ConfEnviron(), |
| 84 | + }, &container.HostConfig{ |
| 85 | + Mounts: []mount.Mount{{ |
| 86 | + Type: mount.TypeBind, |
| 87 | + Source: i.sharedDirPath, |
| 88 | + Target: "/shared", |
| 89 | + ReadOnly: true, |
| 90 | + }}, |
| 91 | + }, &network.NetworkingConfig{ |
| 92 | + EndpointsConfig: map[string]*network.EndpointSettings{ |
| 93 | + networkName: { |
| 94 | + NetworkID: i.dockerNetworkId, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, nil, containerName) |
| 98 | + if err != nil { |
| 99 | + inst.logger.Fatal("failed to create icinga-notifications container", zap.Error(err)) |
| 100 | + } |
| 101 | + inst.containerId = cont.ID |
| 102 | + inst.logger = inst.logger.With(zap.String("container-id", cont.ID)) |
| 103 | + inst.logger.Debug("created container") |
| 104 | + |
| 105 | + err = utils.ForwardDockerContainerOutput(context.Background(), i.dockerClient, cont.ID, |
| 106 | + false, utils.NewLineWriter(func(line []byte) { |
| 107 | + inst.logger.Debug("container output", |
| 108 | + zap.ByteString("line", line)) |
| 109 | + })) |
| 110 | + if err != nil { |
| 111 | + inst.logger.Fatal("failed to attach to container output", zap.Error(err)) |
| 112 | + } |
| 113 | + |
| 114 | + err = i.dockerClient.ContainerStart(context.Background(), cont.ID, types.ContainerStartOptions{}) |
| 115 | + if err != nil { |
| 116 | + inst.logger.Fatal("failed to start container", zap.Error(err)) |
| 117 | + } |
| 118 | + inst.logger.Debug("started container") |
| 119 | + |
| 120 | + inst.info.host = utils.MustString(utils.DockerContainerAddress(context.Background(), i.dockerClient, cont.ID)) |
| 121 | + |
| 122 | + i.runningMutex.Lock() |
| 123 | + i.running[inst] = struct{}{} |
| 124 | + i.runningMutex.Unlock() |
| 125 | + |
| 126 | + return inst |
| 127 | +} |
| 128 | + |
| 129 | +func (i *dockerCreator) Cleanup() { |
| 130 | + i.runningMutex.Lock() |
| 131 | + instances := make([]*dockerInstance, 0, len(i.running)) |
| 132 | + for inst := range i.running { |
| 133 | + instances = append(instances, inst) |
| 134 | + } |
| 135 | + i.runningMutex.Unlock() |
| 136 | + |
| 137 | + for _, inst := range instances { |
| 138 | + inst.Cleanup() |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +type dockerInstance struct { |
| 143 | + info |
| 144 | + icingaNotificationsDockerBinary *dockerCreator |
| 145 | + logger *zap.Logger |
| 146 | + containerId string |
| 147 | +} |
| 148 | + |
| 149 | +var _ services.IcingaNotificationsBase = (*dockerInstance)(nil) |
| 150 | + |
| 151 | +func (i *dockerInstance) Cleanup() { |
| 152 | + i.icingaNotificationsDockerBinary.runningMutex.Lock() |
| 153 | + delete(i.icingaNotificationsDockerBinary.running, i) |
| 154 | + i.icingaNotificationsDockerBinary.runningMutex.Unlock() |
| 155 | + |
| 156 | + err := i.icingaNotificationsDockerBinary.dockerClient.ContainerRemove(context.Background(), i.containerId, types.ContainerRemoveOptions{ |
| 157 | + Force: true, |
| 158 | + RemoveVolumes: true, |
| 159 | + }) |
| 160 | + if err != nil { |
| 161 | + panic(err) |
| 162 | + } |
| 163 | + i.logger.Debug("removed container") |
| 164 | +} |
0 commit comments