예제 #1
0
        private void MakeSureTasksThatWereEnabledAreEnabled()
        {
            foreach (Tuple <string, string> machineNameAndTaskName in _existingTaskDetailsByMachineNameAndTaskName.Keys)
            {
                string schedulerServerTasksMachineName = machineNameAndTaskName.Item1;
                string taskName = machineNameAndTaskName.Item2;

                try
                {
                    ScheduledTaskDetails existingTaskDetails =
                        _existingTaskDetailsByMachineNameAndTaskName[machineNameAndTaskName];

                    if (existingTaskDetails == null || existingTaskDetails.IsEnabled)
                    {
                        ScheduledTaskDetails currentTaskDetails =
                            _taskScheduler.GetScheduledTaskDetails(schedulerServerTasksMachineName, taskName);

                        if (currentTaskDetails != null && !currentTaskDetails.IsEnabled)
                        {
                            _taskScheduler.ToggleTaskEnabled(schedulerServerTasksMachineName, taskName, true);
                        }
                    }
                }
                catch (Exception exc)
                {
                    PostDiagnosticMessage(string.Format("Error while making sure that tasks that were enabled are enabled on machine '{0}'. Exception: {1}", schedulerServerTasksMachineName, exc), DiagnosticMessageType.Error);
                }
            }
        }
예제 #2
0
        public void Prepare_should_add_steps_to_update_scheduled_tasks_when_settings_has_changed()
        {
            // arrange
            const string exePath = "exe path has changed";

            SchedulerAppTask schedulerAppTask =
                _projectInfo.SchedulerAppTasks.Second();

            ScheduledTaskDetails runningTaskDetails =
                GetTaskDetails(
                    schedulerAppTask,
                    exePath,
                    true,
                    false,
                    new ScheduledTaskRepetition(
                        schedulerAppTask.Repetition.Interval,
                        schedulerAppTask.Repetition.Duration,
                        schedulerAppTask.Repetition.StopAtDurationEnd));

            _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(runningTaskDetails);

            _passwordCollectorFake
            .Setup(x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns("password");

            // act
            _deployTask.Prepare();

            // assert
            Assert.AreEqual(4, _deployTask.SubTasks.Count(x => x is UpdateSchedulerTaskDeploymentStep));
        }
예제 #3
0
        public void Prepare_should_collect_password_when_tasks_settings_has_changed()
        {
            // arrange
            const string     exePath          = "exe path has changed";
            SchedulerAppTask schedulerAppTask = _projectInfo.SchedulerAppTasks.First();

            ScheduledTaskDetails runningTaskDetails =
                GetTaskDetails(
                    schedulerAppTask,
                    exePath,
                    true,
                    false,
                    new ScheduledTaskRepetition(
                        schedulerAppTask.Repetition.Interval,
                        schedulerAppTask.Repetition.Duration,
                        schedulerAppTask.Repetition.StopAtDurationEnd));

            _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(runningTaskDetails);

            _passwordCollectorFake
            .Setup(x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns("password");

            // act
            _deployTask.Prepare();

            // assert
            _passwordCollectorFake.Verify(
                x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()),
                Times.Exactly(2));
        }
예제 #4
0
        public void Prepare_should_fail_if_task_is_running()
        {
            // arrange
            SchedulerAppTask schedulerAppTask =
                _projectInfo.SchedulerAppTasks.Second();

            ScheduledTaskDetails runningTaskDetails =
                GetTaskDetails(
                    schedulerAppTask,
                    Path.Combine(
                        _environmentInfo.SchedulerAppsBaseDirPath,
                        _projectInfo.SchedulerAppDirName,
                        schedulerAppTask.ExecutableName),
                    true,
                    true,
                    new ScheduledTaskRepetition(
                        schedulerAppTask.Repetition.Interval,
                        schedulerAppTask.Repetition.Duration,
                        schedulerAppTask.Repetition.StopAtDurationEnd));

            _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(runningTaskDetails);

            // act assert
            Assert.Throws <DeploymentTaskException>(() => _deployTask.Prepare());
        }
예제 #5
0
        private static void EnsureTaskIsNotRunning(ScheduledTaskDetails taskDetails, string schedulerServerTasksMachineName)
        {
            if (taskDetails == null || !taskDetails.IsRunning)
            {
                return;
            }

            throw
                new DeploymentTaskException(
                    string.Format(
                        "Task: {0} on machine: {1} is already running. Deployment aborted. Last run time: {2}, next run time: {3}",
                        schedulerServerTasksMachineName,
                        taskDetails.Name,
                        taskDetails.LastRunTime,
                        taskDetails.NextRunTime));
        }
예제 #6
0
        public void Prepare_should_collect_password_only_once_when_tasks_settings_has_changed_and_username_is_the_same()
        {
            // arrange
            SchedulerAppTask schedulerAppTask1 = _projectInfo.SchedulerAppTasks.First();
            SchedulerAppTask schedulerAppTask2 = _projectInfo.SchedulerAppTasks.Second();

            EnvironmentInfo environmentInfo =
                DeploymentDataGenerator.GetEnvironmentInfo(
                    new[]
            {
                new EnvironmentUser(schedulerAppTask1.UserId, "user_name"),
                new EnvironmentUser(schedulerAppTask2.UserId, "user_name"),
            });

            _environmentInfoRepositoryFake
            .Setup(x => x.FindByName(It.IsAny <string>()))
            .Returns(environmentInfo);

            const string exePath = "exe path has changed";

            ScheduledTaskDetails runningTaskDetails =
                GetTaskDetails(
                    schedulerAppTask1,
                    exePath,
                    true,
                    false,
                    new ScheduledTaskRepetition(
                        schedulerAppTask1.Repetition.Interval,
                        schedulerAppTask1.Repetition.Duration,
                        schedulerAppTask1.Repetition.StopAtDurationEnd));

            _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(runningTaskDetails);

            _passwordCollectorFake
            .Setup(x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns("password");

            // act
            _deployTask.Prepare();

            // assert
            _passwordCollectorFake.Verify(
                x => x.CollectPasswordForUser(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()),
                Times.Exactly(1));
        }
예제 #7
0
        private TaskSettingsCompareResult CompareTaskSettings(ScheduledTaskDetails taskDetails, SchedulerAppTask schedulerAppTask, EnvironmentInfo environmentInfo)
        {
            if (taskDetails == null)
            {
                return(new TaskSettingsCompareResult(new List <SettingDifference>()));
            }

            string taskExecutablePath = GetTaskExecutablePath(schedulerAppTask, environmentInfo);

            var compareResultBuilder = new TaskSettingsCompareResultBuilder();

            compareResultBuilder
            .CompareValues("Name", taskDetails.Name, schedulerAppTask.Name)
            .CompareValues("ScheduledHour", taskDetails.ScheduledHour, schedulerAppTask.ScheduledHour)
            .CompareValues("ScheduledMinute", taskDetails.ScheduledMinute, schedulerAppTask.ScheduledMinute)
            .CompareValues("ExecutionTimeLimitInMinutes", taskDetails.ExecutionTimeLimitInMinutes, schedulerAppTask.ExecutionTimeLimitInMinutes)
            .CompareValues("Repetition.Interval", taskDetails.Repetition.Interval, schedulerAppTask.Repetition.Interval)
            .CompareValues("Repetition.Duration", taskDetails.Repetition.Duration, schedulerAppTask.Repetition.Duration)
            .CompareValues("Repetition.StopAtDurationEnd", taskDetails.Repetition.StopAtDurationEnd, schedulerAppTask.Repetition.StopAtDurationEnd)
            .CompareValues("ExeAbsolutePath", taskDetails.ExeAbsolutePath, taskExecutablePath);

            return(compareResultBuilder.GetResult());
        }
예제 #8
0
        protected override void DoPrepare()
        {
            EnvironmentInfo environmentInfo = GetEnvironmentInfo();

            _projectInfo = GetProjectInfo <SchedulerAppProjectInfo>();
            _collectedPasswordsByUserName = new Dictionary <string, string>();
            _existingTaskDetailsByMachineNameAndTaskName = new Dictionary <Tuple <string, string>, ScheduledTaskDetails>();

            foreach (string tmpSchedulerServerTasksMachineName in environmentInfo.SchedulerServerTasksMachineNames)
            {
                string schedulerServerTasksMachineName = tmpSchedulerServerTasksMachineName;

                _projectInfo.SchedulerAppTasks
                .ForEach(
                    schedulerAppTask =>
                {
                    ScheduledTaskDetails taskDetails =
                        _taskScheduler.GetScheduledTaskDetails(schedulerServerTasksMachineName, schedulerAppTask.Name);

                    _existingTaskDetailsByMachineNameAndTaskName.Add(
                        Tuple.Create(schedulerServerTasksMachineName, schedulerAppTask.Name),
                        taskDetails);

                    EnsureTaskIsNotRunning(taskDetails, schedulerServerTasksMachineName);

                    // create a step to disable scheduler task
                    if (taskDetails != null && taskDetails.IsEnabled)
                    {
                        AddToggleSchedulerAppTaskEnabledStep(schedulerServerTasksMachineName, taskDetails.Name, false);
                    }
                });
            }

            Lazy <string> binariesDirPathProvider =
                AddStepsToObtainBinaries(environmentInfo);

/* // TODO IMM HI: xxx we don't need this for now - should we parameterize this somehow?
 *    if (_directoryAdapter.Exists(targetDirNetworkPath))
 *    {
 *      AddSubTask(
 *        new BackupFilesDeploymentStep(
 *          targetDirNetworkPath));
 *    }
 */

            // create steps for copying the binaries to target binaries machines
            foreach (string schedulerServerBinariesMachineName in environmentInfo.SchedulerServerBinariesMachineNames)
            {
                string targetDirPath        = GetTargetDirPath(environmentInfo);
                string targetDirNetworkPath = environmentInfo.GetSchedulerServerNetworkPath(schedulerServerBinariesMachineName, targetDirPath);

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

                AddSubTask(
                    new CopyFilesDeploymentStep(
                        _directoryAdapter,
                        binariesDirPathProvider,
                        new Lazy <string>(() => targetDirNetworkPath)));
            }

            foreach (string tmpSchedulerServerTasksMachineName in environmentInfo.SchedulerServerTasksMachineNames)
            {
                string schedulerServerTasksMachineName = tmpSchedulerServerTasksMachineName;

                _projectInfo.SchedulerAppTasks
                .ForEach(
                    schedulerAppTask =>
                {
                    string taskName = schedulerAppTask.Name;

                    Tuple <string, string> machineNameAndTaskName =
                        Tuple.Create(schedulerServerTasksMachineName, taskName);

                    ScheduledTaskDetails existingTaskDetails =
                        _existingTaskDetailsByMachineNameAndTaskName[machineNameAndTaskName];

                    AddTaskConfigurationSteps(
                        environmentInfo,
                        schedulerServerTasksMachineName,
                        schedulerAppTask,
                        existingTaskDetails);

                    // create a step to toggle scheduler task enabled
                    if (existingTaskDetails == null || existingTaskDetails.IsEnabled)
                    {
                        AddToggleSchedulerAppTaskEnabledStep(
                            schedulerServerTasksMachineName,
                            taskName,
                            true);
                    }
                });
            }
        }
예제 #9
0
        private void AddTaskConfigurationSteps(EnvironmentInfo environmentInfo, string schedulerServerTasksMachineName, SchedulerAppTask schedulerAppTask, ScheduledTaskDetails taskDetails = null)
        {
            TaskSettingsCompareResult settingsCompareResult = CompareTaskSettings(taskDetails, schedulerAppTask, environmentInfo);
            bool hasSettingsChanged = settingsCompareResult.AreEqual == false;

            bool taskExists = taskDetails != null;

            if (taskExists)
            {
                LogSettingsDifferences(schedulerAppTask.Name, settingsCompareResult);
            }
            else
            {
                PostDiagnosticMessage(string.Format("Scheduler task [{0}] doesn't exist.", schedulerAppTask.Name), DiagnosticMessageType.Trace);
            }

            EnvironmentUser environmentUser =
                environmentInfo.GetEnvironmentUser(schedulerAppTask.UserId);

            string environmentUserPassword = null;

            if (!taskExists || hasSettingsChanged)
            {
                // collect password if not already collected
                if (!_collectedPasswordsByUserName.TryGetValue(environmentUser.UserName, out environmentUserPassword))
                {
                    environmentUserPassword =
                        PasswordCollectorHelper.CollectPasssword(
                            _passwordCollector,
                            DeploymentInfo.DeploymentId,
                            environmentInfo,
                            schedulerServerTasksMachineName,
                            environmentUser,
                            OnDiagnosticMessagePosted);

                    _collectedPasswordsByUserName.Add(environmentUser.UserName, environmentUserPassword);
                }
            }

            string taskExecutablePath =
                GetTaskExecutablePath(schedulerAppTask, environmentInfo);

            if (!taskExists)
            {
                // create a step for scheduling a new app
                AddSubTask(
                    new CreateSchedulerTaskDeploymentStep(
                        schedulerServerTasksMachineName,
                        schedulerAppTask.Name,
                        taskExecutablePath,
                        environmentUser.UserName,
                        environmentUserPassword,
                        schedulerAppTask.ScheduledHour,
                        schedulerAppTask.ScheduledMinute,
                        schedulerAppTask.ExecutionTimeLimitInMinutes,
                        schedulerAppTask.Repetition,
                        _taskScheduler));
            }
            else if (hasSettingsChanged)
            {
                // create a step for updating an existing scheduler app
                AddSubTask(
                    new UpdateSchedulerTaskDeploymentStep(
                        schedulerServerTasksMachineName,
                        schedulerAppTask.Name,
                        taskExecutablePath,
                        environmentUser.UserName,
                        environmentUserPassword,
                        schedulerAppTask.ScheduledHour,
                        schedulerAppTask.ScheduledMinute,
                        schedulerAppTask.ExecutionTimeLimitInMinutes,
                        schedulerAppTask.Repetition,
                        _taskScheduler));
            }
        }
예제 #10
0
        public void SetUp()
        {
            _projectInfoRepositoryFake     = new Mock <IProjectInfoRepository>();
            _environmentInfoRepositoryFake = new Mock <IEnvironmentInfoRepository>();
            _artifactsRepositoryFake       = new Mock <IArtifactsRepository>();
            _taskSchedulerFake             = new Mock <ITaskScheduler>();
            _passwordCollectorFake         = new Mock <IPasswordCollector>();
            _directoryAdapterFake          = new Mock <IDirectoryAdapter>();
            _fileAdapterFake    = new Mock <IFileAdapter>();
            _zipFileAdapterFake = new Mock <IZipFileAdapter>();

            _projectInfo = ProjectInfoGenerator.GetSchedulerAppProjectInfo();

            SchedulerAppTask schedulerAppTask1 = _projectInfo.SchedulerAppTasks.First();
            SchedulerAppTask schedulerAppTask2 = _projectInfo.SchedulerAppTasks.Second();

            _environmentInfo =
                DeploymentDataGenerator.GetEnvironmentInfo(
                    new[]
            {
                new EnvironmentUser(schedulerAppTask1.UserId, "user_name_1"),
                new EnvironmentUser(schedulerAppTask2.UserId, "user_name_2"),
            });

            _projectInfoRepositoryFake.Setup(pir => pir.FindByName(It.IsAny <string>()))
            .Returns(_projectInfo);

            string exeAbsolutePath1 =
                Path.Combine(
                    _environmentInfo.SchedulerAppsBaseDirPath,
                    _projectInfo.SchedulerAppDirName,
                    schedulerAppTask1.ExecutableName);

            var scheduledTaskRepetition1 =
                new ScheduledTaskRepetition(
                    schedulerAppTask1.Repetition.Interval,
                    schedulerAppTask1.Repetition.Duration,
                    schedulerAppTask1.Repetition.StopAtDurationEnd);

            ScheduledTaskDetails taskDetails1 =
                GetTaskDetails(schedulerAppTask1, exeAbsolutePath1, true, false, scheduledTaskRepetition1);

            ScheduledTaskDetails taskDetailsDisabled1 =
                GetTaskDetails(schedulerAppTask1, exeAbsolutePath1, false, false, scheduledTaskRepetition1);

            int timesCalled11 = 0;

            _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.First(), schedulerAppTask1.Name))
            .Returns(() =>
            {
                timesCalled11++;

                if (timesCalled11 == 1)
                {
                    return(taskDetails1);
                }

                if (timesCalled11 == 2)
                {
                    return(taskDetailsDisabled1);
                }

                throw new Exception("Unexpected number of calls!");
            });

            int timesCalled21 = 0;

            _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.Second(), schedulerAppTask1.Name))
            .Returns(() =>
            {
                timesCalled21++;

                if (timesCalled21 == 1)
                {
                    return(taskDetails1);
                }

                if (timesCalled21 == 2)
                {
                    return(taskDetailsDisabled1);
                }

                throw new Exception("Unexpected number of calls!");
            });

            string exeAbsolutePath2 =
                Path.Combine(
                    _environmentInfo.SchedulerAppsBaseDirPath,
                    _projectInfo.SchedulerAppDirName,
                    schedulerAppTask2.ExecutableName);

            var scheduledTaskRepetition2 =
                new ScheduledTaskRepetition(
                    schedulerAppTask2.Repetition.Interval,
                    schedulerAppTask2.Repetition.Duration,
                    schedulerAppTask2.Repetition.StopAtDurationEnd);

            ScheduledTaskDetails taskDetails2 =
                GetTaskDetails(schedulerAppTask2, exeAbsolutePath2, true, false, scheduledTaskRepetition2);

            ScheduledTaskDetails taskDetailsDisabled2 =
                GetTaskDetails(schedulerAppTask2, exeAbsolutePath2, false, false, scheduledTaskRepetition2);

            int timesCalled12 = 0;

            _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.First(), schedulerAppTask2.Name))
            .Returns(() =>
            {
                timesCalled12++;

                if (timesCalled12 == 1)
                {
                    return(taskDetails2);
                }

                if (timesCalled12 == 2)
                {
                    return(taskDetailsDisabled2);
                }

                throw new Exception("Unexpected number of calls!");
            });

            int timesCalled22 = 0;

            _taskSchedulerFake
            .Setup(x => x.GetScheduledTaskDetails(_environmentInfo.SchedulerServerTasksMachineNames.Second(), schedulerAppTask2.Name))
            .Returns(() =>
            {
                timesCalled22++;

                if (timesCalled22 == 1)
                {
                    return(taskDetails2);
                }

                if (timesCalled22 == 2)
                {
                    return(taskDetailsDisabled2);
                }

                throw new Exception("Unexpected number of calls!");
            });

            _environmentInfoRepositoryFake
            .Setup(x => x.FindByName(It.IsAny <string>()))
            .Returns(_environmentInfo);

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

            _deployTask =
                new DeploySchedulerAppDeploymentTask(
                    _projectInfoRepositoryFake.Object,
                    _environmentInfoRepositoryFake.Object,
                    _artifactsRepositoryFake.Object,
                    _taskSchedulerFake.Object,
                    _passwordCollectorFake.Object,
                    _directoryAdapterFake.Object,
                    _fileAdapterFake.Object,
                    _zipFileAdapterFake.Object);

            _deployTask.Initialize(DeploymentInfoGenerator.GetSchedulerAppDeploymentInfo());
        }