No Description

onehost_create_all 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env bash
  2. #
  3. # Register all Hâpy Cluster Nodes
  4. #
  5. . /usr/lib/eole/ihm.sh
  6. #
  7. # NAME: copy_ssh_id
  8. # AIM: Copy the ssh key on the host
  9. # PARAM: the hostname of the node
  10. #
  11. function copy_ssh_id()
  12. {
  13. local host=${1}
  14. CLEF=$(cat ~oneadmin/.ssh/id_rsa.pub)
  15. ssh ${host} bash <<EOF
  16. if ! grep -qs "$CLEF" ~oneadmin/.ssh/authorized_keys; then
  17. echo "$CLEF" >> ~oneadmin/.ssh/authorized_keys
  18. chown oneadmin:oneadmin ~oneadmin/.ssh/authorized_keys
  19. su - oneadmin -c "ssh-keyscan $host"
  20. cat ~oneadmin/.ssh/id_rsa.pub >> ~oneadmin/.ssh/authorized_keys
  21. fi
  22. EOF
  23. [[ ${?} -ne 0 ]] && EchoRouge "Erreur lors de l'échange de clés SSH avec le noeud ${host}"
  24. REMOTEKEY=$(su - oneadmin -c "ssh ${host} 'cat ~oneadmin/.ssh/id_rsa.pub'")
  25. if ! grep -qs "$REMOTEKEY" ~oneadmin/.ssh/authorized_keys; then
  26. su - oneadmin -c "echo ${REMOTEKEY} >> ~oneadmin/.ssh/authorized_keys"
  27. su - oneadmin -c "ssh-keyscan $host"
  28. fi
  29. }
  30. function copy_files()
  31. {
  32. local host=${1}
  33. su - oneadmin -c "scp .one/* ${host}:.one/"
  34. }
  35. #
  36. # NAME: sync_db
  37. # AIM: Sync onedb in HA and sqlite mode
  38. # PARAM: the hostname of the node
  39. #
  40. function sync_db()
  41. {
  42. local ip=${1}
  43. local user="oneadmin"
  44. local DBFILE="/var/lib/one/one.db"
  45. local DBBCK="/tmp/one.db.bck"
  46. local ret=0
  47. if [[ ! -f ${DBBCK} ]]
  48. then
  49. cmd="onedb backup --sqlite ${DBFILE} ${DBBCK}"
  50. ret=$(su - ${user} -c -- "${cmd}")
  51. fi
  52. cmd2="scp ${DBBCK} ${ip}:${DBFILE}.leader"
  53. ret=$(su - ${user} -c -- "${cmd2}")
  54. return ${?}
  55. }
  56. #
  57. # NAME: sync_nodes
  58. # AIM: force nodes sync with rsync (ONE 5.6.1 bug)
  59. # PARAM: none
  60. #
  61. function sync_nodes()
  62. {
  63. # Need to sync hosts with rsync after creation, ONE 5.6.1 bug
  64. cmd2="onehost sync -f --rsync"
  65. ret2=$(su - ${ONEUSER} -c -- "${cmd2}")
  66. return ${?}
  67. }
  68. #
  69. # NAME: register_node
  70. # AIM: register the node in OpenNebula master
  71. # PARAM: the node hostname
  72. #
  73. function register_node()
  74. {
  75. cmd="onehost create -i kvm -v kvm -c 0 ${1}"
  76. ret=$(su - ${ONEUSER} -c -- "${cmd}")
  77. if [[ ${?} -ne 0 ]]
  78. then
  79. EchoRouge "Hosts register failed $res"
  80. else
  81. EchoVert "Hosts register OK"
  82. fi
  83. }
  84. #
  85. # NAME: wait_node_ok
  86. # AIM: Wait until the node is OK or ERROR
  87. # PARAM: The node name
  88. #
  89. function wait_node_ok()
  90. {
  91. local cmd="onehost show ${1} | awk '/^STATE/ {print \$3}'"
  92. local spinstr='|/-\'
  93. local delay=0.75
  94. local cnt=0
  95. while [ 1 ]
  96. do
  97. st=$(su - ${ONEUSER} -c "${cmd}")
  98. [[ ${st} == "MONITORED" ]] && break
  99. [[ ${st} == '' ]] && break
  100. if [[ ${st} == "ERROR" ]]
  101. then
  102. if [ $cnt -gt 160 ]; then
  103. EchoRouge "Erreur lors de l'enregistrement du noeud ${host} !"
  104. break
  105. fi
  106. fi
  107. local temp=${spinstr#?}
  108. printf " [%c] " "$spinstr"
  109. local spinstr=$temp${spinstr%"$temp"}
  110. sleep $delay
  111. printf "\b\b\b\b\b\b"
  112. cnt=$((cnt+1))
  113. done
  114. printf " \b\b\b\b"
  115. }
  116. function init_ha_leader() {
  117. # server with index 1 exists if already instanciate
  118. onezone show 0 | grep -A 3 ^"HA & FEDERATION SYNC STATUS" | tail -n 1 | grep -q ^" 1 "
  119. if [ ! $? = 0 ]; then
  120. FOLLOWER=$(CreoleGet one_nodes)
  121. for follower in $FOLLOWER; do
  122. onezone server-add 0 --name $follower --rpc http://$follower:2633/RPC2
  123. done
  124. fi
  125. }
  126. #
  127. # MAIN
  128. #
  129. HAPY_ACTIF=$(echo $(CreoleGet activer_onesinglenode))
  130. if [[ $HAPY_ACTIF == "non" ]]
  131. then
  132. EchoRouge "Le serveur de virtualisation n'est pas activé dans l'interface de configuration du module"
  133. exit 1
  134. fi
  135. HAPY_NODE_SUPPORT=$(echo $(CreoleGet activer_multinode))
  136. if [[ $HAPY_NODE_SUPPORT == "non" ]]
  137. then
  138. EchoRouge "Le mode multi-noeuds n'est pas activé dans l'interface de configuration du module"
  139. exit 1
  140. <<<<<<< HEAD
  141. fi
  142. HAPY_HA=$(CreoleGet activer_one_ha "non")
  143. master=1
  144. if [[ ${HAPY_HA} == "oui" ]]; then
  145. idx=$(CreoleGet one_ha_server_index)
  146. if [[ ${idx} != "0" ]]; then
  147. EchoBleu "Mode HA: (on) Index : [${idx}]"
  148. echo -e "\t$(basename ${0}) doit être lancé uniquement sur le leader (index 0)"
  149. exit 0
  150. else
  151. master=0
  152. fi
  153. fi
  154. =======
  155. fi
  156. HAPY_HA=$(CreoleGet activer_one_ha "non")
  157. master=1
  158. if [[ ${HAPY_HA} == "oui" ]]; then
  159. idx=$(CreoleGet one_ha_server_index)
  160. if [[ ${idx} != "0" ]]; then
  161. EchoBleu "Mode HA: (on) Index : [${idx}]"
  162. echo -e "\t$(basename ${0}) doit être lancé uniquement sur le leader (index 0)"
  163. exit 0
  164. else
  165. master=0
  166. fi
  167. fi
  168. >>>>>>> dcc8eeeecee5c5824e31c73b5ddf04590714f606
  169. declare -a HAPY_SLV=('')
  170. ONEUSER=$(CreoleGet virt_user)
  171. HAPY_SLV=$(echo $(CreoleGet one_nodes) | sed -e "s/\n/ /g")
  172. DBMODE=$(CreoleGet one_database_type "none")
  173. echo -e "\n"
  174. EchoBleu "Vous allez inscrire un noeud dans une grappe Hâpy"
  175. EchoBleu "Pour ce faire vous devez vous munir du mot de passe de l'utilisateur 'oneadmin' de chacun des noeuds"
  176. Question_ouinon "Voulez-vous commencer ?" 'True' "oui"
  177. if [[ $? -ne 0 ]]
  178. then
  179. EchoOrange "Abandon de l'enregistrement"
  180. exit 1
  181. fi
  182. for host in ${HAPY_SLV}; do
  183. echo -e "\n"
  184. EchoOrange "Traitement du noeud ${host}"
  185. echo
  186. EchoVert " * Gestion des clés SSH"
  187. echo
  188. copy_ssh_id ${host}
  189. if [ $master = 0 ]; then
  190. copy_files ${host}
  191. fi
  192. EchoVert " * Enregistrement du noeud"
  193. register_node ${host}
  194. if [[ ${HAPY_HA} == "oui" ]]
  195. then
  196. if [[ $DBMODE == "sqlite" ]]
  197. then
  198. if [[ $(CreoleGet one_ha_server_index) == "0" ]]
  199. then
  200. sync_db ${host}
  201. fi
  202. fi
  203. fi
  204. sync_nodes
  205. if [[ ${HAPY_HA} != "oui" ]]
  206. <<<<<<< HEAD
  207. =======
  208. then
  209. >>>>>>> dcc8eeeecee5c5824e31c73b5ddf04590714f606
  210. wait_node_ok ${HAPY_SLV[${i}]}
  211. if [[ ${?} -ne 0 ]]
  212. then
  213. EchoRouge "Erreur lors de l'enregistrement du noeud ${HAPY_SLV[${i}]} !"
  214. fi
  215. fi
  216. done
  217. if [ $master = 0 ]; then
  218. init_ha_leader
  219. fi
  220. echo -e "\n"
  221. EchoVert "Enregistrement des noeuds terminé"