はじめに
環境毎にリリース作業を行った後大変なのが、動作確認ですよね!
ansibleとかで作ってもいいですが、納品先でもちゃんと動作するか確認したいなんて言われるのを考えると、webサービスとかでしたらサクッとターミナルで実行できるcurlコマンドとその結果を出したいですよね?
毎回コピペしてそのログをまたコピペとか大変だから、まあshスクリプトで動作確認スクリプトを作っとくかぁってなるんですが、エビデンス用に実行したコマンド
とその出力結果
を出力してターミナルにコピペで実行できるようにしたいけど、思ったより難しいって悩んでたりしませんか?
(私だけだったらすません。。)
シェルの性質上 確認用の部分だけ実行したコマンドを出力するのって思ったより面倒なんですよね。
ってことで、それを楽にするシェル関数を紹介します!
便利なシェル関数を用意する
まずこんな関数の詰まったシェルスクリプトを作ります。
functions.sh
#------------------------------------------------------------
# output text with the color
# echo_c {text}
#------------------------------------------------------------
function echo_c() {
echo $'\e[32m'$@$'\e[0m'
}
#------------------------------------------------------------
# output executed command with the color
# exec_cmd_f {command}
#------------------------------------------------------------
function exec_cmd() {
echo_c "$@"
result=`sh <(echo "$@")`
if [ ! $? = 0 ]; then
echo "Error Occurred, and exit."
exit 1
fi
}
#------------------------------------------------------------
# output executed command in file[ here document ] with the color
# exec_cmd_f {filename}
#------------------------------------------------------------
function exec_cmd_f() {
exec_cmd `cat $1`
}
#------------------------------------------------------------
# output exec_cmd result
#------------------------------------------------------------
function get_cmd_result() {
echo "${result}"
}
#------------------------------------------------------------
# exec_cmd & get_cmd_result
#------------------------------------------------------------
function exec_cmd_re() {
exec_cmd "$@"
get_cmd_result
}
#------------------------------------------------------------
# exec_cmd_f & get_cmd_result
#------------------------------------------------------------
function exec_cmd_f_re() {
exec_cmd_f "$1"
get_cmd_result
}
動作確認シェルスクリプトを作成
#!/usr/bin/env bash
source ./function.sh
# token 取得
exec_cmd_f <<CMD
curl -s http://localhost/api/token
-H "Content-Type: application/json"
-d '{"token": {"client_id": "test", "scopes": ["read", "write"]}}'
-X POST -u hoge@fuga.com:abcdefg
CMD
get_cmd_result
ACCESS_TOKEN=`get_cmd_result | jq -r .token.token`
# log確認
exec_cmd_re "tail -2 ../logs/access.log | grep token | grep hoge@fuga"
# profile取得
exec_cmd_f <<CMD
curl -s http://localhost/api/profile/hoge
-H "Authorization: token ${ACCESS_TOKEN}"
CMD
get_cmd_result | jq .
動作確認シェルスクリプトを実行
今回はapiの動作確認をサンプルに作ってますが、通常のフロントやバックオフィスの動作確認もcurlとgrep等を駆使すればいけます。(まあそこまでシェルで頑張る必要もないですが)
$ ./check.sh
結果はこちらです。
実際は実行したコマンドに色がついてます。
コマンドをターミナルにコピペして実行できてとっても便利!
出力結果
curl -s http://localhost/api/token -H "Content-Type: application/json" -d '{"token": {"client_id"
: "test", "scopes": ["read", "write"]}}' -X POST -u hoge@fuga.com:abcdefg
{
"token": {
"id": 123456789012,
"client_id": "test",
"token": "nbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHb1L07Qe7K0GarZRmB",
"created_at": "2018-01-01T01:01:01+09:00",
"expires_at": null,
"scopes": [
"read",
"write"
]
}
}
tail -2 ../logs/access.log | grep token | grep hoge@fuga
172.20.0.1 - hoge@fuga.com [18/Jan/2018:21:33:23 +0000] "POST /api/token HTTP/1.1" 200 630 "-" "c
url/7.55.0"
curl -s http://localhost/api/profile/hoge -H "Authorization: token nbyTyWzO75vRK5h6xBArLIARNPvkSj
tQBMHb1L07Qe7K0GarZRmB"
{
"token": {
"id": 123456789012,
"name": "hoge",
"email": "hoge@fuga.com",
"created_at": "2018-01-01T01:01:01+09:00",
"gender": "m",
"birthday": "1975-01-05"
}
}
おわりに
これで煩わしい動作確認も、いつの間にかシェルスクリプト作りが楽しくてたまらなくなってるはず!?