createservice.ps1 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. param(
  16. [String]
  17. [Parameter(Mandatory=$true )]
  18. $username,
  19. [String]
  20. [Parameter(Mandatory=$true )]
  21. $password,
  22. [String]
  23. [Parameter(Mandatory=$true )]
  24. $servicename,
  25. [String]
  26. [Parameter(Mandatory=$true )]
  27. $hdpResourcesDir,
  28. [String]
  29. [Parameter(Mandatory=$true )]
  30. $servicecmdpath
  31. )
  32. function Invoke-Cmd ($command)
  33. {
  34. Write-Output $command
  35. $out = cmd.exe /C "$command" 2>&1
  36. Write-Output $out
  37. return $out
  38. }
  39. function Invoke-CmdChk ($command)
  40. {
  41. Write-Output $command
  42. $out = cmd.exe /C "$command" 2>&1
  43. Write-Output $out
  44. if (-not ($LastExitCode -eq 0))
  45. {
  46. throw "Command `"$out`" failed with exit code $LastExitCode "
  47. }
  48. return $out
  49. }
  50. ### Stops and deletes the Hadoop service.
  51. function StopAndDeleteHadoopService(
  52. [String]
  53. [Parameter( Position=0, Mandatory=$true )]
  54. $service
  55. )
  56. {
  57. Write-Output "Stopping $service"
  58. $s = Get-Service $service -ErrorAction SilentlyContinue
  59. if( $s -ne $null )
  60. {
  61. Stop-Service $service
  62. $cmd = "sc.exe delete $service"
  63. Invoke-Cmd $cmd
  64. }
  65. }
  66. # Convenience method for processing command-line credential objects
  67. # Assumes $credentialsHash is a hash with one of the following being true:
  68. # - keys "username" and "password"/"passwordBase64" are set to strings
  69. # - key "credentialFilePath" is set to the path of a serialized PSCredential object
  70. function Get-HadoopUserCredentials($credentialsHash)
  71. {
  72. Write-Output "Using provided credentials for username $($credentialsHash["username"])" | Out-Null
  73. $username = $credentialsHash["username"]
  74. if($username -notlike "*\*")
  75. {
  76. $username = "$ENV:COMPUTERNAME\$username"
  77. }
  78. $securePassword = $credentialsHash["password"] | ConvertTo-SecureString -AsPlainText -Force
  79. $creds = New-Object System.Management.Automation.PSCredential $username, $securePassword
  80. return $creds
  81. }
  82. ### Creates and configures the service.
  83. function CreateAndConfigureHadoopService(
  84. [String]
  85. [Parameter( Position=0, Mandatory=$true )]
  86. $service,
  87. [String]
  88. [Parameter( Position=1, Mandatory=$true )]
  89. $hdpResourcesDir,
  90. [String]
  91. [Parameter( Position=2, Mandatory=$true )]
  92. $serviceBinDir,
  93. [String]
  94. [Parameter( Position=3, Mandatory=$true )]
  95. $servicecmdpath,
  96. [System.Management.Automation.PSCredential]
  97. [Parameter( Position=4, Mandatory=$true )]
  98. $serviceCredential
  99. )
  100. {
  101. if ( -not ( Get-Service "$service" -ErrorAction SilentlyContinue ) )
  102. {
  103. Write-Output "Creating service `"$service`" as $serviceBinDir\$service.exe"
  104. $xcopyServiceHost_cmd = "copy /Y `"$hdpResourcesDir\namenode.exe`" `"$serviceBinDir\$service.exe`""
  105. Invoke-CmdChk $xcopyServiceHost_cmd
  106. #HadoopServiceHost.exe will write to this log but does not create it
  107. #Creating the event log needs to be done from an elevated process, so we do it here
  108. if( -not ([Diagnostics.EventLog]::SourceExists( "$service" )))
  109. {
  110. [Diagnostics.EventLog]::CreateEventSource( "$service", "" )
  111. }
  112. Write-Output "Adding service $service"
  113. if ($serviceCredential.Password.get_Length() -ne 0)
  114. {
  115. $s = New-Service -Name "$service" -BinaryPathName "$serviceBinDir\$service.exe" -Credential $serviceCredential -DisplayName "Apache Hadoop $service"
  116. if ( $s -eq $null )
  117. {
  118. throw "CreateAndConfigureHadoopService: Service `"$service`" creation failed"
  119. }
  120. }
  121. else
  122. {
  123. # Separately handle case when password is not provided
  124. # this path is used for creating services that run under (AD) Managed Service Account
  125. # for them password is not provided and in that case service cannot be created using New-Service commandlet
  126. $serviceUserName = $serviceCredential.UserName
  127. $cred = $serviceCredential.UserName.Split("\")
  128. # Throw exception if domain is not specified
  129. if (($cred.Length -lt 2) -or ($cred[0] -eq "."))
  130. {
  131. throw "Environment is not AD or domain is not specified"
  132. }
  133. $cmd="$ENV:WINDIR\system32\sc.exe create `"$service`" binPath= `"$serviceBinDir\$service.exe`" obj= $serviceUserName DisplayName= `"Apache Hadoop $service`" "
  134. try
  135. {
  136. Invoke-CmdChk $cmd
  137. }
  138. catch
  139. {
  140. throw "CreateAndConfigureHadoopService: Service `"$service`" creation failed"
  141. }
  142. }
  143. $cmd="$ENV:WINDIR\system32\sc.exe failure $service reset= 30 actions= restart/5000"
  144. Invoke-CmdChk $cmd
  145. $cmd="$ENV:WINDIR\system32\sc.exe config $service start= demand"
  146. Invoke-CmdChk $cmd
  147. Write-Output "Creating service config ${serviceBinDir}\$service.xml"
  148. $cmd = "$servicecmdpath --service $service > `"$serviceBinDir\$service.xml`""
  149. Invoke-CmdChk $cmd
  150. }
  151. else
  152. {
  153. Write-Output "Service `"$service`" already exists, Removing `"$service`""
  154. StopAndDeleteHadoopService $service
  155. CreateAndConfigureHadoopService $service $hdpResourcesDir $serviceBinDir $servicecmdpath $serviceCredential
  156. }
  157. }
  158. try
  159. {
  160. Write-Output "Creating credential object"
  161. ###
  162. ### Create the Credential object from the given username and password or the provided credentials file
  163. ###
  164. $serviceCredential = Get-HadoopUserCredentials -credentialsHash @{"username" = $username; "password" = $password}
  165. $username = $serviceCredential.UserName
  166. Write-Output "Username: $username"
  167. Write-Output "Creating service $service"
  168. ###
  169. ### Create Service
  170. ###
  171. CreateAndConfigureHadoopService $servicename $hdpResourcesDir $hdpResourcesDir $servicecmdpath $serviceCredential
  172. Write-Output "Done"
  173. }
  174. catch
  175. {
  176. Write-Output "Failure"
  177. exit 1
  178. }