Created
January 12, 2017 03:57
-
-
Save hoto17296/f75b79d8a1b15c470c1ce836c352fd22 to your computer and use it in GitHub Desktop.
全てのサーバに対して一括操作をするためのコマンド群
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 -eu | |
PRODUCTNAME-help() { | |
echo -e "usage: PRODUCTNAME <command> [<args>]\n" | |
echo -e "commands:" | |
echo -e " hosts サーバ一覧を表示" | |
echo -e " git-log-remote [<hosts>] デプロイされていない差分コミットがあれば表示" | |
echo -e " check-update [<hosts>] yum パッケージの更新を表示" | |
echo -e " update [<hosts>] yum パッケージを更新" | |
echo | |
} | |
PRODUCTNAME-hosts() { | |
cat ${HOME}/.ssh/config | grep 'Host PRODUCTNAME-' | grep -v '*' | awk '{print $2}' | |
} | |
PRODUCTNAME-git-log-remote() { | |
[ $# -eq 0 ] && hosts=$(PRODUCTNAME-hosts) || hosts=$@ | |
for host in $(PRODUCTNAME-hosts) | |
do | |
echo "# $host" | |
set +e | |
ssh -A "$host" 2>/dev/null <<'EOT' | |
cd /var/www/html && \ | |
git fetch && \ | |
git log --oneline --left-right HEAD..origin/master | perl -pe 's/^/\t/g' | |
EOT | |
set -e | |
echo | |
done | |
} | |
function PRODUCTNAME-check-update() { | |
[ $# -eq 0 ] && hosts=$(PRODUCTNAME-hosts) || hosts=$@ | |
for host in $hosts | |
do | |
echo "# $host" | |
ssh -A $host yum check-update 2>/dev/null | grep -v '読み込んだプラグイン' | awk '{print "\t" $0}' | |
if [ $? -ne 0 ]; then | |
echo "Error" | |
return 1 | |
fi | |
echo | |
done | |
} | |
function PRODUCTNAME-update() { | |
[ $# -eq 0 ] && hosts=$(PRODUCTNAME-hosts) || hosts=$@ | |
for host in $hosts | |
do | |
echo "# $host" | |
ssh -A $host sudo yum -y update | |
if [ $? -ne 0 ]; then | |
echo "Error" | |
return 1 | |
fi | |
echo | |
done | |
} | |
set +e | |
subcmd=${1:-help} | |
shift | |
set -e | |
if type "PRODUCTNAME-${subcmd}" >/dev/null 2>&1 ; then | |
PRODUCTNAME-${subcmd} "$@" | |
else | |
echo "'${subcmd}' is not a PRODUCTNAME command." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment