[USP友の会]第3回 一撃サーバー構築シェルスクリプト勉強会 お題のスクリプト

[USP友の会]第3回 一撃サーバー構築シェルスクリプト勉強会 お題のスクリプト

みなさんこんにちは。一撃王と呼ばれたいクヨクヨしたおっさん @nullpopopo ですΞ(´ ゚_。`) 今日の「第3回 一撃サーバー構築シェルスクリプト勉強会」で取り上げる題材シェルスクリプト「VHMAINTE」を公開します。

以下に公開するスクリプトをコピペして「VHMAINTE」というファイル名で作成し、PATHの通ったディレクトリに配置してください。なお、このスクリプトが動く環境は、さくらのクラウドで作成したCentOS6.5の環境、かつスタートアップスクリプトにこちらのエントリで取り上げたものが実行済みであることが条件となります。

#!/bin/bash
set -e
LANG=C

FQDN=$2
BLOGTITLE=$3
AdminUsername=$4
AdminPassword=$5
AdminEmail=$6

# example
# VHMAINTE create wp-cli-test000.blogcube.info "ぬるぽぽぽのブログ" nullpopopo BlogP@ssw0rd_is_here yasutaka.hamada@gmail.com
# VHMAINTE delete wp-cli-test000.blogcube.info

DROOT=/var/www/${FQDN}
ETH0=$(ip a show eth0 | grep inet | grep -v inet6 | awk '{print $2}' | sed -e "s//[0-9]*//")
PARAMFILE=${HOME}/.mysql/MySQLPARAM

if [ ! -x ${HOME}/bin/wp ];
  then
    curl -o ${HOME}/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
    chmod 700 ${HOME}/bin/wp
fi

CREATE(){
DBNAME=$(printf "wpdb%04dn" $(ls /var/www/ | egrep -v "(^html|gz$|^"${ETH0}")" | wc -l))
DBUSER=$(printf "user%04dn" $(ls /var/www/ | egrep -v "(^html|gz$|^"${ETH0}")" | wc -l))
if [ 0 = $(grep ${FQDN} ${PARAMFILE} | wc -l) ];
  then
    echo "DBPARAM ${DBNAME} ${DBUSER} $(cat /dev/urandom | tr -dc '[:alnum:]' | head -c 8)"
    ${FQDN} ${BLOGTITLE} ${AdminUsername} ${AdminPassword} ${AdminEmail}>> ${PARAMFILE}
    mysql -u root -p$(grep ^user ${PARAMFILE} | awk '{print $NF}')
    -e "create database ${DBNAME} character set utf8;"
    PW=$(grep ${FQDN} ${PARAMFILE} | awk '{print $4}')
    mysql -u root -p$(grep ^user ${PARAMFILE} | awk '{print $NF}')
    -e "GRANT ALL PRIVILEGES on ${DBNAME}.* to ${DBUSER}@localhost identified by "${PW}";"
    mysql -u root -p$(grep ^user ${PARAMFILE} | awk '{print $NF}')
    -e "FLUSH PRIVILEGES;"
fi
[ ! -d ${DROOT} ] && mkdir -p ${DROOT}
wp core download --locale=ja --path=${DROOT}
cd ${DROOT}
wp core config --dbname=$(grep ${FQDN} ${PARAMFILE} | awk '{print $2}')
    --dbuser=$(grep ${FQDN} ${PARAMFILE} | awk '{print $3}')
    --dbpass=$(grep ${FQDN} ${PARAMFILE} | awk '{print $4}')
wp core install --url=${FQDN} --title=${BLOGTITLE} --admin_user=${AdminUsername} --admin_password=${AdminPassword} --admin_email=${AdminEmail}
cd
cp -p /etc/nginx/conf.d/000_VHOST.conf /etc/nginx/conf.d/${FQDN}.conf
sed -i s/${ETH0}/${FQDN}/g /etc/nginx/conf.d/${FQDN}.conf
sed -i 's/^}$//g' /etc/nginx/conf.d/${FQDN}.conf
echo "    access_log  /var/log/nginx/${FQDN}_access.log  main;" >> /etc/nginx/conf.d/${FQDN}.conf
echo "    error_log  /var/log/nginx/${FQDN}_error.log warn;" >> /etc/nginx/conf.d/${FQDN}.conf
echo "}" >> /etc/nginx/conf.d/${FQDN}.conf
service nginx reload
}

DELETE(){
mv /etc/nginx/conf.d/${FQDN}.conf /etc/nginx/conf.d/${FQDN}.conf.DISABLED_$(date +%Y%m%d%H%M%S)
service nginx reload
[ -d ${DROOT} ] && tar czf ${DROOT}_$(date +%Y%m%d).tar.gz ${DROOT} && rm -fr ${DROOT}
DBNAME=$(grep ${FQDN} ${PARAMFILE} | awk '{print $2}')
DBUSER=$(grep ${FQDN} ${PARAMFILE} | awk '{print $3}')
if [ 1 = $(grep ${FQDN} ${PARAMFILE} | wc -l) ];
  then
    mysqldump -u root -p$(grep ^user ${PARAMFILE} | awk '{print $NF}')
    ${DBNAME} > /var/www/${FQDN}_${DBNAME}_$(date +%Y%m%d).sql
    gzip /var/www/${FQDN}_${DBNAME}_$(date +%Y%m%d).sql
    mysql -u root -p$(grep ^user ${PARAMFILE} | awk '{print $NF}')
    -e "drop database ${DBNAME};"
    mysql -u root -p$(grep ^user ${PARAMFILE} | awk '{print $NF}')
    -e "DELETE FROM mysql.user WHERE user = '${DBUSER}';"
    mysql -u root -p$(grep ^user ${PARAMFILE} | awk '{print $NF}')
    -e "FLUSH PRIVILEGES;"
    cp -p ${PARAMFILE} ${PARAMFILE}.$(date +%Y%m%d%H%M%S)
    grep -v ${DBUSER} ${PARAMFILE}.$(date +%Y%m%d%H%M%S) > ${PARAMFILE}
fi
}

case "$1" in
  [cC][rR][eE][aA][tT][eE])
    [ ! $# = 6 ] && echo "USAGE: $(basename $0) create FQDN BLOGTITLE AdminUsername AdminPassword AdminEmail" && exit 1
    CREATE
    ;;
  [dD][eE][lL][eE][tT][eE])
    [ ! $# = 2 ] && echo "USAGE: $(basename $0) delete FQDN" && exit 1
    DELETE
    ;;
  *)
    echo "USAGE: $(basename $0)  FQDN BLOGTITLE AdminUsername AdminPassword AdminEmail"
    exit 1
    ;;
esac

スライドはこちらになります。