生産可能なシェル起動スクリプト
アプリケーションを構築し、コンテナーテクノロジを通じて利用できるようにする場合はENTRYPOINT うんCMD dockerfileの末尾にある.使用しているフレームワークといくつかの要件に応じて、プロジェクトを実行するためにbashスクリプトを使用する方が良いでしょう.それが利用できるとき、一般に、あなたは実行されるコマンドの束を見るでしょうthe following script I created for the project Django Multiple Schemas :
任意のコマンドが0以外のステータスを返すと直ちに終了する
次のスクリプトを考えてみましょう
この議論は全くよい.これは、生産のバグから私たちを保護することができます.多くの潜在的な問題を解決することができますが、環境変数が必要かもしれません.説明するには
置換された変数をエラーとして扱う
次のスクリプトがあります.
両方のオプションのスクリプトを含める
パイプラインが失敗した場合は、すべてのコマンドが失敗した場合に
説明するには、以下のスクリプトを想像してください.我々が使用していることに注意してください
労働時間外での作業をシールドする
すべてをラップします.これはbashスクリプトが先頭にあるエントリです.
チェックアウトできますall the code I put in this article on GitHub . そこにはdocker-compose file すべてのサービスで、私がここで示した引数の各々をテストする🤙.
#!/usr/bin/env bash
python manage.py makemigrations
python manage.py migrate
python manage.py seed --create-super-user
python manage.py runserver 0.0.0.0:8000
コマンドを仮定しましょうpython manage.py migrate
実行に失敗しました.何が起こる🤔? 答えは直感的ですが、エラーがあってもスクリプトは上手く実行されます🤯. コマンドを実行するpython manage.py seed --create-super-user
続いてpython manage.py runserver 0.0.0.0:8000
. それを固定する方法?少しの引数について少し知っていましょうThe Set Builtin .任意のコマンドが0以外のステータスを返すと直ちに終了する
次のスクリプトを考えてみましょう
#!/usr/bin/env bash
python the_set_builtin/sample_1.py
python the_set_builtin/sample_2_force_error.py
python the_set_builtin/sample_3.py
実行すると、次の出力があります.▶ docker-compose up why-use-argument-e-with-bug
Creating the-set-builtin_why-use-argument-e-with-bug_1 ... done
Attaching to the-set-builtin_why-use-argument-e-with-bug_1
why-use-argument-e-with-bug_1 | I am the /app/the_set_builtin/sample_1.py!
why-use-argument-e-with-bug_1 | I am the /app/the_set_builtin/sample_2_force_error.py!
why-use-argument-e-with-bug_1 | Let me force an error 👀
why-use-argument-e-with-bug_1 | I am the /app/the_set_builtin/sample_3.py!
the-set-builtin_why-use-argument-e-with-bug_1 exited with code 0
終了コードは0です😠. さて、オプションを含めるとset -e
そして再び実行すると、出力が変更され、予期しない動作が修正されます.▶ docker-compose up why-use-argument-e-with-fix
Creating the-set-builtin_why-use-argument-e-with-fix_1 ... done
Attaching to the-set-builtin_why-use-argument-e-with-fix_1
why-use-argument-e-with-fix_1 | I am the /app/the_set_builtin/sample_1.py!
why-use-argument-e-with-fix_1 | I am the /app/the_set_builtin/sample_2_force_error.py!
why-use-argument-e-with-fix_1 | Let me force an error 👀
the-set-builtin_why-use-argument-e-with-fix_1 exited with code 1
今終了コードは1です🥳. 引数に関するドキュメントによると-e
:Exit immediately if a pipeline, which may consist of a single simple command, a list, or a compound command returns a non-zero status.
この議論は全くよい.これは、生産のバグから私たちを保護することができます.多くの潜在的な問題を解決することができますが、環境変数が必要かもしれません.説明するには
PORT
:#!/usr/bin/env bash
python manage.py migrate
python manage.py collectstatic --no-input
gunicorn -cpython:gunicorn_config -b 0.0.0.0:${PORT} aladdin.wsgi
変数が指定されていない場合、アプリケーションは重大なパラメータなしで実行される可能性があります.この変数が見つからない場合、スクリプトを停止できる別の引数がありますか?置換された変数をエラーとして扱う
次のスクリプトがあります.
#!/usr/bin/env bash
python the_set_builtin/sample_1.py
python the_set_builtin/sample_2_force_error.py
python the_set_builtin/sample_3.py
python the_set_builtin/sample_4_env_variable.py "$ALADDIN_WISH"
python the_set_builtin/sample_5.py
実行すると次の出力が得られます.▶ docker-compose up why-use-argument-u-with-bug
Starting the-set-builtin_why-use-argument-u-with-bug_1 ... done
Attaching to the-set-builtin_why-use-argument-u-with-bug_1
why-use-argument-u-with-bug_1 | I am the /app/the_set_builtin/sample_1.py!
why-use-argument-u-with-bug_1 | I am the /app/the_set_builtin/sample_2_force_error.py!
why-use-argument-u-with-bug_1 | Let me force an error 👀
why-use-argument-u-with-bug_1 | I am the /app/the_set_builtin/sample_3.py!
why-use-argument-u-with-bug_1 | I am the /app/the_set_builtin/sample_4_env_variable.py!
why-use-argument-u-with-bug_1 | Look! I received the following arguments: ['the_set_builtin/sample_4_env_variable.py', '']
why-use-argument-u-with-bug_1 | Length: 2
why-use-argument-u-with-bug_1 | I am the /app/the_set_builtin/sample_5.py!
the-set-builtin_why-use-argument-u-with-bug_1 exited with code 0
オプションでset -u
, 以下はあなたが得るものです.▶ docker-compose up why-use-argument-u-with-fix
Starting the-set-builtin_why-use-argument-u-with-fix_1 ... done
Attaching to the-set-builtin_why-use-argument-u-with-fix_1
why-use-argument-u-with-fix_1 | I am the /app/the_set_builtin/sample_1.py!
why-use-argument-u-with-fix_1 | I am the /app/the_set_builtin/sample_2_force_error.py!
why-use-argument-u-with-fix_1 | Let me force an error 👀
why-use-argument-u-with-fix_1 | I am the /app/the_set_builtin/sample_3.py!
why-use-argument-u-with-fix_1 | ./scripts/with-argument-u.sh: line 10: ALADDIN_WISH: unbound variable
the-set-builtin_why-use-argument-u-with-fix_1 exited with code 1
それは揺れている🤘! 説明を見ましょう-u
:Treat unset variables and parameters other than the special parameters ‘@’ or ‘*’ as an error when performing parameter expansion. An error message will be written to the standard error, and a non-interactive shell will exit.
両方のオプションのスクリプトを含める
set -eu
) そして、我々はほとんど行って良いです!保護のもう一つの層を加えることができるので、私は「ほとんど」を書きました.最後に見ましょう.パイプラインが失敗した場合は、すべてのコマンドが失敗した場合に
説明するには、以下のスクリプトを想像してください.我々が使用していることに注意してください
set -e
より安全にする引数#!/usr/bin/env bash
# https://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin
# • -e: Exit immediately if a command exits with a non-zero status.
set -e
this-is-a-fake-command-my-friend | echo "You...are late."
echo "A thousand apologies, O patient one."
2番目のコマンドがパイプラインで失敗した場合は、次のように出力します.▶ docker-compose up why-use-option-pipefail-with-bug
Starting the-set-builtin_why-use-option-pipefail-with-bug_1 ... done
Attaching to the-set-builtin_why-use-option-pipefail-with-bug_1
why-use-option-pipefail-with-bug_1 | ./scripts/without-option-pipefail.sh: line 7: this-is-a-fake-command-my-friend: command not found
why-use-option-pipefail-with-bug_1 | You...are late.
why-use-option-pipefail-with-bug_1 | A thousand apologies, O patient one.
the-set-builtin_why-use-option-pipefail-with-bug_1 exited with code 0
オプションを含むset -e -o pipefail
, 出力は次のように変更されます.▶ docker-compose up why-use-option-pipefail-with-fix
Starting the-set-builtin_why-use-option-pipefail-with-fix_1 ... done
Attaching to the-set-builtin_why-use-option-pipefail-with-fix_1
why-use-option-pipefail-with-fix_1 | ./scripts/with-option-pipefail.sh: line 9: this-is-a-fake-command-my-friend: command not found
why-use-option-pipefail-with-fix_1 | You...are late.
the-set-builtin_why-use-option-pipefail-with-fix_1 exited with code 127
解説If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.
労働時間外での作業をシールドする
すべてをラップします.これはbashスクリプトが先頭にあるエントリです.
set -eu -o pipefail
. 常にすべてのスクリプトに挿入します.この単純な保護は、多くの間、あなたを助けるでしょうTSHOOT 🙏. つの例は、適用されなければならない移行なしであなたのアプリケーションを実行しているときです.チェックアウトできますall the code I put in this article on GitHub . そこにはdocker-compose file すべてのサービスで、私がここで示した引数の各々をテストする🤙.
Reference
この問題について(生産可能なシェル起動スクリプト), 我々は、より多くの情報をここで見つけました https://dev.to/willianantunes/production-ready-shell-startup-scripts-the-set-builtin-48dmテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol