Example #1
0
        public async Task <ModuleSet> GetModulesAsync(CancellationToken token)
        {
            IEnumerable <ModuleRuntimeInfo> moduleStatuses = await this.moduleStatusProvider.GetModules(token);

            var       modules   = new List <IModule>();
            ModuleSet moduleSet = this.deploymentConfig.GetModuleSet();

            foreach (ModuleRuntimeInfo moduleRuntimeInfo in moduleStatuses)
            {
                if (moduleRuntimeInfo.Type != "docker" || !(moduleRuntimeInfo is ModuleRuntimeInfo <DockerReportedConfig> dockerRuntimeInfo))
                {
                    Events.InvalidModuleType(moduleRuntimeInfo);
                    continue;
                }

                if (!moduleSet.Modules.TryGetValue(dockerRuntimeInfo.Name, out IModule configModule) || !(configModule is DockerModule dockerModule))
                {
                    dockerModule = new DockerModule(dockerRuntimeInfo.Name, string.Empty, ModuleStatus.Unknown, RestartPolicy.Unknown, new DockerConfig(string.Empty), new ConfigurationInfo(), null);
                }

                Option <ModuleState> moduleStateOption = await this.moduleStateStore.Get(moduleRuntimeInfo.Name);

                ModuleState moduleState = moduleStateOption.GetOrElse(new ModuleState(0, moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue)));
                // compute module state based on restart policy
                DateTime     lastExitTime        = moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue);
                ModuleStatus moduleRuntimeStatus = this.restartManager.ComputeModuleStatusFromRestartPolicy(moduleRuntimeInfo.ModuleStatus, dockerModule.RestartPolicy, moduleState.RestartCount, lastExitTime);
                string       image = !string.IsNullOrWhiteSpace(dockerRuntimeInfo.Config.Image) ? dockerRuntimeInfo.Config.Image : dockerModule.Config.Image;
                var          dockerReportedConfig = new DockerReportedConfig(image, dockerModule.Config.CreateOptions, dockerRuntimeInfo.Config.ImageHash);
                IModule      module;
                switch (moduleRuntimeInfo.Name)
                {
                case Core.Constants.EdgeHubModuleName:
                    module = new EdgeHubDockerRuntimeModule(
                        dockerModule.DesiredStatus, dockerModule.RestartPolicy, dockerReportedConfig,
                        (int)dockerRuntimeInfo.ExitCode, dockerRuntimeInfo.Description, dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        lastExitTime, moduleState.RestartCount, moduleState.LastRestartTimeUtc,
                        moduleRuntimeStatus, dockerModule.ConfigurationInfo, dockerModule.Env);
                    break;

                case Core.Constants.EdgeAgentModuleName:
                    module = new EdgeAgentDockerRuntimeModule(dockerReportedConfig, moduleRuntimeStatus,
                                                              (int)dockerRuntimeInfo.ExitCode, dockerRuntimeInfo.Description, dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                                                              lastExitTime, dockerModule.ConfigurationInfo, dockerModule.Env);
                    break;

                default:
                    module = new DockerRuntimeModule(
                        moduleRuntimeInfo.Name, dockerModule.Version, dockerModule.DesiredStatus, dockerModule.RestartPolicy, dockerReportedConfig,
                        (int)moduleRuntimeInfo.ExitCode, moduleRuntimeInfo.Description, moduleRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue), lastExitTime,
                        moduleState.RestartCount, moduleState.LastRestartTimeUtc,
                        moduleRuntimeStatus, dockerModule.ConfigurationInfo, dockerModule.Env);
                    break;
                }

                modules.Add(module);
            }
            return(new ModuleSet(modules.ToDictionary(m => m.Name, m => m)));
        }
Example #2
0
        public async Task <ModuleSet> GetModulesAsync(CancellationToken token)
        {
            IEnumerable <ModuleRuntimeInfo> moduleStatuses = await this.moduleStatusProvider.GetModules(token);

            var       modules   = new List <IModule>();
            ModuleSet moduleSet = this.deploymentConfig.GetModuleSet();

            foreach (ModuleRuntimeInfo moduleRuntimeInfo in moduleStatuses)
            {
                if (moduleRuntimeInfo.Type != "docker" || !(moduleRuntimeInfo is ModuleRuntimeInfo <DockerReportedConfig> dockerRuntimeInfo))
                {
                    Events.InvalidModuleType(moduleRuntimeInfo);
                    continue;
                }

                if (!moduleSet.Modules.TryGetValue(dockerRuntimeInfo.Name, out IModule configModule) || !(configModule is DockerModule dockerModule))
                {
                    // This is given the highest priority so that it's removal is prioritized first before other known modules are processed.
                    dockerModule = new DockerModule(dockerRuntimeInfo.Name, string.Empty, ModuleStatus.Unknown, Core.RestartPolicy.Unknown, new DockerConfig(Constants.UnknownImage, new CreateContainerParameters(), Option.None <string>()), ImagePullPolicy.OnCreate, Core.Constants.HighestPriority, new ConfigurationInfo(), null);
                }

                Option <ModuleState> moduleStateOption = await this.moduleStateStore.Get(moduleRuntimeInfo.Name);

                ModuleState moduleState = moduleStateOption.GetOrElse(new ModuleState(0, moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue)));
                // compute module state based on restart policy
                DateTime     lastExitTime        = moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue);
                ModuleStatus moduleRuntimeStatus = dockerModule.DesiredStatus == ModuleStatus.Running
                    ? this.restartManager.ComputeModuleStatusFromRestartPolicy(moduleRuntimeInfo.ModuleStatus, dockerModule.RestartPolicy, moduleState.RestartCount, lastExitTime)
                    : moduleRuntimeInfo.ModuleStatus;

                string  image = !string.IsNullOrWhiteSpace(dockerRuntimeInfo.Config.Image) ? dockerRuntimeInfo.Config.Image : dockerModule.Config.Image;
                var     dockerReportedConfig = new DockerReportedConfig(image, dockerModule.Config.CreateOptions, dockerRuntimeInfo.Config.ImageHash, dockerModule.Config.Digest);
                IModule module;
                switch (moduleRuntimeInfo.Name)
                {
                case Core.Constants.EdgeHubModuleName:
                    module = new EdgeHubDockerRuntimeModule(
                        dockerModule.DesiredStatus,
                        dockerModule.RestartPolicy,
                        dockerReportedConfig,
                        (int)dockerRuntimeInfo.ExitCode,
                        dockerRuntimeInfo.Description,
                        dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        lastExitTime,
                        moduleState.RestartCount,
                        moduleState.LastRestartTimeUtc,
                        moduleRuntimeStatus,
                        dockerModule.ImagePullPolicy,
                        dockerModule.StartupOrder,
                        dockerModule.ConfigurationInfo,
                        dockerModule.Env);
                    break;

                case Core.Constants.EdgeAgentModuleName:
                    var labels = dockerRuntimeInfo.Config.CreateOptions?.Labels ?? new Dictionary <string, string>();
                    var env    = labels.TryGetValue(Core.Constants.Labels.Env, out string envStr)
                            ? JsonConvert.DeserializeObject <Dictionary <string, EnvVal> >(envStr)
                            : new Dictionary <string, EnvVal>();

                    module = new EdgeAgentDockerRuntimeModule(
                        dockerRuntimeInfo.Config,
                        moduleRuntimeStatus,
                        (int)dockerRuntimeInfo.ExitCode,
                        dockerRuntimeInfo.Description,
                        dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        lastExitTime,
                        dockerModule.ImagePullPolicy,
                        dockerModule.ConfigurationInfo,
                        env);
                    break;

                default:
                    module = new DockerRuntimeModule(
                        moduleRuntimeInfo.Name,
                        dockerModule.Version,
                        dockerModule.DesiredStatus,
                        dockerModule.RestartPolicy,
                        dockerReportedConfig,
                        (int)moduleRuntimeInfo.ExitCode,
                        moduleRuntimeInfo.Description,
                        moduleRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        lastExitTime,
                        moduleState.RestartCount,
                        moduleState.LastRestartTimeUtc,
                        moduleRuntimeStatus,
                        dockerModule.ImagePullPolicy,
                        dockerModule.StartupOrder,
                        dockerModule.ConfigurationInfo,
                        dockerModule.Env);
                    break;
                }

                modules.Add(module);
            }

            return(new ModuleSet(modules.ToDictionary(m => m.Name, m => m)));
        }