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