INSTALL.sh 10 KB

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