/// <summary>
 /// Internal constructor which accept broker as connection
 /// </summary>
 internal AsyncAzureOperation(AzureManagementBroker broker, string operationId, bool completed)
 {
     this.completed = completed;
     // We really only need the client from broker
     this.client = new AzureManagementClient(broker.certificate, broker.subscriptionId);
     this.status = AzureOperationStatus.InProgress;
     this.errorCode = null;
     this.errorMessage = null;
     this.operationId = operationId;
 }
Exemple #2
0
        /// <summary>
        /// After configuring the .cscfg file, this function publishes the package to Azure
        /// </summary>
        public bool PublishServiceToAzure(ReportProgress reportDelegate, ReDoInitializeDB reDoInitializeDB)
        {
            this.reportDelegate = reportDelegate;

            report(10, "Creating a broker object ...");

            AzureManagementBroker broker;

            try
            {
                // Create hostedservice if it does not exist in Azure
                broker = new AzureManagementBroker(_settings.DeploymentName,
                                                   _settings.ManagementCert.Thumbprint,
                                                   new Guid(_settings.SubscriptionId),
                                                   _settings.ServiceName,
                                                   _settings.StorageName,
                                                   _settings.StorageKey,
                                                   null,
                                                   null);

                try
                {
                    broker.CreateHostedServiceIfNotExist(_settings.ServiceName, _settings.Location);
                }
                catch (Exception ex)
                {
                    report(10, "Error: " + ex.Message + ". Please check and try later.");
                    return false;
                }

                report(20, "Adding the certificate to service ...");

                IAsyncAzureOperation opServiceCert = broker.AddCertificateToService(_settings.ServiceCert, Password, CertificateLocation.AzureManagement);
                opServiceCert.WaitForCompletion(TimeSpan.MaxValue);
                if (opServiceCert.GetOperationStatus() == AzureOperationStatus.Failed)
                {
                    report(20, "Error: " + opServiceCert.ErrorMessage());
                    return false;
                }

                /***************************************************************************************
                //**** Create two default firewall filters for the newly-created SQL server
                //1:   0.0.0.0  to 0.0.0.0
                //2.   IP address to IP address
                ***************************************************************************************/
                report(30, "Creating firewall rules ...");

                broker.CreateFirewallRule(_settings.SqlServerName);
                string localIP = broker.CreateFirewallRuleWithIPDetect(_settings.SqlServerName, "FirewallWithIP");
                string[] IPRange = GenerateIPRange(localIP);
                broker.CreateFirewallRule(_settings.SqlServerName, "RangeFireWall", IPRange[0], IPRange[1]);

                // If a user sets InitializeDBOnline = false, then do the initializeDB from local machine
                // or else, create database and InitializeDB at Azure
                if (!_settings.InitializeDBOnline)
                {
                    report(40, "Creating database ...");

                    // Create a new database under the newly-created SQL Server
                    try
                    {
                        List<string> listOfDB = GetExistingDatabases(_settings.SqlServerName, _settings.SqlServerUser, _settings.SqlServerPassword);

                        // If no database named as settings.DBName is found, create one
                        if (listOfDB.IndexOf(_settings.DBName) == -1)
                        {
                            CreateDatabase(_settings.SqlServerName, _settings.DBName, _settings.SqlServerUser, _settings.SqlServerPassword, Maxsize);
                        }
                    }
                    catch (SqlException e)
                    {
                        report(40, "Error from SQL server " + e.Server + ": " + e.Message);
                        return false;
                    }
                    catch (Exception e)
                    {
                        report(40, "Error: " + e.Message);
                        return false;
                    }

                    report(50, "Initializing database ...");

                    if (DoInitializeDB(report, reDoInitializeDB, true) == false)
                    {
                        report(50, "Error: Initializing database failed. Please check AzureSampleService.log for details.");
                        return false;
                    }
                }

                // Start to deploy the service
                AzureDeployment deployment = new AzureDeployment();

                FileInfo localFile;

                // Verify whether CspkgFile exists
                if (File.Exists(CspkgFile))
                {
                    // Upload package file to Azure first
                    localFile = new FileInfo(CspkgFile);
                }
                else
                {
                    report(50, "Error: Please copy the .cspkg file to the current working folder.");
                    return false;
                }

                try
                {
                    report(70, "Copying HPC package to blob storage ...");

                    broker.CopyHpcPackageToBlobStorage(localFile, DestinationName);

                    report(90, "Creating Azure deployment ...");

                    // Start to deploy the service now.
                    IAsyncAzureOperation op = broker.CreateAzureDeployment(File.ReadAllText(_settings.ServiceCscfg), DestinationName, out deployment);
                    op.WaitForCompletion(TimeSpan.MaxValue);
                    if (op.GetOperationStatus() != AzureOperationStatus.Succeeded)
                    {
                        report(90, "Error: " + op.ErrorMessage());
                        return false;
                    }

                    // Delete the service cscfg file
                    File.Delete((_settings.ServiceCscfg));
                }
                catch (Exception ex)
                {
                    report(90, "Error: " + ex.Message);
                    return false;
                }
            }
            catch (Exception exception)
            {
                report(90, "Error: " + exception.Message);
                return false;
            }

            report(100, "Client Portion Complete (Azure deploying now).");
            return true;
        }