Skip to content
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

[Core] az --version: Show command instruction and detailed instruction link when updates available #12981

Merged
merged 5 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/azure-cli-core/azure/cli/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable=line-too-long

from __future__ import print_function

__version__ = "2.3.1"
Expand Down Expand Up @@ -29,6 +31,16 @@
'content_version', 'kwargs', 'client', 'no_wait']
EVENT_FAILED_EXTENSION_LOAD = 'MainLoader.OnFailedExtensionLoad'

_PACKAGE_UPGRADE_INSTRUCTIONS = {"YUM": ("sudo yum update -y azure-cli", "https://aka.ms/doc/UpdateAzureCliYum"),
"ZYPPER": ("sudo zypper refresh && sudo zypper update -y azure-cli", "https://aka.ms/doc/UpdateAzureCliZypper"),
"DEB": ("sudo apt-get update && sudo apt-get install --only-upgrade -y azure-cli", "https://aka.ms/doc/UpdateAzureCliApt"),
"HOMEBREW": ("brew update && brew upgrade azure-cli", "https://aka.ms/doc/UpdateAzureCliHomebrew"),
"PIP": ("curl -L https://aka.ms/InstallAzureCli | bash", "https://aka.ms/doc/UpdateAzureCliLinux"),
"MSI": ("https://aka.ms/installazurecliwindows", "https://aka.ms/doc/UpdateAzureCliMsi"),
"DOCKER": ("docker pull mcr.microsoft.com/azure-cli", "https://aka.ms/doc/UpdateAzureCliDocker")}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just confirm, those short urls are all maintained actively

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of them are active now.


_GENERAL_UPGRADE_INSTRUCTION = 'Instructions can be found at https://aka.ms/doc/InstallAzureCli'


class AzCli(CLI):

Expand Down Expand Up @@ -96,9 +108,36 @@ def show_version(self):
if updates_available == -1:
logger.warning('Unable to check if your CLI is up-to-date. Check your internet connection.')
elif updates_available:
logger.warning('You have %i updates available. Consider updating your CLI installation. '
'Instructions can be found at https://docs.microsoft.com/en-us/cli/azure/install-azure-cli',
updates_available)
warning_msg = 'You have %i updates available. Consider updating your CLI installation'
from azure.cli.core._environment import _ENV_AZ_INSTALLER
installer = os.getenv(_ENV_AZ_INSTALLER)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need to print different messages based on platform? what about show all upgrade instructions?

Copy link
Member Author

@fengzhou-msft fengzhou-msft Apr 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current link shown is doing something similiar, it puts all the links to each package in one page. But we got complains about it's not being useful, some users may not know their operating system or packaging well, they have difficulties to find the right instruction.

instruction_msg = ''
if installer in _PACKAGE_UPGRADE_INSTRUCTIONS:
if installer == 'RPM':
from azure.cli.core.util import get_linux_distro
distname, _ = get_linux_distro()
if not distname:
instruction_msg = '. {}'.format(_GENERAL_UPGRADE_INSTRUCTION)
else:
distname = distname.lower().strip()
if any(x in distname for x in ['centos', 'rhel', 'red hat', 'fedora']):
installer = 'YUM'
elif any(x in distname for x in ['opensuse', 'suse', 'sles']):
installer = 'ZYPPER'
else:
instruction_msg = '. {}'.format(_GENERAL_UPGRADE_INSTRUCTION)
elif installer == 'PIP':
import platform
system = platform.system()
alternative_command = " or '{}' if you used our script for installation. Detailed instructions can be found at {}".format(_PACKAGE_UPGRADE_INSTRUCTIONS[installer][0], _PACKAGE_UPGRADE_INSTRUCTIONS[installer][1]) if system != 'Windows' else ''
instruction_msg = " with 'pip install --upgrade azure-cli'{}".format(alternative_command)
if instruction_msg:
warning_msg += instruction_msg
else:
warning_msg += " with '{}'. Detailed instructions can be found at {}".format(_PACKAGE_UPGRADE_INSTRUCTIONS[installer][0], _PACKAGE_UPGRADE_INSTRUCTIONS[installer][1])
else:
warning_msg += '. {}'.format(_GENERAL_UPGRADE_INSTRUCTION)
logger.warning(warning_msg, updates_available)
else:
print('Your CLI is up-to-date.')

Expand Down
3 changes: 3 additions & 0 deletions src/azure-cli-core/azure/cli/core/_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# --------------------------------------------------------------------------------------------


_ENV_AZ_INSTALLER = 'AZ_INSTALLER'


def get_config_dir():
import os
return os.getenv('AZURE_CONFIG_DIR', None) or os.path.expanduser(os.path.join('~', '.azure'))
3 changes: 2 additions & 1 deletion src/azure-cli-core/azure/cli/core/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def _get_azure_cli_properties(self):
set_custom_properties(result, 'Feedback', self.feedback)
set_custom_properties(result, 'ExtensionManagementDetail', self.extension_management_detail)
set_custom_properties(result, 'Mode', self.mode)
set_custom_properties(result, 'Installer', os.getenv('AZ_INSTALLER'))
from azure.cli.core._environment import _ENV_AZ_INSTALLER
set_custom_properties(result, 'Installer', os.getenv(_ENV_AZ_INSTALLER))

return result

Expand Down
3 changes: 2 additions & 1 deletion src/azure-cli-core/azure/cli/core/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ def test_configured_default_setter(self):

@mock.patch('azure.cli.core.__version__', '7.8.9')
def test_get_az_user_agent(self):
with mock.patch.dict('os.environ', {'AZ_INSTALLER': 'PIP'}):
from azure.cli.core._environment import _ENV_AZ_INSTALLER
with mock.patch.dict('os.environ', {_ENV_AZ_INSTALLER: 'PIP'}):
actual = get_az_user_agent()
self.assertEqual(actual, 'AZURECLI/7.8.9 (PIP)')

Expand Down
24 changes: 20 additions & 4 deletions src/azure-cli-core/azure/cli/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ def _get_version_string(name, version_dict):
_print()
_print('Legal docs and information: aka.ms/AzureCliLegal')
_print()
if sys.version.startswith('2.7'):
_print("* DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. \nA future version of Azure CLI will drop support for Python 2.7.")
_print()
version_string = output.getvalue()

# if unable to query PyPI, use sentinel value to flag that
Expand Down Expand Up @@ -805,8 +802,8 @@ def get_az_user_agent():

agents = ["AZURECLI/{}".format(core_version)]

_ENV_AZ_INSTALLER = 'AZ_INSTALLER'
import os
from azure.cli.core._environment import _ENV_AZ_INSTALLER
if _ENV_AZ_INSTALLER in os.environ:
agents.append('({})'.format(os.environ[_ENV_AZ_INSTALLER]))

Expand All @@ -828,3 +825,22 @@ def user_confirmation(message, yes=False):
except NoTTYException:
raise CLIError(
'Unable to prompt for confirmation as no tty available. Use --yes.')


def get_linux_distro():
if platform.system() != 'Linux':
return None, None

try:
with open('/etc/os-release') as lines:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy and paste a sample for other reviewers:

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.2 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.2 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

tokens = [line.strip() for line in lines]
except Exception: # pylint: disable=broad-except
return None, None

release_info = {}
for token in tokens:
if '=' in token:
k, v = token.split('=', 1)
release_info[k.lower()] = v.strip('"')

return release_info.get('name', None), release_info.get('version_id', None)
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ def _build_issue_info_tup(command_log_file=None):
format_dict["python_info"] = "Python {}".format(platform.python_version())
format_dict["platform"] = "{}".format(platform.platform())
format_dict["auto_gen_comment"] = _AUTO_GEN_COMMENT
format_dict["installer"] = "Installer: {}".format(os.getenv('AZ_INSTALLER') or '')
from azure.cli.core._environment import _ENV_AZ_INSTALLER
format_dict["installer"] = "Installer: {}".format(os.getenv(_ENV_AZ_INSTALLER) or '')

pretty_url_name = _get_extension_repo_url(ext_name) if is_ext else _CLI_ISSUES_URL

Expand Down