Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save mumoshu/b5361d508fe1694fba6c to your computer and use it in GitHub Desktop.

Select an option

Save mumoshu/b5361d508fe1694fba6c to your computer and use it in GitHub Desktop.
test-kitchenのdestroyやsetupが時々失敗するので、リトライをかける

Mac OS X + Test Kitchen + kitchen-docker + boot2docker (on VirtualBox)という環境で kitchen testが頻繁に落ちた。

エラーは2種類。

  • コンテナのdestroy中にfailed to remove one or more containers(kitchen destroyし直すと通る)
  • コンテナのconverge中にconnection closed by remote host(数回リトライすると通る)

VirtualBoxのNICの送受信バッファ増やしてもコンテナ上のSSHの設定改善してもMTU見直しても結局改善しなくて疲れたので結局以下のワークラウンドでやっつけた、という話。

kitchen testをステップ毎にわけると、

$ kitchen destroy PATTERN && kitchen setup PATTERN && kitchen verify PATTERN

とほぼ等価なようだ。 失敗するのはdestroyやsetupなので、以下のように時々失敗するコマンドだけをリトライする。

$ retry kitchen destroy PATTERN && retry kitchen setup PATTERN && kitchen verify PATTERN
# ~/.bash_profileなどに追加しておくと便利
retry () {
echo retry: Running $@ with retries
local label=retry:
local temp_file=$(mktemp -t temp_file.tmp)
local exit_status_file=$(mktemp -t exit_status.tmp)
local trial_num=
local exit_status=
local max_trial_num=5
local re='^[0-9]+$'
if [[ "$MAX_RETRIES" =~ $re ]]; then
echo $label Overring max_trial_num with MAX_RETRIES: $MAX_RETRIES
max_trial_num=$MAX_RETRIES
fi
echo $label temp_file: $temp_file
while test "$trial_num" == "" || (test $trial_num -lt $max_trial_num && test $exit_status -ne 0 && (grep "connection closed by remote host" $temp_file || grep "failed to remove one or more containers" $temp_file)); do
trial_num=$((trial_num + 1))
echo $label Running "$@", Trial $trial_num.
(($@ 2>&1); (echo $? > $exit_status_file)) | tee $temp_file
exit_status=$(cat $exit_status_file)
echo $label Exit status: $exit_status
done
echo $label Removing the temporary file: $temp_file
rm -f $temp_file
echo $label Removing the temporary file: $exit_status_file
rm -f $exit_status_file
if test $exit_status -ne 0 && test $trial_num == $max_trial_num; then
echo $label Giving up retrying $@
return 1
fi
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment