public void BinariesDirPath_returns_path_without_config_template_name_when_artifacts_are_not_env_specific()
        {
            // arrange
              NtServiceProjectInfo projectInfo =
            ProjectInfoGenerator.GetNtServiceProjectInfo(areEnvironmentSpecific: false);

              _directoryAdapterFake.Setup(x => x.Exists(It.IsAny<string>()))
            .Returns(true);

              _deploymentStep =
            new ExtractArtifactsDeploymentStep(
              projectInfo,
              _environmentInfo,
              _deploymentInfo,
              _ArtifactsFilePath,
              _TargetArtifactsDirPath,
              _fileAdapterFake.Object,
              _directoryAdapterFake.Object,
              _zipFileAdapterFake.Object);

              _deploymentStep.Prepare();

              // act, assert
              Assert.IsFalse(_deploymentStep.BinariesDirPath.Contains(_environmentInfo.ConfigurationTemplateName));
        }
        public void BinariesDirPath_fails_when_extracted_binaries_dir_doesnt_exist()
        {
            // arrange
              NtServiceProjectInfo projectInfo =
            ProjectInfoGenerator.GetNtServiceProjectInfo(areEnvironmentSpecific: false);

              _directoryAdapterFake.Setup(x => x.Exists(It.IsAny<string>()))
            .Returns(false);

              _deploymentStep =
            new ExtractArtifactsDeploymentStep(
              projectInfo,
              _environmentInfo,
              _deploymentInfo,
              _ArtifactsFilePath,
              _TargetArtifactsDirPath,
              _fileAdapterFake.Object,
              _directoryAdapterFake.Object,
              _zipFileAdapterFake.Object);

              _deploymentStep.Prepare();

              // act assert
              Assert.Throws<DeploymentTaskException>(
            () => { string binPath = _deploymentStep.BinariesDirPath; });
        }
        public void SetUp()
        {
            _environmentInfo = DeploymentDataGenerator.GetEnvironmentInfo();
              _deploymentInfo = DeploymentInfoGenerator.GetDbDeploymentInfo();
              _projectInfo = ProjectInfoGenerator.GetTerminalAppProjectInfo();
              _fileAdapterFake = new Mock<IFileAdapter>(MockBehavior.Loose);
              _directoryAdapterFake = new Mock<IDirectoryAdapter>(MockBehavior.Loose);
              _zipFileAdapterFake = new Mock<IZipFileAdapter>(MockBehavior.Loose);

              _deploymentStep =
            new ExtractArtifactsDeploymentStep(
              _projectInfo,
              _environmentInfo,
              _deploymentInfo,
              _ArtifactsFilePath,
              _TargetArtifactsDirPath,
              _fileAdapterFake.Object,
              _directoryAdapterFake.Object,
              _zipFileAdapterFake.Object);
        }
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();

              _projectInfo = GetProjectInfo<NtServiceProjectInfo>();

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              _projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              _projectInfo,
              environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              if (_projectInfo.ArtifactsAreEnvironmentSpecific)
              {
            var binariesConfiguratorStep =
              new ConfigureBinariesStep(
            environmentInfo.ConfigurationTemplateName,
            GetTempDirPath());

            AddSubTask(binariesConfiguratorStep);
              }

              bool deployToClusteredEnvironment =
            environmentInfo.EnableFailoverClusteringForNtServices;

              if (deployToClusteredEnvironment)
              {
            PostDiagnosticMessage("Will deploy to a clustered environment.", DiagnosticMessageType.Trace);

            if (string.IsNullOrEmpty(environmentInfo.GetFailoverClusterGroupNameForProject(DeploymentInfo.ProjectName)))
            {
              throw new InvalidOperationException(string.Format("Failover clustering for NT services is enabled for environment '{0}' but there is no cluster group mapping for project '{1}'.", environmentInfo.Name, DeploymentInfo.ProjectName));
            }

            DoPrepareDeploymentToClusteredEnvironment(
              environmentInfo,
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath));
              }
              else
              {
            PostDiagnosticMessage("Will deploy to a non-clustered environment.", DiagnosticMessageType.Trace);

            DoPrepareDeploymentToStandardEnvironment(
              environmentInfo,
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath));
              }
        }
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();
              DbProjectInfo projectInfo = GetProjectInfo<DbProjectInfo>();

              DbProjectConfiguration dbProjectConfiguration =
            environmentInfo.GetDbProjectConfiguration(projectInfo);

              DatabaseServer databaseServer =
            environmentInfo.GetDatabaseServer(dbProjectConfiguration.DatabaseServerId);

              string databaseServerMachineName = databaseServer.MachineName;

              bool databaseExists = _dbVersionProvider.CheckIfDatabaseExists(
            projectInfo.DbName,
            databaseServerMachineName);

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              projectInfo,
              environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              if (databaseExists)
              {
            // create a step for gathering scripts to run
            var gatherDbScriptsToRunDeploymentStep = new GatherDbScriptsToRunDeploymentStep(
              projectInfo.DbName,
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
              databaseServerMachineName,
              environmentInfo.Name,
              DeploymentInfo,
              _dbVersionProvider,
              _createScriptsToRunSelector
              );

            AddSubTask(gatherDbScriptsToRunDeploymentStep);

            // create a step for running scripts
            var runDbScriptsDeploymentStep = new RunDbScriptsDeploymentStep(
            GetScriptRunner(projectInfo.IsTransactional, databaseServerMachineName, projectInfo.DbName, environmentInfo.DatabaseServers.FirstOrDefault(e => e.Id == projectInfo.DatabaseServerId)),
            databaseServerMachineName,
            new DeferredEnumerable<DbScriptToRun>(() => gatherDbScriptsToRunDeploymentStep.ScriptsToRun));

            AddSubTask(runDbScriptsDeploymentStep);
              }

              else
              {
            // create step for dropping database
            var createDatabaseDeploymentStep = new CreateDatabaseDeploymentStep(projectInfo, databaseServer, _dbManagerFactory);

            AddSubTask(createDatabaseDeploymentStep);

            // create step for deploying dacpac
            var publishDatabaseDeploymentStep =
              new PublishDatabaseDeploymentStep(
            projectInfo,
            databaseServer,
            GetTempDirPath(),
            _databasePublisher);

            AddSubTask(publishDatabaseDeploymentStep);
              }

              foreach (string userId in projectInfo.Users)
              {
            var environmentUser = environmentInfo.EnvironmentUsers.SingleOrDefault(x => x.Id == userId);

            if (environmentUser == null)
            {
              throw new DeploymentTaskException(string.Format("User [{0}] doesn't exist in enviroment configuration [{1}] in project [{2}]", userId, environmentInfo.Name, projectInfo.Name));
            }

            string user = _userNameNormalizer.ConvertToPreWin2000UserName(environmentUser.UserName, environmentInfo.DomainName);

            IDbManager manager = _dbManagerFactory.CreateDbManager(databaseServerMachineName);

               if (databaseExists && manager.UserExists(projectInfo.DbName, user))
            {
              foreach (string dbUserRole in _dbUserRoles)
              {
            if (!manager.CheckIfUserIsInRole(projectInfo.DbName, user, dbUserRole))
            {
              AddSubTask(new AddRoleToUserStep(manager, projectInfo.DbName, user, dbUserRole));
            }
              }
            }
            else
            {
              AddSubTask(new AddUserToDatabaseStep(manager, projectInfo.DbName, user));
              foreach (string dbUserRole in _dbUserRoles)
              {
            AddSubTask(new AddRoleToUserStep(manager, projectInfo.DbName, user, dbUserRole));
              }
            }
              }
        }
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();
              _projectInfo = GetProjectInfo<PowerShellScriptProjectInfo>();

              IEnumerable<string> targetMachineNames = _projectInfo.GetTargetMachines(environmentInfo);

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              _projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              _projectInfo,
              environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              if (_projectInfo.ArtifactsAreEnvironmentSpecific)
              {
            var binariesConfiguratorStep =
              new ConfigureBinariesStep(
            environmentInfo.ConfigurationTemplateName,
            GetTempDirPath());

            AddSubTask(binariesConfiguratorStep);
              }

              if (_projectInfo.IsRemote == false)
              {
            // Run powershell script
            var runPowerShellScriptStep =
              new RunPowerShellScriptStep(
            _projectInfo.IsRemote,
            null,
            new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
            _projectInfo.ScriptName);

            AddSubTask(runPowerShellScriptStep);

            return;
              }

              foreach (var targetMachineName in targetMachineNames)
              {
            // Create temp dir on remote machine
            var createRemoteTempDirStep = new CreateRemoteTempDirStep(targetMachineName);

            AddSubTask(createRemoteTempDirStep);

            // Copy files to remote machine
            string machineName = targetMachineName;

            var copyFilesDeploymentStep = new CopyFilesDeploymentStep(
              _directoryAdapter,
              srcDirPathProvider : new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
              dstDirPath: new Lazy<string>(() => EnvironmentInfo.GetNetworkPath(machineName, createRemoteTempDirStep.RemoteTempDirPath)));

            AddSubTask(copyFilesDeploymentStep);

            // Run powershell script
            var runPowerShellScriptStep =
              new RunPowerShellScriptStep(
            _projectInfo.IsRemote,
            targetMachineName,
            new Lazy<string>(() => createRemoteTempDirStep.RemoteTempDirPath),
            _projectInfo.ScriptName);

            AddSubTask(runPowerShellScriptStep);

            // Delete remote temp dir
            var removeRemoteDirectory = new RemoveRemoteDirectoryStep(
              targetMachineName,
              new Lazy<string>(() => createRemoteTempDirStep.RemoteTempDirPath));

            AddSubTask(removeRemoteDirectory);
              }
        }
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();

              var projectInfo = GetProjectInfo<ProjectInfo>();

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              projectInfo,
              environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              if (projectInfo.ArtifactsAreEnvironmentSpecific)
              {
            var binariesConfiguratorStep =
              new ConfigureBinariesStep(
            environmentInfo.ConfigurationTemplateName,
            GetTempDirPath());

            AddSubTask(binariesConfiguratorStep);
              }

              AddSubTask(
            new CopyFilesDeploymentStep(
              _directoryAdapter,
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
              new Lazy<string>(() => PreparePackageDirPath(PackageDirPath))));
        }
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();
              TerminalAppProjectInfo projectInfo = GetProjectInfo<TerminalAppProjectInfo>();

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              projectInfo,
              environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              if (projectInfo.ArtifactsAreEnvironmentSpecific)
              {
            var binariesConfiguratorStep =
              new ConfigureBinariesStep(
            environmentInfo.ConfigurationTemplateName,
            GetTempDirPath());

            AddSubTask(binariesConfiguratorStep);
              }

              var extractVersionDeploymentStep =
            new ExtractVersionDeploymentStep(
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
              projectInfo.TerminalAppExeName
              );

              AddSubTask(extractVersionDeploymentStep);

              var prepareVersionedFolderDeploymentStep =
            new PrepareVersionedFolderDeploymentStep(
              DeploymentInfo.ProjectName,
              environmentInfo.GetTerminalServerNetworkPath(environmentInfo.TerminalAppsBaseDirPath),
              projectInfo.TerminalAppDirName,
              new Lazy<string>(() => extractVersionDeploymentStep.Version));

              AddSubTask(prepareVersionedFolderDeploymentStep);

              AddSubTask(
            new CleanDirectoryDeploymentStep(
              _directoryAdapter,
              _fileAdapter,
              new Lazy<string>(() => prepareVersionedFolderDeploymentStep.VersionDeploymentDirPath),
              excludedDirs: new string[] { }));

              AddSubTask(
            new CopyFilesDeploymentStep(
              _directoryAdapter,
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
              new Lazy<string>(() => prepareVersionedFolderDeploymentStep.VersionDeploymentDirPath)));

              AddSubTask(
            new UpdateApplicationShortcutDeploymentStep(
              environmentInfo.GetTerminalServerNetworkPath(environmentInfo.TerminalAppsShortcutFolder),
              new Lazy<string>(() => prepareVersionedFolderDeploymentStep.VersionDeploymentDirPath),
              projectInfo.TerminalAppExeName,
              projectInfo.Name));
        }
        protected override void DoPrepare()
        {
            _environmentInfo = GetEnvironmentInfo();
              _projectInfo = GetProjectInfo<ExtensionProjectInfo>();
              _extendedProjectInfo = GetProjectInfo<NtServiceProjectInfo>(_projectInfo.ExtendedProjectName);

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              _projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              _projectInfo,
              _environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              bool deployToClusteredEnvironment =
            _environmentInfo.EnableFailoverClusteringForNtServices;

              if (deployToClusteredEnvironment)
              {
            PostDiagnosticMessage("Will deploy to a clustered environment.", DiagnosticMessageType.Trace);

            if (string.IsNullOrEmpty(_environmentInfo.GetFailoverClusterGroupNameForProject(_extendedProjectInfo.Name)))
            {
              throw new InvalidOperationException(string.Format("Failover clustering for NT services is enabled for environment '{0}' but there is no cluster group mapping for project '{1}'.", _environmentInfo.Name, _extendedProjectInfo.Name));
            }

            DoPrepareDeploymentToClusteredEnvironment(
              new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
              _extendedProjectInfo);
              }
              else
              {
            PostDiagnosticMessage("Will deploy to a non-clustered environment.", DiagnosticMessageType.Trace);

            AddSubTask(
              new StopNtServiceDeploymentStep(
            _ntServiceManager,
            _environmentInfo.AppServerMachineName,
            _extendedProjectInfo.NtServiceName));

            DoPrepareCommonDeploymentSteps(_environmentInfo.GetAppServerNetworkPath, new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath) );

            AddSubTask(
              new StartNtServiceDeploymentStep(
            _ntServiceManager,
            _environmentInfo.AppServerMachineName,
            _extendedProjectInfo.NtServiceName));
              }
        }
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();
              WebAppProjectInfo projectInfo = GetProjectInfo<WebAppProjectInfo>();
              WebAppInputParams inputParams = (WebAppInputParams)DeploymentInfo.InputParams;

              if (inputParams.OnlyIncludedWebMachines != null)
              {
            if (!inputParams.OnlyIncludedWebMachines.Any())
            {
              throw new DeploymentTaskException("If inputParams OnlyIncludedWebMachines has been specified, it must contain at least one web machine.");
            }

            string[] invalidMachineNames =
              inputParams.OnlyIncludedWebMachines
            .Except(environmentInfo.WebServerMachineNames)
            .ToArray();

            if (invalidMachineNames.Any())
            {
              throw new DeploymentTaskException(string.Format("Invalid web machines '{0}' have been specified.", string.Join(",", invalidMachineNames)));
            }
              }

              if (projectInfo == null)
              {
            throw new InvalidOperationException(string.Format("Project info must be of type '{0}'.", typeof(WebAppProjectInfo).FullName));
              }

              // create a step for downloading the artifacts
              var downloadArtifactsDeploymentStep =
            new DownloadArtifactsDeploymentStep(
              projectInfo,
              DeploymentInfo,
              GetTempDirPath(),
              _artifactsRepository);

              AddSubTask(downloadArtifactsDeploymentStep);

              // create a step for extracting the artifacts
              var extractArtifactsDeploymentStep =
            new ExtractArtifactsDeploymentStep(
              projectInfo,
              environmentInfo,
              DeploymentInfo,
              downloadArtifactsDeploymentStep.ArtifactsFilePath,
              GetTempDirPath(),
              _fileAdapter,
              _directoryAdapter,
              _zipFileAdapter);

              AddSubTask(extractArtifactsDeploymentStep);

              if (projectInfo.ArtifactsAreEnvironmentSpecific)
              {
            var binariesConfiguratorStep =
              new ConfigureBinariesStep(
            environmentInfo.ConfigurationTemplateName,
            GetTempDirPath());

            AddSubTask(binariesConfiguratorStep);
              }

              WebAppProjectConfiguration configuration =
            environmentInfo.GetWebAppProjectConfiguration(projectInfo);

              CheckConfiguration(configuration);

              string webSiteName = configuration.WebSiteName;
              string webAppName = configuration.WebAppName;
              IisAppPoolInfo appPoolInfo = environmentInfo.GetAppPoolInfo(configuration.AppPoolId);

              IEnumerable<string> webMachinesToDeployTo =
            (inputParams.OnlyIncludedWebMachines ?? environmentInfo.WebServerMachineNames)
              .Distinct();

              foreach (string webServerMachineName in webMachinesToDeployTo)
              {
            /* // TODO IMM HI: xxx we don't need this for now - should we parameterize this somehow?
            string webApplicationPhysicalPath =
              _iisManager.GetWebApplicationPath(
            webServerMachineName,
            string.Format("{0}/{1}", webSiteName, webAppName));

            if (!string.IsNullOrEmpty(webApplicationPhysicalPath))
            {
              string webApplicationNetworkPath =
            string.Format(
              "\\\\{0}\\{1}${2}",
              webServerMachineName,
              webApplicationPhysicalPath[0],
              webApplicationPhysicalPath.Substring(2));

              if (Directory.Exists(webApplicationNetworkPath))
              {
            var backupFilesDeploymentStep =
              new BackupFilesDeploymentStep(
                webApplicationNetworkPath);

            AddSubTask(backupFilesDeploymentStep);
              }
            }
            */

            // create a step for creating a WebDeploy package
            // TODO IMM HI: add possibility to specify physical path on the target machine
            var createWebDeployPackageDeploymentStep =
              new CreateWebDeployPackageDeploymentStep(
            _msDeploy,
            new Lazy<string>(() => extractArtifactsDeploymentStep.BinariesDirPath),
            webSiteName,
            webAppName);

            AddSubTask(createWebDeployPackageDeploymentStep);

            // create a step for deploying the WebDeploy package to the target machine
            var deployWebDeployPackageDeploymentStep =
              new DeployWebDeployPackageDeploymentStep(
            _msDeploy,
            webServerMachineName,
            new Lazy<string>(() => createWebDeployPackageDeploymentStep.PackageFilePath));

            AddSubTask(deployWebDeployPackageDeploymentStep);

            // check if the app pool exists on the target machine
            if (_applicationConfiguration.CheckIfAppPoolExists && !_iisManager.AppPoolExists(webServerMachineName, appPoolInfo.Name))
            {
              // create a step for creating a new app pool
              var createAppPoolDeploymentStep =
            new CreateAppPoolDeploymentStep(
              _iisManager,
              webServerMachineName,
              appPoolInfo);

              AddSubTask(createAppPoolDeploymentStep);

              // create a step for assigning the app pool to the web application
              var setAppPoolDeploymentStep =
            new SetAppPoolDeploymentStep(
              _iisManager,
              webServerMachineName,
              webSiteName,
              appPoolInfo,
              webAppName);

              AddSubTask(setAppPoolDeploymentStep);
            }
              }
        }