Exemple #1
0
 public ProcessHookService(DockerClient client, ILogger <ProcessHookService> logger, ContainerHookConfig config)
 {
     this.processHook = new ContainerHook(client, config);
     this.logStream   = null;
     this.logger      = logger;
 }
        public static AggregateProcessHookService AggregateProcessHookServiceFactory(IServiceProvider services)
        {
            var config               = services.GetService <IConfiguration>();
            var configLogger         = services.GetService <ILogger <AggregateProcessHookService> >();
            var aggregateServiceList = new AggregateProcessHookService(configLogger);

            configLogger.LogInformation("Read config");
            var rootConfigPath  = config.GetValue <string>("ROOT_CONFIG_PATH");
            var destConfigPath  = config.GetValue <string>("DEST_CONFIG_PATH");
            var mountConfigPath = config.GetValue <string>("MOUNT_CONFIG_PATH");
            var serviceSection  = config.GetSection("services").GetChildren();
            var dockerSection   = config.GetSection("docker");
            var email           = dockerSection.GetValue <string>("email");
            var username        = dockerSection.GetValue <string>("username");
            var password        = config.GetValue <string>("DOCKER_PASSWORD");

            foreach (var child in serviceSection)
            {
                configLogger.LogInformation($"Reading config for service:{child.Key}");
                var dockerClient = services.GetService <DockerClient>();
                var logger       = services.GetService <ILogger <ProcessHookService> >();
                var hookConfig   = new ContainerHookConfig();
                hookConfig.Capabilities = child.GetSection("capabilities").Get <List <string> >();
                if (hookConfig.Capabilities != null)
                {
                    using (logger.BeginScope(child.Key))
                    {
                        foreach (var cap in hookConfig.Capabilities)
                        {
                            logger.LogDebug(cap);
                        }
                    }
                }

                if (rootConfigPath != null)
                {
                    hookConfig.ConfigSrc     = Path.Join(rootConfigPath, child.Key);
                    hookConfig.ConfigVolSrc  = Path.Join(destConfigPath, child.Key);
                    hookConfig.ConfigVolDest = mountConfigPath;
                }
                hookConfig.MemCap     = child.GetValue <long>("mem");
                hookConfig.Privileged = child.GetValue <bool>("privileged", false);
                hookConfig.SetRestartPolicy(child.GetValue <string>("restartPolicy", "no"));

                if (child.GetSection("env").Exists())
                {
                    hookConfig.EnvVariables = child.GetSection("env")
                                              .GetChildren()
                                              .Select(item => new KeyValuePair <string, string>(item.Key, item.Value))
                                              .ToDictionary(x => x.Key, x => x.Value);
                }

                if (child.GetSection("ports").Exists())
                {
                    hookConfig.PortMappings = child.GetSection("ports")
                                              .GetChildren()
                                              .Select(item => new KeyValuePair <string, string>(item.Key, item.Value))
                                              .ToDictionary(x => x.Key, x => x.Value);
                }

                if (child.GetSection("mounts").Exists())
                {
                    hookConfig.Mounts = child.GetSection("mounts")
                                        .GetChildren()
                                        .Select(item => new KeyValuePair <string, string>(item.Key, item.Value))
                                        .ToDictionary(x => x.Key, x => x.Value);
                }

                hookConfig.Name         = child.GetValue <string>("imageName");
                hookConfig.NetworkMode  = child.GetValue <string>("networkMode", "bridge");
                hookConfig.Tag          = child.GetValue <string>("imageTag");
                hookConfig.SafeName     = child.Key;
                hookConfig.ForceUpgrade = child.GetValue("forceUpgrade", false);
                hookConfig.SetAuthConfig(email, username, password);
                aggregateServiceList.AddProcess(new ProcessHookService(dockerClient, logger, hookConfig));
            }
            aggregateServiceList.StartServices();
            return(aggregateServiceList);
        }