INSTALL.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #!/usr/bin/env bash
  2. #
  3. # JimV-C
  4. #
  5. # Copyright (C) 2017 JimV <james.iter.cn@gmail.com>
  6. #
  7. # Author: James Iter <james.iter.cn@gmail.com>
  8. #
  9. # This script will help you to automation install JimV-C.
  10. #
  11. # Example:
  12. # Pass arguments to the installer.
  13. # curl https://raw.githubusercontent.com/jamesiter/JimV-C/dev/INSTALL.sh | bash -s -- --version dev
  14. # bash -c "$(curl -fsSL https://raw.githubusercontent.com/jamesiter/JimV-C/dev/INSTALL.sh)" -- --version dev
  15. export PYPI='https://mirrors.aliyun.com/pypi/simple/'
  16. export JIMVC_REPOSITORY_URL='https://github.com/jamesiter/JimV-C.git'
  17. export JIMVC_REPOSITORY_URL_CN='https://gitee.com/jimit/JimV-C.git'
  18. export JIMVC_REPOSITORY_RAW_URL='https://raw.githubusercontent.com/jamesiter/JimV-C'
  19. export COUNTRY=`curl http://iit.im/ip/country`
  20. export GENERATE_PASSWORD_SCRIPT_TMP_PATH='/tmp/gen_pswd.sh'
  21. export SMTP_HOST=''
  22. export SMTP_USER=''
  23. export SMTP_PASSWORD=''
  24. if [ ${COUNTRY} = 'CN' ]; then
  25. export JIMVC_REPOSITORY_URL=${JIMVC_REPOSITORY_URL_CN}
  26. fi
  27. ARGS=`getopt -o h --long rdb_root_password:,rdb_jimv_password:,redis_password:,jwt_secret:,secret_key:,version:,help -n 'INSTALL.sh' -- "$@"`
  28. eval set -- "${ARGS}"
  29. while true
  30. do
  31. case "$1" in
  32. --rdb_root_password)
  33. export RDB_ROOT_PSWD=$2
  34. shift 2
  35. ;;
  36. --rdb_jimv_password)
  37. export RDB_JIMV_PSWD=$2
  38. shift 2
  39. ;;
  40. --redis_password)
  41. export REDIS_PSWD=$2
  42. shift 2
  43. ;;
  44. --jwt_secret)
  45. export JWT_SECRET=$2
  46. shift 2
  47. ;;
  48. --secret_key)
  49. export SECRET_KEY=$2
  50. shift 2
  51. ;;
  52. --version)
  53. export JIMV_VERSION=$2
  54. shift 2
  55. ;;
  56. -h|--help)
  57. echo 'INSTALL.sh [-h|--help|--rdb_root_password|--rdb_jimv_password|--redis_password|--jwt_secret|--secret_key|--version]'
  58. exit 0
  59. ;;
  60. --)
  61. shift
  62. break
  63. ;;
  64. *)
  65. echo "Internal error!"
  66. exit 1
  67. ;;
  68. esac
  69. done
  70. function check_precondition() {
  71. source /etc/os-release
  72. case ${ID} in
  73. centos|fedora|rhel)
  74. if [ ${VERSION_ID} -lt 7 ]; then
  75. echo "System version must greater than or equal to 7, We found ${VERSION_ID}."
  76. exit 1
  77. fi
  78. ;;
  79. *)
  80. echo "${ID} is unknown, Please to be installed manually."
  81. exit 1
  82. ;;
  83. esac
  84. }
  85. function ensure_hosts_layout() {
  86. echo "______________________________________________________________________________"
  87. /bin/cat /etc/hosts
  88. echo
  89. read -p "您已经布局好 /etc/hosts 了吗 [Y/N]? " answer
  90. if [ "x_"${answer} != "x_Y" ] && [ "x_"${answer} != "x_y" ]; then
  91. echo "OK, good bye!";
  92. exit 0
  93. fi
  94. }
  95. function sync_ssh_key_pair() {
  96. sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config
  97. rm -f ~/.ssh/id_rsa ~/.ssh/id_rsa.pub; echo -e "\n\n\n" | ssh-keygen -N ""
  98. echo >> ~/.ssh/authorized_keys
  99. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  100. chmod 0600 ~/.ssh/authorized_keys
  101. DHOSTS=`egrep -v '^$|localhost|jimvc' /etc/hosts | awk '{ print $1; }'`
  102. for dhost in ${DHOSTS}
  103. do
  104. scp -r ~/.ssh ${dhost}:~/
  105. # 关闭 SSH 服务器端 Key 校验
  106. ssh ${dhost} "sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config"
  107. done
  108. }
  109. function sync_hosts_file() {
  110. DHOSTS=`egrep -v '^$|localhost|jimvc' /etc/hosts | awk '{ print $1; }'`
  111. for dhost in ${DHOSTS}
  112. do
  113. scp /etc/hosts ${dhost}:/etc/hosts
  114. done
  115. }
  116. function prepare() {
  117. if [ ! ${JIMV_VERSION} ] || [ ${#JIMV_VERSION} -eq 0 ]; then
  118. export JIMV_VERSION='master'
  119. fi
  120. if [ ! -e ${GENERATE_PASSWORD_SCRIPT_TMP_PATH} ]; then
  121. curl ${JIMVC_REPOSITORY_RAW_URL}'/'${JIMV_VERSION}'/misc/gen_pswd.sh' -o ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  122. chmod +x ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  123. fi
  124. if [ ! ${RDB_ROOT_PSWD} ] || [ ${#RDB_ROOT_PSWD} -eq 0 ]; then
  125. export RDB_ROOT_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
  126. fi
  127. if [ ! ${RDB_JIMV_PSWD} ] || [ ${#RDB_JIMV_PSWD} -eq 0 ]; then
  128. export RDB_JIMV_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
  129. fi
  130. if [ ! ${REDIS_PSWD} ] || [ ${#REDIS_PSWD} -eq 0 ]; then
  131. export REDIS_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  132. fi
  133. if [ ! ${JWT_SECRET} ] || [ ${#JWT_SECRET} -eq 0 ]; then
  134. export JWT_SECRET=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  135. fi
  136. if [ ! ${SECRET_KEY} ] || [ ${#SECRET_KEY} -eq 0 ]; then
  137. export SECRET_KEY=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  138. fi
  139. rm -f ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  140. yum install epel-release -y
  141. yum install python2-pip git psmisc -y
  142. pip install --upgrade pip -i ${PYPI}
  143. pip install virtualenv -i ${PYPI}
  144. }
  145. function set_ntp() {
  146. yum install ntp -y
  147. systemctl start ntpd
  148. systemctl enable ntpd
  149. timedatectl set-timezone Asia/Shanghai
  150. timedatectl set-ntp true
  151. timedatectl status
  152. }
  153. function clear_up_environment() {
  154. systemctl stop firewalld
  155. systemctl disable firewalld
  156. systemctl stop NetworkManager
  157. systemctl disable NetworkManager
  158. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/sysconfig/selinux
  159. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
  160. setenforce 0
  161. }
  162. function install_MariaDB() {
  163. # 安装 MariaDB
  164. yum install mariadb mariadb-server -y
  165. # 配置 MariaDB
  166. mkdir -p /etc/systemd/system/mariadb.service.d
  167. echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
  168. echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
  169. systemctl --system daemon-reload
  170. sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
  171. sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
  172. sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
  173. sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
  174. sed -i '/\[mysqld\]/a\max_connections = 10000' /etc/my.cnf
  175. # 启动并使其随机启动
  176. systemctl enable mariadb.service
  177. systemctl start mariadb.service
  178. # 初始化 MariaDB
  179. mysql_secure_installation << EOF
  180. Y
  181. ${RDB_ROOT_PSWD}
  182. ${RDB_ROOT_PSWD}
  183. Y
  184. Y
  185. Y
  186. Y
  187. EOF
  188. # 测试是否部署成功
  189. mysql -u root -p${RDB_ROOT_PSWD} -e 'show databases'
  190. }
  191. function install_Redis() {
  192. # 安装 Redis
  193. yum install redis -y
  194. # 配置 Redis
  195. echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
  196. sysctl -p
  197. sed -i 's@^daemonize no@daemonize yes@g' /etc/redis.conf
  198. sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
  199. sed -i 's@^appendonly no@appendonly yes@g' /etc/redis.conf
  200. echo "requirepass ${REDIS_PSWD}" >> /etc/redis.conf
  201. # 启动并使其随机启动
  202. systemctl enable redis.service
  203. systemctl start redis.service
  204. }
  205. function install_Nginx() {
  206. export NGINX_JIMV_URL=${JIMVC_REPOSITORY_RAW_URL}'/'${JIMV_VERSION}'/misc/nginx_jimv.conf'
  207. # 安装 Nginx
  208. yum install nginx -y
  209. # 配置 Nginx
  210. mkdir -p /etc/jimv/keys
  211. chown -R www.www /var/lib/nginx
  212. sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
  213. sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
  214. curl ${NGINX_JIMV_URL} >> /etc/nginx/nginx.conf
  215. # 启动并使其随机启动
  216. systemctl enable nginx.service
  217. systemctl start nginx.service
  218. }
  219. function create_web_user() {
  220. useradd -m www
  221. }
  222. function create_web_sites_directory() {
  223. su - www -c "mkdir ~/sites"
  224. }
  225. function clone_and_checkout_JimVC() {
  226. su - www -c "git clone ${JIMVC_REPOSITORY_URL} ~/sites/JimV-C"
  227. su - www -c "cd ~/sites/JimV-C && git checkout ${JIMV_VERSION}"
  228. }
  229. function install_dependencies_library() {
  230. # 指定 www 用户 pip 源
  231. su - www -c "mkdir -p ~/.pip"
  232. su - www -c "cat > ~/.pip/pip.conf << EOF
  233. [global]
  234. index-url = ${PYPI}
  235. EOF
  236. "
  237. # 创建 python 虚拟环境
  238. su - www -c "virtualenv --system-site-packages ~/venv"
  239. # 导入 python 虚拟环境
  240. su - www -c "source ~/venv/bin/activate"
  241. # 使切入 www 用户时自动导入 python 虚拟环境
  242. su - www -c "echo '. ~/venv/bin/activate' >> .bashrc"
  243. # 安装 JimV-C 所需扩展库
  244. su - www -c "pip install -r ~/sites/JimV-C/requirements.txt -i ${PYPI}"
  245. }
  246. function fit_www_user_permission() {
  247. mkdir -p /var/log/jimv
  248. chown www.www /var/log/jimv
  249. mkdir -p /run/jimv
  250. chown www.www /run/jimv
  251. }
  252. function initialization_db() {
  253. # 建立 JimV 数据库专属用户
  254. mysql -u root -p${RDB_ROOT_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
  255. # 初始化数据库
  256. su - www -c "mysql -u jimv -p${RDB_JIMV_PSWD} < ~/sites/JimV-C/misc/init.sql"
  257. # 确认是否初始化成功
  258. mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
  259. }
  260. function generate_config_file() {
  261. cp -v /home/www/sites/JimV-C/jimvc.conf /etc/jimvc.conf
  262. sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
  263. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
  264. sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
  265. sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
  266. sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
  267. sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
  268. sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
  269. }
  270. function generate_pid_directory_ad_reboot() {
  271. chmod +x /etc/rc.d/rc.local
  272. echo '' >> /etc/rc.d/rc.local
  273. echo 'mkdir /run/jimv' >> /etc/rc.d/rc.local
  274. echo 'chown www.www /run/jimv' >> /etc/rc.d/rc.local
  275. }
  276. function display_summary_information() {
  277. echo
  278. echo "=== 信息汇总"
  279. echo "MariaDB root 密码: [${RDB_ROOT_PSWD}]"
  280. echo "MariaDB jimv 密码: [${RDB_JIMV_PSWD}]"
  281. echo "Redis 端口: [6379]"
  282. echo "Redis 密码: [${REDIS_PSWD}]"
  283. echo "======================="
  284. echo
  285. echo "JimV-C 已经安装完成,您再需如下几步就能完成整个 JimV 的部署: "
  286. echo
  287. echo "--->"
  288. echo "1: 执行 '/home/www/sites/JimV-C/startup.sh' 启动 JimV-C。"
  289. echo
  290. echo "--->"
  291. echo "2: 通过 Web 页面 http://`hostname -I` 初始化 JimV-C。"
  292. echo
  293. echo "--->"
  294. echo "3: 到 [计算节点] 执行如下命令,进行 JimV-N 的部署。"
  295. 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"
  296. echo
  297. echo "--->"
  298. echo "4: 享受 JimV 给您带来的 '简单、快速、灵活' 开创虚拟机实例的快乐。。。。。"
  299. echo
  300. }
  301. function deploy() {
  302. check_precondition
  303. clear_up_environment
  304. ensure_hosts_layout
  305. sync_ssh_key_pair
  306. sync_hosts_file
  307. prepare
  308. set_ntp
  309. create_web_user
  310. create_web_sites_directory
  311. clone_and_checkout_JimVC
  312. fit_www_user_permission
  313. install_MariaDB
  314. install_Redis
  315. install_Nginx
  316. install_dependencies_library
  317. initialization_db
  318. generate_pid_directory_ad_reboot
  319. generate_config_file
  320. display_summary_information
  321. }
  322. deploy