|  | 
|  | 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 | +	err = configFile.Chmod(0666) // defaults to 0600, might result in being unreadable from container UID 1000 | 
|  | 68 | +	if err != nil { | 
|  | 69 | +		panic(err) | 
|  | 70 | +	} | 
|  | 71 | +	idb := &services.IcingaNotifications{IcingaNotificationsBase: inst} | 
|  | 72 | +	for _, option := range options { | 
|  | 73 | +		option(idb) | 
|  | 74 | +	} | 
|  | 75 | +	if err = idb.WriteConfig(configFile); err != nil { | 
|  | 76 | +		panic(err) | 
|  | 77 | +	} | 
|  | 78 | +	inst.configFileName = configFile.Name() | 
|  | 79 | +	err = configFile.Close() | 
|  | 80 | +	if err != nil { | 
|  | 81 | +		panic(err) | 
|  | 82 | +	} | 
|  | 83 | + | 
|  | 84 | +	containerName := fmt.Sprintf("%s-%d", i.containerNamePrefix, atomic.AddUint32(&i.containerCounter, 1)) | 
|  | 85 | +	inst.logger = inst.logger.With(zap.String("container-name", containerName)) | 
|  | 86 | +	networkName, err := utils.DockerNetworkName(context.Background(), i.dockerClient, i.dockerNetworkId) | 
|  | 87 | +	if err != nil { | 
|  | 88 | +		panic(err) | 
|  | 89 | +	} | 
|  | 90 | + | 
|  | 91 | +	dockerImage := utils.GetEnvDefault("ICINGA_TESTING_NOTIFICATIONS_IMAGE", "icinga-notifications:latest") | 
|  | 92 | +	err = utils.DockerImagePull(context.Background(), inst.logger, i.dockerClient, dockerImage, false) | 
|  | 93 | +	if err != nil { | 
|  | 94 | +		panic(err) | 
|  | 95 | +	} | 
|  | 96 | + | 
|  | 97 | +	cont, err := i.dockerClient.ContainerCreate(context.Background(), &container.Config{ | 
|  | 98 | +		Image: dockerImage, | 
|  | 99 | +	}, &container.HostConfig{ | 
|  | 100 | +		Mounts: []mount.Mount{{ | 
|  | 101 | +			Type:     mount.TypeBind, | 
|  | 102 | +			Source:   inst.configFileName, | 
|  | 103 | +			Target:   "/etc/icinga-notifications/config.yml", | 
|  | 104 | +			ReadOnly: true, | 
|  | 105 | +		}, { | 
|  | 106 | +			Type:     mount.TypeBind, | 
|  | 107 | +			Source:   i.sharedDirPath, | 
|  | 108 | +			Target:   "/shared", | 
|  | 109 | +			ReadOnly: true, | 
|  | 110 | +		}}, | 
|  | 111 | +	}, &network.NetworkingConfig{ | 
|  | 112 | +		EndpointsConfig: map[string]*network.EndpointSettings{ | 
|  | 113 | +			networkName: { | 
|  | 114 | +				NetworkID: i.dockerNetworkId, | 
|  | 115 | +			}, | 
|  | 116 | +		}, | 
|  | 117 | +	}, nil, containerName) | 
|  | 118 | +	if err != nil { | 
|  | 119 | +		inst.logger.Fatal("failed to create icinga-notifications container", zap.Error(err)) | 
|  | 120 | +	} | 
|  | 121 | +	inst.containerId = cont.ID | 
|  | 122 | +	inst.logger = inst.logger.With(zap.String("container-id", cont.ID)) | 
|  | 123 | +	inst.logger.Debug("created container") | 
|  | 124 | + | 
|  | 125 | +	err = utils.ForwardDockerContainerOutput(context.Background(), i.dockerClient, cont.ID, | 
|  | 126 | +		false, utils.NewLineWriter(func(line []byte) { | 
|  | 127 | +			inst.logger.Debug("container output", | 
|  | 128 | +				zap.ByteString("line", line)) | 
|  | 129 | +		})) | 
|  | 130 | +	if err != nil { | 
|  | 131 | +		inst.logger.Fatal("failed to attach to container output", zap.Error(err)) | 
|  | 132 | +	} | 
|  | 133 | + | 
|  | 134 | +	err = i.dockerClient.ContainerStart(context.Background(), cont.ID, types.ContainerStartOptions{}) | 
|  | 135 | +	if err != nil { | 
|  | 136 | +		inst.logger.Fatal("failed to start container", zap.Error(err)) | 
|  | 137 | +	} | 
|  | 138 | +	inst.logger.Debug("started container") | 
|  | 139 | + | 
|  | 140 | +	inst.info.host = utils.MustString(utils.DockerContainerAddress(context.Background(), i.dockerClient, cont.ID)) | 
|  | 141 | + | 
|  | 142 | +	i.runningMutex.Lock() | 
|  | 143 | +	i.running[inst] = struct{}{} | 
|  | 144 | +	i.runningMutex.Unlock() | 
|  | 145 | + | 
|  | 146 | +	return inst | 
|  | 147 | +} | 
|  | 148 | + | 
|  | 149 | +func (i *dockerCreator) Cleanup() { | 
|  | 150 | +	i.runningMutex.Lock() | 
|  | 151 | +	instances := make([]*dockerBinaryInstance, 0, len(i.running)) | 
|  | 152 | +	for inst := range i.running { | 
|  | 153 | +		instances = append(instances, inst) | 
|  | 154 | +	} | 
|  | 155 | +	i.runningMutex.Unlock() | 
|  | 156 | + | 
|  | 157 | +	for _, inst := range instances { | 
|  | 158 | +		inst.Cleanup() | 
|  | 159 | +	} | 
|  | 160 | +} | 
|  | 161 | + | 
|  | 162 | +type dockerBinaryInstance struct { | 
|  | 163 | +	info | 
|  | 164 | +	icingaNotificationsDockerBinary *dockerCreator | 
|  | 165 | +	logger                          *zap.Logger | 
|  | 166 | +	containerId                     string | 
|  | 167 | +	configFileName                  string | 
|  | 168 | +} | 
|  | 169 | + | 
|  | 170 | +var _ services.IcingaNotificationsBase = (*dockerBinaryInstance)(nil) | 
|  | 171 | + | 
|  | 172 | +func (i *dockerBinaryInstance) Cleanup() { | 
|  | 173 | +	i.icingaNotificationsDockerBinary.runningMutex.Lock() | 
|  | 174 | +	delete(i.icingaNotificationsDockerBinary.running, i) | 
|  | 175 | +	i.icingaNotificationsDockerBinary.runningMutex.Unlock() | 
|  | 176 | + | 
|  | 177 | +	err := i.icingaNotificationsDockerBinary.dockerClient.ContainerRemove(context.Background(), i.containerId, types.ContainerRemoveOptions{ | 
|  | 178 | +		Force:         true, | 
|  | 179 | +		RemoveVolumes: true, | 
|  | 180 | +	}) | 
|  | 181 | +	if err != nil { | 
|  | 182 | +		panic(err) | 
|  | 183 | +	} | 
|  | 184 | +	i.logger.Debug("removed container") | 
|  | 185 | + | 
|  | 186 | +	err = os.Remove(i.configFileName) | 
|  | 187 | +	if err != nil { | 
|  | 188 | +		panic(err) | 
|  | 189 | +	} | 
|  | 190 | +} | 
0 commit comments