INSTALL.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 set_ntp() {
  116. yum install ntp -y
  117. systemctl start ntpd
  118. systemctl enable ntpd
  119. timedatectl set-timezone Asia/Shanghai
  120. timedatectl set-ntp true
  121. timedatectl status
  122. }
  123. function clear_up_environment() {
  124. systemctl stop firewalld
  125. systemctl disable firewalld
  126. systemctl stop NetworkManager
  127. systemctl disable NetworkManager
  128. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/sysconfig/selinux
  129. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
  130. setenforce 0
  131. }
  132. function install_JimVC() {
  133. cat > /etc/yum.repos.d/JimV.repo << EOF
  134. [JimV]
  135. name=JimV - \$basearch
  136. baseurl=http://repo.iit.im/centos/7/os/\$basearch
  137. failovermethod=priority
  138. enabled=1
  139. gpgcheck=0
  140. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  141. EOF
  142. yum install jimv-controller -y
  143. }
  144. function generate_passwords() {
  145. export GENERATE_PASSWORD_SCRIPT='/usr/bin/gen_pswd.sh'
  146. if [[ ! ${RDB_ROOT_PSWD} ]] || [[ ${#RDB_ROOT_PSWD} -eq 0 ]]; then
  147. export RDB_ROOT_PSWD=`${GENERATE_PASSWORD_SCRIPT}`
  148. fi
  149. if [[ ! ${RDB_JIMV_PSWD} ]] || [[ ${#RDB_JIMV_PSWD} -eq 0 ]]; then
  150. export RDB_JIMV_PSWD=`${GENERATE_PASSWORD_SCRIPT}`
  151. fi
  152. if [[ ! ${REDIS_PSWD} ]] || [[ ${#REDIS_PSWD} -eq 0 ]]; then
  153. export REDIS_PSWD=`${GENERATE_PASSWORD_SCRIPT} 128`
  154. fi
  155. if [[ ! ${JWT_SECRET} ]] || [[ ${#JWT_SECRET} -eq 0 ]]; then
  156. export JWT_SECRET=`${GENERATE_PASSWORD_SCRIPT} 128`
  157. fi
  158. if [[ ! ${SECRET_KEY} ]] || [[ ${#SECRET_KEY} -eq 0 ]]; then
  159. export SECRET_KEY=`${GENERATE_PASSWORD_SCRIPT} 128`
  160. fi
  161. }
  162. function create_web_user() {
  163. useradd -M www
  164. }
  165. function install_MariaDB() {
  166. # 安装 MariaDB
  167. yum install mariadb mariadb-server -y
  168. # 配置 MariaDB
  169. mkdir -p /etc/systemd/system/mariadb.service.d
  170. echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
  171. echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
  172. systemctl --system daemon-reload
  173. sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
  174. sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
  175. sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
  176. sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
  177. sed -i '/\[mysqld\]/a\max_connections = 10000' /etc/my.cnf
  178. # 启动并使其随机启动
  179. systemctl enable mariadb.service
  180. systemctl start mariadb.service
  181. # 初始化 MariaDB
  182. mysql_secure_installation << EOF
  183. Y
  184. ${RDB_ROOT_PSWD}
  185. ${RDB_ROOT_PSWD}
  186. Y
  187. Y
  188. Y
  189. Y
  190. EOF
  191. # 测试是否部署成功
  192. mysql -u root -p${RDB_ROOT_PSWD} -e 'show databases'
  193. }
  194. function install_Redis() {
  195. # 安装 Redis
  196. yum install redis -y
  197. # 配置 Redis
  198. echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
  199. sysctl -p
  200. sed -i 's@^daemonize no@daemonize yes@g' /etc/redis.conf
  201. sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
  202. echo "requirepass ${REDIS_PSWD}" >> /etc/redis.conf
  203. # 启动并使其随机启动
  204. systemctl enable redis.service
  205. systemctl start redis.service
  206. }
  207. function install_Nginx() {
  208. export NGINX_JIMV_URL=${JIMVC_REPOSITORY_RAW_URL}'/'${JIMV_VERSION}'/misc/nginx_jimv.conf'
  209. # 安装 Nginx
  210. yum install nginx -y
  211. # 配置 Nginx
  212. mkdir -p /etc/jimv/keys
  213. chown -R www.www /var/lib/nginx
  214. sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
  215. sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
  216. curl ${NGINX_JIMV_URL} >> /etc/nginx/nginx.conf
  217. # 启动并使其随机启动
  218. systemctl enable nginx.service
  219. systemctl start nginx.service
  220. }
  221. function initialization_db() {
  222. # 建立 JimV 数据库专属用户
  223. mysql -u root -p${RDB_ROOT_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
  224. # 初始化数据库
  225. mysql -u jimv -p${RDB_JIMV_PSWD} < /usr/share/jimv/controller/misc/init.sql
  226. # 确认是否初始化成功
  227. mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
  228. }
  229. function generate_config_file() {
  230. sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
  231. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
  232. sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
  233. sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
  234. sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
  235. sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
  236. sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
  237. }
  238. function start_JimVC() {
  239. systemctl start jimvc.service
  240. systemctl enable jimvc.service
  241. }
  242. function display_summary_information() {
  243. echo
  244. echo "=== 信息汇总"
  245. echo "MariaDB root 密码: [${RDB_ROOT_PSWD}]"
  246. echo "MariaDB jimv 密码: [${RDB_JIMV_PSWD}]"
  247. echo "Redis 端口: [6379]"
  248. echo "Redis 密码: [${REDIS_PSWD}]"
  249. echo "======================="
  250. echo
  251. echo "JimV-C 已经安装完成,您再需如下几步就能完成整个 JimV 的部署: "
  252. echo
  253. echo "--->"
  254. echo "1: 通过 Web 页面 http://`hostname -I` 初始化 JimV-C。"
  255. echo
  256. echo "--->"
  257. echo "2: 到 [计算节点] 执行如下命令,进行 JimV-N 的部署。"
  258. 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"
  259. echo
  260. echo "--->"
  261. echo "3: 享受 JimV 给您带来的 '简单、快速、灵活' 开创虚拟机实例的快乐。。。。。"
  262. echo
  263. }
  264. function deploy() {
  265. check_precondition
  266. clear_up_environment
  267. ensure_hosts_layout
  268. sync_ssh_key_pair
  269. sync_hosts_file
  270. prepare
  271. set_ntp
  272. create_web_user
  273. install_JimVC
  274. generate_passwords
  275. install_MariaDB
  276. install_Redis
  277. install_Nginx
  278. initialization_db
  279. generate_config_file
  280. start_JimVC
  281. display_summary_information
  282. }
  283. deploy