您的位置:首页 > 运维架构 > Shell

shell脚本实现离线自动化安装superset2.0

2017-12-04 16:34 1011 查看
#!/bin/bash
NUM=$#
MYSQL_HOST=$1
MYSQL_USER="root"
MYSQL_PASSWORD="root"
MYSQL_USER_S="superset"
MYSQL_PASSWORD_S="superset"
SUPERSET_INSTALL="/tmp"
PIP_INSTALL="/tmp/pip-devel"
SUPERSET_HOME="/opt/venv/lib/python2.7/site-packages/superset"
SUCCESS=0
HIVE_DIR='/usr/hdp/2.5.3.0-37/hive/conf'
HIVE_SERHOST=`cat $HIVE_DIR/hive-site.xml | grep 9083| awk -F ':' '{print $2}' | awk -F '//' '{print $2}'`

function Exit() {
if [ "$?" -ne 0 ]; then
exit 1
fi
}

function Param(){
if [ "$NUM" -ne 1 ];
then
echo "Param number incorrect,Please input mysql_host address as param when running superset_install" && exit 1
fi
}

function ExtractPip() {
echo "Creating Directory ${SUPERSET_INSTALL}..."
if [ ! -d ${SUPERSET_INSTALL} ]
then
mkdir -p ${SUPERSET_INSTALL}
if [ "$?" -ne $SUCCESS ]
then
echo "ERROR: Create Directory ${SUPERSET_INSTALL} failed, INSTALLATION TERMINATED!" && exit 1
fi
fi
echo "Extracting Pip Server files..."
sudo tar -zxf $SUPERSET_INSTALL/pip-devel.tar.gz
sudo tar -zxf ${PIP_INSTALL}/pip-9.0.1.tar.gz -C /opt
if [ "$?" -ne $SUCCESS ]
then
echo "ERROR: Extract files failed, INSTALLATION TERMINATED!" && exit 1
rm -f ${PIP_INSTALL}/pip-9.0.1.tar.gz
rm -f ${SUPERSET_INSTALL}/pip-devel.tar.gz
else
cd /opt/pip-9.0.1/
python /opt/pip-9.0.1/setup.py install
pip install $PIP_INSTALL/wheel-0.30.0.tar.gz
fi
}

function Yum(){
MSQL=`rpm-qa | grep mysql-community-client`
if [ "$?" -ne 0 ]
then
sudo yum -y install mysql-community-client
else
echo "$MSQL already exists"
fi
GCC=`rpm -qa | grep gcc`
if [ "$?" -ne 0 ]
then
sudo yum -y install gcc
else
echo "$GCC already exists"
fi

GCC_C=`rpm -qa | grep gcc-c++`
if [ "$?" -ne 0 ]
then
sudo yum -y install gcc-c++
else
echo "$GCC_C Already exists"
fi

LIBFFI=`rpm -qa | grep libffi-devel`
if [ "$?" -ne 0 ]
then
sudo yum -y install libffi-devel
else
echo "$LIBFFI Already exists"
fi

PD=`rpm -qa | grep python-devel`
if [ "$?" -ne 0 ]
then
sudo yum -y install python-devel
else
echo "$PD Already exists"
fi

OPSSL=`rpm -qa | grep openssl-devel`
if [ "$?" -ne 0 ]
then
sudo yum -y install openssl-devel
else
echo "$OPSSL Already exists"
fi

OPLDAP=`rpm -qa | grep openldap-devel`
if [ "$?" -ne 0 ]
then
sudo yum -y install openldap-devel
else
echo "$OPLDAP Already exists"
fi

EX=`rpm -qa | grep expect`
if [ "$?" -ne 0 ]
then
sudo yum -y install expect
else
echo "$EX Already exists"
fi

SASL=`rpm -qa | grep cyrus-sasl-plain`
if [ "$?" -ne 0 ]
then
sudo yum -y install cyrus-sasl-plain
else
echo "$SASL Already exists"
fi

MCD=`rpm -qa | grep mysql-community-devel`
if [ "$?" -ne 0 ]
then
sudo yum -y install mysql-community-devel
else
echo "$MCD Already exists"
fi
}

function User(){
U_H=`grep "^superset" /etc/passwd`
if [ "$?" -ne 0 ]
then
sudo useradd superset
else
echo "user 'superset' Already exists"
fi
G_H=`groups superset| grep "hadoop"`
if [ "$?" -ne 0 ]
then
sudo usermod -a -G hadoop superset
else
echo "superset is hadoop groups Already exists"
fi
}

function VenvAct(){
if [ ! -d /opt/venv ]
then
pip install $PIP_INSTALL/virtualenv-*
virtualenv /opt/venv
. /opt/venv/bin/activate
else
. /opt/venv/bin/activate
fi
}

function SupersetInstall(){
pip install --no-index --find-links=$PIP_INSTALL -r $PIP_INSTALL/requirements.txt
}

function Mysql(){
MD=`mysql -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -e "select * from information_schema.SCHEMATA where SCHEMA_NAME='superset';"`
if [ ${#MD} -eq 0 ]
then
sudo mysql -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -e "create database superset;"
else
echo "superset database Already exists"
fi
MU=`mysql -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -e "select * from mysql.user where user='superset';"`
if [ ${#MU} -eq 0 ]
then
sudo mysql -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -e "grant all privileges on superset.* to superset@\"%\" identified by \"superset\" with grant option; flush privileges;"
sudo mysql -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -e "grant all privileges on superset.* to superset@\"localhost\" identified by \"superset\" with grant option; flush privileges;"
sudo mysql -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -e "grant all privileges on superset.* to superset@\"$MYSQL_HOST\" identified by \"superset\" with grant option; flush privileges;"
else
echo "superset user Already exists"
fi
}

function InitFile(){
sed -i 's/^SQLALCHEMY_DATABASE_URI/#SQLALCHEMY_DATABASE_URI/g' $SUPERSET_HOME/config.py
sed -i "60a SQLALCHEMY_DATABASE_URI = 'mysql://superset:superset@$MYSQL_HOST/superset'" $SUPERSET_HOME/config.py
sed -i 's/SUPERSET_WEBSERVER_PORT = 8088/SUPERSET_WEBSERVER_PORT = 8089/g' $SUPERSET_HOME/config.py
sed -i "s/#\s*'zh':\|#'zh'/'zh':/g" $SUPERSET_HOME/config.py
sed -i "s/'en':/#'en':/g" $SUPERSET_HOME/config.py
sed -i "s/'it':/#'it':/g" $SUPERSET_HOME/config.py
sed -i "s/BABEL_DEFAULT_LOCALE = 'en'/BABEL_DEFAULT_LOCALE = 'zh'/g" $SUPERSET_HOME/config.py
}
function SupersetInit(){
/usr/bin/expect <<-EOF
set timeout -1
spawn fabmanager create-admin --app superset
expect "*name*"
send "\n"
expect "*first*"
send "\n"
expect "*last*"
send "\n"
expect "*Email*"
send "\n"
expect "*Password*"
send "admin\n"
expect "*confirmation*"
send "admin\n"
expect eof
EOF
superset db upgrade
#superset load_examples
superset init
}
function Mysqldbs(){
MDB=`mysql -h $MYSQL_HOST -u$MYSQL_USER_S -p$MYSQL_PASSWORD_S -e "select * from superset.dbs where database_name='hive';"`
if [ ${#MDB} -eq 0 ]
then
sudo mysql -h $MYSQL_HOST -usuperset -psuperset -e "insert into superset.dbs(created_on,changed_on,database_name,sqlalchemy_uri,extra) values(now(),now(),\"hive\",\"impala://$HIVE_SERHOST:10000/default\",'{\"metadata_params\":{},\"engine_params\":{\"connect_args\":{\"auth_mechanism\":\"PLAIN\"}}}');"
else
echo "superset_hive database Already exists"
fi
}

function Translate(){
yes | cp $PIP_INSTALL/messages.po /opt/venv/lib64/python2.7/site-packages/flask_appbuilder/translations/zh/LC_MESSAGES
cd /opt/venv/lib64/python2.7/site-packages/flask_appbuilder/
pybabel compile -d translations
}

function Stat(){
RL=`cat /etc/rc.local| grep superset`
if [ "$?" -ne 0  ]
then
echo ". /opt/venv/bin/activate;superset runserver" >> /etc/rc.local
fi

NT=`netstat -nlpt | gre
a007
p 8089`
if [ "$?" -ne 0  ]
then
cd /opt
nohup superset runserver >/var/log/superset.log 2>&1 &
fi
}

Param
ExtractPip
Yum
User
Mysql
VenvAct
SupersetInstall
InitFile
SupersetInit
Mysqldbs
Translate
Stat
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: