jimv.standalone.install.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #!/usr/bin/env bash
  2. #
  3. # JimV-StandAlone
  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.
  10. #
  11. export PYPI='https://mirrors.aliyun.com/pypi/simple/'
  12. export SMTP_HOST=''
  13. export SMTP_USER=''
  14. export SMTP_PASSWORD=''
  15. export TOKEN=`echo $[$RANDOM]|md5sum|cut -c 1-32`
  16. export SHOW_WARNING_VTX=false
  17. ARGS=`getopt -o h --long rdb_root_password:,rdb_jimv_password:,redis_password:,jwt_secret:,secret_key:,version:,help -n 'INSTALL.sh' -- "$@"`
  18. eval set -- "${ARGS}"
  19. while true
  20. do
  21. case "$1" in
  22. --rdb_root_password)
  23. export RDB_ROOT_PSWD=$2
  24. shift 2
  25. ;;
  26. --rdb_jimv_password)
  27. export RDB_JIMV_PSWD=$2
  28. shift 2
  29. ;;
  30. --redis_password)
  31. export REDIS_PSWD=$2
  32. shift 2
  33. ;;
  34. --jwt_secret)
  35. export JWT_SECRET=$2
  36. shift 2
  37. ;;
  38. --secret_key)
  39. export SECRET_KEY=$2
  40. shift 2
  41. ;;
  42. --version)
  43. export JIMV_VERSION=$2
  44. export JIMVC_DOWNLOAD_URL=$(sed s@master@${JIMV_VERSION}@ <<< ${JIMVC_DOWNLOAD_URL})
  45. shift 2
  46. ;;
  47. -h|--help)
  48. echo 'INSTALL.sh [-h|--help|--rdb_root_password|--rdb_jimv_password|--redis_password|--jwt_secret|--secret_key|--version]'
  49. exit 0
  50. ;;
  51. --)
  52. shift
  53. break
  54. ;;
  55. *)
  56. echo "Internal error!"
  57. exit 1
  58. ;;
  59. esac
  60. done
  61. function check_precondition() {
  62. source /etc/os-release
  63. case ${ID} in
  64. centos|fedora|rhel)
  65. if [[ ${VERSION_ID} -lt 7 ]]; then
  66. echo "System version must greater than or equal to 7, We found ${VERSION_ID}."
  67. exit 1
  68. fi
  69. ;;
  70. *)
  71. echo "${ID} is unknown, Please to be installed manually."
  72. exit 1
  73. ;;
  74. esac
  75. if [[ `egrep -c '(vmx|svm)' /proc/cpuinfo` -eq 0 ]]; then
  76. export SHOW_WARNING_VTX=true
  77. fi
  78. }
  79. function custom_repository_origin() {
  80. cat > /etc/yum.repos.d/JimV.repo << EOF
  81. [JimV]
  82. name=JimV - \$basearch
  83. baseurl=http://repo.jimv.cn/centos/7/os/\$basearch
  84. http://repo.jimv.io/centos/7/os/\$basearch
  85. http://repo.iit.im/centos/7/os/\$basearch
  86. failovermethod=priority
  87. enabled=1
  88. gpgcheck=0
  89. gpgkey=https://repo.jimv.cn/RPM-GPG-KEY-JIMV-114EA591
  90. EOF
  91. yum install epel-release -y
  92. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  93. mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
  94. mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
  95. curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  96. curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  97. sed -i '/aliyuncs/d' /etc/yum.repos.d/*.repo
  98. yum clean all
  99. yum makecache
  100. }
  101. function clear_up_environment() {
  102. systemctl stop firewalld
  103. systemctl disable firewalld
  104. systemctl stop NetworkManager
  105. systemctl disable NetworkManager
  106. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/sysconfig/selinux
  107. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
  108. setenforce 0
  109. }
  110. function create_web_user() {
  111. useradd -M www
  112. }
  113. function install_JimVC() {
  114. yum install jimv-controller -y
  115. }
  116. function generate_passwords() {
  117. export GENERATE_PASSWORD_SCRIPT='/usr/bin/gen_pswd.sh'
  118. if [[ ! ${RDB_ROOT_PSWD} ]] || [[ ${#RDB_ROOT_PSWD} -eq 0 ]]; then
  119. export RDB_ROOT_PSWD=`${GENERATE_PASSWORD_SCRIPT}`
  120. fi
  121. if [[ ! ${RDB_JIMV_PSWD} ]] || [[ ${#RDB_JIMV_PSWD} -eq 0 ]]; then
  122. export RDB_JIMV_PSWD=`${GENERATE_PASSWORD_SCRIPT}`
  123. fi
  124. if [[ ! ${REDIS_PSWD} ]] || [[ ${#REDIS_PSWD} -eq 0 ]]; then
  125. export REDIS_PSWD=`${GENERATE_PASSWORD_SCRIPT} 128`
  126. fi
  127. if [[ ! ${JWT_SECRET} ]] || [[ ${#JWT_SECRET} -eq 0 ]]; then
  128. export JWT_SECRET=`${GENERATE_PASSWORD_SCRIPT} 128`
  129. fi
  130. if [[ ! ${SECRET_KEY} ]] || [[ ${#SECRET_KEY} -eq 0 ]]; then
  131. export SECRET_KEY=`${GENERATE_PASSWORD_SCRIPT} 128`
  132. fi
  133. }
  134. function install_MariaDB() {
  135. # 安装 MariaDB
  136. yum install mariadb mariadb-server -y
  137. # 配置 MariaDB
  138. mkdir -p /etc/systemd/system/mariadb.service.d
  139. echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
  140. echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
  141. systemctl --system daemon-reload
  142. sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
  143. sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
  144. sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
  145. sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
  146. sed -i '/\[mysqld\]/a\max_connections = 10000' /etc/my.cnf
  147. # 启动并使其随机启动
  148. systemctl enable mariadb.service
  149. systemctl start mariadb.service
  150. # 初始化 MariaDB
  151. mysql_secure_installation << EOF
  152. Y
  153. ${RDB_ROOT_PSWD}
  154. ${RDB_ROOT_PSWD}
  155. Y
  156. Y
  157. Y
  158. Y
  159. EOF
  160. # 测试是否部署成功
  161. mysql -u root -p${RDB_ROOT_PSWD} -e 'show databases'
  162. }
  163. function install_Redis() {
  164. # 安装 Redis
  165. yum install redis -y
  166. # 配置 Redis
  167. echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
  168. sysctl -p
  169. sed -i 's@^daemonize no@daemonize yes@g' /etc/redis.conf
  170. sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
  171. echo "requirepass ${REDIS_PSWD}" >> /etc/redis.conf
  172. # 启动并使其随机启动
  173. systemctl enable redis.service
  174. systemctl start redis.service
  175. }
  176. function install_Nginx() {
  177. # 安装 Nginx
  178. yum install nginx -y
  179. # 配置 Nginx
  180. mkdir -p /etc/jimv/keys
  181. chown -R www.www /var/lib/nginx
  182. sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
  183. sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
  184. cat /usr/share/jimv/controller/misc/nginx_jimv.conf >> /etc/nginx/nginx.conf
  185. # 启动并使其随机启动
  186. systemctl enable nginx.service
  187. systemctl start nginx.service
  188. }
  189. function initialization_db() {
  190. # 建立 JimV 数据库专属用户
  191. mysql -u root -p${RDB_ROOT_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
  192. # 初始化数据库
  193. mysql -u jimv -p${RDB_JIMV_PSWD} < /usr/share/jimv/controller/misc/init.sql
  194. # 确认是否初始化成功
  195. mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
  196. }
  197. function generate_token() {
  198. redis-cli -a ${REDIS_PSWD} --raw ZADD Z:Token $(($(date +%s) + 86400)) ${TOKEN}
  199. }
  200. function generate_JimVC_config_file() {
  201. sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
  202. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
  203. sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
  204. sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
  205. sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
  206. sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
  207. sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
  208. }
  209. function start_JimVC() {
  210. systemctl start jimvc.service
  211. systemctl enable jimvc.service
  212. sleep 2
  213. }
  214. function Init_JimVC() {
  215. curl -XPOST -H "Content-Type: application/json" -d '{"storage_mode": 0, "vm_network": "net-br0", "vm_manage_network": "net-br0", "storage_path": "/opt/Images"}' http://127.0.0.1/api/config
  216. }
  217. function JimVN_pretreatment() {
  218. yum install epel-release -y
  219. yum install jq net-tools bind-utils -y
  220. yum install redis -y
  221. # 代替语句 ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
  222. SERVER_IP=`hostname -I`; export SERVER_IP=${SERVER_IP%% *}
  223. export SERVER_NETMASK=`ifconfig | grep ${SERVER_IP} | grep -Eo 'netmask ?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'`
  224. export GATEWAY=`route -n | grep '^0.0.0.0' | awk '{ print $2; }'`
  225. export DNS1=`nslookup 127.0.0.1 | grep Server | grep -Eo '([0-9]*\.){3}[0-9]*'`
  226. export NIC=`ifconfig | grep ${SERVER_IP} -B 1 | head -1 | cut -d ':' -f 1`
  227. export NODE_ID=`python -c "with open('/etc/machine-id', 'r') as f: _str = f.read().strip(); import string; import hashlib; m = hashlib.md5(); m.update(_str); print int(string.atoi(m.hexdigest(), 16).__str__()[:16])"`
  228. JOIN_RESPONSE=`curl http://127.0.0.1/api/misc/_join/${NODE_ID}/${TOKEN}`
  229. export REDIS_HOST=`echo ${JOIN_RESPONSE} | jq -r '.data.redis_host'`
  230. export REDIS_PORT=`echo ${JOIN_RESPONSE} | jq -r '.data.redis_port'`
  231. export REDIS_PSWD=`echo ${JOIN_RESPONSE} | jq -r '.data.redis_password'`
  232. export VM_NETWORK=`echo ${JOIN_RESPONSE} | jq -r '.data.vm_network'`
  233. export VM_NETWORK_MANAGE=`echo ${JOIN_RESPONSE} | jq -r '.data.vm_manage_network'`
  234. }
  235. function install_libvirt_and_libguestfish() {
  236. # 安装 libvirt
  237. uname -m | grep -q 'x86_64' && echo 'centos' >/etc/yum/vars/contentdir || echo 'altarch' >/etc/yum/vars/contentdir
  238. yum install centos-release-qemu-ev -y
  239. yum install jimv-node -y
  240. yum install librbd1-10.2.5 -y
  241. }
  242. function tune_linux_kernel_parameters() {
  243. sed -i '/^net.ipv4.ip_local_port_range/d' /etc/sysctl.conf
  244. echo 'net.ipv4.ip_local_port_range = 15900 20000' >> /etc/sysctl.conf
  245. sysctl -p
  246. }
  247. function handle_ssh_client_config() {
  248. # 关闭 SSH 服务器端 Key 校验
  249. sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config
  250. }
  251. function handle_net_bonding_bridge() {
  252. # 参考地址: https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/networking_guide/
  253. cat > /etc/sysconfig/network-scripts/ifcfg-${VM_NETWORK} << EOF
  254. DEVICE=${VM_NETWORK}
  255. NAME=${VM_NETWORK}
  256. TYPE=Bridge
  257. BOOTPROTO=static
  258. ONBOOT=yes
  259. DELAY=0
  260. IPADDR=${SERVER_IP}
  261. NETMASK=${SERVER_NETMASK}
  262. GATEWAY=${GATEWAY}
  263. DNS1=${DNS1}
  264. DNS2=8.8.8.8
  265. IPV6INIT=no
  266. EOF
  267. cat > /etc/sysconfig/network-scripts/ifcfg-bond0 << EOF
  268. DEVICE=bond0
  269. NAME=bond0
  270. TYPE=Bond
  271. BRIDGE=${VM_NETWORK}
  272. BONDING_MASTER=yes
  273. ONBOOT=yes
  274. BOOTPROTO=none
  275. BONDING_OPTS="mode=balance-alb xmit_hash_policy=layer3+4"
  276. EOF
  277. cat > /etc/sysconfig/network-scripts/ifcfg-${NIC} << EOF
  278. DEVICE=${NIC}
  279. NAME=${NIC}
  280. TYPE=Ethernet
  281. BOOTPROTO=none
  282. ONBOOT=yes
  283. MASTER=bond0
  284. SLAVE=yes
  285. EOF
  286. /etc/init.d/network restart
  287. }
  288. function create_network_bridge_in_libvirt() {
  289. cat > /etc/libvirt/qemu/networks/${VM_NETWORK}.xml << EOF
  290. <network>
  291. <uuid>the_uuid</uuid>
  292. <name>${VM_NETWORK}</name>
  293. <forward mode="bridge"/>
  294. <bridge name="${VM_NETWORK}"/>
  295. </network>
  296. EOF
  297. sed -i "s@the_uuid@`uuidgen`@" /etc/libvirt/qemu/networks/${VM_NETWORK}.xml
  298. # 去除默认的 default 网络定义
  299. rm -f /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/autostart/default.xml
  300. # 使其随服务自动创建
  301. cd /etc/libvirt/qemu/networks/autostart/
  302. ln -s ../${VM_NETWORK}.xml ${VM_NETWORK}.xml
  303. }
  304. function start_libvirtd() {
  305. systemctl stop dnsmasq
  306. systemctl disable dnsmasq
  307. systemctl enable libvirtd
  308. systemctl start libvirtd
  309. }
  310. function generate_JimVN_config_file() {
  311. sed -i "s/\"redis_host\".*$/\"redis_host\": \"${REDIS_HOST}\",/" /etc/jimvn.conf
  312. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvn.conf
  313. sed -i "s/\"redis_port\".*$/\"redis_port\": \"${REDIS_PORT}\",/" /etc/jimvn.conf
  314. }
  315. function start_JimVN() {
  316. systemctl start jimvn.service
  317. systemctl enable jimvn.service
  318. }
  319. function display_summary_information() {
  320. echo
  321. echo "=== 信息汇总"
  322. echo "MariaDB root 密码: [${RDB_ROOT_PSWD}]"
  323. echo "MariaDB jimv 密码: [${RDB_JIMV_PSWD}]"
  324. echo "Redis 端口: [6379]"
  325. echo "Redis 密码: [${REDIS_PSWD}]"
  326. echo "======================="
  327. echo
  328. echo "JimV 已经安装完成,您再需如下几步就能完成整个 JimV 的部署: "
  329. echo
  330. echo "--->"
  331. echo "1: 登入 Web 页面 http://`hostname -I`"
  332. echo " 进入【系统配置】 --> 【IP 池】,配置您的 IP 池。"
  333. echo
  334. echo "--->"
  335. echo "2: 进入【模板镜像】 --> 【公共镜像】,添加模板。"
  336. echo
  337. echo "--->"
  338. echo "3: 享受 JimV 给您带来的 '简单、快速、灵活' 开创虚拟机实例的快乐。。。。。"
  339. echo
  340. if [[ ${SHOW_WARNING_VTX} = true ]]; then
  341. echo "警告:请检查 CPU 是否开启 VT 技术。未开启 VT 技术的计算节点,将以 QEMU 模式运行虚拟机。"
  342. echo
  343. fi
  344. echo "已经通过 'systemctl enable jimvc.service' 把 JimV-C 注册为随系统启动的服务。"
  345. echo "您还可以通过命令 'systemctl [start|stop|status] jimvc.service' 来管理本机的 JimV-C。"
  346. echo
  347. echo "已经通过 'systemctl enable jimvn.service' 把 JimV-N 注册为随系统启动的服务。"
  348. echo "您还可以通过命令 'systemctl [start|stop|status] jimvn.service' 来管理本机的 JimV-N。"
  349. echo
  350. }
  351. function deploy() {
  352. check_precondition
  353. custom_repository_origin
  354. clear_up_environment
  355. create_web_user
  356. install_JimVC
  357. generate_passwords
  358. install_MariaDB
  359. install_Redis
  360. install_Nginx
  361. initialization_db
  362. generate_token
  363. generate_JimVC_config_file
  364. start_JimVC
  365. Init_JimVC
  366. JimVN_pretreatment
  367. install_libvirt_and_libguestfish
  368. tune_linux_kernel_parameters
  369. handle_ssh_client_config
  370. handle_net_bonding_bridge
  371. create_network_bridge_in_libvirt
  372. start_libvirtd
  373. generate_JimVN_config_file
  374. start_JimVN
  375. display_summary_information
  376. }
  377. deploy