Last active
December 24, 2015 06:39
-
-
Save GedowFather/6758561 to your computer and use it in GitHub Desktop.
BashScript for getting list of OpenStack deleted user's VM.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Keystoneから削除済みのユーザが所有者となっているVMのリストを表示します。 | |
# | |
# * jq コマンド必須です | |
# http://stedolan.github.io/jq/ | |
# # How to install | |
# wget -O /usr/bin/jq http://stedolan.github.io/jq/download/linux64/jq | |
# chmod +x /usr/bin/jq | |
# | |
# * このスクリプトはrootユーザでの実行のみ想定しています | |
# パスワード等の情報が ~/.bashrc に記述済みであるとします | |
# | |
PATH=/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
################### | |
# | |
# 設定 | |
# | |
TENANT_NAME=test | |
################### | |
# | |
# 開始の出力 | |
# | |
echo "=======================================" | |
echo " Retire User's VM Report" | |
echo "=======================================" | |
################### | |
# | |
# コマンド確認 | |
# | |
# | |
# jq is json processor | |
# | |
type jq > /dev/null 2>&1 | |
[ $? -ne 0 ] && echo "not found jq." && exit 1 | |
################### | |
# | |
# Keystone認証 | |
# | |
# | |
# 環境変数読み込み | |
# | |
. ~/.bashrc | |
export OS_TENANT_NAME=$TENANT_NAME | |
# | |
# 認証データ取得 | |
# | |
TOKENS=`eval " | |
curl -s -d '{ | |
\"auth\":{ | |
\"passwordCredentials\":{ | |
\"username\": \"$OS_USERNAME\", \"password\":\"$OS_PASSWORD\" | |
}, | |
\"tenantName\":\"$OS_TENANT_NAME\" | |
} | |
}' -H 'Content-type: application/json' ${OS_AUTH_URL}tokens | |
"` | |
# | |
# トークンID, テナントID, API URL 取得 | |
# | |
TOKEN_ID=`echo "$TOKENS" | jq '.access.token.id' -r` | |
API_URL=` echo "$TOKENS" \ | |
| jq '.access.serviceCatalog[] | select(.type == "compute").endpoints[].adminURL' -r` | |
[ "$TOKEN_ID" = "null" ] && echo "can not get token id." && exit 1 | |
################### | |
# | |
# データ収集 | |
# | |
# | |
# VMとユーザIDのリスト | |
# | |
SERVER_DETAIL=`curl -s $API_URL/servers/detail -H "X-Auth-Token: $TOKEN_ID"` | |
echo "$SERVER_DETAIL" | head -1 | grep "^{" > /dev/null 2>&1 | |
[ $? -ne 0 ] && echo "can not get server list." && exit 1 | |
# | |
# Keystoneユーザのリスト | |
# | |
KEYSTONE_USER_LIST=`keystone user-list` | |
[ $? -ne 0 ] && echo "can not get keystone users." && exit 1 | |
################### | |
# | |
# データ編集 | |
# | |
# | |
# VM所有ユーザIDのユニークリスト | |
# | |
VM_UNIQUE_USERS=`echo "$SERVER_DETAIL" \ | |
| jq '[.servers[].user_id] | unique | .[]' -r` | |
[ -z "$VM_UNIQUE_USERS" ] && echo "not exist vm." && exit 0 | |
# | |
# KeystoneユーザIDのリスト | |
# | |
KEYSTONE_USERS=`echo "$KEYSTONE_USER_LIST" \ | |
| grep -v "^| *id .* email *|" \ | |
| grep "^| " \ | |
| awk '{print $2'}` | |
################### | |
# | |
# ユーザ不在VMリスト | |
# | |
# | |
# 削除済みユーザのリスト | |
# | |
RETIRE_USERS=() | |
for user_id in ${VM_UNIQUE_USERS[@]} | |
do | |
echo "$KEYSTONE_USERS" | grep "^$user_id$" > /dev/null 2>&1 | |
if [ $? -ne 0 ]; | |
then | |
RETIRE_USERS=("${RETIRE_USERS[@]}" "$user_id") | |
fi | |
done | |
[ -z "$RETIRE_USERS" ] && echo "not found retire users." && exit 0 | |
# | |
# 所有者不在VMのリスト | |
# | |
RETIRE_USERS_VMs=() | |
for user_id in ${RETIRE_USERS[@]} | |
do | |
VMs_JSON=`echo "$SERVER_DETAIL" \ | |
| jq ".servers[] | select(.user_id == \"$user_id\") | {user_id, name, id}" --compact-output` | |
[ -z "$VMs_JSON" ] && continue | |
for json in ${VMs_JSON[@]} | |
do | |
VM_ID=` echo "$json" | jq '.id' -r` | |
VM_NAME=` echo "$json" | jq '.name' -r` | |
VM_USER_ID=`echo "$json" | jq '.user_id' -r` | |
RETIRE_USERS_VMs=("${RETIRE_USERS_VMs[@]}" "$VM_ID:$VM_NAME:$VM_USER_ID") | |
done | |
done | |
[ -z "$RETIRE_USERS_VMs" ] && echo "not found retire user's VM." && exit 0 | |
################### | |
# | |
# データの表示 | |
# | |
# | |
# ユーザID/VM名/VM ID のリスト | |
# | |
echo "USER_ID : VM_NAME ( VM_ID )" | |
echo "-----------------------------" | |
for data in ${RETIRE_USERS_VMs[@]} | |
do | |
VM_ID=` echo "$data" | cut -d: -f1` | |
VM_NAME=` echo "$data" | cut -d: -f2` | |
VM_USER_ID=`echo "$data" | cut -d: -f3` | |
SPACE_NUM=$((24 - ${#VM_USER_ID})) | |
SPACES=`eval "printf ' %.0s' {1..$SPACE_NUM}"` | |
echo "$VM_USER_ID$SPACES: $VM_NAME ( $VM_ID )" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment