INSTALL.sh 9.3 KB

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