#!/bin/bash
set -euo pipefail
# -----------------------------------------------------
# 設定ファイル・パス定義
# -----------------------------------------------------
CONF_FILE="conf.json"
INCLUDE_FILE="include_tags.conf"
EXCLUDE_FILE="exclude_tags.conf"
MAP_FILE="test_map.csv"
LOG_DIR="./logs"
MODE=${1:-"include"}
# -----------------------------------------------------
# jq チェック
# -----------------------------------------------------
command -v jq >/dev/null || { echo "jq が必要です"; exit 1; }
# -----------------------------------------------------
# conf.json 読み込み
# -----------------------------------------------------
AWX_URL=$(jq -r '.url' "$CONF_FILE")
TEMPLATE_ID=$(jq -r '.template_id' "$CONF_FILE")
TOKEN=$(jq -r '.token' "$CONF_FILE")
AUTH_HEADER="Authorization: Bearer ${TOKEN}"
CONTENT_TYPE="Content-Type: application/json"
mkdir -p "$LOG_DIR"
# -----------------------------------------------------
# ログ出力
# -----------------------------------------------------
log() {
echo "[$(date +'%F %T')] $*"
}
# -----------------------------------------------------
# モード+タグ から試験名を取得
# -----------------------------------------------------
get_test_name() {
local tag="$1"
local mode="$2"
awk -F',' -v t="$tag" -v m="$mode" '$3 == t && $1 == m { print $2 }' "$MAP_FILE"
}
# -----------------------------------------------------
# ジョブログ取得
# -----------------------------------------------------
fetch_job_log() {
local job_id="$1"
curl -s -H "$AUTH_HEADER" "${AWX_URL}/api/v2/jobs/${job_id}/stdout/?format=txt"
}
# -----------------------------------------------------
# テンプレート更新(job_tags / skip_tags / description)
# -----------------------------------------------------
update_template() {
local job_tags="$1"
local skip_tags="$2"
local description="$3"
curl -s -X PATCH "${AWX_URL}/api/v2/job_templates/${TEMPLATE_ID}/" \
-H "$AUTH_HEADER" -H "$CONTENT_TYPE" \
-d "{\"job_tags\": \"$job_tags\", \"skip_tags\": \"$skip_tags\", \"description\": \"$description\"}" > /dev/null
}
# -----------------------------------------------------
# ジョブ実行
# -----------------------------------------------------
launch_job() {
local response
response=$(curl -s -X POST "${AWX_URL}/api/v2/job_templates/${TEMPLATE_ID}/launch/" \
-H "$AUTH_HEADER" -H "$CONTENT_TYPE")
echo "$response" | jq -r '.job'
}
# -----------------------------------------------------
# includeモード処理
# -----------------------------------------------------
run_include_mode() {
while read -r tag || [[ -n "$tag" ]]; do
[[ -z "$tag" ]] && continue
test_name=$(get_test_name "$tag" "include")
[[ -z "$test_name" ]] && test_name="unknown"
log "▶ include: $tag / $test_name"
update_template "$tag" "" "$test_name"
job_id=$(launch_job)
log "→ Job ID: $job_id"
sleep 3
log_file="${LOG_DIR}/${tag}_${test_name}.log"
fetch_job_log "$job_id" > "$log_file"
log "→ ログ保存: $log_file"
sleep 5
done < "$INCLUDE_FILE"
}
# -----------------------------------------------------
# excludeモード処理
# -----------------------------------------------------
run_exclude_mode() {
while read -r tags || [[ -n "$tags" ]]; do
[[ -z "$tags" ]] && continue
fname=$(echo "$tags" | tr ',' '_')
tag_arr=(${tags//,/ })
test_name=$(get_test_name "${tag_arr[0]}" "exclude")
[[ -z "$test_name" ]] && test_name="除外: $tags"
log "▶ exclude: $tags / $test_name"
update_template "" "$tags" "$test_name"
job_id=$(launch_job)
log "→ Job ID: $job_id"
sleep 3
log_file="${LOG_DIR}/skip_${fname}.log"
fetch_job_log "$job_id" > "$log_file"
log "→ ログ保存: $log_file"
sleep 5
done < "$EXCLUDE_FILE"
}
# -----------------------------------------------------
# メイン処理
# -----------------------------------------------------
main() {
log "===== AWXジョブ実行開始(モード: $MODE)====="
case "$MODE" in
include) run_include_mode ;;
exclude) run_exclude_mode ;;
*)
echo "エラー: モードは 'include' または 'exclude'"
exit 1
;;
esac
update_template "" "" ""
log "→ テンプレートのタグと説明をリセットしました"
log "===== 実行完了 ====="
}
# -----------------------------------------------------
# エントリーポイント
# -----------------------------------------------------
main
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme