private async Task GenerateDockerOutline(DockerOutline outline, ProvisioningData provisioningData, IoTHubConnectionStrings iotHubConnectionStrings) { await Task.Run(() => { int serviceCount = 0; foreach (KeyValuePair <string, List <DeviceDescription> > provisioningEntry in provisioningData) { iotHubConnectionStrings.TryGetValue(provisioningEntry.Key, out string iotHubConnectionString); foreach (DeviceDescription device in provisioningEntry.Value) { serviceCount++; string deviceType = device.name.ToLower(); string serviceKey = $"sh.d.{deviceType}.{provisioningEntry.Key.ToLower()}"; var service = new ServiceDescription { image = GetContainerImageName(deviceType), environment = new List <EnvironmentSetting>() }; service.environment.Add(new EnvironmentSetting { name = HardwareIdSetting, value = device.hardwareId }); service.environment.Add(new EnvironmentSetting { name = DigitalTwinsManagementApiSetting, value = DigitalTwinsApiEndpoint }); service.environment.Add(new EnvironmentSetting { name = MessageIntervalSetting, value = MessageInterval > 0 ? MessageInterval.ToString() : MessageIntervalDefault.ToString() }); service.environment.Add(new EnvironmentSetting { name = SasTokenSetting, value = device.SasToken }); service.environment.Add(new EnvironmentSetting { name = IoTHubConnectionStringSetting, value = iotHubConnectionString }); service.environment.Add(new EnvironmentSetting { name = StartupDelayInSecondsSetting, value = Math.Floor(serviceCount / DigitalTwinsApiCallLimiter).ToString() }); outline.services.Add(serviceKey, service); } } }); }
private async Task ProcessDockerProvisioningData(DockerOutline outline, ProvisioningData provisioningData) { await Task.Run(async() => { string iotFile = await File.ReadAllTextAsync(IoTConnectionFile); Dictionary <string, Dictionary <string, string> > iotInfo = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, string> > >(iotFile); foreach (var kvPair in outline.services) { var service = kvPair.Value; var containerImage = FormatContainerImageName(service.image); service.image = containerImage; if (service.environment != null) { var apiUrlSetting = service.environment.FirstOrDefault(e => e.name == DigitalTwinsManagementApiSetting); if (apiUrlSetting != null) { apiUrlSetting.value = DigitalTwinsApiEndpoint; } var messageIntervalSetting = service.environment.FirstOrDefault(e => e.name == MessageIntervalSetting); if (messageIntervalSetting != null) { messageIntervalSetting.value = MessageInterval > 0 ? MessageInterval.ToString() : MessageIntervalDefault.ToString(); } var qualifiers = kvPair.Key.Split('.'); var name = qualifiers.Last().ToLower(); if (provisioningData.ContainsKey(name)) { var serviceType = service.environment.FirstOrDefault(e => e.name == SensorTypeSetting); if (serviceType != null) { if (iotInfo.ContainsKey(name)) { Dictionary <string, string> iotConnectionStrings = iotInfo[name]; if (iotConnectionStrings.ContainsKey(serviceType.value.ToLower())) { var iotSetting = service.environment.FirstOrDefault(s => s.name == IoTHubConnectionStringSetting); if (iotSetting != null) { iotSetting.value = iotConnectionStrings[serviceType.value.ToLower()]; } } } DeviceDescription device = provisioningData[name].FirstOrDefault(d => d.sensors.Any(s => s.dataType.ToLower() == serviceType.value.ToLower())); if (device != null) { var sasTokenSetting = service.environment.FirstOrDefault(e => e.name == SasTokenSetting); if (sasTokenSetting != null) { sasTokenSetting.value = device.SasToken; } var hardwareIdSetting = service.environment.FirstOrDefault(e => e.name == HardwareIdSetting); if (hardwareIdSetting != null) { hardwareIdSetting.value = device.hardwareId; } } } } } } }); }
private async Task ProcessKubernetesProvisioningData(KubernetesOutline outline, ProvisioningData provisioningData) { await Task.Run(async() => { string iotFile = await File.ReadAllTextAsync(IoTConnectionFile); Dictionary <string, Dictionary <string, string> > iotInfo = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, string> > >(iotFile); foreach (var deployment in outline.Deployments) { try { var metadata = deployment.metadata; if (metadata != null) { var qualifiers = metadata.name.Split('.'); var room = qualifiers.Last().ToLower(); if (provisioningData.ContainsKey(room)) { foreach (var container in deployment.spec.template.spec.containers) { var apiUrlSetting = container.env.FirstOrDefault(e => e.name == DigitalTwinsManagementApiSetting); if (apiUrlSetting != null) { apiUrlSetting.value = DigitalTwinsApiEndpoint; } var messageIntervalSetting = container.env.FirstOrDefault(e => e.name == MessageIntervalSetting); if (messageIntervalSetting != null) { messageIntervalSetting.value = MessageInterval > 0 ? MessageInterval.ToString() : MessageIntervalDefault.ToString(); } var containerImage = FormatContainerImageName(container.image); container.image = containerImage; var typeSetting = container.env.FirstOrDefault(s => s.name == SensorTypeSetting); if (typeSetting != null) { if (iotInfo.ContainsKey(room)) { Dictionary <string, string> iotConnectionStrings = iotInfo[room]; if (iotConnectionStrings.ContainsKey(typeSetting.value.ToLower())) { var iotSetting = container.env.FirstOrDefault(s => s.name == IoTHubConnectionStringSetting); if (iotSetting != null) { iotSetting.value = iotConnectionStrings[typeSetting.value.ToLower()]; } } } DeviceDescription device = provisioningData[room].FirstOrDefault(d => d.sensors.Any(s => s.dataType.ToLower() == typeSetting.value.ToLower())); if (device != null) { var sasTokenSetting = container.env.FirstOrDefault(e => e.name == SasTokenSetting); if (sasTokenSetting != null) { sasTokenSetting.value = device.SasToken; } var hardwareIdSetting = container.env.FirstOrDefault(e => e.name == HardwareIdSetting); if (hardwareIdSetting != null) { hardwareIdSetting.value = device.hardwareId; } } } } } } } catch (Exception e) { Console.WriteLine(e); } } }); }
private async Task ProcessExistingDockerOutline(DockerOutline outline, ProvisioningData provisioningData, IoTHubConnectionStrings iotHubConnectionStrings) { await Task.Run(() => { foreach (var kvPair in outline.services) { var service = kvPair.Value; var containerImage = FormatContainerImageName(service.image); service.image = containerImage; if (service.environment != null) { var apiUrlSetting = service.environment.FirstOrDefault(e => e.name == DigitalTwinsManagementApiSetting); if (apiUrlSetting != null) { apiUrlSetting.value = DigitalTwinsApiEndpoint; } var messageIntervalSetting = service.environment.FirstOrDefault(e => e.name == MessageIntervalSetting); if (messageIntervalSetting != null) { messageIntervalSetting.value = MessageInterval > 0 ? MessageInterval.ToString() : MessageIntervalDefault.ToString(); } var qualifiers = kvPair.Key.Split('.'); var name = qualifiers.Last().ToLower(); if (provisioningData.ContainsKey(name)) { if (iotHubConnectionStrings.ContainsKey(name)) { string iotConnectionString = iotHubConnectionStrings[name]; var iotSetting = service.environment.FirstOrDefault(s => s.name == IoTHubConnectionStringSetting); if (iotSetting != null) { iotSetting.value = iotConnectionString; } } DeviceDescription device = provisioningData[name].FirstOrDefault(); if (device != null) { var sasTokenSetting = service.environment.FirstOrDefault(e => e.name == SasTokenSetting); if (sasTokenSetting != null) { sasTokenSetting.value = device.SasToken; } var hardwareIdSetting = service.environment.FirstOrDefault(e => e.name == HardwareIdSetting); if (hardwareIdSetting != null) { hardwareIdSetting.value = device.hardwareId; } } } } } }); }
private async Task GenerateKubernetesOutline(KubernetesOutline outline, ProvisioningData provisioningData, IoTHubConnectionStrings iotHubConnectionStrings) { await Task.Run(() => { int deploymentCount = 0; foreach (KeyValuePair <string, List <DeviceDescription> > provisioningEntry in provisioningData) { iotHubConnectionStrings.TryGetValue(provisioningEntry.Key, out string iotHubConnectionString); foreach (DeviceDescription device in provisioningEntry.Value) { deploymentCount++; string deviceType = device.name.ToLower(); string serviceKey = $"sh.d.{deviceType}.{provisioningEntry.Key.ToLower()}"; var container = new KubernetesContainer { name = $"device-{deviceType}", image = GetContainerImageName(deviceType), imagePullPolicy = "Always", env = new List <KubernetesEnvironmentSetting>() }; container.env.Add(new KubernetesEnvironmentSetting { name = HardwareIdSetting, value = device.hardwareId }); container.env.Add(new KubernetesEnvironmentSetting { name = IoTHubConnectionStringSetting, value = iotHubConnectionString }); container.env.Add(new KubernetesEnvironmentSetting { name = DigitalTwinsManagementApiSetting, value = DigitalTwinsApiEndpoint }); container.env.Add(new KubernetesEnvironmentSetting { name = MessageIntervalSetting, value = MessageInterval > 0 ? MessageInterval.ToString() : MessageIntervalDefault.ToString() }); container.env.Add(new KubernetesEnvironmentSetting { name = SasTokenSetting, value = device.SasToken }); container.env.Add(new KubernetesEnvironmentSetting { name = StartupDelayInSecondsSetting, value = Math.Floor(deploymentCount / DigitalTwinsApiCallLimiter).ToString() }); var template = new KubernetesTemplate { metadata = new KubernetesMetadata { labels = new KubernetesLabels { app = serviceKey, component = serviceKey } }, spec = new KubernetesSpec { containers = new List <KubernetesContainer> { container } } }; var spec = new KubernetesSpec { template = template }; var deployment = new KubernetesDeployment { apiVersion = "extensions/v1beta1", kind = "Deployment", metadata = new KubernetesMetadata { name = serviceKey }, spec = spec }; outline.Deployments.Add(deployment); } } }); }
private async Task ProcessExistingKubernetesOutline(KubernetesOutline outline, ProvisioningData provisioningData, IoTHubConnectionStrings iotHubConnectionStrings) { await Task.Run(() => { foreach (var deployment in outline.Deployments) { try { var metadata = deployment.metadata; if (metadata != null) { var qualifiers = metadata.name.Split('.'); var room = qualifiers.Last().ToLower(); if (provisioningData.ContainsKey(room)) { foreach (var container in deployment.spec.template.spec.containers) { var apiUrlSetting = container.env.FirstOrDefault(e => e.name == DigitalTwinsManagementApiSetting); if (apiUrlSetting != null) { apiUrlSetting.value = DigitalTwinsApiEndpoint; } var messageIntervalSetting = container.env.FirstOrDefault(e => e.name == MessageIntervalSetting); if (messageIntervalSetting != null) { messageIntervalSetting.value = MessageInterval > 0 ? MessageInterval.ToString() : MessageIntervalDefault.ToString(); } var containerImage = FormatContainerImageName(container.image); container.image = containerImage; if (iotHubConnectionStrings.ContainsKey(room)) { string connectionString = iotHubConnectionStrings[room]; var iotSetting = container.env.FirstOrDefault(s => s.name == IoTHubConnectionStringSetting); if (iotSetting != null) { iotSetting.value = connectionString; } } DeviceDescription device = provisioningData[room].FirstOrDefault(); if (device != null) { var sasTokenSetting = container.env.FirstOrDefault(e => e.name == SasTokenSetting); if (sasTokenSetting != null) { sasTokenSetting.value = device.SasToken; } var hardwareIdSetting = container.env.FirstOrDefault(e => e.name == HardwareIdSetting); if (hardwareIdSetting != null) { hardwareIdSetting.value = device.hardwareId; } } } } } } catch (Exception e) { Console.WriteLine(e); } } }); }