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