ambari-agent.ps1 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License
  15. # description: ambari-agent service
  16. # processname: ambari-agent
  17. $VERSION="1.3.0-SNAPSHOT"
  18. $HASH="testhash"
  19. switch ($($args[0])){
  20. "--version" {
  21. echo "$VERSION"
  22. exit 0
  23. }
  24. "--hash" {
  25. echo "$HASH"
  26. exit 0
  27. }
  28. }
  29. # Handle spaces in command line arguments properly
  30. $quoted_args=@()
  31. ForEach ($arg in $args)
  32. {
  33. if($arg.Contains(' '))
  34. {
  35. $arg = """" + $arg + """"
  36. }
  37. $quoted_args = $quoted_args + @($arg)
  38. }
  39. $args = $quoted_args
  40. $AMBARI_AGENT="ambari-agent"
  41. $AMBARI_SVC_NAME = "Ambari Agent"
  42. $current_directory = (Get-Item -Path ".\" -Verbose).FullName
  43. #environment variables used in python, check if they exists, otherwise set them to $current_directory
  44. #and pass to child python process
  45. $Env:PYTHONPATH="$current_directory\sbin;$($Env:PYTHONPATH)"
  46. $Env:PYTHON = "python.exe"
  47. $AMBARI_LOG_DIR="\var\log\ambari-agent"
  48. $OUTFILE_STDOUT=Join-Path -path $AMBARI_LOG_DIR -childpath "ambari-agent.stdout"
  49. $OUTFILE_STDERR=Join-Path -path $AMBARI_LOG_DIR -childpath "ambari-agent.stderr"
  50. $LOGFILE=Join-Path -path $AMBARI_LOG_DIR -childpath "ambari-agent.log"
  51. $AMBARI_AGENT_PY_SCRIPT=Join-Path -path $PSScriptRoot -childpath "sbin\service_wrapper.py"
  52. if($AMBARI_AGENT_PY_SCRIPT.Contains(' '))
  53. {
  54. $AMBARI_AGENT_PY_SCRIPT = """" + $AMBARI_AGENT_PY_SCRIPT + """"
  55. }
  56. $OK=1
  57. $NOTOK=0
  58. $retcode=0
  59. function _exit($code)
  60. {
  61. $host.SetShouldExit($code)
  62. exit $code
  63. }
  64. function _detect_python()
  65. {
  66. if(![boolean]$(Get-Command $Env:PYTHON -ErrorAction SilentlyContinue))
  67. {
  68. echo "ERROR: Can not find python.exe in PATH. Add python executable to PATH and try again."
  69. _exit(1)
  70. }
  71. }
  72. function _echo([switch]$off)
  73. {
  74. if($off)
  75. {
  76. try
  77. {
  78. stop-transcript|out-null
  79. }
  80. catch [System.InvalidOperationException]
  81. {}
  82. }
  83. else
  84. {
  85. try
  86. {
  87. start-transcript|out-null
  88. }
  89. catch [System.InvalidOperationException]
  90. {}
  91. }
  92. }
  93. Function _pstart_brief($cmd_args)
  94. {
  95. #start python with -u to make stdout and stderr unbuffered
  96. $arguments = @("-u",$AMBARI_AGENT_PY_SCRIPT) + $cmd_args
  97. $psi = New-Object System.Diagnostics.ProcessStartInfo
  98. $psi.RedirectStandardError = $True
  99. $psi.RedirectStandardOutput = $True
  100. $psi.UseShellExecute = $False
  101. $psi.FileName = $Env:PYTHON
  102. $psi.Arguments = $arguments
  103. #$psi.WindowStyle = WindowStyle.Hidden
  104. $process = [Diagnostics.Process]::Start($psi)
  105. $process.WaitForExit()
  106. Write-Output $process.StandardOutput.ReadToEnd()
  107. }
  108. Function _start($cmd_args)
  109. {
  110. echo "Starting $AMBARI_SVC_NAME..."
  111. _echo -off
  112. _pstart_brief($cmd_args)
  113. $cnt = 0
  114. do
  115. {
  116. Start-Sleep -Milliseconds 250
  117. $svc = Get-Service -Name $AMBARI_SVC_NAME
  118. $cnt += 1
  119. if ($cnt -eq 120)
  120. {
  121. echo "$AMBARI_SVC_NAME still starting...".
  122. return
  123. }
  124. }
  125. until($svc.status -eq "Running")
  126. echo "$AMBARI_SVC_NAME is running"
  127. }
  128. Function _pstart($cmd_args)
  129. {
  130. New-Item -ItemType Directory -Force -Path $AMBARI_LOG_DIR | Out-Null
  131. $arguments = @($AMBARI_AGENT_PY_SCRIPT) + $cmd_args
  132. $p = New-Object System.Diagnostics.Process
  133. $p.StartInfo.UseShellExecute = $false
  134. $p.StartInfo.FileName = $Env:PYTHON
  135. $p.StartInfo.Arguments = $arguments
  136. [void]$p.Start();
  137. echo "Verifying $AMBARI_AGENT process status..."
  138. if (!$p){
  139. echo "ERROR: $AMBARI_AGENT start failed"
  140. $host.SetShouldExit(-1)
  141. exit
  142. }
  143. echo "Agent log at: $LOGFILE"
  144. $p.WaitForExit()
  145. }
  146. Function _pstart_ioredir($cmd_args)
  147. {
  148. New-Item -ItemType Directory -Force -Path $AMBARI_LOG_DIR | Out-Null
  149. #start python with -u to make stdout and stderr unbuffered
  150. $arguments = @("-u",$AMBARI_AGENT_PY_SCRIPT) + $cmd_args
  151. $process = Start-Process -FilePath $Env:PYTHON -ArgumentList $arguments -WindowStyle Hidden -RedirectStandardError $OUTFILE_STDERR -RedirectStandardOutput $OUTFILE_STDOUT -PassThru
  152. echo "Verifying $AMBARI_AGENT process status..."
  153. if (!$process){
  154. echo "ERROR: $AMBARI_AGENT start failed"
  155. $host.SetShouldExit(-1)
  156. exit
  157. }
  158. echo "Agent stdout at: $OUTFILE_STDOUT"
  159. echo "Agent stderr at: $OUTFILE_STDERR"
  160. echo "Agent log at: $LOGFILE"
  161. $process.WaitForExit()
  162. }
  163. Function _stop($cmd_args){
  164. echo "Stopping $AMBARI_SVC_NAME..."
  165. _pstart_brief($cmd_args)
  166. $cnt = 0
  167. do
  168. {
  169. Start-Sleep -Milliseconds 250
  170. $svc = Get-Service -Name $AMBARI_SVC_NAME
  171. $cnt += 1
  172. if ($cnt -eq 40)
  173. {
  174. echo "$AMBARI_SVC_NAME still stopping...".
  175. return
  176. }
  177. }
  178. until($svc.status -eq "Stopped")
  179. echo "$AMBARI_SVC_NAME is stopped"
  180. }
  181. Function _status($cmd_args){
  182. echo "Getting $AMBARI_SVC_NAME status..."
  183. _pstart_brief($cmd_args)
  184. }
  185. # check for python before any action
  186. _detect_python
  187. switch ($($args[0])){
  188. "start"
  189. {
  190. _start $args
  191. }
  192. "debug"
  193. {
  194. echo "Starting ambari-agent"
  195. _pstart_ioredir $args
  196. echo "Ambari Agent finished"
  197. }
  198. "stop" {_stop $args}
  199. "restart"
  200. {
  201. _stop @("stop")
  202. _start @("start")
  203. }
  204. "status" {_status $args}
  205. "setup"
  206. {
  207. echo "Installing ambari-agent"
  208. _pstart $args
  209. echo "Ambari Agent installation finished"
  210. }
  211. default
  212. {
  213. echo "Usage: ambari-agent {start|stop|restart|setup|status} [options]"
  214. echo "Use ambari-agent <action> --help to get details on options available."
  215. echo "Or, simply invoke ambari-agent.py --help to print the options."
  216. $retcode=1
  217. }
  218. }
  219. $host.SetShouldExit($retcode)
  220. exit