Keyringrc.cfg 使用诗歌通过 python 脚本安装软件包时出现权限问题

问题描述 投票:0回答:1

我有一个由

supervisor
管理的应用程序,以用户
app_user
身份运行。当脚本启动时,它会检查是否有新的软件包并尝试使用下面的代码安装它们。但是它会抛出错误
[Errno 13] Permission denied: '/root/.config/python_keyring/keyringrc.cfg'

安装包功能:

import os
import sys
import subprocess
import logging

logger = logging.getLogger("general_log")

def install_app_packages():
    """
    Run poetry install to update application packages
    """
    python_path = sys.executable
    current_dir = os.path.dirname(os.path.realpath(__file__))
    project_directory = os.path.dirname(current_dir)
    poetry_install_command = "poetry install --with prod --no-root"
    activate_path = f"{python_path[:-7]}/activate"
    export_poetry = 'export PATH="/opt/apps/venv/poetry/bin:$PATH"'
    full_command = f"{export_poetry} && source {activate_path} && {poetry_install_command}"
    
    try:

        subprocess.run(
            full_command,
            shell=True,
            cwd=project_directory,
            executable="/bin/ash"
        )

        return True

    except Exception as e:
        logger.error(f"Error while running Poetry Install: {e.__str__()}")
        return False

详细错误:

[2024-07-29 12:53:01 +0300] [24580] [INFO] Using worker: sync
[2024-07-29 12:53:02 +0300] [24581] [INFO] Booting worker with pid: 24581
Installing dependencies from lock file

Package operations: 1 install, 0 updates, 0 removals

  - Installing semver (3.0.2)

  PermissionError

  [Errno 13] Permission denied: '/root/.config/python_keyring/keyringrc.cfg'

  at /usr/lib/python3.11/pathlib.py:1013 in stat
      1009│         """
      1010│         Return the result of the stat() system call on this path, like
      1011│         os.stat() does.
      1012│         """
    → 1013│         return os.stat(self, follow_symlinks=follow_symlinks)
      1014│ 
      1015│     def owner(self):
      1016│         """
      1017│         Return the login name of the file owner.

Cannot install semver.

[2024-07-29 12:53:03 +0300] [24580] [ERROR] Worker (pid:24581) exited with code 3
[2024-07-29 12:53:03 +0300] [24580] [ERROR] Shutting down: Master
[2024-07-29 12:53:03 +0300] [24580] [ERROR] Reason: Worker failed to boot.

有什么作用吗?

1。使用gunicorn直接调用时安装即可工作

当我直接使用

gunicorn
运行应用程序并以相同的
app_user
运行时,它会成功安装。

(poetry-venv)project_directory$ gunicorn --bind 0.0.0.0:8030 server.wsgi --error-logfile /tmp/gunicorn_error.log --access-logfile /tmp/gunicorn_access.log --preload

2。更改 Supervisor 以 root 身份运行

在应用程序的主管conf文件中,如果我指定

user=root
group=root
而不是
app_user
,安装也会成功。

所以想知道这里会发生什么。应用程序权限或一些诗歌设置?为什么脚本试图在 root 下查找 keyringrc.cfg 而“root”却没有在这里发挥作用?

我之前有一个关于权限的问题,但我还没有找到答案。会不会有关系?

python python-3.x supervisord python-poetry
1个回答
0
投票
沮丧,Google 和 SO 来救援。

所以它尝试访问

/root/*

 目录的原因是因为进程 
(subprocess in the case of supervisor)
 以 root 身份运行。所以我需要找到一种方法来运行这个过程作为
app_user
。为此,必须在子流程文件中定义一个 
environment
 变量,如 
here on SO 所定义。

environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8,HOME="/home/app_user",USER="app_user"
这将覆盖默认 

environment

 文件中找到的 
/etc/supervisor.conf
 变量,并强制子进程使用子进程文件中的值。

© www.soinside.com 2019 - 2024. All rights reserved.