如何在Bash函数中使用局部变量

时间:2017-02-10 10:36:08

标签: bash

我正在尝试编写一个Bash脚本,它将停止Docker容器的存储库,重新构建它们,并对它们运行一些测试(使用Pytest)。为了使代码DRY,我尝试按如下方式定义函数wait_for_container

docker stop $(docker ps -a -q)

docker-compose build

docker-compose up -d

function wait_for_container {
    local CONTAINER=$1
    local PORT=$2

    ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER)

    until nc -z $CONTAINER $PORT
    do
        echo "Waiting for the $CONTAINER container..."
        sleep 0.5
    done
    echo "$CONTAINER listening at $ADDR:$PORT"
}

RETHINKDB_CONTAINER=ipercroncompose_rethinkdb_1
RETHINKDB_PORT=28015
wait_for_container $RETHINKDB_CONTAINER $RETHINKDB_PORT

RABBITMQ_CONTAINER=ipercroncompose_rabbitmq_1
RABBITMQ_PORT=5672
wait_for_container $RABBITMQ_CONTAINER $RABBITMQ_PORT

cd test
pytest

然而,我发现这不起作用:我反复得到

nc: getaddrinfo: Temporary failure in name resolution
Waiting for the ipercroncompose_rethinkdb_1 container...

另一方面,以下非DRY脚本 工作:

docker stop $(docker ps -a -q)

docker-compose build

docker-compose up -d

RETHINKDB_CONTAINER=ipercroncompose_rethinkdb_1
RETHINKDB_ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $RETHINKDB_CONTAINER)
RETHINKDB_PORT=28015

until nc -z $RETHINKDB_ADDR $RETHINKDB_PORT
do
    echo "Waiting for the RethinkDB container..."
    sleep 0.5
done
echo "RethinkDB listening at ${RETHINKDB_ADDR}:${RETHINKDB_PORT}."

RABBITMQ_CONTAINER=ipercroncompose_rabbitmq_1
RABBITMQ_ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $RABBITMQ_CONTAINER)
RABBITMQ_PORT=5672

until nc -z $RABBITMQ_ADDR $RABBITMQ_PORT
do
    echo "Waiting for the RabbitMQ container..."
    sleep 0.5
done
echo "RabbitMQ listening at ${RABBITMQ_ADDR}:${RABBITMQ_PORT}."

cd test
pytest

和回声

RethinkDB listening at 172.18.0.2:28015.
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
RabbitMQ listening at 172.19.0.2:5672.

接着是Pytests的结果。如何改进wait_for_container功能以达到同样的效果?

1 个答案:

答案 0 :(得分:0)

根据Grisha LevitFred的评论,以下是改编后的脚本:

docker stop $(docker ps -a -q)
docker-compose build
docker-compose up -d

function wait_for_container {
    local CONTAINER=$1
    local PORT=$2

    local ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER)
    echo $ADDR

    until nc -z $ADDR $PORT
    do
        echo "Waiting for the $CONTAINER container..."
        sleep 0.5
    done
    echo "$CONTAINER listening at $ADDR:$PORT"
}

wait_for_container ipercroncompose_rethinkdb_1 28015
wait_for_container ipercroncompose_rabbitmq_1 5672

cd test
pytest

问题确实是netcat需要IP地址作为其第一个输入,而不是Docker容器名称。 (我还将ADDR变量设为本地)。