//--// public bool GetInputs(out CloudWebDeployInputs result) { result = new CloudWebDeployInputs( ); result.Credentials = AzureCredentialsProvider.GetUserSubscriptionCredentials( ); if (result.Credentials == null) { result = null; return(false); } Console.WriteLine("Enter suggested namespace prefix: "); result.NamePrefix = Console.ReadLine( ); if (string.IsNullOrEmpty(result.NamePrefix)) { result = null; return(false); } Console.WriteLine("Enter suggested location: "); result.Location = Console.ReadLine( ); if (string.IsNullOrEmpty(result.Location)) { result.Location = "Central US"; } result.SBNamespace = result.NamePrefix + "-ns"; result.StorageAccountName = result.NamePrefix.ToLowerInvariant() + "storage"; result.EventHubNameDevices = "ehdevices"; result.EventHubNameAlerts = "ehalerts"; result.WebSiteDirectory = "..\\..\\..\\..\\WebSite\\ConnectTheDotsWebSite"; // Default for running the tool from the bin/debug or bin/release directory (i.e within VS) return(true); }
//--// public bool GetInputs(out CloudWebDeployInputs result) { result = new CloudWebDeployInputs( ); result.Credentials = AzureConsoleHelper.GetUserSubscriptionCredentials( ); if (result.Credentials == null) { result = null; return(false); } ServiceBusNamespace selectedNamespace = AzureConsoleHelper.SelectNamespace(result.Credentials); if (selectedNamespace == null) { result = null; Console.WriteLine("Quiting..."); return(false); } result.NamePrefix = selectedNamespace.Name; if (result.NamePrefix.EndsWith("-ns")) { result.NamePrefix = result.NamePrefix.Substring(0, result.NamePrefix.Length - 3); } result.SBNamespace = selectedNamespace.Name; result.StorageAccountName = result.NamePrefix.ToLowerInvariant( ) + "storage"; result.Location = selectedNamespace.Region; return(true); }
//--// public bool GetInputs(out CloudWebDeployInputs result) { result = new CloudWebDeployInputs( ); result.Credentials = AzureConsoleHelper.GetUserSubscriptionCredentials( ); if (result.Credentials == null) { result = null; return(false); } ServiceBusNamespace selectedNamespace = AzureConsoleHelper.SelectNamespace(result.Credentials); if (selectedNamespace == null) { result = null; Console.WriteLine("Quiting..."); return(false); } result.NamePrefix = selectedNamespace.Name; result.Location = selectedNamespace.Region; result.SBNamespace = result.NamePrefix + "-ns"; result.StorageAccountName = result.NamePrefix.ToLowerInvariant( ) + "storage"; result.EventHubNameDevices = "ehdevices"; result.EventHubNameAlerts = "ehalerts"; return(true); }
//--// public bool GetInputs( out CloudWebDeployInputs result ) { result = new CloudWebDeployInputs( ); result.Credentials = AzureConsoleHelper.GetUserSubscriptionCredentials( ); if( result.Credentials == null ) { result = null; return false; } ServiceBusNamespace selectedNamespace = AzureConsoleHelper.SelectNamespace( result.Credentials ); if( selectedNamespace == null ) { result = null; Console.WriteLine( "Quiting..." ); return false; } /* Console.WriteLine( "Need to select or not Transform flag." ); Console.WriteLine( "If selected, the input and output file name will be \"web.config\" placed in Web project location." ); Console.WriteLine( "Otherwise, input file name will be \"web.PublishTemplate.config\" and output - \"" + String.Format("web.{0}.config", result.NamePrefix) + "\"."); for( ;; ) { Console.WriteLine( "Do you want to use Transform flag? (y/n)" ); string answer = Console.ReadLine( ); string request = "not use"; result.Transform = false; if( !string.IsNullOrEmpty( answer ) && answer.ToLower( ).StartsWith( "y" ) ) { result.Transform = true; request = "use"; } if( ConsoleHelper.Confirm( "Are you sure you want to " + request + " Transform flag?" ) ) { break; } } */ result.NamePrefix = selectedNamespace.Name; if( result.NamePrefix.EndsWith( "-ns") ) { result.NamePrefix = result.NamePrefix.Substring( 0, result.NamePrefix.Length - 3 ); } result.SBNamespace = selectedNamespace.Name; result.StorageAccountName = result.NamePrefix.ToLowerInvariant( ) + "storage"; result.Location = selectedNamespace.Region; result.EventHubNameDevices = "ehdevices"; result.EventHubNameAlerts = "ehalerts"; //result.WebSiteDirectory = "..\\..\\..\\..\\WebSite\\ConnectTheDotsWebSite"; // Default for running the tool from the bin/debug or bin/release directory (i.e within VS) return true; }
//--// public bool GetInputs(out CloudWebDeployInputs result) { result = new CloudWebDeployInputs( ); result.Credentials = AzureConsoleHelper.GetUserSubscriptionCredentials( ); if (result.Credentials == null) { result = null; return(false); } ServiceBusNamespace selectedNamespace = AzureConsoleHelper.SelectNamespace(result.Credentials); if (selectedNamespace == null) { result = null; Console.WriteLine("Quiting..."); return(false); } result.NamePrefix = selectedNamespace.Name; result.Location = selectedNamespace.Region; /* * Console.WriteLine( "Need to select or not Transform flag." ); * Console.WriteLine( "If selected, the input and output file name will be \"web.config\" placed in Web project location." ); * Console.WriteLine( "Otherwise, input file name will be \"web.PublishTemplate.config\" and output - \"" + * String.Format("web.{0}.config", result.NamePrefix) + "\"."); * * for( ;; ) * { * Console.WriteLine( "Do you want to use Transform flag? (y/n)" ); * * string answer = Console.ReadLine( ); * string request = "not use"; * result.Transform = false; * if( !string.IsNullOrEmpty( answer ) && answer.ToLower( ).StartsWith( "y" ) ) * { * result.Transform = true; * request = "use"; * } * if( ConsoleHelper.Confirm( "Are you sure you want to " + request + " Transform flag?" ) ) * { * break; * } * } */ result.SBNamespace = result.NamePrefix + "-ns"; result.StorageAccountName = result.NamePrefix.ToLowerInvariant( ) + "storage"; result.EventHubNameDevices = "ehdevices"; result.EventHubNameAlerts = "ehalerts"; //result.WebSiteDirectory = "..\\..\\..\\..\\WebSite\\ConnectTheDotsWebSite"; // Default for running the tool from the bin/debug or bin/release directory (i.e within VS) return(true); }
private bool SelectNamespace(ref CloudWebDeployInputs inputs) { Console.WriteLine("Retrieving a list of created namespaces..."); ServiceBusNamespace[] namespaces = AzureHelper.GetNamespaces(inputs.Credentials); int namespaceCount = namespaces.Length; Console.WriteLine("Created namespaces: "); for (int currentNamespace = 1; currentNamespace <= namespaceCount; ++currentNamespace) { Console.WriteLine(currentNamespace + ": " + namespaces[currentNamespace - 1].Name + " (" + namespaces[currentNamespace - 1].Region + ")"); } Console.WriteLine("0: Exit without processing"); for ( ;;) { Console.WriteLine("Please select namespace you want to use: "); string answer = Console.ReadLine( ); int selection = 0; if (!int.TryParse(answer, out selection) || selection > namespaceCount || selection < 0) { Console.WriteLine("Incorrect namespace number."); continue; } if (selection == 0) { return(false); } if (ConsoleHelper.Confirm("Are you sure you want to select " + namespaces[selection - 1].Name + " namespace?")) { if (namespaces[selection - 1].Name.EndsWith("-ns")) { namespaces[selection - 1].Name = namespaces[selection - 1].Name.Substring(0, namespaces[selection - 1].Name.Length - 3); } inputs.NamePrefix = namespaces[selection - 1].Name; inputs.Location = namespaces[selection - 1].Region; return(true); } } }
public bool Run( ) { CloudWebDeployInputs inputs = null; if (!GetInputs(out inputs)) { return(false); } if (!CreateWeb(inputs)) { return(false); } Console.ReadLine( ); return(true); }
public bool Run( ) { CloudWebDeployInputs inputs = null; if (!GetInputs(out inputs)) { return(false); } if (!CreateWeb(inputs)) { return(false); } Console.WriteLine("Please hit enter to close."); Console.ReadLine( ); return(true); }
private bool CreateWeb( CloudWebDeployInputs inputs ) { Console.WriteLine( "Retrieving namespace metadata..." ); // Create Namespace ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient( inputs.Credentials ); var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription( inputs.SBNamespace ); string nsConnectionString = nsDescription.NamespaceDescriptions.First( ( d ) => String.Equals( d.AuthorizationType, "SharedAccessAuthorization" ) ).ConnectionString; NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString( nsConnectionString ); StorageManagementClient stgMgmt = new StorageManagementClient( inputs.Credentials ); var keyResponse = stgMgmt.StorageAccounts.GetKeys( inputs.StorageAccountName.ToLowerInvariant( ) ); if( keyResponse.StatusCode != System.Net.HttpStatusCode.OK ) { Console.WriteLine( "Error retrieving access keys for storage account {0} in Location {1}: {2}", inputs.StorageAccountName, inputs.Location, keyResponse.StatusCode ); return false; } var storageKey = keyResponse.PrimaryKey; EventHubDescription ehDevices = nsManager.GetEventHub( inputs.EventHubNameDevices ); string ehDevicesWebSiteConnectionString = new ServiceBusConnectionStringBuilder( nsConnectionString ) { SharedAccessKeyName = "WebSite", SharedAccessKey = ( ehDevices.Authorization.First( ( d ) => String.Equals( d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase) ) as SharedAccessAuthorizationRule ).PrimaryKey, }.ToString( ); string ehAlertsWebSiteConnectionString = string.Empty; try { EventHubDescription ehAlerts = nsManager.GetEventHub( inputs.EventHubNameAlerts ); ehAlertsWebSiteConnectionString = new ServiceBusConnectionStringBuilder( nsConnectionString ) { SharedAccessKeyName = "WebSite", SharedAccessKey = ( ehAlerts.Authorization.First( ( d ) => String.Equals( d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase ) ) as SharedAccessAuthorizationRule ).PrimaryKey, }.ToString( ); } catch { } Console.WriteLine( "Started processing..." ); // Write a new web.config template file var doc = new XmlDocument { PreserveWhitespace = true }; //var inputFileName = ( inputs.Transform ? "\\web.PublishTemplate.config" : "\\web.config" ); string inputFileName = "web.PublishTemplate.config"; //var outputFileName = ( inputs.Transform ? String.Format("\\web.{0}.config", inputs.NamePrefix) : "\\web.config" ); string outputFileName = "web.config"; //doc.Load( inputs.WebSiteDirectory + inputFileName ); string inputFilePath = Environment.CurrentDirectory +@"\"; Console.WriteLine("Opening and updating " + inputFilePath + inputFileName); doc.Load( inputFilePath + inputFileName ); doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubDevices']/@value" ).Value = inputs.EventHubNameDevices; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubAlerts']/@value" ) .Value = inputs.EventHubNameAlerts; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionString']/@value" ).Value = nsConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringDevices']/@value" ).Value = ehDevicesWebSiteConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringAlerts']/@value" ).Value = ehAlertsWebSiteConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.Storage.ConnectionString']/@value" ) .Value = String.Format( "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", inputs.StorageAccountName, storageKey ); //var outputFile = System.IO.Path.GetFullPath( inputs.WebSiteDirectory + outputFileName ); string outputFilePath = Environment.GetFolderPath( Environment.SpecialFolder.Desktop ); //Console.WriteLine(outputFilePath); var outputFile = outputFilePath + @"\" + outputFileName; Console.WriteLine( "Writing updates to " + outputFile ); doc.Save( outputFile ); Console.WriteLine( " " ); Console.WriteLine( "Web config saved to {0}", outputFile ); Console.WriteLine( " " ); return true; }
private bool CreateWeb(CloudWebDeployInputs inputs) { Console.WriteLine("Retrieving namespace metadata..."); // Create Namespace ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient(inputs.Credentials); var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription(inputs.SBNamespace); string nsConnectionString = nsDescription.NamespaceDescriptions.First( (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization") ).ConnectionString; NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString); EventHubDescription ehDevices = nsManager.GetEventHub(inputs.EventHubNameDevices); EventHubDescription ehAlerts = nsManager.GetEventHub(inputs.EventHubNameAlerts); StorageManagementClient stgMgmt = new StorageManagementClient(inputs.Credentials); var keyResponse = stgMgmt.StorageAccounts.GetKeys(inputs.StorageAccountName.ToLowerInvariant( )); if (keyResponse.StatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("Error retrieving access keys for storage account {0} in Location {1}: {2}", inputs.StorageAccountName, inputs.Location, keyResponse.StatusCode); return(false); } var storageKey = keyResponse.PrimaryKey; string ehDevicesWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString) { SharedAccessKeyName = "WebSite", SharedAccessKey = (ehDevices.Authorization.First((d) => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey, }.ToString( ); string ehAlertsWebSiteConnectionString = new ServiceBusConnectionStringBuilder(nsConnectionString) { SharedAccessKeyName = "WebSite", SharedAccessKey = (ehAlerts.Authorization.First((d) => String.Equals(d.KeyName, "WebSite", StringComparison.InvariantCultureIgnoreCase)) as SharedAccessAuthorizationRule).PrimaryKey, }.ToString( ); Console.WriteLine("Started processing..."); // Write a new web.config template file var doc = new XmlDocument { PreserveWhitespace = true }; //var inputFileName = ( inputs.Transform ? "\\web.PublishTemplate.config" : "\\web.config" ); string inputFileName = "web.PublishTemplate.config"; //var outputFileName = ( inputs.Transform ? String.Format("\\web.{0}.config", inputs.NamePrefix) : "\\web.config" ); string outputFileName = "web.config"; //doc.Load( inputs.WebSiteDirectory + inputFileName ); string inputFilePath = Environment.CurrentDirectory + @"\"; Console.WriteLine("Opening and updating " + inputFilePath + inputFileName); doc.Load(inputFilePath + inputFileName); doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubDevices']/@value").Value = inputs.EventHubNameDevices; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.ServiceBus.EventHubAlerts']/@value") .Value = inputs.EventHubNameAlerts; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionString']/@value").Value = nsConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringDevices']/@value").Value = ehDevicesWebSiteConnectionString; doc.SelectSingleNode( "/configuration/appSettings/add[@key='Microsoft.ServiceBus.ConnectionStringAlerts']/@value").Value = ehAlertsWebSiteConnectionString; doc.SelectSingleNode("/configuration/appSettings/add[@key='Microsoft.Storage.ConnectionString']/@value") .Value = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", inputs.StorageAccountName, storageKey); //var outputFile = System.IO.Path.GetFullPath( inputs.WebSiteDirectory + outputFileName ); string outputFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //Console.WriteLine(outputFilePath); var outputFile = outputFilePath + @"\" + outputFileName; Console.WriteLine("Writing updates to " + outputFile); doc.Save(outputFile); Console.WriteLine(" "); Console.WriteLine("Web config saved to {0}", outputFile); Console.WriteLine(" "); return(true); }
bool Run( ) { CloudWebDeployInputs inputs = null; if (!GetInputs(out inputs)) { return(false); } Console.WriteLine("Retrieving namespace metadata..."); // Create Namespace ServiceBusManagementClient sbMgmt = new ServiceBusManagementClient(inputs.Credentials); var nsDescription = sbMgmt.Namespaces.GetNamespaceDescription(inputs.SBNamespace); string nsConnectionString = nsDescription.NamespaceDescriptions.First( (d) => String.Equals(d.AuthorizationType, "SharedAccessAuthorization") ).ConnectionString; NamespaceManager nsManager = NamespaceManager.CreateFromConnectionString(nsConnectionString); EventHubDescription ehDevices = AzureConsoleHelper.SelectEventHub(nsManager, inputs.Credentials); var serviceNamespace = inputs.SBNamespace; var hubName = ehDevices.Path; var sharedAccessAuthorizationRule = ehDevices.Authorization.FirstOrDefault((d) => d.Rights.Contains(AccessRights.Listen)) as SharedAccessAuthorizationRule; if (sharedAccessAuthorizationRule == null) { Console.WriteLine("Cannot locate Authorization rule for WebSite key."); return(false); } var receiverKeyName = sharedAccessAuthorizationRule.KeyName; var receiverKey = sharedAccessAuthorizationRule.PrimaryKey; //Console.WriteLine("Starting temperature processor with {0} partitions.", partitionCount); CancellationTokenSource cts = new CancellationTokenSource( ); int closedReceivers = 0; AutoResetEvent receiversStopped = new AutoResetEvent(false); MessagingFactory factory = MessagingFactory.Create(ServiceBusEnvironment.CreateServiceUri("sb", serviceNamespace, ""), new MessagingFactorySettings { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(receiverKeyName, receiverKey), TransportType = TransportType.Amqp }); EventHubClient eventHubClient = factory.CreateEventHubClient(hubName); EventHubConsumerGroup eventHubConsumerGroup = eventHubClient.GetDefaultConsumerGroup( ); int partitionCount = ehDevices.PartitionCount; for (int i = 0; i < partitionCount; i++) { Task.Factory.StartNew((state) => { try { _ConsoleBuffer.Add(string.Format("Starting worker to process partition: {0}", state)); var receiver = eventHubConsumerGroup.CreateReceiver(state.ToString( ), DateTime.UtcNow); _ConsoleBuffer.Add(string.Format("Waiting for start receiving messages: {0} ...", state)); while (true) { // Receive could fail, I would need a retry policy etc... var messages = receiver.Receive(10); foreach (var message in messages) { //var eventBody = Newtonsoft.Json.JsonConvert.DeserializeObject<TemperatureEvent>(Encoding.Default.GetString(message.GetBytes())); //Console.WriteLine("{0} [{1}] Temperature: {2}", DateTime.Now, message.PartitionKey, eventBody.Temperature); _ConsoleBuffer.Add(message.PartitionKey + " sent message:" + Encoding.Default.GetString(message.GetBytes( ))); } if (cts.IsCancellationRequested) { Console.WriteLine("Stopping: {0}", state); receiver.Close( ); if (Interlocked.Increment(ref closedReceivers) >= partitionCount) { receiversStopped.Set(); } break; } } } catch (Exception ex) { _ConsoleBuffer.Add(ex.Message); } }, i); } Console.ReadLine( ); cts.Cancel( ); //waiting for all receivers to stop receiversStopped.WaitOne( ); bool saveToFile; for ( ;;) { Console.WriteLine("Do you want to save received data to file? (y/n)"); string answer = Console.ReadLine( ); string request = "do not"; saveToFile = false; if (!string.IsNullOrEmpty(answer) && answer.ToLower( ).StartsWith("y")) { saveToFile = true; request = ""; } if (ConsoleHelper.Confirm("Are you sure you " + request + " want to save received data?")) { break; } } if (saveToFile) { string fileName = inputs.SBNamespace + DateTime.UtcNow.ToString("_d_MMM_h_mm") + ".log"; string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string fileFullName = filePath + @"\" + fileName; if (_ConsoleBuffer.FlushToFile(fileFullName)) { Console.WriteLine("Output was saved to your desktop, at " + fileFullName + " file."); } } Console.WriteLine("Wait for all receivers to close and then press ENTER."); Console.ReadLine( ); return(true); }