-
Notifications
You must be signed in to change notification settings - Fork 2
Add create/kill background process #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattia-lecci
wants to merge
7
commits into
opentap:main
Choose a base branch
from
mattia-lecci:background-process
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
740ca60
Add create/kill background process
mattia-lecci b9e2701
Improve interface and logging
mattia-lecci 0447e84
Add check if background process is still alive
mattia-lecci 1394f1f
Add Pid class to better handle Input/Output PIDs
mattia-lecci c6eb9e9
Centralize bg processes operations and tracking to SshResource
mattia-lecci 5ed4721
fix commit issue
mattia-lecci 9a9f3e0
Add "linux only" information
mattia-lecci File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //Copyright 2019-2020 Keysight Technologies | ||
| // | ||
| //Licensed under the Apache License, Version 2.0 (the "License"); | ||
| //you may not use this file except in compliance with the License. | ||
| //You may obtain a copy of the License at | ||
| // | ||
| //http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| //Unless required by applicable law or agreed to in writing, software | ||
| //distributed under the License is distributed on an "AS IS" BASIS, | ||
| //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| //See the License for the specific language governing permissions and | ||
| //limitations under the License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.ComponentModel; | ||
| using Renci.SshNet; | ||
|
|
||
| namespace OpenTap.Plugins.Ssh | ||
| { | ||
|
|
||
| public class Pid | ||
| { | ||
| #region Settings | ||
| public uint pid { get; private set; } | ||
|
|
||
| #endregion | ||
|
|
||
| public Pid(uint pid) | ||
| { | ||
| this.pid = pid; | ||
| } | ||
|
|
||
| public Pid(string pid) | ||
| { | ||
| this.pid = uint.Parse(pid); | ||
| } | ||
|
|
||
| public override string ToString() | ||
| { | ||
| return pid.ToString(); | ||
| } | ||
|
|
||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
OpenTap.Plugins.Ssh/Background/SshBackgroundCommandStep.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //Copyright 2019-2020 Keysight Technologies | ||
| // | ||
| //Licensed under the Apache License, Version 2.0 (the "License"); | ||
| //you may not use this file except in compliance with the License. | ||
| //You may obtain a copy of the License at | ||
| // | ||
| //http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| //Unless required by applicable law or agreed to in writing, software | ||
| //distributed under the License is distributed on an "AS IS" BASIS, | ||
| //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| //See the License for the specific language governing permissions and | ||
| //limitations under the License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.ComponentModel; | ||
| using Renci.SshNet; | ||
|
|
||
| namespace OpenTap.Plugins.Ssh.Background | ||
| { | ||
|
|
||
| [Display("Background SSH Command", "Run a command in the background using a session setup by an SSH Session step, SSH Instrument or SSH Dut.", Groups: new[] { "SSH", "Background (Linux only)" })] | ||
| public class BackgroundSshCommandStep : SshStepBase | ||
| { | ||
| #region Settings | ||
| public string Command { get; set; } | ||
|
|
||
| [Output] | ||
| [Display("Process PID", Description:"The PID of the background process", Group: "Response")] | ||
| public Pid pid { get; private set; } | ||
|
|
||
| #endregion | ||
|
|
||
| public BackgroundSshCommandStep() | ||
| { | ||
| Name = "Background SSH Command: {Command}"; | ||
| } | ||
|
|
||
| public override void Run() | ||
| { | ||
| pid = SshResource.StartBackgroundProcess(Command); | ||
| Log.Info($"Running command `{Command}` in the background"); | ||
| } | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
OpenTap.Plugins.Ssh/Background/SshCheckBackgroundCommandStep.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| //Copyright 2019-2020 Keysight Technologies | ||
| // | ||
| //Licensed under the Apache License, Version 2.0 (the "License"); | ||
| //you may not use this file except in compliance with the License. | ||
| //You may obtain a copy of the License at | ||
| // | ||
| //http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| //Unless required by applicable law or agreed to in writing, software | ||
| //distributed under the License is distributed on an "AS IS" BASIS, | ||
| //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| //See the License for the specific language governing permissions and | ||
| //limitations under the License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.ComponentModel; | ||
| using Renci.SshNet; | ||
|
|
||
| namespace OpenTap.Plugins.Ssh.Background | ||
| { | ||
|
|
||
| [Display("Check Background SSH Command", "Checks if a background command is still running.", Groups: new[] { "SSH", "Background (Linux only)" })] | ||
| public class CheckBackgroundSshCommandStep : SshStepBase | ||
| { | ||
| #region Settings | ||
| [Display("Process PID", "Select the Background SSH Command to kill")] | ||
|
|
||
| public Input<Pid> InputPid { get; set; } | ||
|
|
||
| #endregion | ||
|
|
||
| public CheckBackgroundSshCommandStep() | ||
| { | ||
| Name = "Check Background SSH Command: PID={Process PID}"; | ||
| InputPid = new Input<Pid>(); | ||
| } | ||
|
|
||
| public override void Run() | ||
| { | ||
| if (InputPid == null || InputPid.Value == null) | ||
| { | ||
| throw new ArgumentNullException("No PID was set"); | ||
| } | ||
| Pid pid = InputPid.Value; | ||
|
|
||
| bool isRunning = SshResource.IsBackgroundProcessRunning(pid); | ||
| if (isRunning) | ||
| { | ||
| Log.Info($"Process with PID={pid} is alive"); | ||
| UpgradeVerdict(Verdict.Pass); | ||
| return; | ||
| } | ||
|
|
||
| Log.Info($"Process with PID={pid} is not alive"); | ||
| UpgradeVerdict(Verdict.Fail); | ||
| } | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
OpenTap.Plugins.Ssh/Background/SshKillBackgroundCommandStep.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| //Copyright 2019-2020 Keysight Technologies | ||
| // | ||
| //Licensed under the Apache License, Version 2.0 (the "License"); | ||
| //you may not use this file except in compliance with the License. | ||
| //You may obtain a copy of the License at | ||
| // | ||
| //http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| //Unless required by applicable law or agreed to in writing, software | ||
| //distributed under the License is distributed on an "AS IS" BASIS, | ||
| //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| //See the License for the specific language governing permissions and | ||
| //limitations under the License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.ComponentModel; | ||
| using Renci.SshNet; | ||
|
|
||
| namespace OpenTap.Plugins.Ssh.Background | ||
| { | ||
|
|
||
| [Display("Kill Background SSH Command", "Kills a command running in the background.", Groups: new[] { "SSH", "Background (Linux only)" })] | ||
| public class KillBackgroundSshCommandStep : SshStepBase | ||
| { | ||
| #region Settings | ||
| [Display("Process PID", "Select the Background SSH Command to kill")] | ||
|
|
||
| public Input<Pid> InputPid { get; set; } | ||
|
|
||
| #endregion | ||
|
|
||
| public KillBackgroundSshCommandStep() | ||
| { | ||
| Name = "Kill Background SSH Command: PID={Process PID}"; | ||
| InputPid = new Input<Pid>(); | ||
| } | ||
|
|
||
| public override void Run() | ||
| { | ||
| if (InputPid == null) | ||
| { | ||
| throw new ArgumentNullException("No PID was set"); | ||
| } | ||
| Pid pid = InputPid.Value; | ||
|
|
||
| if (SshResource.KillBackgroundProcess(pid)) | ||
| { | ||
| UpgradeVerdict(Verdict.Pass); | ||
| return; | ||
| } | ||
|
|
||
| UpgradeVerdict(Verdict.Fail); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exit status doesn't check if the background process exited. It checks if the
killcommand successfully sent theSIGTERMsignal to the process. If the process ignores the signal, or doesn't handle it (because it's hanging for example) thenkillwill still return 0 without exiting the process