Skip to content
20 changes: 8 additions & 12 deletions src/azure-cli/azure/cli/command_modules/util/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def upgrade_version(cmd, update_all=None, yes=None): # pylint: disable=too-many
logger.warning("Exit the container to pull latest image with 'docker pull mcr.microsoft.com/azure-cli' "
"or run 'pip install --upgrade azure-cli' in this container")
elif installer == 'MSI':
exit_code = _upgrade_on_windows()
_upgrade_on_windows()
else:
logger.warning(UPGRADE_MSG)
if exit_code:
Expand Down Expand Up @@ -188,25 +188,21 @@ def _upgrade_on_windows():
This also gives the user a chance to manually install the MSI in case of msiexec.exe failure.
"""
import platform
import subprocess
import sys

if platform.architecture()[0] == '32bit':
msi_url = 'https://aka.ms/installazurecliwindows'
else:
msi_url = 'https://aka.ms/installazurecliwindowsx64'
logger.warning("Updating Azure CLI with MSI from %s", msi_url)
tmp_dir, msi_path = _download_from_url(msi_url)
_, msi_path = _download_from_url(msi_url)

logger.warning("Installing MSI")
import subprocess
exit_code = subprocess.call(['msiexec.exe', '/i', msi_path])

if exit_code:
logger.warning("Installation Failed. You may manually install %s", msi_path)
else:
from azure.cli.core.util import rmtree_with_retry
logger.warning("Succeeded. Deleting %s", tmp_dir)
rmtree_with_retry(tmp_dir)
Copy link
Member

Choose a reason for hiding this comment

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

I suggest we find a way to preserve this logic, otherwise the temporary MSI will not be deleted.

Copy link
Contributor Author

@bebound bebound Sep 18, 2023

Choose a reason for hiding this comment

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

The MSI is in the tempfile.mkdtemp() directory (C:\\Users\\xx\\AppData\\Local\\Temp\\tmp1s_cgvau), it appears that windows can delete it automatically.
Manage drive space with Storage Sense

Alternatively, we can save the MSI in .azure or Temp\azure-cli and add logic to clean MSI when run az command.

return exit_code
subprocess.Popen(['msiexec.exe', '/i', msi_path])
logger.warning("Installation started. Please complete the upgrade in the opened window.\nTo update extensions, "
"please run `az upgrade` again after completing the upgrade.")
sys.exit(0)


def _download_from_url(url):
Expand Down