INSTALL.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 SMTP_HOST=''
  17. export SMTP_USER=''
  18. export SMTP_PASSWORD=''
  19. ARGS=`getopt -o h --long rdb_root_password:,rdb_jimv_password:,redis_password:,jwt_secret:,secret_key:,version:,help -n 'INSTALL.sh' -- "$@"`
  20. eval set -- "${ARGS}"
  21. while true
  22. do
  23. case "$1" in
  24. --rdb_root_password)
  25. export RDB_ROOT_PSWD=$2
  26. shift 2
  27. ;;
  28. --rdb_jimv_password)
  29. export RDB_JIMV_PSWD=$2
  30. shift 2
  31. ;;
  32. --redis_password)
  33. export REDIS_PSWD=$2
  34. shift 2
  35. ;;
  36. --jwt_secret)
  37. export JWT_SECRET=$2
  38. shift 2
  39. ;;
  40. --secret_key)
  41. export SECRET_KEY=$2
  42. shift 2
  43. ;;
  44. --version)
  45. export JIMV_VERSION=$2
  46. export JIMVC_DOWNLOAD_URL=$(sed s@master@${JIMV_VERSION}@ <<< ${JIMVC_DOWNLOAD_URL})
  47. shift 2
  48. ;;
  49. -h|--help)
  50. echo 'INSTALL.sh [-h|--help|--rdb_root_password|--rdb_jimv_password|--redis_password|--jwt_secret|--secret_key|--version]'
  51. exit 0
  52. ;;
  53. --)
  54. shift
  55. break
  56. ;;
  57. *)
  58. echo "Internal error!"
  59. exit 1
  60. ;;
  61. esac
  62. done
  63. function check_precondition() {
  64. source /etc/os-release
  65. case ${ID} in
  66. centos|fedora|rhel)
  67. if [[ ${VERSION_ID} -lt 7 ]]; then
  68. echo "System version must greater than or equal to 7, We found ${VERSION_ID}."
  69. exit 1
  70. fi
  71. ;;
  72. *)
  73. echo "${ID} is unknown, Please to be installed manually."
  74. exit 1
  75. ;;
  76. esac
  77. }
  78. function ensure_hosts_layout() {
  79. echo "______________________________________________________________________________"
  80. /bin/cat /etc/hosts
  81. echo
  82. read -p "您已经布局好 /etc/hosts 了吗 [Y/N]? " answer
  83. if [[ "x_"${answer} != "x_Y" ]] && [[ "x_"${answer} != "x_y" ]]; then
  84. echo "OK, good bye!";
  85. exit 0
  86. fi
  87. }
  88. function sync_ssh_key_pair() {
  89. sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config
  90. rm -f ~/.ssh/id_rsa ~/.ssh/id_rsa.pub; echo -e "\n\n\n" | ssh-keygen -N ""
  91. echo >> ~/.ssh/authorized_keys
  92. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  93. chmod 0600 ~/.ssh/authorized_keys
  94. DHOSTS=`egrep -v '^$|localhost|jimvc' /etc/hosts | awk '{ print $1; }'`
  95. for dhost in ${DHOSTS}
  96. do
  97. scp -r ~/.ssh ${dhost}:~/
  98. # 关闭 SSH 服务器端 Key 校验
  99. ssh ${dhost} "sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config"
  100. done
  101. }
  102. function sync_hosts_file() {
  103. DHOSTS=`egrep -v '^$|localhost|jimvc' /etc/hosts | awk '{ print $1; }'`
  104. for dhost in ${DHOSTS}
  105. do
  106. scp /etc/hosts ${dhost}:/etc/hosts
  107. done
  108. }
  109. function prepare() {
  110. if [[ ! ${JIMV_VERSION} ]] || [[ ${#JIMV_VERSION} -eq 0 ]]; then
  111. export JIMV_VERSION='master'
  112. fi
  113. yum install epel-release -y
  114. }
  115. function clear_up_environment() {
  116. systemctl stop firewalld
  117. systemctl disable firewalld
  118. systemctl stop NetworkManager
  119. systemctl disable NetworkManager
  120. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/sysconfig/selinux
  121. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
  122. setenforce 0
  123. }
  124. function install_JimVC() {
  125. cat > /etc/yum.repos.d/JimV.repo << EOF
  126. [JimV]
  127. name=JimV - \$basearch
  128. baseurl=http://repo.iit.im/centos/7/os/\$basearch
  129. failovermethod=priority
  130. enabled=1
  131. gpgcheck=0
  132. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  133. EOF
  134. yum install jimv-controller -y
  135. }
  136. function generate_passwords() {
  137. export GENERATE_PASSWORD_SCRIPT='/usr/bin/gen_pswd.sh'
  138. if [[ ! ${RDB_ROOT_PSWD} ]] || [[ ${#RDB_ROOT_PSWD} -eq 0 ]]; then
  139. export RDB_ROOT_PSWD=`${GENERATE_PASSWORD_SCRIPT}`
  140. fi
  141. if [[ ! ${RDB_JIMV_PSWD} ]] || [[ ${#RDB_JIMV_PSWD} -eq 0 ]]; then
  142. export RDB_JIMV_PSWD=`${GENERATE_PASSWORD_SCRIPT}`
  143. fi
  144. if [[ ! ${REDIS_PSWD} ]] || [[ ${#REDIS_PSWD} -eq 0 ]]; then
  145. export REDIS_PSWD=`${GENERATE_PASSWORD_SCRIPT} 128`
  146. fi
  147. if [[ ! ${JWT_SECRET} ]] || [[ ${#JWT_SECRET} -eq 0 ]]; then
  148. export JWT_SECRET=`${GENERATE_PASSWORD_SCRIPT} 128`
  149. fi
  150. if [[ ! ${SECRET_KEY} ]] || [[ ${#SECRET_KEY} -eq 0 ]]; then
  151. export SECRET_KEY=`${GENERATE_PASSWORD_SCRIPT} 128`
  152. fi
  153. }
  154. function create_web_user() {
  155. useradd -M www
  156. }
  157. function install_MariaDB() {
  158. # 安装 MariaDB
  159. yum install mariadb mariadb-server -y
  160. # 配置 MariaDB
  161. mkdir -p /etc/systemd/system/mariadb.service.d
  162. echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
  163. echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
  164. systemctl --system daemon-reload
  165. sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
  166. sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
  167. sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
  168. sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
  169. sed -i '/\[mysqld\]/a\max_connections = 10000' /etc/my.cnf
  170. # 启动并使其随机启动
  171. systemctl enable mariadb.service
  172. systemctl start mariadb.service
  173. # 初始化 MariaDB
  174. mysql_secure_installation << EOF
  175. Y
  176. ${RDB_ROOT_PSWD}
  177. ${RDB_ROOT_PSWD}
  178. Y
  179. Y
  180. Y
  181. Y
  182. EOF
  183. # 测试是否部署成功
  184. mysql -u root -p${RDB_ROOT_PSWD} -e 'show databases'
  185. }
  186. function install_Redis() {
  187. # 安装 Redis
  188. yum install redis -y
  189. # 配置 Redis
  190. echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
  191. sysctl -p
  192. sed -i 's@^daemonize no@daemonize yes@g' /etc/redis.conf
  193. sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
  194. echo "requirepass ${REDIS_PSWD}" >> /etc/redis.conf
  195. # 启动并使其随机启动
  196. systemctl enable redis.service
  197. systemctl start redis.service
  198. }
  199. function install_Nginx() {
  200. # 安装 Nginx
  201. yum install nginx -y
  202. # 配置 Nginx
  203. mkdir -p /etc/jimv/keys
  204. chown -R www.www /var/lib/nginx
  205. sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
  206. sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
  207. cat /usr/share/jimv/controller/misc/nginx_jimv.conf >> /etc/nginx/nginx.conf
  208. # 启动并使其随机启动
  209. systemctl enable nginx.service
  210. systemctl start nginx.service
  211. }
  212. function initialization_db() {
  213. # 建立 JimV 数据库专属用户
  214. mysql -u root -p${RDB_ROOT_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
  215. # 初始化数据库
  216. mysql -u jimv -p${RDB_JIMV_PSWD} < /usr/share/jimv/controller/misc/init.sql
  217. # 确认是否初始化成功
  218. mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
  219. }
  220. function generate_config_file() {
  221. sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
  222. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
  223. sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
  224. sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
  225. sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
  226. sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
  227. sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
  228. }
  229. function start_JimVC() {
  230. systemctl start jimvc.service
  231. systemctl enable jimvc.service
  232. }
  233. function display_summary_information() {
  234. echo
  235. echo "=== 信息汇总"
  236. echo "MariaDB root 密码: [${RDB_ROOT_PSWD}]"
  237. echo "MariaDB jimv 密码: [${RDB_JIMV_PSWD}]"
  238. echo "Redis 端口: [6379]"
  239. echo "Redis 密码: [${REDIS_PSWD}]"
  240. echo "======================="
  241. echo
  242. echo "JimV-C 已经安装完成,您再需如下几步就能完成整个 JimV 的部署: "
  243. echo
  244. echo "--->"
  245. echo "1: 通过 Web 页面 http://`hostname -I` 初始化 JimV-C。"
  246. echo
  247. echo "--->"
  248. echo "2: 到 [计算节点] 执行如下命令,进行 JimV-N 的部署。"
  249. 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"
  250. echo
  251. echo "--->"
  252. echo "3: 享受 JimV 给您带来的 '简单、快速、灵活' 开创虚拟机实例的快乐。。。。。"
  253. echo
  254. }
  255. function deploy() {
  256. check_precondition
  257. clear_up_environment
  258. ensure_hosts_layout
  259. sync_ssh_key_pair
  260. sync_hosts_file
  261. prepare
  262. create_web_user
  263. install_JimVC
  264. generate_passwords
  265. install_MariaDB
  266. install_Redis
  267. install_Nginx
  268. initialization_db
  269. generate_config_file
  270. start_JimVC
  271. display_summary_information
  272. }
  273. deploy