| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- #!/usr/bin/env bash
- #
- # JimV-C
- #
- # Copyright (C) 2017 JimV <james.iter.cn@gmail.com>
- #
- # Author: James Iter <james.iter.cn@gmail.com>
- #
- # This script will help you to automation install JimV-C.
- #
- # Example:
- # Pass arguments to the installer.
- # curl https://raw.githubusercontent.com/jamesiter/JimV-C/master/INSTALL.sh | bash -s -- --version dev
- export PYPI='https://mirrors.aliyun.com/pypi/simple/'
- export JIMVC_REPOSITORY_URL='https://github.com/jamesiter/JimV-C.git'
- export JIMVC_REPOSITORY_URL_CN='https://gitee.com/jimit/JimV-C.git'
- export JIMVC_REPOSITORY_RAW_URL='https://raw.githubusercontent.com/jamesiter/JimV-C'
- export COUNTRY=`curl http://iit.im/ip/country`
- export GENERATE_PASSWORD_SCRIPT_TMP_PATH='/tmp/gen_pswd.sh'
- export SMTP_HOST=''
- export SMTP_USER=''
- export SMTP_PASSWORD=''
- if [ ${COUNTRY} = 'CN' ]; then
- export JIMVC_REPOSITORY_URL=${JIMVC_REPOSITORY_URL_CN}
- fi
- ARGS=`getopt -o h --long rdb_root_password:,rdb_jimv_password:,redis_password:,jwt_secret:,secret_key:,version:,help -n 'INSTALL.sh' -- "$@"`
- eval set -- "${ARGS}"
- while true
- do
- case "$1" in
- --rdb_root_password)
- export RDB_ROOT_PSWD=$2
- shift 2
- ;;
- --rdb_jimv_password)
- export RDB_JIMV_PSWD=$2
- shift 2
- ;;
- --redis_password)
- export REDIS_PSWD=$2
- shift 2
- ;;
- --jwt_secret)
- export JWT_SECRET=$2
- shift 2
- ;;
- --secret_key)
- export SECRET_KEY=$2
- shift 2
- ;;
- --version)
- export JIMV_VERSION=$2
- shift 2
- ;;
- -h|--help)
- echo 'INSTALL.sh [-h|--help|--rdb_root_password|--rdb_jimv_password|--redis_password|--jwt_secret|--secret_key|--version]'
- exit 0
- ;;
- --)
- shift
- break
- ;;
- *)
- echo "Internal error!"
- exit 1
- ;;
- esac
- done
- function check_precondition() {
- source /etc/os-release
- case ${ID} in
- centos|fedora|rhel)
- if [ ${VERSION_ID} -lt 7 ]; then
- echo "System version must greater than or equal to 7, We found ${VERSION_ID}."
- exit 1
- fi
- ;;
- *)
- echo "${ID} is unknown, Please to be installed manually."
- exit 1
- ;;
- esac
- }
- function ensure_hosts_layout() {
- echo "______________________________________________________________________________"
- /bin/cat /etc/hosts
- echo
- read -p "您已经布局好 /etc/hosts 了吗 [Y/N]? " answer
- if [ "x_"${answer} != "x_Y" ] && [ "x_"${answer} != "x_y" ]; then
- echo "OK, good bye!";
- exit 0
- fi
- }
- function sync_ssh_key_pair() {
- sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config
- rm -f ~/.ssh/id_rsa ~/.ssh/id_rsa.pub; echo -e "\n\n\n" | ssh-keygen -N ""
- echo >> ~/.ssh/authorized_keys
- cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- chmod 0600 ~/.ssh/authorized_keys
- DHOSTS=`egrep -v '^$|localhost|jimvc' /etc/hosts | awk '{ print $1; }'`
- for dhost in ${DHOSTS}
- do
- scp -r ~/.ssh ${dhost}:~/
- # 关闭 SSH 服务器端 Key 校验
- ssh ${dhost} "sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config"
- done
- }
- function sync_hosts_file() {
- DHOSTS=`egrep -v '^$|localhost|jimvc' /etc/hosts | awk '{ print $1; }'`
- for dhost in ${DHOSTS}
- do
- scp /etc/hosts ${dhost}:/etc/hosts
- done
- }
- function prepare() {
- if [ ! ${JIMV_VERSION} ] || [ ${#JIMV_VERSION} -eq 0 ]; then
- export JIMV_VERSION='master'
- fi
- if [ ! -e ${GENERATE_PASSWORD_SCRIPT_TMP_PATH} ]; then
- curl ${JIMVC_REPOSITORY_RAW_URL}'/'${JIMV_VERSION}'/misc/gen_pswd.sh' -o ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
- chmod +x ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
- fi
- if [ ! ${RDB_ROOT_PSWD} ] || [ ${#RDB_ROOT_PSWD} -eq 0 ]; then
- export RDB_ROOT_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
- fi
- if [ ! ${RDB_JIMV_PSWD} ] || [ ${#RDB_JIMV_PSWD} -eq 0 ]; then
- export RDB_JIMV_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
- fi
- if [ ! ${REDIS_PSWD} ] || [ ${#REDIS_PSWD} -eq 0 ]; then
- export REDIS_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
- fi
- if [ ! ${JWT_SECRET} ] || [ ${#JWT_SECRET} -eq 0 ]; then
- export JWT_SECRET=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
- fi
- if [ ! ${SECRET_KEY} ] || [ ${#SECRET_KEY} -eq 0 ]; then
- export SECRET_KEY=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
- fi
- rm -f ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
- yum install epel-release -y
- yum install python2-pip git psmisc -y
- pip install --upgrade pip -i ${PYPI}
- pip install virtualenv -i ${PYPI}
- }
- function set_ntp() {
- yum install ntp -y
- systemctl start ntpd
- systemctl enable ntpd
- timedatectl set-timezone Asia/Shanghai
- timedatectl set-ntp true
- timedatectl status
- }
- function clear_up_environment() {
- systemctl stop firewalld
- systemctl disable firewalld
- systemctl stop NetworkManager
- systemctl disable NetworkManager
- sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/sysconfig/selinux
- sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
- setenforce 0
- }
- function install_MariaDB() {
- # 安装 MariaDB
- yum install mariadb mariadb-server -y
- # 配置 MariaDB
- mkdir -p /etc/systemd/system/mariadb.service.d
- echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
- echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
- systemctl --system daemon-reload
- sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
- sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
- sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
- sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
- sed -i '/\[mysqld\]/a\max_connections = 10000' /etc/my.cnf
- # 启动并使其随机启动
- systemctl enable mariadb.service
- systemctl start mariadb.service
- # 初始化 MariaDB
- mysql_secure_installation << EOF
- Y
- ${RDB_ROOT_PSWD}
- ${RDB_ROOT_PSWD}
- Y
- Y
- Y
- Y
- EOF
- # 测试是否部署成功
- mysql -u root -p${RDB_ROOT_PSWD} -e 'show databases'
- }
- function install_Redis() {
- # 安装 Redis
- yum install redis -y
- # 配置 Redis
- echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
- sysctl -p
- sed -i 's@^daemonize no@daemonize yes@g' /etc/redis.conf
- sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
- sed -i 's@^appendonly no@appendonly yes@g' /etc/redis.conf
- echo "requirepass ${REDIS_PSWD}" >> /etc/redis.conf
- # 启动并使其随机启动
- systemctl enable redis.service
- systemctl start redis.service
- }
- function install_Nginx() {
- export NGINX_JIMV_URL=${JIMVC_REPOSITORY_RAW_URL}'/'${JIMV_VERSION}'/misc/nginx_jimv.conf'
- # 安装 Nginx
- yum install nginx -y
- # 配置 Nginx
- mkdir -p /etc/jimv/keys
- chown -R www.www /var/lib/nginx
- sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
- sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
- curl ${NGINX_JIMV_URL} >> /etc/nginx/nginx.conf
- # 启动并使其随机启动
- systemctl enable nginx.service
- systemctl start nginx.service
- }
- function create_web_user() {
- useradd -m www
- }
- function create_web_sites_directory() {
- su - www -c "mkdir ~/sites"
- }
- function clone_and_checkout_JimVC() {
- su - www -c "git clone ${JIMVC_REPOSITORY_URL} ~/sites/JimV-C"
- su - www -c "cd ~/sites/JimV-C && git checkout ${JIMV_VERSION}"
- }
- function install_dependencies_library() {
- # 创建 python 虚拟环境
- su - www -c "virtualenv --system-site-packages ~/venv"
- # 导入 python 虚拟环境
- su - www -c "source ~/venv/bin/activate"
- # 使切入 www 用户时自动导入 python 虚拟环境
- su - www -c "echo '. ~/venv/bin/activate' >> .bashrc"
- # 安装 JimV-C 所需扩展库
- su - www -c "pip install -r ~/sites/JimV-C/requirements.txt -i ${PYPI}"
- }
- function fit_www_user_permission() {
- mkdir -p /var/log/jimv
- chown www.www /var/log/jimv
- mkdir -p /run/jimv
- chown www.www /run/jimv
- }
- function initialization_db() {
- # 建立 JimV 数据库专属用户
- mysql -u root -p${RDB_ROOT_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
- # 初始化数据库
- su - www -c "mysql -u jimv -p${RDB_JIMV_PSWD} < ~/sites/JimV-C/misc/init.sql"
- # 确认是否初始化成功
- mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
- }
- function generate_config_file() {
- cp -v /home/www/sites/JimV-C/jimvc.conf /etc/jimvc.conf
- sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
- sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
- sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
- sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
- sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
- sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
- sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
- }
- function generate_pid_directory_ad_reboot() {
- chmod +x /etc/rc.d/rc.local
- echo '' >> /etc/rc.d/rc.local
- echo 'mkdir /run/jimv' >> /etc/rc.d/rc.local
- echo 'chown www.www /run/jimv' >> /etc/rc.d/rc.local
- }
- function display_summary_information() {
- echo
- echo "=== 信息汇总"
- echo "MariaDB root 密码: [${RDB_ROOT_PSWD}]"
- echo "MariaDB jimv 密码: [${RDB_JIMV_PSWD}]"
- echo "Redis 端口: [6379]"
- echo "Redis 密码: [${REDIS_PSWD}]"
- echo "======================="
- echo
- echo "记录下上面信息,安装 JimV-N 时需要用到。"
- echo "现在可以通过命令 '/home/www/sites/JimV-C/startup.sh' 启动 JimV-C。"
- echo "======================="
- echo
- echo "通过 Web 页面完成 JimV-C 的初始化操作。然后到 [计算节点] 执行如下命令,进行 JimV-N 的部署。"
- echo "curl https://raw.githubusercontent.com/jamesiter/JimV-N/master/INSTALL.sh | bash -s -- --redis_host `hostname -I` --redis_password ${REDIS_PSWD} --redis_port 6379"
- }
- function deploy() {
- check_precondition
- clear_up_environment
- ensure_hosts_layout
- sync_ssh_key_pair
- sync_hosts_file
- prepare
- set_ntp
- create_web_user
- create_web_sites_directory
- clone_and_checkout_JimVC
- fit_www_user_permission
- install_MariaDB
- install_Redis
- install_Nginx
- install_dependencies_library
- initialization_db
- generate_pid_directory_ad_reboot
- generate_config_file
- display_summary_information
- }
- deploy
|