azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

az containerapp exec fails to run shell scripts in non-interactive environment

Open codepic opened this issue 2 years ago • 9 comments

Related command

$cmd = "/app/data/clean.sh"
& az containerapp exec `
    --name $container_name `
    --resource-group $rg_name `
    --command $cmd `
    --debug

Describe the bug

When running az containerapp exec non-interactively, terminal control operation fails. The azure cli tries to resize the terminal (evidenced by os.get_terminal_size() call followed by OSError: [Errno 25] Inappropriate ioctl for device error). The underlying error happens in termios.tcgetattr(fd) function. See Additional Context below for details.

To Reproduce

  1. Deploy Container Apps Environment
  2. Create a shell script (hello.sh)
#!/bin/bash
echo 'hello'
  1. Deploy Container with the shell script
FROM mcr.microsoft.com/dotnet/runtime:6.0-jammy
COPY ./hello.sh /hello.sh
RUN chmod +x /hello.sh
ENTRYPOINT ["tail", "-f", "/dev/null"]
  1. Run the shell script with az containerapp exec locally to verify it works (it should work both on windows and ubuntu)
  2. Run the shell script with az containerapp exec in github actions (it fails, both with ubuntu-latest and windows-latest)

Expected behavior Should echo 'hello'

Environment summary

Github Actions

Current runner version:
Operating System
  Ubuntu
  22.04
  LTS
Runner Image
  Image: ubuntu-22.04
  Version: 20230409
  Included Software: https://github.com/actions/runner-images/blob/ubuntu22/20230409.1/images/linux/Ubuntu2204-Readme.md
  Image Release: https://github.com/actions/runner-images/releases/tag/ubuntu22%2F20230409.1
Runner Image Provisioner
  5.0.139.1

Additional context

Exception in thread Thread-1 (read_ssh):
Traceback (most recent call last):
  File "/opt/az/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/az/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/az/azcliextensions/containerapp/azext_containerapp/_ssh_utils.py", line 123, in read_ssh
    _resize_terminal(connection)
  File "/opt/az/azcliextensions/containerapp/azext_containerapp/_ssh_utils.py", line 154, in _resize_terminal
    size = os.get_terminal_size()
OSError: [Errno 25] Inappropriate ioctl for device
DEBUG: cli.azure.cli.core.azclierror: Traceback (most recent call last):
  File "/opt/az/lib/python3.10/site-packages/knack/cli.py", line 233, in invoke
    cmd_result = self.invocation.execute(args)
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 663, in execute
    raise ex
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 726, in _run_jobs_serially
    results.append(self._run_job(expanded_arg, cmd_copy))
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 697, in _run_job
    result = cmd_copy(params)
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 333, in __call__
    return self.handler(*args, **kwargs)
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/command_operation.py", line 121, in handler
    return op(**command_args)
  File "/opt/az/azcliextensions/containerapp/azext_containerapp/custom.py", line 2716, in containerapp_ssh
    writer = get_stdin_writer(conn)
  File "/opt/az/azcliextensions/containerapp/azext_containerapp/_ssh_utils.py", line 187, in get_stdin_writer
    tty.setcbreak(sys.stdin.fileno())  # needed to prevent printing arrow key characters
  File "/opt/az/lib/python3.10/tty.py", line 32, in setcbreak
    mode = tcgetattr(fd)
termios.error: (25, 'Inappropriate ioctl for device')

codepic avatar Apr 18 '23 08:04 codepic

Thank you for opening this issue, we will look into it.

yonzhan avatar Apr 18 '23 08:04 yonzhan

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @calvinsID.

Issue Details

Related command

$cmd = "/app/data/clean.sh"
& az containerapp exec `
    --name $container_name `
    --resource-group $rg_name `
    --command $cmd `
    --debug

Describe the bug

When running az containerapp exec non-interactively, terminal control operation fails. The azure cli tries to resize the terminal (evidenced by os.get_terminal_size() call followed by OSError: [Errno 25] Inappropriate ioctl for device error). The underlying error happens in termios.tcgetattr(fd) function. See Additional Context below for details.

To Reproduce

  1. Deploy Container Apps Environment
  2. Create a shell script (hello.sh)
#!/bin/bash
echo 'hello'
  1. Deploy Container with the shell script
FROM mcr.microsoft.com/dotnet/runtime:6.0-jammy
COPY ./hello.sh /hello.sh
RUN chmod +x /hello.sh
ENTRYPOINT ["tail", "-f", "/dev/null"]
  1. Run the shell script with az containerapp exec locally to verify it works (it should work both on windows and ubuntu)
  2. Run the shell script with az containerapp exec in github actions (it fails, both with ubuntu-latest and windows-latest)

Expected behavior Should echo 'hello'

Environment summary

Github Actions

Current runner version:
Operating System
  Ubuntu
  22.04
  LTS
Runner Image
  Image: ubuntu-22.04
  Version: 20230409
  Included Software: https://github.com/actions/runner-images/blob/ubuntu22/20230409.1/images/linux/Ubuntu2204-Readme.md
  Image Release: https://github.com/actions/runner-images/releases/tag/ubuntu22%2F20230409.1
Runner Image Provisioner
  5.0.139.1

Additional context

Exception in thread Thread-1 (read_ssh):
Traceback (most recent call last):
  File "/opt/az/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/az/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/opt/az/azcliextensions/containerapp/azext_containerapp/_ssh_utils.py", line 123, in read_ssh
    _resize_terminal(connection)
  File "/opt/az/azcliextensions/containerapp/azext_containerapp/_ssh_utils.py", line 154, in _resize_terminal
    size = os.get_terminal_size()
OSError: [Errno 25] Inappropriate ioctl for device
DEBUG: cli.azure.cli.core.azclierror: Traceback (most recent call last):
  File "/opt/az/lib/python3.10/site-packages/knack/cli.py", line 233, in invoke
    cmd_result = self.invocation.execute(args)
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 663, in execute
    raise ex
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 726, in _run_jobs_serially
    results.append(self._run_job(expanded_arg, cmd_copy))
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 697, in _run_job
    result = cmd_copy(params)
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 333, in __call__
    return self.handler(*args, **kwargs)
  File "/opt/az/lib/python3.10/site-packages/azure/cli/core/commands/command_operation.py", line 121, in handler
    return op(**command_args)
  File "/opt/az/azcliextensions/containerapp/azext_containerapp/custom.py", line 2716, in containerapp_ssh
    writer = get_stdin_writer(conn)
  File "/opt/az/azcliextensions/containerapp/azext_containerapp/_ssh_utils.py", line 187, in get_stdin_writer
    tty.setcbreak(sys.stdin.fileno())  # needed to prevent printing arrow key characters
  File "/opt/az/lib/python3.10/tty.py", line 32, in setcbreak
    mode = tcgetattr(fd)
termios.error: (25, 'Inappropriate ioctl for device')
Author: codepic
Assignees: SaurabhSharma-MSFT
Labels:

bug, Service Attention, ContainerApp

Milestone: -

ghost avatar Apr 20 '23 21:04 ghost

Just ran into this myself..

onionhammer avatar Jun 20 '23 19:06 onionhammer

I am experiencing the same issue using Azure Devops with a self-hosted Windows Server agent. image cliv2

Is there a workaround? I need to run some code in the container apps using an automation server :/

kienzen87 avatar Sep 14 '23 09:09 kienzen87

I haven't tested this on CI yet, but I faced what appears to be the same issue in an Azure Developer CLI hook. I wrapped my invocation in a PowerShell script:

$PostDeployJob = Start-Job -ScriptBlock { az containerapp exec -g ... -n ... --command /scripts/postdeploy.sh }
Wait-Job -Job $PostDeployJob -Timeout 10
Stop-Job -Job $PostDeployJob

Now, that's only useful if you can safely assume that a few seconds is enough to complete the script, but it's something.

rytmis avatar Dec 07 '23 14:12 rytmis

workaround from #17530 that worked for me: script -q -c 'az container exec --name <Name> --resource-group <GroupName> --command "<Actual_Command_To_Run_In_Container>"' /dev/null

dinoframe avatar Dec 18 '23 17:12 dinoframe

@rytmis @dinoframe I tried both of your approaches. They run from my desktop, but not from Azure Devops CI

kienzen87 avatar Dec 19 '23 09:12 kienzen87

@kienzen87 @rytmis @dinoframe Any working solution to execute this in azure cli task in cd pipeline?

srinivasmummareddy avatar Jan 31 '24 21:01 srinivasmummareddy

@kienzen87 @srinivasmummareddy how about if you try the following instead of using AzureCLI@2 task?:

steps:
  - powershell: # Run a script in Windows PowerShell on Windows, and pwsh on Linux and macOS.
  - pwsh: # Run a script in PowerShell Core on Windows, macOS, and Linux.

As documented in PowerShell@2 - PowerShell v2 task

codepic avatar Apr 26 '24 13:04 codepic