Python subprocess.run退出代码不使用bash脚本

2024-10-02 08:21:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python调用Bash脚本。我使用run()函数来实现Python 3.5中引入的功能

我想用returncode来做一些事情,所以我用这个:

result = subprocess.run(["./app/first_deployment.sh", arg], stdout=subprocess.PIPE,)

if result.returncode == 0:
    # do something

我的Bash文件:

# First condition
if grep -q 'string' file.txt
then
    # Second condition
    if grep -q 'anotherstring' file.txt
    then
        echo "Success"
        exit 0
    else
        echo "Fail message 2"
        exit 1
    fi
else
    echo "Fail message 1"
    exit 1
fi

因此,它似乎起了作用,我在日志中看到了正确的消息。但是result.returncode始终是代码0,这意味着成功。为什么会这样?我怎样才能确保它有效

更新(完整脚本):

#!/bin/bash
basedir="/home/dpa/clients"
user=$1
archive_url=$2
repo_name=$3
port=$4
deployment_tag=$5

mkdir $basedir/$user
mkdir $basedir/$user/$repo_name
curl -o $basedir/$user/$repo_name/$deployment_tag.tar.gz $archive_url
mkdir $basedir/$user/$repo_name/$deployment_tag
tar -xvf $basedir/$user/$repo_name/$deployment_tag.tar.gz -C $basedir/$user/$repo_name/$deployment_tag --strip-components 1
rm -rf $basedir/$user/$repo_name/$deployment_tag.tar.gz

# Check if a production.yml file exists in the new directory
if [ -f "$basedir/$user/$repo_name/$deployment_tag/production.yml" ]
then
    # Check for the websecure endpoint
    if grep -q 'traefik.http.routers.$$$UNIQUE_DEPLOYMENT_TAG-secure.entrypoints=websecure' $basedir/$user/$repo_name/$deployment_tag/production.yml
    then
        # Check for the host rule
        if grep -q 'traefik.http.routers.$$$UNIQUE_DEPLOYMENT_TAG-secure.rule=Host' $basedir/$user/$repo_name/$deployment_tag/production.yml
        then
            # Check if the proxy network exists
            if grep -q 'network=proxy' $basedir/$user/$repo_name/$deployment_tag/production.yml
            then
                sed -i "s/\$\$\$PORT/${port}/g" $basedir/$user/$repo_name/$deployment_tag/production.yml
                sed -i "s/\$\$\$UNIQUE_DEPLOYMENT_TAG/${deployment_tag}/g" $basedir/$user/$repo_name/$deployment_tag/production.yml
                # docker-compose -f $basedir/$user/$repo_name/$deployment_tag/production.yml build
                # docker-compose -f $basedir/$user/$repo_name/$deployment_tag/production.yml up -d
                
                echo "Deployment succesfull! Your app is online :)"
                exit 0
            else
                echo "Proxy network rule not found in yml config."
                exit 1
            fi
        else
            echo "Traefik host rule not found in yml config."
            exit 1
        fi
    else
        echo "Traefik websecure endpoint not found in yml config."
        set -x
        exit 1
    fi
else
    echo "No production.yml could be found. Please follow the docs and include the correct YAML file."
    exit 1
fi

更新2(输出):

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1845  100  1845    0     0   7561      0 --:--:-- --:--:-- --:--:--  7530
+ mkdir /home/dpa/clients/foo/testapi1/testapi1-305983855
+ tar -xvf /home/dpa/clients/foo/testapi1/testapi1-305983855.tar.gz -C /home/dpa/clients/foo/testapi1/testapi1-305983855 --strip-components 1
+ rm -rf /home/dpa/clients/foo/testapi1/testapi1-305983855.tar.gz
+ '[' -f /home/dpa/clients/foo/testapi1/testapi1-305983855/production.yml ']'
+ grep -q 'traefik.http.routers.$$$UNIQUE_DEPLOYMENT_TAG-secure.entrypoints=websecure' /home/dpa/clients/foo/testapi1/testapi1-305983855/production.yml
+ echo 'Traefik websecure endpoint not found in yml config.'
+ exit 1
CompletedProcess(args=['./app/first_deployment.sh', 'foo', 'https://codeload.github.com/foo/testapi1/legacy.tar.gz/master?token=changedthis', 'testapi1', '7039', 'testapi1-305983855'], returncode=0, stdout=b'foo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/.github/\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/.github/dependabot.yml\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/.github/workflows/\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/.github/workflows/ci.yml\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/.gitignore\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/.vscode/\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/.vscode/settings.json\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/docker-compose.yml\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/local.yml\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/production.yml\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/testapi1/\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/testapi1/.dockerignore\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/testapi1/Dockerfile\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/testapi1/app/\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/testapi1/app/__init__.py\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/testapi1/app/main.py\nfoo-testapi1-b1c9fd5be165b850d2b94cde30affa622b5c3621/testapi1/requirements.txt\nTraefik websecure endpoint not found in yml config.\n')

更新3:

因此,在遵循简化脚本的建议后,我尝试使用下面的配置创建bash脚本文件。这给了我。。退出代码1!正如所料。太好了,这似乎奏效了

#!/bin/bash
basedir="/home/dpa/clients"
user=$1
archive_url=$2
repo_name=$3
port=$4
deployment_tag=$5

# # Check if a production.yml file exists in the new directory
if [ -f "$basedir/$user/$repo_name/$deployment_tag/production.yml" ]
then
    echo "Test complete"
    exit 0

else
    echo "No production.yml could be found. Please follow the docs and include the correct YAML file."
    exit 1
fi

现在,当我在前面添加了mkdir行时。它以退出代码0结束。这很奇怪,因为它应该给我一个退出代码1,因为目录不存在。因此,以下代码:

#!/bin/bash
basedir="/home/dpa/clients"
user=$1
archive_url=$2
repo_name=$3
port=$4
deployment_tag=$5

mkdir "$basedir/$user"

# # Check if a production.yml file exists in the new directory
if [ -f "$basedir/$user/$repo_name/$deployment_tag/production.yml" ]
then
    echo "Test complete"
    exit 0

else
    echo "No production.yml could be found. Please follow the docs and include the correct YAML file."
    exit 1
fi

我用cdls等命令尝试了这一点,结果都是相同的,退出代码为0。因此,出于某种原因,每当shell命令成功运行时,Python函数中就会出现退出代码0。因为文件检查只起作用。因此,这一定是一个与Python相关的问题


Tags: thenameechoifymltagexitdeployment

热门问题