public static void AreEqualPublishContext(ServiceSettings settings, string configPath, string deploymentName, string label, string packagePath, string subscriptionId, PublishContext actual) { AreEqualServiceSettings(settings, actual.ServiceSettings); Assert.AreEqual<string>(configPath, actual.ConfigPath); Assert.AreEqual<string>(deploymentName, actual.DeploymentName); Assert.AreEqual<string>(label, actual.ServiceName); Assert.AreEqual<string>(packagePath, actual.PackagePath); Assert.AreEqual<string>(subscriptionId, actual.SubscriptionId); Assert.IsTrue(File.Exists(actual.ConfigPath)); Assert.IsTrue(File.Exists(actual.PackagePath)); }
private Uri UploadPackage(PublishContext context) { WriteVerboseWithTimestamp( Resources.PublishUploadingPackageMessage, context.ServiceSettings.StorageServiceName); return(CloudBlobUtility.UploadPackageToBlob( ServiceManagementChannel, context.ServiceSettings.StorageServiceName, subscriptionId, context.PackagePath, new BlobRequestOptions())); }
private PublishContext CreatePublishContext( string name, string slot, string location, string affinityGroup, string storageServiceName, string deploymentName) { string serviceName; AzureService cloudServiceProject = GetCurrentServiceProject(); // If the name provided is different than existing name change it if (!string.IsNullOrEmpty(name) && name != cloudServiceProject.ServiceName) { cloudServiceProject.ChangeServiceName(name, cloudServiceProject.Paths); } // If there's no storage service provided, try using the default one if (string.IsNullOrEmpty(storageServiceName)) { storageServiceName = Subscription.CurrentStorageAccount; } // Use default location if not location and affinity group provided location = string.IsNullOrEmpty(location) && string.IsNullOrEmpty(affinityGroup) ? GetDefaultLocation() : location; ServiceSettings serviceSettings = ServiceSettings.LoadDefault( cloudServiceProject.Paths.Settings, slot, location, affinityGroup, Subscription.SubscriptionName, storageServiceName, name, cloudServiceProject.ServiceName, out serviceName ); PublishContext context = new PublishContext( serviceSettings, Path.Combine(GetCurrentDirectory(), cloudServiceProject.Paths.CloudPackage), Path.Combine(GetCurrentDirectory(), cloudServiceProject.Paths.CloudConfiguration), serviceName, deploymentName, cloudServiceProject.Paths.RootPath); return(context); }
public void TestDeploymentSettingsTestDoesNotConfigPathFail() { string label = "MyLabel"; string deploymentName = service.ServiceName; string doesNotExistDir = Path.Combine(Directory.GetCurrentDirectory(), "qewindw443298.cscfg"); try { PublishContext deploySettings = new PublishContext( settings, packagePath, doesNotExistDir, label, deploymentName, rootPath); Assert.Fail("No exception was thrown"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(FileNotFoundException)); Assert.AreEqual<string>(string.Format(Resources.PathDoesNotExistForElement, Resources.ServiceConfiguration, doesNotExistDir), ex.Message); } }
private void UpgradeDeployment(PublishContext context) { UpgradeDeploymentInput upgradeDeploymentInput = new UpgradeDeploymentInput { PackageUrl = UploadPackage(context), Configuration = General.GetConfiguration(context.ConfigPath), Label = context.ServiceName, Mode = UpgradeType.Auto }; WriteVerboseWithTimestamp(Resources.PublishUpgradingMessage); CertificateList uploadedCertificates = ServiceManagementChannel.ListCertificates( subscriptionId, context.ServiceName); AddCertificates(uploadedCertificates, context); ServiceManagementChannel.UpgradeDeploymentBySlot( subscriptionId, context.ServiceName, context.ServiceSettings.Slot, upgradeDeploymentInput); }
private void AddCertificates(CertificateList uploadedCertificates, PublishContext context) { string name = context.ServiceName; AzureService cloudServiceProject = new AzureService(context.RootPath, null); if (cloudServiceProject.Components.CloudConfig.Role != null) { foreach (ConfigCertificate certElement in cloudServiceProject.Components.CloudConfig.Role. SelectMany(r => r.Certificates ?? new ConfigCertificate[0]).Distinct()) { if (uploadedCertificates == null || (uploadedCertificates.Count(c => c.Thumbprint.Equals( certElement.thumbprint, StringComparison.OrdinalIgnoreCase)) < 1)) { X509Certificate2 cert = General.GetCertificateFromStore(certElement.thumbprint); CertificateFile certFile = null; try { certFile = new CertificateFile { Data = Convert.ToBase64String(cert.Export(X509ContentType.Pfx, string.Empty)), Password = string.Empty, CertificateFormat = "pfx" }; } catch (CryptographicException exception) { throw new ArgumentException(string.Format( Resources.CertificatePrivateKeyAccessError, certElement.name), exception); } CallSync(() => ServiceManagementChannel.AddCertificates(subscriptionId, name, certFile)); } } } }
private PublishContext CreatePublishContext( string name, string slot, string location, string affinityGroup, string storageServiceName, string deploymentName) { string serviceName; CloudServiceProject cloudServiceProject = GetCurrentServiceProject(); // If the name provided is different than existing name change it if (!string.IsNullOrEmpty(name) && name != cloudServiceProject.ServiceName) { cloudServiceProject.ChangeServiceName(name, cloudServiceProject.Paths); } // If there's no storage service provided, try using the default one if (string.IsNullOrEmpty(storageServiceName)) { storageServiceName = Subscription.CurrentStorageAccount; } // Use default location if not location and affinity group provided location = string.IsNullOrEmpty(location) && string.IsNullOrEmpty(affinityGroup) ? GetDefaultLocation() : location; ServiceSettings serviceSettings = ServiceSettings.LoadDefault( cloudServiceProject.Paths.Settings, slot, location, affinityGroup, Subscription.SubscriptionName, storageServiceName, name, cloudServiceProject.ServiceName, out serviceName ); PublishContext context = new PublishContext( serviceSettings, Path.Combine(GetCurrentDirectory(), cloudServiceProject.Paths.CloudPackage), Path.Combine(GetCurrentDirectory(), cloudServiceProject.Paths.CloudConfiguration), serviceName, deploymentName, cloudServiceProject.Paths.RootPath); return context; }
private void CreateDeployment(PublishContext context) { CreateDeploymentInput deploymentInput = new CreateDeploymentInput { PackageUrl = UploadPackage(context), Configuration = General.GetConfiguration(context.ConfigPath), Label = context.ServiceName, Name = context.DeploymentName, StartDeployment = true, }; WriteVerboseWithTimestamp(Resources.PublishStartingMessage); CertificateList uploadedCertificates = ServiceManagementChannel.ListCertificates( subscriptionId, context.ServiceName); AddCertificates(uploadedCertificates, context); ServiceManagementChannel.CreateOrUpdateDeployment( subscriptionId, context.ServiceName, context.ServiceSettings.Slot, deploymentInput); }
private void AddCertificates(CertificateList uploadedCertificates, PublishContext context) { string name = context.ServiceName; CloudServiceProject cloudServiceProject = new CloudServiceProject(context.RootPath, null); if (cloudServiceProject.Components.CloudConfig.Role != null) { foreach (ConfigCertificate certElement in cloudServiceProject.Components.CloudConfig.Role. SelectMany(r => r.Certificates ?? new ConfigCertificate[0]).Distinct()) { if (uploadedCertificates == null || (uploadedCertificates.Count(c => c.Thumbprint.Equals( certElement.thumbprint, StringComparison.OrdinalIgnoreCase)) < 1)) { X509Certificate2 cert = General.GetCertificateFromStore(certElement.thumbprint); CertificateFile certFile = null; try { certFile = new CertificateFile { Data = Convert.ToBase64String(cert.Export(X509ContentType.Pfx, string.Empty)), Password = string.Empty, CertificateFormat = "pfx" }; } catch (CryptographicException exception) { throw new ArgumentException(string.Format( Resources.CertificatePrivateKeyAccessError, certElement.name), exception); } CallSync(() => ServiceManagementChannel.AddCertificates(subscriptionId, name, certFile)); } } } }
private Uri UploadPackage(PublishContext context) { WriteVerboseWithTimestamp( Resources.PublishUploadingPackageMessage, context.ServiceSettings.StorageServiceName); return CloudBlobUtility.UploadPackageToBlob( ServiceManagementChannel, context.ServiceSettings.StorageServiceName, subscriptionId, context.PackagePath, new BlobRequestOptions()); }
private void UpdateCacheWorkerRolesCloudConfiguration(PublishContext context) { string connectionString = GetStorageServiceConnectionString(context.ServiceSettings.StorageServiceName); CloudServiceProject cloudServiceProject = new CloudServiceProject(context.RootPath, null); ConfigConfigurationSetting connectionStringConfig = new ConfigConfigurationSetting { name = Resources.CachingConfigStoreConnectionStringSettingName, value = string.Empty }; cloudServiceProject.Components.ForEachRoleSettings( r => Array.Exists<ConfigConfigurationSetting>(r.ConfigurationSettings, c => c.Equals(connectionStringConfig)), delegate(RoleSettings r) { int index = Array.IndexOf<ConfigConfigurationSetting>(r.ConfigurationSettings, connectionStringConfig); r.ConfigurationSettings[index] = new ConfigConfigurationSetting { name = Resources.CachingConfigStoreConnectionStringSettingName, value = connectionString }; }); cloudServiceProject.Components.Save(cloudServiceProject.Paths); }
public void TestDeploymentSettingsTestNullLabelFail() { string deploymentName = service.ServiceName; try { PublishContext deploySettings = new PublishContext( settings, packagePath, configPath, null, deploymentName, rootPath); Assert.Fail("No exception was thrown"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentException)); Assert.IsTrue(string.Compare( string.Format(Resources.InvalidOrEmptyArgumentMessage, "serviceName"), ex.Message, true) == 0); } }
/// <summary> /// Publishes a service project on Windows Azure. /// </summary> /// <param name="name">The cloud service name</param> /// <param name="slot">The deployment slot</param> /// <param name="location">The deployment location</param> /// <param name="affinityGroup">The deployment affinity group</param> /// <param name="storageAccount">The storage account to store the package</param> /// <param name="deploymentName">The deployment name</param> /// <param name="launch">Launch the service after publishing</param> /// <returns>The created deployment</returns> public Deployment PublishCloudService( string name = null, string slot = null, string location = null, string affinityGroup = null, string storageAccount = null, string deploymentName = null, bool launch = false) { // Initialize publish context PublishContext context = CreatePublishContext( name, slot, location, affinityGroup, storageAccount, deploymentName); WriteVerbose(string.Format(Resources.PublishServiceStartMessage, context.ServiceName)); // Set package runtime information WriteVerboseWithTimestamp(Resources.RuntimeDeploymentStart, context.ServiceName); PrepareCloudServicePackagesRuntime(context); // Verify storage account exists WriteVerboseWithTimestamp( Resources.PublishVerifyingStorageMessage, context.ServiceSettings.StorageServiceName); CreateStorageServiceIfNotExist( context.ServiceSettings.StorageServiceName, context.ServiceName, context.ServiceSettings.Location, context.ServiceSettings.AffinityGroup); // Update cache worker roles configuration WriteVerboseWithTimestamp( Resources.PublishPreparingDeploymentMessage, context.ServiceName, subscriptionId); UpdateCacheWorkerRolesCloudConfiguration(context); // Create cloud package AzureTool.Validate(); if (File.Exists(context.PackagePath)) { File.Delete(context.PackagePath); } AzureService cloudServiceProject = new AzureService(context.RootPath, null); string unused; cloudServiceProject.CreatePackage(DevEnv.Cloud, out unused, out unused); // Publish cloud service WriteVerboseWithTimestamp(Resources.PublishConnectingMessage); CreateCloudServiceIfNotExist( context.ServiceName, affinityGroup: context.ServiceSettings.AffinityGroup, location: context.ServiceSettings.Location); if (DeploymentExists(context.ServiceName, context.ServiceSettings.Slot)) { // Upgrade the deployment UpgradeDeployment(context); } else { // Create new deployment CreateDeployment(context); } // Get the deployment id and show it. WriteVerboseWithTimestamp(Resources.PublishCreatedDeploymentMessage, GetDeploymentId(context)); // Verify the deployment succeeded by checking that each of the roles are running VerifyDeployment(context); // Get object of the published deployment Deployment deployment = ServiceManagementChannel.GetDeploymentBySlot( subscriptionId, context.ServiceName, context.ServiceSettings.Slot); if (launch) { General.LaunchWebPage(deployment.Url.ToString()); } return(deployment); }
private void VerifyDeployment(PublishContext context) { try { WriteVerboseWithTimestamp(Resources.PublishInitializingMessage); Dictionary <string, RoleInstance> roleInstanceSnapshot = new Dictionary <string, RoleInstance>(); // Continue polling for deployment until all of the roles // indicate they're ready Deployment deployment = new Deployment(); do { deployment = ServiceManagementChannel.GetDeploymentBySlot( subscriptionId, context.ServiceName, context.ServiceSettings.Slot); // The goal of this loop is to output a message whenever the status of a role // instance CHANGES. To do that, we have to remember the last status of all role instances // and that's what the roleInstanceSnapshot array is for foreach (RoleInstance currentInstance in deployment.RoleInstanceList) { // We only care about these three statuses, ignore other intermediate statuses if (string.Equals(currentInstance.InstanceStatus, RoleInstanceStatus.BusyRole) || string.Equals(currentInstance.InstanceStatus, RoleInstanceStatus.ReadyRole) || string.Equals(currentInstance.InstanceStatus, RoleInstanceStatus.CreatingRole)) { bool createdOrChanged = false; // InstanceName is unique and concatenates the role name and instance name if (roleInstanceSnapshot.ContainsKey(currentInstance.InstanceName)) { // If we already have a snapshot of that role instance, update it RoleInstance previousInstance = roleInstanceSnapshot[currentInstance.InstanceName]; if (!string.Equals(previousInstance.InstanceStatus, currentInstance.InstanceStatus)) { // If the instance status changed, we need to output a message previousInstance.InstanceStatus = currentInstance.InstanceStatus; createdOrChanged = true; } } else { // If this is the first time we run through, we also need to output a message roleInstanceSnapshot[currentInstance.InstanceName] = currentInstance; createdOrChanged = true; } if (createdOrChanged) { string statusResource; switch (currentInstance.InstanceStatus) { case RoleInstanceStatus.BusyRole: statusResource = Resources.PublishInstanceStatusBusy; break; case RoleInstanceStatus.ReadyRole: statusResource = Resources.PublishInstanceStatusReady; break; default: statusResource = Resources.PublishInstanceStatusCreating; break; } WriteVerboseWithTimestamp( Resources.PublishInstanceStatusMessage, currentInstance.InstanceName, currentInstance.RoleName, statusResource); } } } // If a deployment has many roles to initialize, this // thread must throttle requests so the Azure portal // doesn't reply with a "too many requests" error Thread.Sleep(SleepDuration); }while (deployment.RoleInstanceList.Any(r => r.InstanceStatus != RoleInstanceStatus.ReadyRole)); WriteVerboseWithTimestamp(Resources.PublishCreatedWebsiteMessage, deployment.Url); } catch (ServiceManagementClientException) { throw new InvalidOperationException( string.Format(Resources.CannotFindDeployment, context.ServiceName, context.ServiceSettings.Slot)); } }
private string GetDeploymentId(PublishContext context) { Deployment deployment = new Deployment(); do { // If a deployment has many roles to initialize, this // thread must throttle requests so the Azure portal // doesn't reply with a "too many requests" error Thread.Sleep(SleepDuration); try { deployment = ServiceManagementChannel.GetDeploymentBySlot( subscriptionId, context.ServiceName, context.ServiceSettings.Slot); } catch (Exception e) { if (e.Message != Resources.InternalServerErrorMessage) { throw; } } } while (deployment.Status != DeploymentStatus.Starting && deployment.Status != DeploymentStatus.Running); return deployment.PrivateID; }
public void TestDeploymentSettingsTestNullSettingsFail() { string label = "MyLabel"; string deploymentName = service.ServiceName; try { PublishContext deploySettings = new PublishContext( null, packagePath, configPath, label, deploymentName, rootPath); Assert.Fail("No exception was thrown"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentException)); Assert.AreEqual<string>(Resources.InvalidServiceSettingMessage, ex.Message); } }
private void PrepareCloudServicePackagesRuntime(PublishContext context) { CloudServiceProject cloudServiceProject = new CloudServiceProject(context.RootPath, null); string warning = cloudServiceProject.ResolveRuntimePackageUrls(); if (!string.IsNullOrEmpty(warning)) { WriteWarning(Resources.RuntimeMismatchWarning, context.ServiceName); WriteWarning(warning); } }
public void TestDeploymentSettingsTestWithDefaultServiceSettings() { string label = "MyLabel"; string deploymentName = service.ServiceName; settings.Subscription = "TestSubscription2"; PublishContext deploySettings = new PublishContext( settings, packagePath, configPath, label, deploymentName, rootPath); AzureAssert.AreEqualPublishContext(settings, configPath, deploymentName, label, packagePath, "f62b1e05-af8f-4205-8f98-325079adc155", deploySettings); }
private void UpgradeDeployment(PublishContext context) { UpgradeDeploymentInput upgradeDeploymentInput = new UpgradeDeploymentInput { PackageUrl = UploadPackage(context), Configuration = General.GetConfiguration(context.ConfigPath), Label = context.ServiceName, Mode = UpgradeType.Auto }; WriteVerboseWithTimestamp(Resources.PublishUpgradingMessage); CertificateList uploadedCertificates = ServiceManagementChannel.ListCertificates( subscriptionId, context.ServiceName); AddCertificates(uploadedCertificates, context); ServiceManagementChannel.UpgradeDeploymentBySlot( subscriptionId, context.ServiceName, context.ServiceSettings.Slot, upgradeDeploymentInput); }
public void TestDeploymentSettingsTestWithFullServiceSettings() { string label = "MyLabel"; string deploymentName = service.ServiceName; ServiceSettings fullSettings = ServiceSettingsTestData.Instance.Data[ServiceSettingsState.Sample1]; PublishContext deploySettings = new PublishContext( fullSettings, packagePath, configPath, label, deploymentName, rootPath); AzureAssert.AreEqualPublishContext( fullSettings, configPath, deploymentName, label, packagePath, "f62b1e05-af8f-4205-8f98-325079adc155", deploySettings); }
private void VerifyDeployment(PublishContext context) { try { WriteVerboseWithTimestamp(Resources.PublishInitializingMessage); Dictionary<string, RoleInstance> roleInstanceSnapshot = new Dictionary<string, RoleInstance>(); // Continue polling for deployment until all of the roles // indicate they're ready Deployment deployment = new Deployment(); do { deployment = ServiceManagementChannel.GetDeploymentBySlot( subscriptionId, context.ServiceName, context.ServiceSettings.Slot); // The goal of this loop is to output a message whenever the status of a role // instance CHANGES. To do that, we have to remember the last status of all role instances // and that's what the roleInstanceSnapshot array is for foreach (RoleInstance currentInstance in deployment.RoleInstanceList) { // We only care about these three statuses, ignore other intermediate statuses if (string.Equals(currentInstance.InstanceStatus, RoleInstanceStatus.BusyRole) || string.Equals(currentInstance.InstanceStatus, RoleInstanceStatus.ReadyRole) || string.Equals(currentInstance.InstanceStatus, RoleInstanceStatus.CreatingRole)) { bool createdOrChanged = false; // InstanceName is unique and concatenates the role name and instance name if (roleInstanceSnapshot.ContainsKey(currentInstance.InstanceName)) { // If we already have a snapshot of that role instance, update it RoleInstance previousInstance = roleInstanceSnapshot[currentInstance.InstanceName]; if (!string.Equals(previousInstance.InstanceStatus, currentInstance.InstanceStatus)) { // If the instance status changed, we need to output a message previousInstance.InstanceStatus = currentInstance.InstanceStatus; createdOrChanged = true; } } else { // If this is the first time we run through, we also need to output a message roleInstanceSnapshot[currentInstance.InstanceName] = currentInstance; createdOrChanged = true; } if (createdOrChanged) { string statusResource; switch (currentInstance.InstanceStatus) { case RoleInstanceStatus.BusyRole: statusResource = Resources.PublishInstanceStatusBusy; break; case RoleInstanceStatus.ReadyRole: statusResource = Resources.PublishInstanceStatusReady; break; default: statusResource = Resources.PublishInstanceStatusCreating; break; } WriteVerboseWithTimestamp( Resources.PublishInstanceStatusMessage, currentInstance.InstanceName, currentInstance.RoleName, statusResource); } } } // If a deployment has many roles to initialize, this // thread must throttle requests so the Azure portal // doesn't reply with a "too many requests" error Thread.Sleep(SleepDuration); } while (deployment.RoleInstanceList.Any(r => r.InstanceStatus != RoleInstanceStatus.ReadyRole)); WriteVerboseWithTimestamp(Resources.PublishCreatedWebsiteMessage, deployment.Url); } catch (ServiceManagementClientException) { throw new InvalidOperationException( string.Format(Resources.CannotFindDeployment, context.ServiceName, context.ServiceSettings.Slot)); } }
public static void AreEqualDeploymentSettings(PublishContext expected, PublishContext actual) { AreEqualPublishContext(expected.ServiceSettings, expected.ConfigPath, expected.DeploymentName, expected.ServiceName, expected.PackagePath, expected.SubscriptionId, actual); }