public void EnableRemoteDesktop()
        {
            Validate.ValidateStringIsNullOrEmpty(Username, "Username");
            if (Password == null)
            {
                throw new ArgumentNullException("Password");
            }

            string plainPassword = GetPlainPassword();
            if (!IsPasswordComplex(plainPassword))
            {
                throw new ArgumentException(Properties.Resources.EnableAzureRemoteDesktopCommand_Enable_NeedComplexPassword);
            }

            AzureService service = new AzureService(GetServiceRootPath(), null);
            WebRole[] webRoles = service.Components.Definition.WebRole ?? new WebRole[0];
            WorkerRole[] workerRoles = service.Components.Definition.WorkerRole ?? new WorkerRole[0];

            string forwarderName = GetForwarderName(webRoles, workerRoles);
            RemoveOtherRemoteForwarders(webRoles, workerRoles, forwarderName);
            AddRemoteAccess(webRoles, workerRoles);

            X509Certificate2 cert = ChooseCertificate();
            ServiceConfigurationSchema.Certificate certElement = new ServiceConfigurationSchema.Certificate
            {
                name = "Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption",
                thumbprintAlgorithm = ThumbprintAlgorithmTypes.sha1,
                thumbprint = cert.Thumbprint
            };
            string encryptedPassword = Encrypt(plainPassword, cert);

            UpdateServiceConfigurations(service, forwarderName, certElement, encryptedPassword);
            service.Components.Save(service.Paths);
        }
        public void RemoveAzureServiceProcessTest()
        {
            SimpleServiceManagement channel = new SimpleServiceManagement();
            bool serviceDeleted = false;
            bool deploymentDeleted = false;
            channel.GetDeploymentBySlotThunk = ar =>
            {
                if (deploymentDeleted) throw new EndpointNotFoundException();
                return new Deployment(serviceName, ArgumentConstants.Slots[Slot.Production], DeploymentStatus.Suspended);
            };
            channel.DeleteHostedServiceThunk = ar => serviceDeleted = true;
            channel.DeleteDeploymentBySlotThunk = ar =>
            {
                deploymentDeleted = true;
            };

            using (FileSystemHelper files = new FileSystemHelper(this))
            {

                files.CreateAzureSdkDirectoryAndImportPublishSettings();
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                new RemoveAzureServiceCommand(channel).RemoveAzureServiceProcess(service.Paths.RootPath, string.Empty);
                Assert.IsTrue(deploymentDeleted);
                Assert.IsTrue(serviceDeleted);
            }
        }
 public void SetAzureInstancesProcessTestsEmptyRoleNameFail()
 {
     using (FileSystemHelper files = new FileSystemHelper(this))
     {
         AzureService service = new AzureService(files.RootPath, serviceName, null);
         Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, string.Empty, 10), string.Format(Resources.InvalidOrEmptyArgumentMessage, Resources.RoleName));
     }
 }
 public void AzureServiceAddExistingRoleFail()
 {
     using (FileSystemHelper files = new FileSystemHelper(this))
     {
         AzureService service = new AzureService(files.RootPath, serviceName, null);
         service.AddWebRole("WebRole");
         Testing.AssertThrows<ArgumentException>(() => service.AddWebRole("WebRole"), string.Format(Resources.AddRoleMessageRoleExists, "WebRole"));
     }
 }
 public void GetNextPortAllNull()
 {
     using (FileSystemHelper files = new FileSystemHelper(this))
     {
         int expectedPort = int.Parse(Resources.DefaultWebPort);
         AzureService service = new AzureService(files.RootPath, serviceName, null);
         int nextPort = service.Components.GetNextPort();
         Assert.AreEqual<int>(expectedPort, nextPort);
     }
 }
        public void SetAzureInstancesProcessNegativeRoleInstanceFail()
        {
            string roleName = "WebRole1";

            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, roleName, -1), string.Format(Resources.InvalidInstancesCount, roleName));
            }
        }
        public void AzureServiceAddNewWorkerRoleTest()
        {
            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                RoleInfo workerRole = service.AddWorkerRole("MyWorkerRole", 10);

                AzureAssert.AzureServiceExists(Path.Combine(files.RootPath, serviceName), Resources.GeneralScaffolding, serviceName, workerRoles: new WorkerRoleInfo[] { (WorkerRoleInfo)workerRole }, workerScaff: Path.Combine(Resources.NodeScaffolding, Resources.WorkerRole), roles: new RoleInfo[] { workerRole });
            }
        }
        public string StopAzureEmulatorProcess()
        {
            string standardOutput;
            string standardError;

            AzureService service = new AzureService();
            SafeWriteObject(Resources.StopEmulatorMessage);
            service.StopEmulator(out standardOutput, out standardError);
            SafeWriteObject(Resources.StoppedEmulatorMessage);
            return null;
        }
        internal void CreatePackage(string rootPath)
        {
            Debug.Assert(!string.IsNullOrEmpty(rootPath), "rootPath cannot be null or empty.");
            Debug.Assert(Directory.Exists(rootPath), "rootPath does not exist.");

            _azureService = new AzureService(rootPath, null);

            string unused = null;
            _azureService.CreatePackage(DevEnv.Cloud, out unused, out unused);

            Debug.Assert(File.Exists(Path.Combine(rootPath, Resources.CloudPackageFileName)));
        }
 public void GetNextPortAddingThirdEndpoint()
 {
     using (FileSystemHelper files = new FileSystemHelper(this))
     {
         int expectedPort = int.Parse(Resources.DefaultPort) + 1;
         AzureService service = new AzureService(files.RootPath, serviceName, null);
         service.AddWebRole();
         service.AddWebRole();
         service = new AzureServiceWrapper(service.Paths.RootPath, null);
         int nextPort = service.Components.GetNextPort();
         Assert.AreEqual<int>(expectedPort, nextPort);
     }
 }
        public void DisableRemoteDesktop()
        {
            AzureService service = new AzureService(GetServiceRootPath(), null);
            WebRole[] webRoles = service.Components.Definition.WebRole ?? new WebRole[0];
            WorkerRole[] workerRoles = service.Components.Definition.WorkerRole ?? new WorkerRole[0];

            string forwarderName = GetForwarderName(webRoles, workerRoles);
            if (forwarderName != null)
            {
                UpdateServiceConfigurations(service, forwarderName);
                service.Components.Save(service.Paths);
            }
        }
        public void CreateLocalPackageWithWorkerRoleTest()
        {
            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                string standardOutput;
                string standardError;
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                service.AddWorkerRole();
                service.CreatePackage(DevEnv.Local, out standardOutput, out standardError);

                AzureAssert.ScaffoldingExists(Path.Combine(service.Paths.LocalPackage, @"roles\WorkerRole1\approot"), Path.Combine(Resources.NodeScaffolding, Resources.WorkerRole));
            }
        }
        internal string NewAzureServiceProcess(string parentDirectory, string serviceName)
        {
            string message;
            AzureService newService;

            // Create scaffolding structure
            //
            newService = new AzureService(parentDirectory, serviceName, null);

            message = string.Format(Resources.NewServiceCreatedMessage, newService.Paths.RootPath);

            return message;
        }
 public void GetNextPortNullWebEndpointAndNullWorkerRole()
 {
     using (FileSystemHelper files = new FileSystemHelper(this))
     {
         int expectedPort = int.Parse(Resources.DefaultWebPort);
         AzureService service = new AzureService(files.RootPath, serviceName, null);
         service.AddWebRole();
         service = new AzureService(service.Paths.RootPath, null);
         service.Components.Definition.WebRole.ToList().ForEach(wr => wr.Endpoints = null);
         int nextPort = service.Components.GetNextPort();
         Assert.AreEqual<int>(expectedPort, nextPort);
     }
 }
        public string StartAzureEmulatorProcess(string rootPath)
        {
            string standardOutput;
            string standardError;

            StringBuilder message = new StringBuilder();
            AzureService service = new AzureService(rootPath ,null);
            SafeWriteObject(string.Format(Resources.CreatingPackageMessage, "local"));
            service.CreatePackage(DevEnv.Local, out standardOutput, out standardError);
            SafeWriteObject(Resources.StartingEmulator);
            service.StartEmulator(Launch.ToBool(), out standardOutput, out standardError);
            SafeWriteObject(standardOutput);
            SafeWriteObject(Resources.StartedEmulator);
            return message.ToString();
        }
        public void SetAzureInstancesProcessTests()
        {
            int newRoleInstances = 10;

            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                service.AddWebRole();
                new SetAzureInstancesCommand().SetAzureInstancesProcess("WebRole1", newRoleInstances, service.Paths.RootPath);
                service = new AzureService(service.Paths.RootPath, null);

                Assert.AreEqual<int>(newRoleInstances, service.Components.CloudConfig.Role[0].Instances.count);
                Assert.AreEqual<int>(newRoleInstances, service.Components.LocalConfig.Role[0].Instances.count);
            }
        }
        internal string GetServiceSettingsPath(bool global)
        {
            string path;

            if (global)
            {
                path = new GlobalComponents(GlobalPathInfo.GlobalSettingsDirectory).GlobalPaths.GlobalSettings;
            }
            else
            {
                path = new AzureService(base.GetServiceRootPath(), null).Paths.Settings;
            }

            return(path);
        }
        internal string GetServiceSettingsPath(bool global)
        {
            string path;

            if (global)
            {
                path = new GlobalComponents(GlobalPathInfo.GlobalSettingsDirectory).GlobalPaths.GlobalSettings;
            }
            else
            {
                path = new AzureService(base.GetServiceRootPath(), null).Paths.Settings;
            }

            return path;
        }
 public static void VerifyRoleSettings(AzureService service)
 {
     IEnumerable<ServiceConfigurationSchema.RoleSettings> settings =
         Enumerable.Concat(
             service.Components.CloudConfig.Role,
             service.Components.LocalConfig.Role);
     foreach (ServiceConfigurationSchema.RoleSettings roleSettings in settings)
     {
         Assert.AreEqual(
             1,
             roleSettings
                 .Certificates
                 .Where(c => c.name == "Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption")
                 .Count());
     }
 }
        internal string AddAzureNodeWebRoleProcess(string webRoleName, int instances, string rootPath)
        {
            string result;
            AzureService service = new AzureService(rootPath, null);
            RoleInfo webRole = service.AddWebRole(webRoleName, instances);
            try
            {
                service.ChangeRolePermissions(webRole);
            }
            catch (UnauthorizedAccessException)
            {
                SafeWriteObject(Resources.AddRoleMessageInsufficientPermissions);
            }

            result = string.Format(Resources.AddRoleMessageCreate, rootPath, webRole.Name);
            return result;
        }
        public void CreateLocalPackageWithOneWebRoleTest()
        {
            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                string standardOutput;
                string standardError;
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                RoleInfo webRoleInfo = service.AddWebRole();
                string logsDir = Path.Combine(service.Paths.RootPath, webRoleInfo.Name, "server.js.logs");
                string logFile = Path.Combine(logsDir, "0.txt");
                string targetLogsFile = Path.Combine(service.Paths.LocalPackage, "roles", webRoleInfo.Name, @"approot\server.js.logs\0.txt");
                files.CreateDirectory(logsDir);
                files.CreateEmptyFile(logFile);
                service.CreatePackage(DevEnv.Local, out standardOutput, out standardError);

                AzureAssert.ScaffoldingExists(Path.Combine(service.Paths.LocalPackage, @"roles\WebRole1\approot"), Path.Combine(Resources.NodeScaffolding, Resources.WebRole));
                Assert.IsTrue(File.Exists(targetLogsFile));
            }
        }
        public void SetDeploymentStatusProcessTest()
        {
            SimpleServiceManagement channel = new SimpleServiceManagement();
            string newStatus = DeploymentStatus.Running;
            string currentStatus = DeploymentStatus.Suspended;
            bool statusUpdated = false;
            channel.UpdateDeploymentStatusBySlotThunk = ar =>
            {
                statusUpdated = true;
                channel.GetDeploymentBySlotThunk = ar2 => new Deployment(serviceName, slot, newStatus);
            };
            channel.GetDeploymentBySlotThunk = ar => new Deployment(serviceName, slot, currentStatus);

            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                files.CreateAzureSdkDirectoryAndImportPublishSettings();
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                string result = new StartAzureService(channel).SetDeploymentStatusProcess(service.Paths.RootPath, newStatus, slot, Data.ValidSubscriptionName[0], serviceName);

                Assert.IsTrue(statusUpdated);
            }
        }
        public void SetDeploymentStatusProcessDeploymentDoesNotExistTest()
        {
            SimpleServiceManagement channel = new SimpleServiceManagement();
            string newStatus = DeploymentStatus.Running;
            string resultMessage;
            string expectedMessage = string.Format(Resources.ServiceSlotDoesNotExist, serviceName, slot);
            bool statusUpdated = false;
            channel.UpdateDeploymentStatusBySlotThunk = ar =>
            {
                statusUpdated = true;
                channel.GetDeploymentBySlotThunk = ar2 => new Deployment(serviceName, slot, newStatus);
            };
            channel.GetDeploymentBySlotThunk = ar => { throw new EndpointNotFoundException(); };

            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                files.CreateAzureSdkDirectoryAndImportPublishSettings();
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                resultMessage = new DeploymentStatusManager(channel).SetDeploymentStatusProcess(service.Paths.RootPath, newStatus, slot, Data.ValidSubscriptionName[0], serviceName);

                Assert.IsFalse(statusUpdated);
                Assert.AreEqual<string>(expectedMessage, resultMessage);
            }
        }
        public void SetAzureInstancesProcessTestsRoleNameDoesNotExistFail()
        {
            string roleName = "WebRole1";

            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                AzureService service = new AzureService(files.RootPath, serviceName, null);
                Testing.AssertThrows<ArgumentException>(() => service.SetRoleInstances(service.Paths, roleName, 10), string.Format(Resources.RoleNotFoundMessage, roleName));
            }
        }
 private static void VerifyDisableRoleSettings(AzureService service)
 {
     IEnumerable<ServiceConfigurationSchema.RoleSettings> settings =
         Enumerable.Concat(
             service.Components.CloudConfig.Role,
             service.Components.LocalConfig.Role);
     foreach (ServiceConfigurationSchema.RoleSettings roleSettings in settings)
     {
         Assert.AreEqual(
             1,
             roleSettings.ConfigurationSettings
                 .Where(c => c.name == "Microsoft.WindowsAzure.Plugins.RemoteAccess.Enabled" && c.value == "false")
                 .Count());
     }
 }
        /// <summary>
        /// Initialize our model of the AzureService located at the given
        /// path along with its DeploymentSettings and SubscriptionId.
        /// </summary>
        /// <param name="rootPath">Root path of the Azure service.</param>
        internal void InitializeSettingsAndCreatePackage(string rootPath)
        {
            Debug.Assert(!string.IsNullOrEmpty(rootPath), "rootPath cannot be null or empty.");
            Debug.Assert(Directory.Exists(rootPath), "rootPath does not exist.");

            _azureService = new AzureService(rootPath, null);

            // If user provided a service name, change current service name to use it.
            //
            if (!string.IsNullOrEmpty(Name))
            {
                _azureService.ChangeServiceName(Name, _azureService.Paths);
            }

            ServiceSettings defaultSettings = ServiceSettings.LoadDefault(
                _azureService.Paths.Settings,
                Slot,
                Location,
                Subscription,
                StorageAccountName,
                Name,
                _azureService.ServiceName,
                out _hostedServiceName);

            subscriptionId =
                new GlobalComponents(GlobalPathInfo.GlobalSettingsDirectory)
                .GetSubscriptionId(defaultSettings.Subscription);

            SafeWriteObjectWithTimestamp(String.Format(Resources.PublishPreparingDeploymentMessage,
                _hostedServiceName, subscriptionId));

            CreatePackage();

            _deploymentSettings = new DeploymentSettings(
                defaultSettings,
                _azureService.Paths.CloudPackage,
                _azureService.Paths.CloudConfiguration,
                _hostedServiceName,
                string.Format(Resources.ServiceDeploymentName, defaultSettings.Slot));
        }
        public void EnableRemoteDesktopForWebRole()
        {
            using (FileSystemHelper files = new FileSystemHelper(this))
            {
                files.CreateAzureSdkDirectoryAndImportPublishSettings();
                string root = files.CreateNewService("NEW_SERVICE");
                new AddAzureNodeWebRoleCommand().AddAzureNodeWebRoleProcess("WebRole", 1, root);
                EnableRemoteDesktop("user", "GoodPassword!");

                // Verify the role has been setup with forwarding, access,
                // and certs
                AzureService service = new AzureService(root, null);
                VerifyWebRole(service.Components.Definition.WebRole[0], true);
                VerifyRoleSettings(service);
            }
        }
        private void UpdateServiceConfigurations(AzureService service, string forwarderName)
        {
            foreach (ServiceConfiguration config in new[] { service.Components.LocalConfig, service.Components.CloudConfig })
            {
                foreach (ServiceConfigurationSchema.RoleSettings role in config.Role)
                {
                    if (role.ConfigurationSettings != null)
                    {
                        ServiceConfigurationSchema.ConfigurationSetting setting = role.ConfigurationSettings.
                            FirstOrDefault<ServiceConfigurationSchema.ConfigurationSetting>(t => t.name.Equals(
                                "Microsoft.WindowsAzure.Plugins.RemoteAccess.Enabled"));
                        if (setting != null)
                        {
                            setting.value = "false";
                        }

                        if (role.name == forwarderName)
                        {
                            ServiceConfigurationSchema.ConfigurationSetting forwarderSetting = role.ConfigurationSettings.
                                FirstOrDefault<ServiceConfigurationSchema.ConfigurationSetting>(t => t.name.Equals(
                                    "Microsoft.WindowsAzure.Plugins.RemoteForwarder.Enabled"));
                            if (forwarderSetting != null)
                            {
                                forwarderSetting.value = "false";
                            }
                        }
                    }
                }
            }
        }
 public void GetNextPortWithEmptyPortIndpoints()
 {
     using (FileSystemHelper files = new FileSystemHelper(this))
     {
         int expectedPort = int.Parse(Resources.DefaultPort);
         AzureService service = new AzureService(files.RootPath, serviceName, null);
         service.AddWebRole();
         service.Components.Definition.WebRole[0].Endpoints.InputEndpoint = null;
         service.Components.Save(service.Paths);
         service.AddWebRole();
         service = new AzureServiceWrapper(service.Paths.RootPath, null);
         int nextPort = service.Components.GetNextPort();
         Assert.AreEqual<int>(expectedPort, nextPort);
     }
 }
        public void InitializeArgs(string rootPath)
        {
            azureService = new AzureService(rootPath, null);
            azureService.Components.Settings.Location = SetLocation();
            azureService.Components.Settings.Subscription = new GlobalComponents(GlobalPathInfo.GlobalSettingsDirectory).GetSubscriptionId(Subscription);
            this.subscriptionId = azureService.Components.Settings.Subscription;

            // Issue #101: what to do in case of having empty storage account name?
            azureService.Components.Settings.StorageAccountName = SetStorageAccountName();
        }
 public void SetAzureInstancesProcess(string roleName, int instances, string rootPath)
 {
     AzureService service = new AzureService(rootPath, null);
     service.SetRoleInstances(service.Paths, roleName, instances);
 }