DockerEnvironmentProvider(IRuntimeInfoProvider runtimeInfoProvider, IEntityStore<string, ModuleState> store, IRestartPolicyManager restartPolicyManager, string operatingSystemType, string architecture)
 {
     this.moduleStatusProvider = runtimeInfoProvider;
     this.store = Preconditions.CheckNotNull(store, nameof(store));
     this.operatingSystemType = operatingSystemType;
     this.architecture = architecture;
     this.restartPolicyManager = Preconditions.CheckNotNull(restartPolicyManager, nameof(restartPolicyManager));
 }
        public static async Task <DockerEnvironmentProvider> CreateAsync(IRuntimeInfoProvider runtimeInfoProvider, IEntityStore <string, ModuleState> store,
                                                                         IRestartPolicyManager restartPolicyManager)
        {
            SystemInfo systemInfo = await Preconditions.CheckNotNull(runtimeInfoProvider, nameof(runtimeInfoProvider)).GetSystemInfo();

            return(new DockerEnvironmentProvider(
                       runtimeInfoProvider, store, restartPolicyManager,
                       systemInfo.OperatingSystemType, systemInfo.Architecture,
                       systemInfo.Version));
        }
 KubernetesEnvironmentProvider(
     IRuntimeInfoProvider runtimeInfoProvider,
     IEntityStore <string, ModuleState> store,
     string operatingSystemType,
     string architecture,
     string version)
 {
     this.moduleStatusProvider = runtimeInfoProvider;
     this.store = Preconditions.CheckNotNull(store, nameof(store));
     this.operatingSystemType = operatingSystemType;
     this.architecture        = architecture;
     this.version             = version;
 }
        public static async Task <KubernetesEnvironmentProvider> CreateAsync(
            IRuntimeInfoProvider runtimeInfoProvider,
            IEntityStore <string, ModuleState> store,
            CancellationToken token)
        {
            SystemInfo systemInfo = await Preconditions.CheckNotNull(runtimeInfoProvider, nameof(runtimeInfoProvider)).GetSystemInfo(token);

            return(new KubernetesEnvironmentProvider(
                       runtimeInfoProvider,
                       store,
                       systemInfo.OperatingSystemType,
                       systemInfo.Architecture,
                       systemInfo.Version));
        }
 public DockerEnvironment(IRuntimeInfoProvider moduleStatusProvider,
                          DeploymentConfig deploymentConfig,
                          IEntityStore <string, ModuleState> moduleStateStore,
                          IRestartPolicyManager restartManager,
                          string operatingSystemType,
                          string architecture)
 {
     this.moduleStatusProvider = moduleStatusProvider;
     this.deploymentConfig     = deploymentConfig;
     this.moduleStateStore     = moduleStateStore;
     this.restartManager       = restartManager;
     this.operatingSystemType  = operatingSystemType;
     this.architecture         = architecture;
 }
 public KubernetesEnvironment(
     IRuntimeInfoProvider moduleStatusProvider,
     DeploymentConfig deploymentConfig,
     IEntityStore <string, ModuleState> moduleStateStore,
     string operatingSystemType,
     string architecture,
     string version)
 {
     this.moduleStatusProvider = moduleStatusProvider;
     this.deploymentConfig     = deploymentConfig;
     this.moduleStateStore     = moduleStateStore;
     this.operatingSystemType  = operatingSystemType;
     this.architecture         = architecture;
     this.version = version;
 }
 public LogsRequestToOptionsMapper(
     IRuntimeInfoProvider runtimeInfoProvider,
     LogsContentEncoding contentEncoding,
     LogsContentType contentType,
     LogOutputFraming outputFraming,
     Option <LogsOutputGroupingConfig> outputGroupingConfig,
     bool follow)
 {
     this.runtimeInfoProvider  = Preconditions.CheckNotNull(runtimeInfoProvider, nameof(runtimeInfoProvider));
     this.contentType          = contentType;
     this.contentEncoding      = contentEncoding;
     this.outputFraming        = outputFraming;
     this.outputGroupingConfig = outputGroupingConfig;
     this.follow = follow;
 }
        protected override void Load(ContainerBuilder builder)
        {
            // IKubernetesClient
            builder.Register(
                c =>
            {
                if (this.enableServiceCallTracing)
                {
                    // enable tracing of k8s requests made by the client
                    var loggerFactory = c.Resolve <ILoggerFactory>();
                    ILogger logger    = loggerFactory.CreateLogger(typeof(Kubernetes));
                    ServiceClientTracing.IsEnabled = true;
                    ServiceClientTracing.AddTracingInterceptor(new DebugTracer(logger));
                }

                // load the k8s config from KUBECONFIG or $HOME/.kube/config or in-cluster if its available
                KubernetesClientConfiguration kubeConfig = Option.Maybe(Environment.GetEnvironmentVariable("KUBECONFIG"))
                                                           .Else(() => Option.Maybe(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".kube", "config")))
                                                           .Filter(File.Exists)
                                                           .Map(path => KubernetesClientConfiguration.BuildConfigFromConfigFile(path))
                                                           .GetOrElse(KubernetesClientConfiguration.InClusterConfig);

                return(new Kubernetes(kubeConfig));
            })
            .As <IKubernetes>()
            .SingleInstance();

            // IModuleClientProvider
            builder.Register(
                c => new ModuleClientProvider(
                    c.Resolve <ISdkModuleClientProvider>(),
                    this.upstreamProtocol,
                    this.proxy,
                    this.productInfo.OrDefault(),
                    this.closeOnIdleTimeout,
                    this.idleTimeout))
            .As <IModuleClientProvider>()
            .SingleInstance();

            // IModuleManager
            builder.Register(c => new ModuleManagementHttpClient(this.managementUri, this.apiVersion, Core.Constants.EdgeletClientApiVersion))
            .As <IModuleManager>()
            .As <IIdentityManager>()
            .SingleInstance();

            // IModuleIdentityLifecycleManager
            var identityBuilder = new ModuleIdentityProviderServiceBuilder(this.resourceName.Hostname, this.resourceName.DeviceId, this.edgeDeviceHostName);

            builder.Register(c => new KubernetesModuleIdentityLifecycleManager(c.Resolve <IIdentityManager>(), identityBuilder, this.workloadUri))
            .As <IModuleIdentityLifecycleManager>()
            .SingleInstance();

            // CombinedKubernetesConfigProvider
            builder.Register(
                c =>
            {
                bool enableKubernetesExtensions = this.experimentalFeatures.Enabled && this.experimentalFeatures.EnableExtensions;
                return(new CombinedKubernetesConfigProvider(this.dockerAuthConfig, this.workloadUri, this.managementUri, enableKubernetesExtensions));
            })
            .As <ICombinedConfigProvider <CombinedKubernetesConfig> >()
            .SingleInstance();

            // ICommandFactory
            builder.Register(
                c =>
            {
                var loggerFactory            = c.Resolve <ILoggerFactory>();
                var kubernetesCommandFactory = new KubernetesCommandFactory();
                ICommandFactory factory      = new LoggingCommandFactory(kubernetesCommandFactory, loggerFactory);
                return(Task.FromResult(factory));
            })
            .As <Task <ICommandFactory> >()
            .SingleInstance();

            // IPlanner
            builder.Register(
                async c =>
            {
                var configProvider             = c.Resolve <ICombinedConfigProvider <CombinedKubernetesConfig> >();
                ICommandFactory commandFactory = await c.Resolve <Task <ICommandFactory> >();
                IPlanner planner = new KubernetesPlanner(this.deviceNamespace, this.resourceName, c.Resolve <IKubernetes>(), commandFactory, configProvider);
                return(planner);
            })
            .As <Task <IPlanner> >()
            .SingleInstance();

            // KubernetesRuntimeInfoProvider
            builder.Register(c => new KubernetesRuntimeInfoProvider(this.deviceNamespace, c.Resolve <IKubernetes>(), c.Resolve <IModuleManager>()))
            .As <IRuntimeInfoProvider>()
            .As <IRuntimeInfoSource>()
            .SingleInstance();

            // KubernetesDeploymentProvider
            builder.Register(
                c => new KubernetesDeploymentMapper(
                    this.deviceNamespace,
                    this.edgeDeviceHostName,
                    this.proxyImage,
                    this.proxyConfigPath,
                    this.proxyConfigVolumeName,
                    this.proxyConfigMapName,
                    this.proxyTrustBundlePath,
                    this.proxyTrustBundleVolumeName,
                    this.proxyTrustBundleConfigMapName,
                    this.persistentVolumeName,
                    this.storageClassName,
                    this.apiVersion,
                    this.workloadUri,
                    this.managementUri))
            .As <IKubernetesDeploymentMapper>();

            // KubernetesServiceMapper
            builder.Register(c => new KubernetesServiceMapper(this.defaultMapServiceType))
            .As <IKubernetesServiceMapper>();

            // KubernetesPvcMapper
            builder.Register(c => new KubernetesPvcMapper(this.persistentVolumeName, this.storageClassName, this.persistentVolumeClaimSizeMb))
            .As <IKubernetesPvcMapper>();

            // KubernetesServiceAccountProvider
            builder.Register(c => new KubernetesServiceAccountMapper())
            .As <IKubernetesServiceAccountMapper>();

            // EdgeDeploymentController
            builder.Register(
                c =>
            {
                var deploymentSelector = $"{Constants.K8sEdgeDeviceLabel}={KubeUtils.SanitizeK8sValue(this.resourceName.DeviceId)},{Constants.K8sEdgeHubNameLabel}={KubeUtils.SanitizeK8sValue(this.resourceName.Hostname)}";
                IEdgeDeploymentController watchOperator = new EdgeDeploymentController(
                    this.resourceName,
                    deploymentSelector,
                    this.deviceNamespace,
                    c.Resolve <IKubernetes>(),
                    c.Resolve <IModuleIdentityLifecycleManager>(),
                    c.Resolve <IKubernetesServiceMapper>(),
                    c.Resolve <IKubernetesDeploymentMapper>(),
                    c.Resolve <IKubernetesPvcMapper>(),
                    c.Resolve <IKubernetesServiceAccountMapper>());

                return(watchOperator);
            })
            .As <IEdgeDeploymentController>()
            .SingleInstance();

            // IEdgeDeploymentOperator
            builder.Register(
                c =>
            {
                IEdgeDeploymentOperator watchOperator = new EdgeDeploymentOperator(
                    this.resourceName,
                    this.deviceNamespace,
                    c.Resolve <IKubernetes>(),
                    c.Resolve <IEdgeDeploymentController>());

                return(watchOperator);
            })
            .As <IEdgeDeploymentOperator>()
            .SingleInstance();

            // IKubernetesEnvironmentOperator
            builder.Register(
                c =>
            {
                IKubernetesEnvironmentOperator watchOperator = new KubernetesEnvironmentOperator(
                    this.deviceNamespace,
                    c.Resolve <IRuntimeInfoSource>(),
                    c.Resolve <IKubernetes>());

                return(watchOperator);
            })
            .As <IKubernetesEnvironmentOperator>()
            .SingleInstance();

            // Task<IEnvironmentProvider>
            builder.Register(
                async c =>
            {
                var moduleStateStore     = c.Resolve <IEntityStore <string, ModuleState> >();
                var restartPolicyManager = c.Resolve <IRestartPolicyManager>();
                IRuntimeInfoProvider runtimeInfoProvider       = c.Resolve <IRuntimeInfoProvider>();
                IEnvironmentProvider dockerEnvironmentProvider = await DockerEnvironmentProvider.CreateAsync(runtimeInfoProvider, moduleStateStore, restartPolicyManager);
                return(dockerEnvironmentProvider);
            })
            .As <Task <IEnvironmentProvider> >()
            .SingleInstance();
        }
 public CodeCompletionProvider(IScriptParser parser, IRuntimeInfoProvider infoProvider)
 {
     this.parser = parser;
     this.infoProvider = infoProvider;
 }
 public ModuleLogsRequestHandler(ILogsProvider logsProvider, IRuntimeInfoProvider runtimeInfoProvider)
 {
     this.logsProvider        = Preconditions.CheckNotNull(logsProvider, nameof(logsProvider));
     this.runtimeInfoProvider = Preconditions.CheckNotNull(runtimeInfoProvider, nameof(runtimeInfoProvider));
 }
Exemple #11
0
 public LogsProvider(IRuntimeInfoProvider runtimeInfoProvider, ILogsProcessor logsProcessor)
 {
     this.runtimeInfoProvider = Preconditions.CheckNotNull(runtimeInfoProvider, nameof(runtimeInfoProvider));
     this.logsProcessor       = Preconditions.CheckNotNull(logsProcessor, nameof(logsProcessor));
 }
Exemple #12
0
        protected override void Load(ContainerBuilder builder)
        {
            // ILogsUploader
            builder.Register(c => new AzureBlobLogsUploader(this.iotHubHostName, this.deviceId))
            .As <ILogsUploader>()
            .SingleInstance();

            // Task<ILogsProvider>
            builder.Register(
                async c =>
            {
                var logsProcessor = new LogsProcessor(new LogMessageParser(this.iotHubHostName, this.deviceId));
                IRuntimeInfoProvider runtimeInfoProvider = await c.Resolve <Task <IRuntimeInfoProvider> >();
                return(new LogsProvider(runtimeInfoProvider, logsProcessor) as ILogsProvider);
            })
            .As <Task <ILogsProvider> >()
            .SingleInstance();

            // IRequestManager
            builder.Register(
                c =>
            {
                var requestHandlers = new List <IRequestHandler>
                {
                    new PingRequestHandler(),
                    new TaskStatusRequestHandler()
                };
                return(new RequestManager(requestHandlers, this.requestTimeout) as IRequestManager);
            })
            .As <IRequestManager>()
            .SingleInstance();

            if (this.experimentalFeatures.EnableUploadLogs)
            {
                // Task<IRequestHandler> - LogsUploadRequestHandler
                builder.Register(
                    async c =>
                {
                    var logsUploader            = c.Resolve <ILogsUploader>();
                    var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                    var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                    IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                    ILogsProvider logsProvider = await logsProviderTask;
                    return(new LogsUploadRequestHandler(logsUploader, logsProvider, runtimeInfoProvider) as IRequestHandler);
                })
                .As <Task <IRequestHandler> >()
                .SingleInstance();
            }

            if (this.experimentalFeatures.EnableGetLogs)
            {
                // Task<IRequestHandler> - LogsRequestHandler
                builder.Register(
                    async c =>
                {
                    var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                    var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                    IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                    ILogsProvider logsProvider = await logsProviderTask;
                    return(new LogsRequestHandler(logsProvider, runtimeInfoProvider) as IRequestHandler);
                })
                .As <Task <IRequestHandler> >()
                .SingleInstance();
            }

            // Task<IRequestHandler> - RestartRequestHandler
            builder.Register(
                async c =>
            {
                var environmentProviderTask = c.Resolve <Task <IEnvironmentProvider> >();
                var commandFactoryTask      = c.Resolve <Task <ICommandFactory> >();
                var configSourceTask        = c.Resolve <Task <IConfigSource> >();
                IEnvironmentProvider environmentProvider = await environmentProviderTask;
                ICommandFactory commandFactory           = await commandFactoryTask;
                IConfigSource configSource = await configSourceTask;
                return(new RestartRequestHandler(environmentProvider, configSource, commandFactory) as IRequestHandler);
            })
            .As <Task <IRequestHandler> >()
            .SingleInstance();

            // ISdkModuleClientProvider
            builder.Register(c => new SdkModuleClientProvider())
            .As <ISdkModuleClientProvider>()
            .SingleInstance();

            // IEdgeAgentConnection
            builder.Register(
                c =>
            {
                var serde = c.Resolve <ISerde <DeploymentConfig> >();
                var deviceClientprovider = c.Resolve <IModuleClientProvider>();
                var requestManager       = c.Resolve <IRequestManager>();
                var deviceManager        = c.Resolve <IDeviceManager>();
                bool enableSubscriptions = !this.experimentalFeatures.DisableCloudSubscriptions;
                IEdgeAgentConnection edgeAgentConnection = new EdgeAgentConnection(deviceClientprovider, serde, requestManager, deviceManager, enableSubscriptions, this.configRefreshFrequency);
                return(edgeAgentConnection);
            })
            .As <IEdgeAgentConnection>()
            .SingleInstance();

            // Task<IStreamRequestListener>
            builder.Register(
                async c =>
            {
                if (this.enableStreams)
                {
                    var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                    var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                    var edgeAgentConnection     = c.Resolve <IEdgeAgentConnection>();
                    IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                    ILogsProvider logsProvider       = await logsProviderTask;
                    var streamRequestHandlerProvider = new StreamRequestHandlerProvider(logsProvider, runtimeInfoProvider);
                    return(new StreamRequestListener(streamRequestHandlerProvider, edgeAgentConnection) as IStreamRequestListener);
                }
                else
                {
                    return(new NullStreamRequestListener() as IStreamRequestListener);
                }
            })
            .As <Task <IStreamRequestListener> >()
            .SingleInstance();

            // Task<IConfigSource>
            builder.Register(
                async c =>
            {
                var edgeAgentConnection          = c.Resolve <IEdgeAgentConnection>();
                var twinConfigSource             = new TwinConfigSource(edgeAgentConnection, this.configuration);
                var backupSourceTask             = c.Resolve <Task <IDeploymentBackupSource> >();
                IConfigSource backupConfigSource = new BackupConfigSource(await backupSourceTask, twinConfigSource);
                return(backupConfigSource);
            })
            .As <Task <IConfigSource> >()
            .SingleInstance();

            // IReporter
            builder.Register(
                c =>
            {
                var runtimeInfoDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(DockerReportedRuntimeInfo),
                    [Constants.Unknown] = typeof(UnknownRuntimeInfo)
                };

                var edgeAgentDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(EdgeAgentDockerRuntimeModule),
                    [Constants.Unknown] = typeof(UnknownEdgeAgentModule)
                };

                var edgeHubDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(EdgeHubDockerRuntimeModule),
                    [Constants.Unknown] = typeof(UnknownEdgeHubModule)
                };

                var moduleDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType] = typeof(DockerRuntimeModule)
                };

                var deserializerTypesMap = new Dictionary <Type, IDictionary <string, Type> >
                {
                    { typeof(IRuntimeInfo), runtimeInfoDeserializerTypes },
                    { typeof(IEdgeAgentModule), edgeAgentDeserializerTypes },
                    { typeof(IEdgeHubModule), edgeHubDeserializerTypes },
                    { typeof(IModule), moduleDeserializerTypes }
                };

                var edgeAgentConnection = c.Resolve <IEdgeAgentConnection>();
                return(new IoTHubReporter(
                           edgeAgentConnection,
                           new TypeSpecificSerDe <AgentState>(deserializerTypesMap),
                           this.versionInfo) as IReporter);
            })
            .As <IReporter>()
            .SingleInstance();

            base.Load(builder);
        }
Exemple #13
0
        protected override void Load(ContainerBuilder builder)
        {
            // ILogsUploader
            builder.Register(c => new AzureBlobLogsUploader(this.iotHubHostName, this.deviceId))
            .As <ILogsUploader>()
            .SingleInstance();

            // Task<ILogsProvider>
            builder.Register(
                async c =>
            {
                var logsProcessor = new LogsProcessor(new LogMessageParser(this.iotHubHostName, this.deviceId));
                IRuntimeInfoProvider runtimeInfoProvider = await c.Resolve <Task <IRuntimeInfoProvider> >();
                return(new LogsProvider(runtimeInfoProvider, logsProcessor) as ILogsProvider);
            })
            .As <Task <ILogsProvider> >()
            .SingleInstance();

            // Task<IStreamRequestListener>
            builder.Register(
                async c =>
            {
                if (this.enableStreams)
                {
                    var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                    var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                    IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                    ILogsProvider logsProvider       = await logsProviderTask;
                    var streamRequestHandlerProvider = new StreamRequestHandlerProvider(logsProvider, runtimeInfoProvider);
                    return(new StreamRequestListener(streamRequestHandlerProvider) as IStreamRequestListener);
                }
                else
                {
                    return(new NullStreamRequestListener() as IStreamRequestListener);
                }
            })
            .As <Task <IStreamRequestListener> >()
            .SingleInstance();

            // Task<IRequestManager>
            builder.Register(
                async c =>
            {
                var logsUploader            = c.Resolve <ILogsUploader>();
                var runtimeInfoProviderTask = c.Resolve <Task <IRuntimeInfoProvider> >();
                var logsProviderTask        = c.Resolve <Task <ILogsProvider> >();
                IRuntimeInfoProvider runtimeInfoProvider = await runtimeInfoProviderTask;
                ILogsProvider logsProvider = await logsProviderTask;
                var requestHandlers        = new List <IRequestHandler>
                {
                    new PingRequestHandler(),
                    new LogsUploadRequestHandler(logsUploader, logsProvider, runtimeInfoProvider)
                };
                return(new RequestManager(requestHandlers, this.requestTimeout) as IRequestManager);
            })
            .As <Task <IRequestManager> >()
            .SingleInstance();

            // Task<IEdgeAgentConnection>
            builder.Register(
                async c =>
            {
                var serde = c.Resolve <ISerde <DeploymentConfig> >();
                var deviceClientprovider      = c.Resolve <IModuleClientProvider>();
                var streamRequestListenerTask = c.Resolve <Task <IStreamRequestListener> >();
                var requestManagerTask        = c.Resolve <Task <IRequestManager> >();
                IStreamRequestListener streamRequestListener = await streamRequestListenerTask;
                IRequestManager requestManager           = await requestManagerTask;
                IEdgeAgentConnection edgeAgentConnection = new EdgeAgentConnection(deviceClientprovider, serde, requestManager, streamRequestListener, this.configRefreshFrequency);
                return(edgeAgentConnection);
            })
            .As <Task <IEdgeAgentConnection> >()
            .SingleInstance();

            // Task<IConfigSource>
            builder.Register(
                async c =>
            {
                var serde = c.Resolve <ISerde <DeploymentConfigInfo> >();
                var edgeAgentConnectionTask              = c.Resolve <Task <IEdgeAgentConnection> >();
                IEncryptionProvider encryptionProvider   = await c.Resolve <Task <IEncryptionProvider> >();
                IEdgeAgentConnection edgeAgentConnection = await edgeAgentConnectionTask;
                var twinConfigSource             = new TwinConfigSource(edgeAgentConnection, this.configuration);
                IConfigSource backupConfigSource = new FileBackupConfigSource(this.backupConfigFilePath, twinConfigSource, serde, encryptionProvider);
                return(backupConfigSource);
            })
            .As <Task <IConfigSource> >()
            .SingleInstance();

            // Task<IReporter>
            builder.Register(
                async c =>
            {
                var runtimeInfoDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(DockerReportedRuntimeInfo),
                    [Constants.Unknown] = typeof(UnknownRuntimeInfo)
                };

                var edgeAgentDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(EdgeAgentDockerRuntimeModule),
                    [Constants.Unknown] = typeof(UnknownEdgeAgentModule)
                };

                var edgeHubDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType]        = typeof(EdgeHubDockerRuntimeModule),
                    [Constants.Unknown] = typeof(UnknownEdgeHubModule)
                };

                var moduleDeserializerTypes = new Dictionary <string, Type>
                {
                    [DockerType] = typeof(DockerRuntimeModule)
                };

                var deserializerTypesMap = new Dictionary <Type, IDictionary <string, Type> >
                {
                    { typeof(IRuntimeInfo), runtimeInfoDeserializerTypes },
                    { typeof(IEdgeAgentModule), edgeAgentDeserializerTypes },
                    { typeof(IEdgeHubModule), edgeHubDeserializerTypes },
                    { typeof(IModule), moduleDeserializerTypes }
                };

                var edgeAgentConnectionTask = c.Resolve <Task <IEdgeAgentConnection> >();
                IEdgeAgentConnection edgeAgentConnection = await edgeAgentConnectionTask;

                return(new IoTHubReporter(
                           edgeAgentConnection,
                           new TypeSpecificSerDe <AgentState>(deserializerTypesMap),
                           this.versionInfo) as IReporter);
            })
            .As <Task <IReporter> >()
            .SingleInstance();

            base.Load(builder);
        }
Exemple #14
0
 public CodeCompletionProvider(IScriptParser parser, IRuntimeInfoProvider infoProvider)
 {
     this.parser       = parser;
     this.infoProvider = infoProvider;
 }
        protected override void Load(ContainerBuilder builder)
        {
            // IModuleClientProvider
            builder.Register(
                c => new ModuleClientProvider(
                    c.Resolve <ISdkModuleClientProvider>(),
                    this.upstreamProtocol,
                    this.proxy,
                    this.productInfo,
                    this.closeOnIdleTimeout,
                    this.idleTimeout))
            .As <IModuleClientProvider>()
            .SingleInstance();

            // IModuleManager
            builder.Register(c => new ModuleManagementHttpClient(this.managementUri, this.apiVersion, Constants.EdgeletClientApiVersion))
            .As <IModuleManager>()
            .As <IIdentityManager>()
            .As <IDeviceManager>()
            .SingleInstance();

            // IModuleIdentityLifecycleManager
            var identityBuilder = new ModuleIdentityProviderServiceBuilder(this.iotHubHostName, this.deviceId, this.gatewayHostName);

            builder.Register(c => new ModuleIdentityLifecycleManager(c.Resolve <IIdentityManager>(), identityBuilder, this.workloadUri))
            .As <IModuleIdentityLifecycleManager>()
            .SingleInstance();

            // ICombinedConfigProvider<CombinedDockerConfig>
            builder.Register(
                async c =>
            {
                IConfigSource configSource = await c.Resolve <Task <IConfigSource> >();
                return(new CombinedEdgeletConfigProvider(this.dockerAuthConfig, configSource) as ICombinedConfigProvider <CombinedDockerConfig>);
            })
            .As <Task <ICombinedConfigProvider <CombinedDockerConfig> > >()
            .SingleInstance();

            // ICommandFactory
            builder.Register(
                async c =>
            {
                var moduleManager = c.Resolve <IModuleManager>();
                var combinedDockerConfigProviderTask = c.Resolve <Task <ICombinedConfigProvider <CombinedDockerConfig> > >();
                var configSourceTask       = c.Resolve <Task <IConfigSource> >();
                var metricsProvider        = c.Resolve <IMetricsProvider>();
                var loggerFactory          = c.Resolve <ILoggerFactory>();
                IConfigSource configSource = await configSourceTask;
                ICombinedConfigProvider <CombinedDockerConfig> combinedDockerConfigProvider = await combinedDockerConfigProviderTask;
                ICommandFactory factory = new EdgeletCommandFactory <CombinedDockerConfig>(moduleManager, configSource, combinedDockerConfigProvider);
                factory = new MetricsCommandFactory(factory, metricsProvider);
                return(new LoggingCommandFactory(factory, loggerFactory) as ICommandFactory);
            })
            .As <Task <ICommandFactory> >()
            .SingleInstance();

            // Task<IRuntimeInfoProvider>
            builder.Register(c => Task.FromResult(new RuntimeInfoProvider <DockerReportedConfig>(c.Resolve <IModuleManager>()) as IRuntimeInfoProvider))
            .As <Task <IRuntimeInfoProvider> >()
            .SingleInstance();

            // Task<IEnvironmentProvider>
            builder.Register(
                async c =>
            {
                var moduleStateStore     = await c.Resolve <Task <IEntityStore <string, ModuleState> > >();
                var restartPolicyManager = c.Resolve <IRestartPolicyManager>();
                IRuntimeInfoProvider runtimeInfoProvider       = await c.Resolve <Task <IRuntimeInfoProvider> >();
                IEnvironmentProvider dockerEnvironmentProvider = await DockerEnvironmentProvider.CreateAsync(runtimeInfoProvider, moduleStateStore, restartPolicyManager, CancellationToken.None);
                return(dockerEnvironmentProvider);
            })
            .As <Task <IEnvironmentProvider> >()
            .SingleInstance();

            // SystemResourcesMetrics
            builder.Register(c => new SystemResourcesMetrics(c.Resolve <IMetricsProvider>(), c.Resolve <IModuleManager>().GetSystemResourcesAsync, this.apiVersion, this.performanceMetricsUpdateFrequency))
            .SingleInstance();
        }
Exemple #16
0
        public async Task AgentStartsUpModules(TestConfig testConfig)
        {
            // Build the docker host URL.
            string       dockerHostUrl = ConfigHelper.TestConfig["dockerHostUrl"];
            DockerClient client        = new DockerClientConfiguration(new Uri(dockerHostUrl)).CreateClient();

            try
            {
                // Remove any running containers with the same name that may be a left-over
                // from previous test runs.
                await RemoveContainer(client, testConfig);

                // Initialize docker configuration for this module.
                DockerConfig dockerConfig = testConfig.ImageCreateOptions != null
                    ? new DockerConfig(testConfig.Image, testConfig.ImageCreateOptions)
                    : new DockerConfig(testConfig.Image);

                // Initialize an Edge Agent module object.
                var dockerModule = new DockerModule(
                    testConfig.Name,
                    testConfig.Version,
                    ModuleStatus.Running,
                    global::Microsoft.Azure.Devices.Edge.Agent.Core.RestartPolicy.OnUnhealthy,
                    dockerConfig,
                    null,
                    null);
                var modules = new Dictionary <string, IModule> {
                    [testConfig.Name] = dockerModule
                };
                var systemModules = new SystemModules(null, null);

                // Start up the agent and run a "reconcile".
                var dockerLoggingOptions = new Dictionary <string, string>
                {
                    { "max-size", "1m" },
                    { "max-file", "1" }
                };
                var loggingConfig = new DockerLoggingConfig("json-file", dockerLoggingOptions);

                string             sharedAccessKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("test"));
                IConfigurationRoot configRoot      = new ConfigurationBuilder().AddInMemoryCollection(
                    new Dictionary <string, string>
                {
                    { "DeviceConnectionString", $"Hostname=fakeiothub;Deviceid=test;SharedAccessKey={sharedAccessKey}" }
                }).Build();

                var runtimeConfig        = new DockerRuntimeConfig("1.24.0", "{}");
                var runtimeInfo          = new DockerRuntimeInfo("docker", runtimeConfig);
                var deploymentConfigInfo = new DeploymentConfigInfo(1, new DeploymentConfig("1.0", runtimeInfo, systemModules, modules));

                var configSource = new Mock <IConfigSource>();
                configSource.Setup(cs => cs.Configuration).Returns(configRoot);
                configSource.Setup(cs => cs.GetDeploymentConfigInfoAsync()).ReturnsAsync(deploymentConfigInfo);

                // TODO: Fix this up with a real reporter. But before we can do that we need to use
                // the real configuration source that talks to IoT Hub above.
                NullReporter reporter = NullReporter.Instance;

                var restartStateStore                = Mock.Of <IEntityStore <string, ModuleState> >();
                var configStore                      = Mock.Of <IEntityStore <string, string> >();
                var deploymentConfigInfoSerde        = Mock.Of <ISerde <DeploymentConfigInfo> >();
                IRestartPolicyManager restartManager = new Mock <IRestartPolicyManager>().Object;

                var dockerCommandFactory = new DockerCommandFactory(client, loggingConfig, configSource.Object, new CombinedDockerConfigProvider(Enumerable.Empty <AuthConfig>()));
                IRuntimeInfoProvider runtimeInfoProvider = await RuntimeInfoProvider.CreateAsync(client);

                IEnvironmentProvider environmentProvider = await DockerEnvironmentProvider.CreateAsync(runtimeInfoProvider, restartStateStore, restartManager);

                var logFactoryMock = new Mock <ILoggerFactory>();
                var logMock        = new Mock <ILogger <LoggingCommandFactory> >();
                logFactoryMock.Setup(l => l.CreateLogger(It.IsAny <string>()))
                .Returns(logMock.Object);
                var commandFactory = new LoggingCommandFactory(dockerCommandFactory, logFactoryMock.Object);

                var credential = new ConnectionStringCredentials("fake");
                var identity   = new Mock <IModuleIdentity>();
                identity.Setup(id => id.Credentials).Returns(credential);
                identity.Setup(id => id.ModuleId).Returns(testConfig.Name);
                IImmutableDictionary <string, IModuleIdentity> identities = new Dictionary <string, IModuleIdentity>()
                {
                    [testConfig.Name] = identity.Object
                }.ToImmutableDictionary();
                var moduleIdentityLifecycleManager = new Mock <IModuleIdentityLifecycleManager>();
                moduleIdentityLifecycleManager.Setup(m => m.GetModuleIdentitiesAsync(It.IsAny <ModuleSet>(), It.IsAny <ModuleSet>())).Returns(Task.FromResult(identities));

                Agent agent = await Agent.Create(
                    configSource.Object,
                    new RestartPlanner(commandFactory),
                    new OrderedPlanRunner(),
                    reporter,
                    moduleIdentityLifecycleManager.Object,
                    environmentProvider,
                    configStore,
                    deploymentConfigInfoSerde,
                    NullEncryptionProvider.Instance);

                await agent.ReconcileAsync(CancellationToken.None);

                // Sometimes the container is still not ready by the time we run the validator.
                // So we attempt validation multiple times and bail only if all of them fail.
                bool      validated   = false;
                int       attempts    = 0;
                const int MaxAttempts = 5;
                while (!validated && attempts < MaxAttempts)
                {
                    validated = testConfig.Validator.Validate();
                    if (!validated)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(5));
                    }

                    ++attempts;
                }

                Assert.Equal(true, validated);
            }
            finally
            {
                await RemoveContainer(client, testConfig);
            }
        }
Exemple #17
0
        protected override void Load(ContainerBuilder builder)
        {
            // IModuleClientProvider
            string edgeAgentConnectionString = $"{this.edgeDeviceConnectionString};{Constants.ModuleIdKey}={Constants.EdgeAgentModuleIdentityName}";

            builder.Register(
                c => new ModuleClientProvider(
                    edgeAgentConnectionString,
                    c.Resolve <ISdkModuleClientProvider>(),
                    this.upstreamProtocol,
                    this.proxy,
                    this.productInfo,
                    this.closeOnIdleTimeout,
                    this.idleTimeout))
            .As <IModuleClientProvider>()
            .SingleInstance();

            // IServiceClient
            builder.Register(c => new RetryingServiceClient(new ServiceClient(this.edgeDeviceConnectionString, this.deviceId)))
            .As <IServiceClient>()
            .SingleInstance();

            // IModuleIdentityLifecycleManager
            builder.Register(c => new ModuleIdentityLifecycleManager(c.Resolve <IServiceClient>(), this.iotHubHostName, this.deviceId, this.gatewayHostName))
            .As <IModuleIdentityLifecycleManager>()
            .SingleInstance();

            // IDockerClient
            builder.Register(c => new DockerClientConfiguration(this.dockerHostname).CreateClient())
            .As <IDockerClient>()
            .SingleInstance();

            // ICombinedConfigProvider<CombinedDockerConfig>
            builder.Register(c => new CombinedDockerConfigProvider(this.dockerAuthConfig))
            .As <ICombinedConfigProvider <CombinedDockerConfig> >()
            .SingleInstance();

            // ICommandFactory
            builder.Register(
                async c =>
            {
                var dockerClient                 = c.Resolve <IDockerClient>();
                var dockerLoggingConfig          = c.Resolve <DockerLoggingConfig>();
                var combinedDockerConfigProvider = c.Resolve <ICombinedConfigProvider <CombinedDockerConfig> >();
                IConfigSource configSource       = await c.Resolve <Task <IConfigSource> >();
                var dockerFactory                = new DockerCommandFactory(dockerClient, dockerLoggingConfig, configSource, combinedDockerConfigProvider);
                return(new LoggingCommandFactory(dockerFactory, c.Resolve <ILoggerFactory>()) as ICommandFactory);
            })
            .As <Task <ICommandFactory> >()
            .SingleInstance();

            // IRuntimeInfoProvider
            builder.Register(
                async c =>
            {
                IRuntimeInfoProvider runtimeInfoProvider = await RuntimeInfoProvider.CreateAsync(c.Resolve <IDockerClient>());
                return(runtimeInfoProvider);
            })
            .As <Task <IRuntimeInfoProvider> >()
            .SingleInstance();

            // Task<IEnvironmentProvider>
            builder.Register(
                async c =>
            {
                var moduleStateStore     = await c.Resolve <Task <IEntityStore <string, ModuleState> > >();
                var restartPolicyManager = c.Resolve <IRestartPolicyManager>();
                IRuntimeInfoProvider runtimeInfoProvider       = await c.Resolve <Task <IRuntimeInfoProvider> >();
                IEnvironmentProvider dockerEnvironmentProvider = await DockerEnvironmentProvider.CreateAsync(runtimeInfoProvider, moduleStateStore, restartPolicyManager);
                return(dockerEnvironmentProvider);
            })
            .As <Task <IEnvironmentProvider> >()
            .SingleInstance();

            // IDeviceManager
            builder.Register(c => new NullDeviceManager())
            .As <IDeviceManager>()
            .SingleInstance();
        }
Exemple #18
0
        protected override void Load(ContainerBuilder builder)
        {
            // IModuleClientProvider
            builder.Register(c => new EnvironmentModuleClientProvider(this.upstreamProtocol, this.proxy, this.productInfo))
            .As <IModuleClientProvider>()
            .SingleInstance();

            // IModuleManager
            builder.Register(c => new ModuleManagementHttpClient(this.managementUri, this.apiVersion, Constants.EdgeletClientApiVersion))
            .As <IModuleManager>()
            .As <IIdentityManager>()
            .SingleInstance();

            // IModuleIdentityLifecycleManager
            var identityBuilder = new ModuleIdentityProviderServiceBuilder(this.iotHubHostName, this.deviceId, this.gatewayHostName);

            builder.Register(c => new ModuleIdentityLifecycleManager(c.Resolve <IIdentityManager>(), identityBuilder, this.workloadUri))
            .As <IModuleIdentityLifecycleManager>()
            .SingleInstance();

            // ICombinedConfigProvider<CombinedDockerConfig>
            builder.Register(
                async c =>
            {
                IConfigSource configSource = await c.Resolve <Task <IConfigSource> >();
                return(new CombinedEdgeletConfigProvider(this.dockerAuthConfig, configSource) as ICombinedConfigProvider <CombinedDockerConfig>);
            })
            .As <Task <ICombinedConfigProvider <CombinedDockerConfig> > >()
            .SingleInstance();

            // ICommandFactory
            builder.Register(
                async c =>
            {
                var moduleManager = c.Resolve <IModuleManager>();
                ICombinedConfigProvider <CombinedDockerConfig> combinedDockerConfigProvider = await c.Resolve <Task <ICombinedConfigProvider <CombinedDockerConfig> > >();
                IConfigSource configSource = await c.Resolve <Task <IConfigSource> >();
                var edgeletCommandFactory  = new EdgeletCommandFactory <CombinedDockerConfig>(moduleManager, configSource, combinedDockerConfigProvider);
                return(new LoggingCommandFactory(edgeletCommandFactory, c.Resolve <ILoggerFactory>()) as ICommandFactory);
            })
            .As <Task <ICommandFactory> >()
            .SingleInstance();

            // Task<IRuntimeInfoProvider>
            builder.Register(c => Task.FromResult(new RuntimeInfoProvider <DockerReportedConfig>(c.Resolve <IModuleManager>()) as IRuntimeInfoProvider))
            .As <Task <IRuntimeInfoProvider> >()
            .SingleInstance();

            // Task<IEnvironmentProvider>
            builder.Register(
                async c =>
            {
                var moduleStateStore     = c.Resolve <IEntityStore <string, ModuleState> >();
                var restartPolicyManager = c.Resolve <IRestartPolicyManager>();
                IRuntimeInfoProvider runtimeInfoProvider       = await c.Resolve <Task <IRuntimeInfoProvider> >();
                IEnvironmentProvider dockerEnvironmentProvider = await DockerEnvironmentProvider.CreateAsync(runtimeInfoProvider, moduleStateStore, restartPolicyManager);
                return(dockerEnvironmentProvider);
            })
            .As <Task <IEnvironmentProvider> >()
            .SingleInstance();
        }