-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Bug fix for webapp ssh #8324
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
Merged
Merged
Bug fix for webapp ssh #8324
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ | |
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from __future__ import print_function | ||
| import subprocess | ||
| import threading | ||
| import time | ||
|
|
||
|
|
@@ -21,6 +20,9 @@ | |
| import sys | ||
| import OpenSSL.crypto | ||
|
|
||
| from fabric import Connection | ||
|
|
||
|
|
||
| from knack.prompting import prompt_pass, NoTTYException | ||
| from knack.util import CLIError | ||
| from knack.log import get_logger | ||
|
|
@@ -2253,11 +2255,11 @@ def _ping_scm_site(cmd, resource_group, name): | |
| requests.get(scm_url + '/api/settings', headers=authorization) | ||
|
|
||
|
|
||
| def _check_for_ready_tunnel(tunnel_server): | ||
| return tunnel_server.is_port_set_to_default() | ||
| def is_webapp_up(tunnel_server): | ||
| return tunnel_server.is_webapp_up() | ||
|
|
||
|
|
||
| def create_tunnel(cmd, resource_group_name, name, port=None, slot=None): | ||
| def create_tunnel_and_session(cmd, resource_group_name, name, port=None, slot=None): | ||
| webapp = show_webapp(cmd, resource_group_name, name, slot) | ||
| is_linux = webapp.reserved | ||
| if not is_linux: | ||
|
|
@@ -2281,46 +2283,70 @@ def create_tunnel(cmd, resource_group_name, name, port=None, slot=None): | |
| tunnel_server = TunnelServer('', port, host_name, profile_user_name, profile_user_password) | ||
| _ping_scm_site(cmd, resource_group_name, name) | ||
|
|
||
| _wait_for_webapp(tunnel_server) | ||
|
|
||
| t = threading.Thread(target=_start_tunnel, args=(tunnel_server,)) | ||
| t.daemon = True | ||
| t.start() | ||
|
|
||
| _wait_for_tunnel(tunnel_server, False) | ||
| logger.warning("SSH is available ( username: %s, password: %s )", ssh_user_name, ssh_user_password) | ||
|
|
||
| s = threading.Thread(target=_start_ssh, | ||
| args=('localhost', tunnel_server.get_port(), ssh_user_name)) | ||
| s = threading.Thread(target=_start_ssh_session, | ||
| args=('localhost', tunnel_server.get_port(), ssh_user_name, ssh_user_password)) | ||
| s.daemon = True | ||
| s.start() | ||
|
|
||
| while s.isAlive() and t.isAlive(): | ||
| time.sleep(5) | ||
|
|
||
|
|
||
| def _wait_for_tunnel(tunnel_server, print_warnings): | ||
| if not _check_for_ready_tunnel(tunnel_server): | ||
| if print_warnings: | ||
| logger.warning('Tunnel is not ready yet, please wait (may take up to 1 minute)') | ||
| while True: | ||
| time.sleep(1) | ||
| if print_warnings: | ||
| logger.warning('.') | ||
| if _check_for_ready_tunnel(tunnel_server): | ||
| break | ||
| def _wait_for_webapp(tunnel_server): | ||
| tries = 0 | ||
| while True: | ||
| if is_webapp_up(tunnel_server): | ||
| break | ||
| if tries == 0: | ||
| logger.warning('Connection is not ready yet, please wait') | ||
| if tries == 60: | ||
| raise CLIError("Timeout Error, Unable to establish a connection") | ||
| tries = tries + 1 | ||
| logger.warning('.') | ||
| time.sleep(1) | ||
|
|
||
|
|
||
| def _start_tunnel(tunnel_server): | ||
| _wait_for_tunnel(tunnel_server, True) | ||
| tunnel_server.start_server() | ||
|
|
||
|
|
||
| def _start_ssh(host_name, port, user_name): | ||
| subprocess.call("ssh -o StrictHostKeyChecking=no {}@{} -p {}".format(user_name, host_name, port), shell=True) | ||
| def _start_ssh_session(hostname, port, username, password): | ||
| tries = 0 | ||
| while True: | ||
| try: | ||
| c = Connection(host=hostname, | ||
| port=port, | ||
| user=username, | ||
| # connect_timeout=60*10, | ||
| connect_kwargs={"password": password}) | ||
| break | ||
| except Exception as ex: # pylint: disable=broad-except | ||
| logger.info(ex) | ||
| if tries == 0: | ||
| logger.warning('Connection is not ready yet, please wait') | ||
| if tries == 60: | ||
| raise CLIError("Timeout Error, Unable to establish a connection") | ||
| tries = tries + 1 | ||
| logger.warning('.') | ||
| time.sleep(1) | ||
| try: | ||
| c.run('cat /etc/motd', pty=True) | ||
| c.run('source /etc/profile; /bin/ash', pty=True) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is supposed to be /bin/bash right? |
||
| except Exception as ex: # pylint: disable=broad-except | ||
| logger.info(ex) | ||
| finally: | ||
| c.close() | ||
|
|
||
|
|
||
| def ssh_webapp(cmd, resource_group_name, name, slot=None): # pylint: disable=too-many-statements | ||
| import platform | ||
| if platform.system() == "Windows": | ||
| raise CLIError('webapp ssh is only supported on linux and mac') | ||
| else: | ||
| create_tunnel(cmd, resource_group_name, name, port=None, slot=slot) | ||
| create_tunnel_and_session(cmd, resource_group_name, name, port=None, slot=slot) | ||
22 changes: 22 additions & 0 deletions
22
src/command_modules/azure-cli-appservice/azure/cli/command_modules/appservice/fabric_license
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,22 @@ | ||
| Copyright (c) 2018 Jeff Forcier. | ||
| All rights reserved. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions are met: | ||
|
|
||
| * Redistributions of source code must retain the above copyright notice, | ||
| this list of conditions and the following disclaimer. | ||
| * Redistributions in binary form must reproduce the above copyright notice, | ||
| this list of conditions and the following disclaimer in the documentation | ||
| and/or other materials provided with the distribution. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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
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.
I would suggest you log an verbose entry to document the exception details:
logger.info(ex). My concern is catching generic exceptions above could swallow non connection related errors which ends up making the diagnosis pretty hard.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.
added