public JobHistoryViewModel(RunbookViewModel runbookViewModel)
        {
            _runbook = (RunbookModelProxy)runbookViewModel.Model;
            Owner    = _runbook.Context.Service;

            Jobs = new ObservableCollection <JobModelProxy>();

            //AsyncExecution.Run(ThreadPriority.Normal, () =>
            Task.Run(() =>
            {
                IList <JobModelProxy> draftJobs     = null;
                IList <JobModelProxy> publishedJobs = null;

                try
                {
                    if (_runbook.DraftRunbookVersionID.HasValue)
                    {
                        draftJobs = Owner.GetJobs(_runbook.DraftRunbookVersionID.Value);
                    }

                    if (_runbook.PublishedRunbookVersionID.HasValue)
                    {
                        publishedJobs = Owner.GetJobs(_runbook.PublishedRunbookVersionID.Value);
                    }
                }
                catch (ApplicationException ex)
                {
                    GlobalExceptionHandler.Show(ex);
                }

                Execute.OnUIThread(() =>
                {
                    if (draftJobs != null)
                    {
                        foreach (var job in draftJobs)
                        {
                            job.BoundRunbookViewModel = runbookViewModel;
                            job.RunbookType           = RunbookType.Draft;
                            Jobs.Add(job);
                        }
                    }

                    if (publishedJobs != null)
                    {
                        foreach (var job in publishedJobs)
                        {
                            job.BoundRunbookViewModel = runbookViewModel;
                            job.RunbookType           = RunbookType.Published;
                            Jobs.Add(job);
                        }
                    }

                    Jobs = Jobs.OrderBy(j => j.StartTime).ToObservableCollection();
                });
            });
        }
        public void Execute(object parameter)
        {
            var shell = IoC.Get<IShell>();
            
            var dialog = new UIAddNewItemDialog();
            dialog.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            var showDialog = dialog.ShowDialog();
            if (showDialog == null || !(bool) showDialog)
                return;

            var reader = new StreamReader(dialog.SelectedTemplate.Path);
            reader.ReadLine(); // Skip the first line since that contains the DESCRIPTION
            var runbookContent = reader.ReadToEnd();

            reader.Close();
                
            var context = IoC.Get<EnvironmentExplorerViewModel>().GetCurrentContext();

            if (context != null)
            {
                var viewModel = default(RunbookViewModel);

                var check = context.Runbooks.FirstOrDefault(r => r.Title.Equals(dialog.CreatedName, StringComparison.InvariantCultureIgnoreCase));
                if (check == null)
                {
                    switch (context.ContextType)
                    {
                        case Core.ContextType.SMA:
                            var runbook = new SMA.Runbook {RunbookName = dialog.CreatedName};

                            viewModel = new RunbookViewModel(new RunbookModelProxy(runbook, context));
                            viewModel.AddSnippet(runbookContent);
                            break;
                        case Core.ContextType.AzureRM:
                        case Core.ContextType.Azure:
                            var azureRunbook = new Vendor.Azure.Runbook {RunbookName = dialog.CreatedName};

                            viewModel = new RunbookViewModel(new RunbookModelProxy(azureRunbook, context));
                            viewModel.AddSnippet(runbookContent);
                            break;
                    }

                    if (viewModel != null)
                        shell.OpenDocument(viewModel);
                }
                else
                {
                    MessageBox.Show("A runbook with the same name already exists.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show("Unable to determine context.");
            }
        }
Beispiel #3
0
        public ExecutionResultViewModel(RunbookViewModel runbookViewModel, Guid jobId, bool isTestRun)
            : this(isTestRun)
        {
            _runbookViewModel = runbookViewModel;
            _jobId            = jobId;

            _backendService = (_runbookViewModel.Model as RunbookModelProxy).Context.Service;

            SubscribeToJob();
        }
        public PrepareRunWindow(RunbookViewModel runbookViewModel)
        {
            _runbookViewModel = runbookViewModel;

            InitializeComponent();

            DataContext = this;
            Inputs = new ObservableCollection<ICompletionData>();

            Loaded += PrepareRunWindowLoaded;
        }
Beispiel #5
0
        public PrepareRunWindow(RunbookViewModel runbookViewModel)
        {
            _runbookViewModel = runbookViewModel;

            InitializeComponent();

            DataContext = this;
            Inputs      = new ObservableCollection <ICompletionData>();

            Loaded += PrepareRunWindowLoaded;
        }
        public JobHistoryViewModel(RunbookViewModel runbookViewModel)
        {
            _runbook = (RunbookModelProxy)runbookViewModel.Model;
            Owner = _runbook.Context.Service;

            Jobs = new ObservableCollection<JobModelProxy>();

            //AsyncExecution.Run(ThreadPriority.Normal, () =>
            Task.Run(() =>
            {
                IList<JobModelProxy> draftJobs = null;
                IList<JobModelProxy> publishedJobs = null;

                try
                {
                    if (_runbook.DraftRunbookVersionID.HasValue)
                        draftJobs = Owner.GetJobs(_runbook.DraftRunbookVersionID.Value);

                    if (_runbook.PublishedRunbookVersionID.HasValue)
                        publishedJobs = Owner.GetJobs(_runbook.PublishedRunbookVersionID.Value);
                }
                catch (ApplicationException ex)
                {
                    GlobalExceptionHandler.Show(ex);
                }

                Execute.OnUIThread(() =>
                {
                    if (draftJobs != null)
                    {
                        foreach (var job in draftJobs)
                        {
                            job.BoundRunbookViewModel = runbookViewModel;
                            job.RunbookType = RunbookType.Draft;
                            Jobs.Add(job);
                        }
                    }

                    if (publishedJobs != null)
                    {
                        foreach (var job in publishedJobs)
                        {
                            job.BoundRunbookViewModel = runbookViewModel;
                            job.RunbookType = RunbookType.Published;
                            Jobs.Add(job);
                        }
                    }

                    Jobs = Jobs.OrderBy(j => j.StartTime).ToObservableCollection();
                });
            });
        }
Beispiel #7
0
        public DebuggerService2(RunbookViewModel runbookViewModel)
        {
            Logger.Initialize(Path.Combine(AppHelper.CachePath, "PowerShellEditorServices.log"), LogLevel.Verbose);

            /*_powerShell = new PowerShellContext();
             * _workspace = new Workspace(_powerShell.PowerShellVersion);
             *
             * _debugService = new DebugService(_powerShell);
             * _debugService.DebuggerStopped += OnDebugStopped;*/
            _editorSession = new EditorSession();
            _editorSession.StartSession();
            _editorSession.DebugService.DebuggerStopped += OnDebugStopped;
            _editorSession.ConsoleService.OutputWritten += OnConsoleOutputWritten;

            _runbookViewModel = runbookViewModel;

            _breakpoints = new List <LineBreakpoint>();
        }
        public DebuggerService2(RunbookViewModel runbookViewModel)
        {
            Logger.Initialize(Path.Combine(AppHelper.CachePath, "PowerShellEditorServices.log"), LogLevel.Verbose);

            /*_powerShell = new PowerShellContext();
            _workspace = new Workspace(_powerShell.PowerShellVersion);

            _debugService = new DebugService(_powerShell);
            _debugService.DebuggerStopped += OnDebugStopped;*/
            _editorSession = new EditorSession();
            _editorSession.StartSession();
            _editorSession.DebugService.DebuggerStopped += OnDebugStopped;
            _editorSession.ConsoleService.OutputWritten += OnConsoleOutputWritten;
                
            _runbookViewModel = runbookViewModel;

            _breakpoints = new List<LineBreakpoint>();
        }
Beispiel #9
0
        public DebuggerService(RunbookViewModel runbookViewModel)
        {
            _codeViewModel           = runbookViewModel;
            _breakpoints             = new List <LineBreakpoint>();
            _variables               = new List <VariableDetailsBase>();
            _cancellationTokenSource = new CancellationTokenSource();

            _initialSessionState     = InitialSessionState.CreateDefault2();
            _runspace                = RunspaceFactory.CreateRunspace(new CustomHost(), _initialSessionState);//(new CustomHost());
            _runspace.ApartmentState = ApartmentState.STA;
            _runspace.ThreadOptions  = PSThreadOptions.ReuseThread;
            _runspace.Open();

            _runspace.Debugger.SetDebugMode(DebugModes.LocalScript);
            _runspace.Debugger.DebuggerStop += OnDebuggerStop;

            _powerShell          = PowerShell.Create();
            _powerShell.Runspace = _runspace;

            SetExecutionPolicy(_powerShell, ExecutionPolicy.RemoteSigned);
        }
Beispiel #10
0
 public IList<ICompletionEntry> GetParameters(RunbookViewModel runbookViewModel, KeywordCompletionData completionData)
 {
     throw new NotImplementedException();
 }
Beispiel #11
0
        public async Task<bool> CheckOut(RunbookViewModel runbook)
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);

            RunbookGetResponse response = await _client.Runbooks.GetAsync(_connectionData.AzureRMGroupName, _connectionData.AzureAutomationAccount, runbook.Runbook.RunbookName, cts.Token);

            if (response.Runbook.Properties.State != "Published")
                return false;

            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);

            RunbookContentResponse runbookContentResponse = await _client.Runbooks.ContentAsync(_connectionData.AzureRMGroupName, _connectionData.AzureAutomationAccount, runbook.Runbook.RunbookName, cts.Token);
            
            // Create draft properties
            RunbookCreateOrUpdateDraftParameters draftParams = new RunbookCreateOrUpdateDraftParameters();
            draftParams.Properties = new RunbookCreateOrUpdateDraftProperties();
            draftParams.Properties.Description = response.Runbook.Properties.Description;
            draftParams.Properties.LogProgress = response.Runbook.Properties.LogProgress;
            draftParams.Properties.LogVerbose = response.Runbook.Properties.LogVerbose;
            draftParams.Properties.RunbookType = response.Runbook.Properties.RunbookType;
            draftParams.Properties.Draft = new RunbookDraft();
            draftParams.Tags = response.Runbook.Tags;
            draftParams.Name = runbook.Runbook.RunbookName;
            draftParams.Location = _connectionData.AzureRMLocation;

            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);

            await _client.Runbooks.CreateOrUpdateWithDraftAsync(_connectionData.AzureRMGroupName, _connectionData.AzureAutomationAccount, draftParams, cts.Token);
            RunbookDraftUpdateParameters draftUpdateParams = new RunbookDraftUpdateParameters()
            {
                Name = runbook.Runbook.RunbookName,
                Stream = runbookContentResponse.Stream.ToString()
            };
            cts = new CancellationTokenSource();
            cts.CancelAfter(TIMEOUT_MS);

            await _client.RunbookDraft.UpdateAsync(_connectionData.AzureRMGroupName, _connectionData.AzureAutomationAccount, draftUpdateParams, cts.Token);

            return true;
        }
Beispiel #12
0
        public async Task<bool> CheckOut(RunbookViewModel runbook)
        {
            var invocationId = string.Empty;

            if (TracingAdapter.IsEnabled)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                TracingAdapter.Enter(invocationId, this, "CheckOut", new Dictionary<string, object>()
                {
                    {
                        "runbook",
                        runbook
                    }
                });
            }

            var publishedContent = runbook.PublishedContent;

            // Check out the runbok
            await SendRequestAsync("runbooks/" + runbook.Runbook.RunbookName.ToUrlSafeString() + "/draft/content", HttpMethod.Put, "").ConfigureAwait(false);

            // Notify SMA Studio that we have a draft and download the content
            //runbook.Runbook.DraftRunbookVersionID = Guid.NewGuid();
            //runbook.GetContent(RunbookType.Draft, true);

            if (TracingAdapter.IsEnabled)
            {
                TracingAdapter.Exit(invocationId, null);
            }

            return true;
        }
Beispiel #13
0
        public void Execute(object parameter)
        {
            var shell = IoC.Get <IShell>();

            var dialog = new UIAddNewItemDialog();

            dialog.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            var showDialog = dialog.ShowDialog();

            if (showDialog == null || !(bool)showDialog)
            {
                return;
            }

            var reader = new StreamReader(dialog.SelectedTemplate.Path);

            reader.ReadLine(); // Skip the first line since that contains the DESCRIPTION
            var runbookContent = reader.ReadToEnd();

            reader.Close();

            var context = IoC.Get <EnvironmentExplorerViewModel>().GetCurrentContext();

            if (context != null)
            {
                var viewModel = default(RunbookViewModel);

                var check = context.Runbooks.FirstOrDefault(r => r.Title.Equals(dialog.CreatedName, StringComparison.InvariantCultureIgnoreCase));
                if (check == null)
                {
                    switch (context.ContextType)
                    {
                    case Core.ContextType.SMA:
                        var runbook = new SMA.Runbook {
                            RunbookName = dialog.CreatedName
                        };

                        viewModel = new RunbookViewModel(new RunbookModelProxy(runbook, context));
                        viewModel.AddSnippet(runbookContent);
                        break;

                    case Core.ContextType.AzureRM:
                    case Core.ContextType.Azure:
                        var azureRunbook = new Vendor.Azure.Runbook {
                            RunbookName = dialog.CreatedName
                        };

                        viewModel = new RunbookViewModel(new RunbookModelProxy(azureRunbook, context));
                        viewModel.AddSnippet(runbookContent);
                        break;
                    }

                    if (viewModel != null)
                    {
                        shell.OpenDocument(viewModel);
                    }
                }
                else
                {
                    MessageBox.Show("A runbook with the same name already exists.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show("Unable to determine context.");
            }
        }
Beispiel #14
0
 public IList<ICompletionEntry> GetParameters(RunbookViewModel runbookViewModel, KeywordCompletionData completionData)
 {
     return completionData.Parameters;
 }
Beispiel #15
0
        /// <summary>
        /// Check out the published runbook and make it a draft, this will overwrite any drafts currently there.
        /// </summary>
        /// <param name="runbookViewModel"></param>
        /// <returns></returns>
        public async Task<bool> CheckOut(RunbookViewModel runbookViewModel)
        {
            Logger.DebugFormat("CheckOut(runbook = {0})", runbookViewModel.DisplayName);

            return await Task.Run(delegate ()
            {
                var context = GetConnection();
                var runbook = runbookViewModel.Runbook;

                try
                {
                    context.AttachTo("Runbooks", runbook.Model);
                }
                catch (InvalidOperationException) { /* already attached */ }

                if (!runbook.DraftRunbookVersionID.HasValue || runbook.DraftRunbookVersionID == Guid.Empty)
                {
                    try
                    {
                        runbook.DraftRunbookVersionID = new Guid?(((SMA.Runbook)runbook.Model).Edit(context));
                    }
                    catch (DataServiceQueryException ex)
                    {
                        /*var xml = default(string);

                        if (ex.InnerException != null)
                            xml = ex.InnerException.Message;
                        else
                            xml = ex.Message;

                        Logger.Error("Error when checking out the runbook.", ex);
                        XmlExceptionHandler.Show(xml);

                        return false;*/
                        throw new ApplicationException("Error when drafting the variable. Please refer to the output for more information.", ex);
                    }
                }
                else
                {
                    //Core.Log.ErrorFormat("The runbook was already checked out.");
                    //MessageBox.Show("The runbook's already checked out.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return false;
                }

                // Download the content of the published runbook
                var publishedContent = GetContent(GetBackendUrl(RunbookType.Published, (RunbookModelProxy)runbookViewModel.Model));

                MemoryStream ms = new MemoryStream();
                byte[] bytes = Encoding.UTF8.GetBytes(publishedContent);
                ms.Write(bytes, 0, bytes.Length);
                ms.Seek(0, SeekOrigin.Begin);

                Stream baseStream = (Stream)ms;
                RunbookVersion entity = (from rv in context.RunbookVersions
                                         where (Guid?)rv.RunbookVersionID == runbook.DraftRunbookVersionID
                                         select rv).FirstOrDefault<RunbookVersion>();

                context.SetSaveStream(entity, baseStream, true, "application/octet-stream", string.Empty);
                context.SaveChanges();

                return true;
            });
        }