ambari-agent.ps1 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. $AMBARI_AGENT="ambari-agent"
  30. $AMBARI_SVC_NAME = "Ambari Agent"
  31. $current_directory = (Get-Item -Path ".\" -Verbose).FullName
  32. #environment variables used in python, check if they exists, otherwise set them to $current_directory
  33. #and pass to child python process
  34. $Env:PYTHONPATH="$current_directory\sbin;$($Env:PYTHONPATH)"
  35. $Env:PYTHON = "python.exe"
  36. $AMBARI_LOG_DIR="\var\log\ambari-agent"
  37. $OUTFILE_STDOUT=Join-Path -path $AMBARI_LOG_DIR -childpath "ambari-agent.stdout"
  38. $OUTFILE_STDERR=Join-Path -path $AMBARI_LOG_DIR -childpath "ambari-agent.stderr"
  39. $LOGFILE=Join-Path -path $AMBARI_LOG_DIR -childpath "ambari-agent.log"
  40. $AMBARI_AGENT_PY_SCRIPT=Join-Path -path $PSScriptRoot -childpath "sbin\service_wrapper.py"
  41. if($AMBARI_AGENT_PY_SCRIPT.Contains(' '))
  42. {
  43. $AMBARI_AGENT_PY_SCRIPT = """" + $AMBARI_AGENT_PY_SCRIPT + """"
  44. }
  45. $OK=1
  46. $NOTOK=0
  47. $retcode=0
  48. function _exit($code)
  49. {
  50. $host.SetShouldExit($code)
  51. exit $code
  52. }
  53. function _detect_python()
  54. {
  55. if(![boolean]$(Get-Command $Env:PYTHON -ErrorAction SilentlyContinue))
  56. {
  57. echo "ERROR: Can not find python.exe in PATH. Add python executable to PATH and try again."
  58. _exit(1)
  59. }
  60. }
  61. function _echo([switch]$off)
  62. {
  63. if($off)
  64. {
  65. try
  66. {
  67. stop-transcript|out-null
  68. }
  69. catch [System.InvalidOperationException]
  70. {}
  71. }
  72. else
  73. {
  74. try
  75. {
  76. start-transcript|out-null
  77. }
  78. catch [System.InvalidOperationException]
  79. {}
  80. }
  81. }
  82. Function _pstart_brief($cmd_args)
  83. {
  84. #start python with -u to make stdout and stderr unbuffered
  85. $arguments = @("-u",$AMBARI_AGENT_PY_SCRIPT) + $cmd_args
  86. $psi = New-Object System.Diagnostics.ProcessStartInfo
  87. $psi.RedirectStandardError = $True
  88. $psi.RedirectStandardOutput = $True
  89. $psi.UseShellExecute = $False
  90. $psi.FileName = $Env:PYTHON
  91. $psi.Arguments = $arguments
  92. #$psi.WindowStyle = WindowStyle.Hidden
  93. $process = [Diagnostics.Process]::Start($psi)
  94. $process.WaitForExit()
  95. Write-Output $process.StandardOutput.ReadToEnd()
  96. }
  97. Function _start($cmd_args)
  98. {
  99. echo "Starting $AMBARI_SVC_NAME..."
  100. _echo -off
  101. _pstart_brief($cmd_args)
  102. $cnt = 0
  103. do
  104. {
  105. Start-Sleep -Milliseconds 250
  106. $svc = Get-Service -Name $AMBARI_SVC_NAME
  107. $cnt += 1
  108. if ($cnt -eq 120)
  109. {
  110. echo "$AMBARI_SVC_NAME still starting...".
  111. return
  112. }
  113. }
  114. until($svc.status -eq "Running")
  115. echo "$AMBARI_SVC_NAME is running"
  116. }
  117. Function _pstart($cmd_args)
  118. {
  119. New-Item -ItemType Directory -Force -Path $AMBARI_LOG_DIR | Out-Null
  120. $arguments = @($AMBARI_AGENT_PY_SCRIPT) + $cmd_args
  121. $p = New-Object System.Diagnostics.Process
  122. $p.StartInfo.UseShellExecute = $false
  123. $p.StartInfo.FileName = $Env:PYTHON
  124. $p.StartInfo.Arguments = $arguments
  125. [void]$p.Start();
  126. echo "Verifying $AMBARI_AGENT process status..."
  127. if (!$p){
  128. echo "ERROR: $AMBARI_AGENT start failed"
  129. $host.SetShouldExit(-1)
  130. exit
  131. }
  132. echo "Agent log at: $LOGFILE"
  133. $p.WaitForExit()
  134. }
  135. Function _pstart_ioredir($cmd_args)
  136. {
  137. New-Item -ItemType Directory -Force -Path $AMBARI_LOG_DIR | Out-Null
  138. #start python with -u to make stdout and stderr unbuffered
  139. $arguments = @("-u",$AMBARI_AGENT_PY_SCRIPT) + $cmd_args
  140. $process = Start-Process -FilePath $Env:PYTHON -ArgumentList $arguments -WindowStyle Hidden -RedirectStandardError $OUTFILE_STDERR -RedirectStandardOutput $OUTFILE_STDOUT -PassThru
  141. echo "Verifying $AMBARI_AGENT process status..."
  142. if (!$process){
  143. echo "ERROR: $AMBARI_AGENT start failed"
  144. $host.SetShouldExit(-1)
  145. exit
  146. }
  147. echo "Agent stdout at: $OUTFILE_STDOUT"
  148. echo "Agent stderr at: $OUTFILE_STDERR"
  149. echo "Agent log at: $LOGFILE"
  150. $process.WaitForExit()
  151. }
  152. Function _stop($cmd_args){
  153. echo "Stopping $AMBARI_SVC_NAME..."
  154. _pstart_brief($cmd_args)
  155. $cnt = 0
  156. do
  157. {
  158. Start-Sleep -Milliseconds 250
  159. $svc = Get-Service -Name $AMBARI_SVC_NAME
  160. $cnt += 1
  161. if ($cnt -eq 40)
  162. {
  163. echo "$AMBARI_SVC_NAME still stopping...".
  164. return
  165. }
  166. }
  167. until($svc.status -eq "Stopped")
  168. echo "$AMBARI_SVC_NAME is stopped"
  169. }
  170. Function _status($cmd_args){
  171. echo "Getting $AMBARI_SVC_NAME status..."
  172. _pstart_brief($cmd_args)
  173. }
  174. # check for python before any action
  175. _detect_python
  176. switch ($($args[0])){
  177. "start"
  178. {
  179. _start $args
  180. }
  181. "debug"
  182. {
  183. echo "Starting ambari-agent"
  184. _pstart_ioredir $args
  185. echo "Ambari Agent finished"
  186. }
  187. "stop" {_stop $args}
  188. "restart"
  189. {
  190. _stop @("stop")
  191. _start @("start")
  192. }
  193. "status" {_status $args}
  194. "setup"
  195. {
  196. echo "Installing ambari-agent"
  197. _pstart $args
  198. echo "Ambari Agent installation finished"
  199. }
  200. default
  201. {
  202. echo "Usage: ambari-agent {start|stop|restart|setup|status} [options]"
  203. echo "Use ambari-agent <action> --help to get details on options available."
  204. echo "Or, simply invoke ambari-agent.py --help to print the options."
  205. $retcode=1
  206. }
  207. }
  208. $host.SetShouldExit($retcode)
  209. exit