Skip to content

Instantly share code, notes, and snippets.

@amashigeseiji
Last active December 22, 2015 08:29
Show Gist options
  • Save amashigeseiji/6445476 to your computer and use it in GitHub Desktop.
Save amashigeseiji/6445476 to your computer and use it in GitHub Desktop.
OpenPNEインストール用シェルスクリプト
#!/bin/sh
# base parameter{{{
conf_file=$HOME'/.opconf';
basedir=
command=
target_dir=
dir=
option=
branch=
#}}}
# functions{{{
# readConf{{{
readConf(){ cat ${conf_file} | sed '/^$/d' | grep -vE '^#'; }
#}}}
# setting{{{
# getCommand{{{
getCommand()
{
local cmds=('install' 'uninstall' 'ls' 'plugin' 'config' 'update')
if [ $# -gt 0 ];then
for cmd in ${cmds[@]}
do
if [ $cmd = $1 ];then
echo $cmd;return 0;
fi
done
echo 'argument invalid.';return 1
else echo 'argument required.';return 2
fi
}
#}}}
#isDir{{{
isDir()
{
local dir=${basedir}$1'/'
echo $dir
if [ ! -e $dir ];then
#ディレクトリが存在しない
return 1
else
if [ -d $dir ];then
#ディレクトリが存在する
return 0
else
#同名ファイルが存在する
return 2
fi
fi
}
#}}}
# setOption{{{
setOption()
{
while getopts 'b:t:v' opt
do
case $opt in
b) option='branch';branch=$OPTARG;break;;
t) option='tag';branch=$OPTARG;break;;
v) option='debug';break;;
?) echo 'unknown option.';exit;;
esac
done
}
#}}}
# configure{{{
configure()
{
if [ ! -e $conf_file ];then echo $conf_file' is not exist.';exit;fi
getValue(){ local pattern=$1;echo `readConf | grep -vE '^_' | grep -E "(${pattern})" | awk '{print $2}'`;}
local -a parameter=(`readConf | grep -vE '^_' | awk '{print $1}'`)
for i in ${parameter[@]}
do
eval $i=`getValue $i`;
done
basedir=${basedir}'/'
if [ ! -z ${prefix} ];then
prefix=${prefix}'.'
fi
plugins=(`readConf | grep -E '^(_plugin)' | awk '{print $2}'`)
}
#}}}
# confCheck{{{
confCheck()
{
local require_parameter=("basedir" "publicdir" "repository" "server_domain" "dbms" "dbuser" "dbhost");
for i in ${require_parameter[@]}
do
if [ ! `readConf | grep -E '^'$i | awk '{print $2}'` ];then echo 'please set '$i' to "'$conf_file'".';exit;fi
done
if [ ! -e $basedir -o ! -d $basedir ];then echo 'basedir: '$basedir' is not exist.';exit;fi
if [ ! -e $publicdir -o ! -d $publicdir ];then echo 'publicdir: '$publicdir' is not exist.';exit;fi
# optionが指定された場合
if [ ! $option = '' ];then
#branch 指定があるとき
if [ $option = 'branch' ];then
if [ -e $repository -a -d $repository ];then
cd $repository && branch=(`git branch | grep $branch`);
if [ -z $branch ];then echo 'branch is not exist.';exit;fi
fi
elif [ $option = 'tag' ];then
if [ -e $repository -a -d $repository ];then
cd $repository && branch=(`git tag | grep $branch`);
echo $branch
if [ -z $branch ];then echo 'tag is not exist.';exit;fi
fi
fi
fi
}
#}}}
#setup{{{
setup()
{
configure;
local ret=
ret=`getCommand $@`
if [ $? -ne 0 ];then
echo $ret;exit;
else
command=$ret
fi
case $command in
install)
if [ $# -gt 1 ];then
target_dir=$2
dir=`isDir $2`;
case $? in
0) echo $dir' is already exist.';exit;;
1) shift 2 && setOption $@
return 0;;
2) echo $dir' is already exist.';exit;;
?) echo 'exception';exit;;
esac
else
echo 'install: argument required(new directory name).';exit;
fi
break;;
uninstall)
if [ $# -gt 1 ];then
target_dir=$2
dir=`isDir $2`;
case $? in
0) return 0;;
1) echo $dir' is not exist.';exit;;
2) echo $dir' is not directory.';exit;;
?) echo 'exception';exit;;
esac
else
echo 'install: argument required(directory name).';exit;
fi
break;;
ls)
if [ $# -gt 1 ];then
target_dir=$2;
dir=`isDir $2`
case $? in
0) return 0;;
1) echo $target_dir' is not exist.';exit;;
2) echo $target_dir' is not directory.';exit;;
?) echo 'exception';exit;;
esac
fi
break;;
plugin)
echo hoge;break;;
config)
vim $conf_file && exit;;
?)
echo 'exception';exit;;
esac
}
#}}}
#}}}
#install{{{
# cloneOpenpne{{{
cloneOpenpne()
{
# repositoryをローカルのディレクトリに指定している場合
if [[ ! ${repository} =~ ^http ]];then
if [ -d ${repository} ];then
# repositoryを更新
echo 'Updating repository...';
cd ${repository} && git fetch --all > /dev/null && echo 'Done.'
else
# なければrepositoryを作成
echo 'Creating repository...';
mkdir -p ${repository};
git clone --mirror https://github.com/openpne/OpenPNE3.git ${repository} > /dev/null && echo 'Done.';
fi
fi
git clone ${repository} ${dir};
# branchが指定されたら切り替える
if [ $option ];then
if [ $option = 'branch' ];then
echo 'Checkout branch.'
cd $dir && git checkout -b $branch 'origin/'$branch
echo 'Done.'
elif [ $option = 'tag' ];then
echo 'Checkout branch.'
cd $dir && git checkout -b $branch $branch
echo 'Done.'
fi
fi
}
# }}}
# clonePlugin{{{
clonePlugin()
{
for i in ${plugins[@]}
do
local repository=''
local plugin_ver=`readConf | grep -E '^_plugin' | grep $i | awk -F' ' '{print $3}'`
local plugin_name=`readConf | grep -E '^_plugin' | grep $i | awk '{print $2}' | awk -F'[/:]' '{print $NF}' | sed -e 's/.git//g'`
echo 'install plugin: '$plugin_name
echo 'plugin_version: '$plugin_ver
if [[ $i =~ ^(http|git) ]];then repository=$i;
else
if [ -e $i -a -d $i ];then repository=$i;
else repository='https://github.com/'$i'.git';
fi
fi
cd ${dir}'plugins/' && git clone ${repository}
if [ ! -z $plugin_ver ];then
cd ${dir}'plugins/'${plugin_name} && git checkout -b $plugin_ver 'origin/'$plugin_ver
fi
done
}
# }}}
# createDatabasesYML{{{
createDatabasesYML()
{
local dbname=$1
local version=$2
local dsn="${dbms}:dbname=${dbname};host=${dbhost}"
if [ $version -gt 33 -o $version -eq 30 ];then
cd ${dir} && php symfony configure:database "${dsn}" ${dbuser} ${dbpassword}
else
if [ $version -eq 32 ];then
echo "all:" >> ${dir}'config/databases.yml'
echo " doctrine:" >> ${dir}'config/databases.yml'
echo " class: sfDoctrineDatabase" >> ${dir}'config/databases.yml'
echo " param:" >> ${dir}'config/databases.yml'
echo " dsn: $dsn" >> ${dir}'config/databases.yml'
echo " username: root" >> ${dir}'config/databases.yml'
fi
fi
echo " encoding: utf8" >> ${dir}'config/databases.yml'
echo " attributes: { 164: true }" >> ${dir}'config/databases.yml'
}
#}}}
# createConfigFile{{{
createConfigFile()
{
local baseUrl=$1;
echo 'Create config file.';
if [ -e ${dir}'config/OpenPNE.yml.sample' ];then
cp ${dir}'config/OpenPNE.yml.sample' ${dir}'config/OpenPNE.yml';
sed -i".bak" -e 's/example.com/'${baseUrl}'/g' ${dir}'config/OpenPNE.yml' && rm ${dir}'config/OpenPNE.yml.bak';
fi
if [ -e ${dir}'config/ProjectConfiguration.class.php.sample' ];then
cp ${dir}'config/ProjectConfiguration.class.php.sample' ${dir}'config/ProjectConfiguration.class.php';
fi
echo 'Done.'
}
#}}}
# install{{{
install()
{
local baseUrl=${prefix}${target_dir}${server_domain}
local openpne_db=`echo ${baseUrl} | sed 's/\./_/g' | sed 's/-/_/g'`
local version=`cat ${dir}data/version.php | grep OPENPNE_VERSION | grep -oE '[0-9.]+' | grep -Eo '^3\.[0-9]' | sed -e 's/\.//g'`
local php_option=
local doctrine_build='doctrine:build --all --and-load --no-confirmation'
echoError(){ if [ $? -eq 0 ];then echo 'Done.';else echo 'error';fi }
# install
echo 'Installing OpenPNE3...'
createConfigFile $baseUrl
clonePlugin
# refer http://d.hatena.ne.jp/kim_upsilon/20111125/1322232675
if [ $version -eq 32 ];then doctrine_build="doctrine:build-all-load";php_option="-d include_path=''";fi
if [ $version -lt 38 ];then
echo 'installing plugin'
cd $dir && php $php_option symfony opPlugin:sync | grep -v 'sfPearFrontendPlugin'
echoError
fi
echo 'permission'
cd $dir && php symfony openpne:permission > /dev/null 2>&1
echoError
echo 'cache clear'
cd $dir && php symfony cache:clear > /dev/null 2>&1
echoError
echo 'databases.yml'
createDatabasesYML $openpne_db $version
echoError
# for op32{{{
if [ $version -eq 32 ];then
local schema=${dir}'plugins/opOpenSocialPlugin/config/doctrine/schema.yml'
local height=`cat ${schema} | grep height`
local height_after=' '`echo $height | sed -e 's/[ ]}/, default: 0 }/g'`
sed -i".bak" -e "s/$height/$height_after/g" ${schema}
cat ${schema} | grep height
fi
# }}}
echo 'build'
cd $dir && php $php_option symfony $doctrine_build > /dev/null
echoError
echo 'publish-assets'
cd $dir && php symfony plugin:publish-assets > /dev/null 2>&1
echoError
#fi
sed -i".bak" -e 's/#RewriteBase/RewriteBase/g' ${dir}'web/.htaccess' && rm ${dir}'web/.htaccess.bak'
ln -s ${dir} ${publicdir}/${baseUrl}
local green="\e[21m"
local color_off="\em"
echo 'install completed.'
printf " ${green}path${color_off}: ${dir}\n"
printf " ${green}url${color_off}: ${baseUrl}\n"
printf " ${green}database${color_off}: ${openpne_db}\n"
printf " ${green}branch${color_off}: ${branch}\n"
}
#}}}
#}}}
# uninstall{{{
uninstall()
{
local target=$1;
local dir=${basedir}${target}
local baseUrl=`cat ${dir}/config/OpenPNE.yml | grep -E '^(base_url)' | awk -F' ' '{print $2}' | sed -e 's/\"//g' | sed -e 's/http:\/\///g'`
local dbname=
local sql=
if [ ! -d ${dir} ];then echo ${dir}' not found.';exit;fi
printf 'Do you delete '$dbname'?(y/n):'
read ans;
if [ $ans = 'y' ];then
echo 'Uninstalling '$target'...'
dbname=`cat ${dir}'/config/databases.yml' | grep dsn | grep -oE 'dbname=.*(;|\r\n|\n|\r)' | awk -F'=' '{print $2}' | sed -e 's/;//g'`
if [ -L ${publicdir}'/'${baseUrl} ];then
/bin/rm ${publicdir}'/'${baseUrl};
echo 'rm: deleted symbolic link '${baseUrl}'.'
fi
if [ -d ${dir} ];then
/bin/rm -rf ${dir};
echo 'rm: deleted '${dir}'.'
fi
if [ `mysql -u${dbuser} -e 'show databases;' | grep ${dbname}` ];then
sql="DROP DATABASE ${dbname};"
eval $mysql_command
echo 'mysql: '$dbname' deleted.'
fi
fi
echo 'Done.'
}
#}}}
# lsdir{{{
lsdir()
{
if [ $target_dir ];then
local baseUrl=`cat ${dir}config/OpenPNE.yml | grep ^base_url | awk '{print $2}' | sed -e 's/"//g' | sed -e 's/http:\/\///g'`
local dbname=`cat ${dir}'/config/databases.yml' | grep dsn | grep -oE 'dbname=.*(;|\r\n|\n|\r)' | awk -F'=' '{print $2}' | sed -e 's/;//g'`
local branch=`cd ${dir} && git branch | grep '\*' | sed -e 's/\* //g'`
echo 'name: '$target_dir
echo 'branch: '$branch
echo 'path: '$dir
echo 'base_url: '$baseUrl
echo 'dbname: '$dbname
else
echo 'installed:'
for i in `ls $basedir`
do
dir=`isDir $i`
echo ' '$i':'
echo ' branch: '`cd ${dir} && git branch | grep '\*' | sed -e 's/\* //g'`
done
fi
}
#}}}
# execute{{{
execute()
{
if [ $command = 'install' ];then
cloneOpenpne;
printf 'Do you install OpenPNE?(y/n):';
read ans;
if [ "$ans" = "y" ];then
install;
else
echo 'Done.'
fi
elif [ $command = 'uninstall' ];then
uninstall $target_dir;
elif [ $command = 'ls' ];then
lsdir
fi
}
#}}}
# usage{{{
usage()
{
echo 'commands:'
echo ' install'
echo ' uninstall'
echo ' ls'
echo ' plugin'
echo ' help'
echo 'options:'
echo ' -b[branch]: set branch name.'
echo ' -u[dir]: uninstall command.'
echo ' -h: show this help.'
echo ''
echo 'set ".opconf" on your home directory.'
confSample;
}
#}}}
# confSample{{{
confSample()
{
echo 'sample configration.'
echo ''
echo ' # OpenPNEをインストールする親ディレクトリ'
echo ' basedir "/home/$username/sns"'
echo ' # 公開ディレクトリ'
echo ' publicdir "/var/www/sns"'
echo ' # repository URLかローカルディレクトリを指定'
echo ' repository "/home/$yourname/repository/OpenPNE3"'
echo ' # server domain設定'
echo ' server_domain "$domain"'
echo ' # url のprefix'
echo ' #prefix ""'
echo ' # db設定'
echo ' dbms "mysql"'
echo ' dbuser "$dbuser"'
echo ' dbpassword ""'
echo ' dbhost "localhost"'
echo ' dbsock ""'
}
#}}}
#}}}
setup $@
confCheck
execute
# vim: set foldmethod=marker:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment