forked from Azure/azure-cli-extensions
-
Notifications
You must be signed in to change notification settings - Fork 4
RDP #27
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
vthiebaut10
wants to merge
23
commits into
main
Choose a base branch
from
rdp
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
RDP #27
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
18042b6
Initial changes to support rdp
vthiebaut10 b8f99ff
RDP
vthiebaut10 b472c13
Merge branch 'Azure:main' into rdp
vthiebaut10 785e146
small fixes
vthiebaut10 7ca1f3a
Merge branch 'rdp' of https://github.com/vthiebaut10/azure-cli-extens…
vthiebaut10 4eca350
Refactor rdp utils
vthiebaut10 856711d
more changes
vthiebaut10 2ff7016
Merge branch 'Azure:main' into rdp
vthiebaut10 f709daf
threads instead of process?
vthiebaut10 2ec3453
changes
vthiebaut10 efbc5d6
more random changes
vthiebaut10 fd72f6e
Update tests
vthiebaut10 0675abf
Style fixes
vthiebaut10 064a097
Remove typos
vthiebaut10 f68abcd
Fix failing tests
vthiebaut10 f08cc79
Add process helper file from network module
vthiebaut10 142ca8c
remove test that tests call rdp
vthiebaut10 5660342
remove any reference to launch and wait
vthiebaut10 2c7a465
delete duplicated process helper
vthiebaut10 40921e9
Merge branch 'Azure:main' into rdp
vthiebaut10 b8b996d
process helper file
vthiebaut10 c2ca7c1
change how we write styled text
vthiebaut10 91f46a8
Style fixes
vthiebaut10 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
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
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,112 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| # pylint: disable=too-few-public-methods | ||
| # pylint: disable=consider-using-with | ||
|
|
||
| import subprocess | ||
| from ctypes import WinDLL, c_int, c_size_t, Structure, WinError, sizeof, pointer | ||
| from ctypes.wintypes import BOOL, DWORD, HANDLE, LPVOID, LPCWSTR, LPDWORD | ||
| from knack.log import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| def _errcheck(is_error_result=(lambda result: not result)): | ||
| def impl(result, func, args): | ||
| # pylint: disable=unused-argument | ||
| if is_error_result(result): | ||
| raise WinError() | ||
|
|
||
| return result | ||
|
|
||
| return impl | ||
|
|
||
|
|
||
| # Win32 CreateJobObject | ||
| kernel32 = WinDLL("kernel32") | ||
| kernel32.CreateJobObjectW.errcheck = _errcheck(lambda result: result == 0) | ||
| kernel32.CreateJobObjectW.argtypes = (LPVOID, LPCWSTR) | ||
| kernel32.CreateJobObjectW.restype = HANDLE | ||
|
|
||
|
|
||
| # Win32 OpenProcess | ||
| PROCESS_TERMINATE = 0x0001 | ||
| PROCESS_SET_QUOTA = 0x0100 | ||
| PROCESS_SYNCHRONIZE = 0x00100000 | ||
| kernel32.OpenProcess.errcheck = _errcheck(lambda result: result == 0) | ||
| kernel32.OpenProcess.restype = HANDLE | ||
| kernel32.OpenProcess.argtypes = (DWORD, BOOL, DWORD) | ||
|
|
||
| # Win32 WaitForSingleObject | ||
| INFINITE = 0xFFFFFFFF | ||
| # kernel32.WaitForSingleObject.errcheck = _errcheck() | ||
| kernel32.WaitForSingleObject.argtypes = (HANDLE, DWORD) | ||
| kernel32.WaitForSingleObject.restype = DWORD | ||
|
|
||
| # Win32 AssignProcessToJobObject | ||
| kernel32.AssignProcessToJobObject.errcheck = _errcheck() | ||
| kernel32.AssignProcessToJobObject.argtypes = (HANDLE, HANDLE) | ||
| kernel32.AssignProcessToJobObject.restype = BOOL | ||
|
|
||
| # Win32 QueryInformationJobObject | ||
| JOBOBJECTCLASS = c_int | ||
| JobObjectBasicProcessIdList = JOBOBJECTCLASS(3) | ||
|
|
||
|
|
||
| class JOBOBJECT_BASIC_PROCESS_ID_LIST(Structure): | ||
| _fields_ = [('NumberOfAssignedProcess', DWORD), | ||
| ('NumberOfProcessIdsInList', DWORD), | ||
| ('ProcessIdList', c_size_t * 1)] | ||
|
|
||
|
|
||
| kernel32.QueryInformationJobObject.errcheck = _errcheck() | ||
| kernel32.QueryInformationJobObject.restype = BOOL | ||
| kernel32.QueryInformationJobObject.argtypes = (HANDLE, JOBOBJECTCLASS, LPVOID, DWORD, LPDWORD) | ||
|
|
||
|
|
||
| def launch_and_wait(command): | ||
| """Windows Only: Runs and waits for the command to exit. It creates a new process and | ||
| associates it with a job object. It then waits for all the job object child processes | ||
| to exit. | ||
| """ | ||
| try: | ||
| job = kernel32.CreateJobObjectW(None, None) | ||
| process = subprocess.Popen(command) | ||
|
|
||
| # Terminate and set quota are required to join process to job | ||
| process_handle = kernel32.OpenProcess( | ||
| PROCESS_TERMINATE | PROCESS_SET_QUOTA, | ||
| False, | ||
| process.pid, | ||
| ) | ||
| kernel32.AssignProcessToJobObject(job, process_handle) | ||
|
|
||
| job_info = JOBOBJECT_BASIC_PROCESS_ID_LIST() | ||
| job_info_size = DWORD(sizeof(job_info)) | ||
|
|
||
| while True: | ||
| kernel32.QueryInformationJobObject( | ||
| job, | ||
| JobObjectBasicProcessIdList, | ||
| pointer(job_info), | ||
| job_info_size, | ||
| pointer(job_info_size)) | ||
|
|
||
| # Wait for the first running child under the job object | ||
| if job_info.NumberOfProcessIdsInList > 0: | ||
| logger.debug("Waiting for process %d", job_info.ProcessIdList[0]) | ||
| # Synchronize access is required to wait on handle | ||
| child_handle = kernel32.OpenProcess( | ||
| PROCESS_SYNCHRONIZE, | ||
| False, | ||
| job_info.ProcessIdList[0], | ||
| ) | ||
| kernel32.WaitForSingleObject(child_handle, INFINITE) | ||
| else: | ||
| break | ||
|
|
||
| except OSError as e: | ||
| logger.error("Could not run '%s' command. Exception: %s", command, str(e)) |
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.