Guest User

Untitled

a guest
Jul 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script runs all nova components in windows of a singe screen.
  4. # Arguments: [nova.conf] [novarc]
  5.  
  6. function locate_binary {
  7. # Called with the basename of a binary, like nova-api
  8. # If we are in a nova checkout, use the bin/* version.
  9. # Otherwise, get whatever is in ${PATH} for system wide nova
  10. bin_name=${1}
  11. var_name=`echo ${bin_name} | sed -e s.-._.`
  12. if [ -d bin ] && [ -f bin/${bin_name} ] ; then
  13. eval ${var_name}="bin/${bin_name}"
  14. else
  15. rv=`which ${bin_name}`
  16. if [ -z "${rv}" ] ; then
  17. echo "FATAL: Unable to find a suitable ${bin_name}"
  18. echo " Either use ${BASH_SOURCE} from a bzr checkout of nova"
  19. echo " or put ${bin_name} in your \$PATH"
  20. exit 1
  21. fi
  22. eval ${var_name}="${rv}"
  23. fi
  24. }
  25.  
  26. function screen_is_running {
  27. # Check for a running screen session
  28. # You can specify a session name other than nova by providing an argument
  29. # Returns 0 if running, 1 otherwise
  30. # Can be used in bash conditionals
  31. session_name=${1}
  32. if [ -z "${session_name}" ] ; then
  33. session_name=nova
  34. fi
  35. captured=`screen -ls | grep ${session_name}`
  36. if [ -z "${captured}" ] ; then
  37. return 1 # not running
  38. fi
  39. return 0
  40. }
  41.  
  42. function start_screen {
  43. # Starts a new screen session
  44. # You can specify a session name other than nova by providing an argument
  45. session_name=${1}
  46. if [ -z "${session_name}" ] ; then
  47. session_name=nova
  48. fi
  49. screen_is_running ${session_name}
  50. if [ $? == 0 ] ; then
  51. echo "FATAL: screen session ${session_name} already exists"
  52. exit 1
  53. fi
  54. # -d -m creates a new detached session per screen(1)
  55. # -S names the session which we will use to attach to later
  56. # -t names the particular window in screen we can attach to later
  57. screen -d -m -S ${session_name} -t default
  58. }
  59.  
  60. function add_to_screen {
  61. # Add a command to a new window in an existing screen session
  62. # Arguments: session-name command
  63. session_name=${1}
  64. command=${2}
  65. if [ -z "${command}" ] ; then
  66. echo "INVALID USAGE: add_to_screen session-name command"
  67. return
  68. fi
  69. screen_is_running ${session_name}
  70. if [ $? == 1 ] ; then
  71. echo "ERROR: No running screen session named ${session_name}"
  72. echo " Skipping command ${command}"
  73. return
  74. fi
  75. basename=`echo ${command} | awk '{print $1}' | xargs basename`
  76. # -X screen_command sends a screen internal command
  77. # (as would be produced by a keystroke, not a command like a shell command)
  78. # see screen(1) sections DEFAULT KEY BINDINGS and CUSTOMIZATION
  79. # 'screen' is equivilent to C-a C-c, which creates a new window
  80. screen -S ${session_name} -X screen -t ${basename}
  81. # ASCII Carriage Return to emulate pressing enter.
  82. CR=`echo -ne '\r'`
  83. # -p selects a window by name (paired with -t in screen command above)
  84. # the stuff command puts the text in screen's input buffer
  85. # this throws the command, followed by enter, to the shell running in screen
  86. screen -S ${session_name} -p ${basename} -X stuff "${command}${CR}"
  87. }
  88.  
  89. function build_screen_keymap {
  90. # Map F1 key to windowlist
  91. # You can specify a session name other than nova by providing an argument
  92. session_name=${1}
  93. if [ -z "${session_name}" ] ; then
  94. session_name=nova
  95. fi
  96. screen_is_running ${session_name}
  97. if [ $? == 1 ] ; then
  98. echo "ERROR: No running screen session named ${session_name}"
  99.  
  100. echo " Skipping command ${command}"
  101. return
  102. fi
  103. basename=`echo ${command} | awk '{print $1}' | xargs basename`
  104. # -X screen_command sends a screen internal command
  105. # (as would be produced by a keystroke, not a command like a shell command)
  106. # see screen(1) sections DEFAULT KEY BINDINGS and CUSTOMIZATION
  107. # 'screen' is equivilent to C-a C-c, which creates a new window
  108. screen -S ${session_name} -X screen -t ${basename}
  109. # ASCII Carriage Return to emulate pressing enter.
  110. CR=`echo -ne '\r'`
  111. # -p selects a window by name (paired with -t in screen command above)
  112. # the stuff command puts the text in screen's input buffer
  113. # this throws the command, followed by enter, to the shell running in screen
  114. screen -S ${session_name} -p ${basename} -X stuff "${command}${CR}"
  115. }
  116.  
  117. function build_screen_keymap {
  118. # Map F1 key to windowlist
  119. # You can specify a session name other than nova by providing an argument
  120. session_name=${1}
  121. if [ -z "${session_name}" ] ; then
  122. session_name=nova
  123. fi
  124. screen_is_running ${session_name}
  125. if [ $? == 1 ] ; then
  126. echo "ERROR: No running screen session named ${session_name}"
  127. echo " Can't add key bindings"
  128. return
  129. fi
  130. screen -S ${session_name} -X bindkey -k k1 windowlist -b
  131. }
  132.  
  133. function drop_from_screen {
  134. # Kill a window in a screen
  135. # Arguments: session_name window_name
  136. session_name=${1}
  137. window_name=${2}
  138. if [ -z "${window_name}" ] ; then
  139. echo "INVALID USAGE: drop_from_screen session-name window-name"
  140. return
  141. fi
  142. screen_is_running ${session_name}
  143. if [ $? == 1 ] ; then
  144. echo "ERROR: No running screen session named ${session_name}"
  145. echo " Can't drop window ${window_name}"
  146. return
  147. fi
  148. screen -S ${session_name} -p ${window_name} -X kill
  149. }
  150.  
  151. function attach_to_screen {
  152. # Attaches to a running but detached screen
  153. # You can specify a session name other than nova by providing an argument
  154. session_name=${1}
  155. if [ -z "${session_name}" ] ; then
  156. session_name=nova
  157. fi
  158. screen -S ${session_name} -r
  159. }
  160.  
  161. function build_screen {
  162. # Launch all the components in a screen session and attach to it
  163. session_name=${1}
  164. if [ -z "${session_name}" ] ; then
  165. session_name=nova
  166. fi
  167. start_screen "${session_name}"
  168. add_to_screen "${session_name}" "${nova_api} --flagfile=${flagfile}"
  169. add_to_screen "${session_name}" "${nova_compute} --flagfile=${flagfile}"
  170. add_to_screen "${session_name}" "${nova_network} --flagfile=${flagfile}"
  171. add_to_screen "${session_name}" "${nova_volume} --flagfile=${flagfile}"
  172. add_to_screen "${session_name}" "${nova_objectstore} --flagfile=${flagfile}"
  173. add_to_screen "${session_name}" "${nova_scheduler} --flagfile=${flagfile}"
  174. add_to_screen "${session_name}" "source ${BASH_SOURCE}"
  175. drop_from_screen "${session_name}" default
  176. build_screen_keymap "${session_name}"
  177. attach_to_screen "${session_name}"
  178. }
  179.  
  180. function determine_flagfile {
  181. # Try to find a flagfile to use.
  182. # Check the first argument for explictness. Try some defaults othereise.
  183. flagfile=${1}
  184. if [ ! -z "${flagfile}" ] ; then
  185. if [ ! -f ${flagfile} ] ; then
  186. echo "WARNING: can't find flagfile: ${flagfile}"
  187. fi
  188. return
  189. fi
  190. # Try a local version
  191. if [ -f nova.conf ] ; then
  192. flagfile="nova.conf"
  193. return
  194. fi
  195. # Try another local version (compatible with nova.sh)
  196. if [ -f bin/nova.conf ] ; then
  197. flagfile="bin/nova.conf"
  198. return
  199.  
  200. fi
  201. # Try the system version
  202. if [ -f /etc/nova/nova.conf ] ; then
  203. flagfile="/etc/nova/nova.conf"
  204. return
  205. fi
  206. echo "WARNING: no flagfile found, using built-in settings"
  207. flagfile=""
  208. }
  209.  
  210. function display_welcome {
  211. clear
  212. echo Thanks for using nova!
  213. echo
  214. echo Your services have been started, you can view their screens by using
  215. echo C-a \" \(Thats Control+a, then press \"\) to view a list of windows and
  216. echo select the one you want to see. You can return here by selecting
  217. echo the window named \"source\".
  218. echo
  219. echo You should now be able to use euca2ools \(euca-run-instances, etc\)
  220. echo to interact with your cloud.
  221. echo
  222. }
  223.  
  224. function source_nova_rc {
  225. # Load novarc, can be passed in as argument, found as novarc, or built new
  226. novarc=${1}
  227. if [ ! -z "${novarc}" ] ; then
  228. source ${novarc}
  229. return
  230. fi
  231. if [ -f novarc ] ; then
  232. source novarc
  233. return
  234. fi
  235. # already aliased
  236. ${nova_manage} --flagfile=${flagfile} project zipfile admin admin /tmp/admin.zip
  237. unzip -d /tmp /tmp/admin.zip novarc
  238. source /tmp/novarc
  239. rm /tmp/admin.zip
  240. rm /tmp/novarc
  241. }
  242.  
  243. # find binaries or die trying
  244. # sets $nova_api, etc as variables
  245. locate_binary nova-api
  246. locate_binary nova-compute
  247. locate_binary nova-network
  248.  
  249. locate_binary nova-volume
  250. locate_binary nova-scheduler
  251. locate_binary nova-objectstore
  252. locate_binary nova-manage
  253.  
  254. determine_flagfile "${1}"
  255.  
  256. if [ $0 == ${BASH_SOURCE} ] ; then
  257. # run from command line, make the screen
  258. build_screen
  259. echo "Screen Detached"
  260. echo -ne '\a'
  261. else
  262. # sourced by bash, load the window with help and a good environment
  263. alias nova-manage="${nova_manage} --flagfile=${flagfile}"
  264. PS1="[nova-screen]${PS1}"
  265. display_welcome
  266. source_nova_rc "${2}"
  267. ${nova_manage} --flagfile=${flagfile} service enable `hostname` nova-compute
  268. ${nova_manage} --flagfile=${flagfile} service enable `hostname` nova-network
  269. ${nova_manage} --flagfile=${flagfile} service enable `hostname` nova-volume
  270. fi
Add Comment
Please, Sign In to add comment