summaryrefslogtreecommitdiff
path: root/ambari-agent/conf/windows/createservice.ps1
blob: f3936e2851eece7ca0369e2cd2f52ed1610582f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License

param(
  [String]
  [Parameter(Mandatory=$true )]
  $username,
  [String]
  [Parameter(Mandatory=$true )]
  $password,
  [String]
  [Parameter(Mandatory=$true )]
  $servicename,
  [String]
  [Parameter(Mandatory=$true )]
  $hdpResourcesDir,
  [String]
  [Parameter(Mandatory=$true )]
  $servicecmdpath
  )

function Invoke-Cmd ($command)
{
  Write-Output $command
  $out = cmd.exe /C "$command" 2>&1
  Write-Output $out
  return $out
}

function Invoke-CmdChk ($command)
{
  Write-Output $command
  $out = cmd.exe /C "$command" 2>&1
  Write-Output $out
  if (-not ($LastExitCode  -eq 0))
  {
    throw "Command `"$out`" failed with exit code $LastExitCode "
  }
  return $out
}

### Stops and deletes the Hadoop service.
function StopAndDeleteHadoopService(
  [String]
  [Parameter( Position=0, Mandatory=$true )]
  $service
)
{
  Write-Output "Stopping $service"
  $s = Get-Service $service -ErrorAction SilentlyContinue

  if( $s -ne $null )
  {
    Stop-Service $service
    $cmd = "sc.exe delete $service"
    Invoke-Cmd $cmd
  }
}

# Convenience method for processing command-line credential objects
# Assumes $credentialsHash is a hash with one of the following being true:
#  - keys "username" and "password"/"passwordBase64" are set to strings
#  - key "credentialFilePath" is set to the path of a serialized PSCredential object
function Get-HadoopUserCredentials($credentialsHash)
{
  Write-Output "Using provided credentials for username $($credentialsHash["username"])" | Out-Null
  $username = $credentialsHash["username"]
  if($username -notlike "*\*")
  {
    $username = "$ENV:COMPUTERNAME\$username"
  }
  $securePassword = $credentialsHash["password"] | ConvertTo-SecureString -AsPlainText -Force
  $creds = New-Object System.Management.Automation.PSCredential $username, $securePassword
  return $creds
}

### Creates and configures the service.
function CreateAndConfigureHadoopService(
  [String]
  [Parameter( Position=0, Mandatory=$true )]
  $service,
  [String]
  [Parameter( Position=1, Mandatory=$true )]
  $hdpResourcesDir,
  [String]
  [Parameter( Position=2, Mandatory=$true )]
  $serviceBinDir,
  [String]
  [Parameter( Position=3, Mandatory=$true )]
  $servicecmdpath,
  [System.Management.Automation.PSCredential]
  [Parameter( Position=4, Mandatory=$true )]
  $serviceCredential
)
{
  if ( -not ( Get-Service "$service" -ErrorAction SilentlyContinue ) )
  {
    Write-Output "Creating service `"$service`" as $serviceBinDir\$service.exe"
    $xcopyServiceHost_cmd = "copy /Y `"$hdpResourcesDir\namenode.exe`" `"$serviceBinDir\$service.exe`""
    Invoke-CmdChk $xcopyServiceHost_cmd

    #HadoopServiceHost.exe will write to this log but does not create it
    #Creating the event log needs to be done from an elevated process, so we do it here
    if( -not ([Diagnostics.EventLog]::SourceExists( "$service" )))
    {
      [Diagnostics.EventLog]::CreateEventSource( "$service", "" )
    }
    Write-Output "Adding service $service"
    if ($serviceCredential.Password.get_Length() -ne 0)
    {
      $s = New-Service -Name "$service" -BinaryPathName "$serviceBinDir\$service.exe" -Credential $serviceCredential -DisplayName "Apache Hadoop $service"
      if ( $s -eq $null )
      {
        throw "CreateAndConfigureHadoopService: Service `"$service`" creation failed"
      }
    }
    else
    {
      # Separately handle case when password is not provided
      # this path is used for creating services that run under (AD) Managed Service Account
      # for them password is not provided and in that case service cannot be created using New-Service commandlet
      $serviceUserName = $serviceCredential.UserName
      $cred = $serviceCredential.UserName.Split("\")

      # Throw exception if domain is not specified
      if (($cred.Length -lt 2) -or ($cred[0] -eq "."))
      {
        throw "Environment is not AD or domain is not specified"
      }

      $cmd="$ENV:WINDIR\system32\sc.exe create `"$service`" binPath= `"$serviceBinDir\$service.exe`" obj= $serviceUserName DisplayName= `"Apache Hadoop $service`" "
      try
      {
        Invoke-CmdChk $cmd
      }
      catch
      {
        throw "CreateAndConfigureHadoopService: Service `"$service`" creation failed"
      }
    }

    $cmd="$ENV:WINDIR\system32\sc.exe failure $service reset= 30 actions= restart/5000"
    Invoke-CmdChk $cmd

    $cmd="$ENV:WINDIR\system32\sc.exe config $service start= demand"
    Invoke-CmdChk $cmd


    Write-Output "Creating service config ${serviceBinDir}\$service.xml"
    $cmd = "$servicecmdpath --service $service > `"$serviceBinDir\$service.xml`""
    Invoke-CmdChk $cmd
  }
  else
  {
    Write-Output "Service `"$service`" already exists, Removing `"$service`""
    StopAndDeleteHadoopService $service
    CreateAndConfigureHadoopService $service $hdpResourcesDir $serviceBinDir $servicecmdpath $serviceCredential
  }
}


try
{
  Write-Output "Creating credential object"
  ###
  ### Create the Credential object from the given username and password or the provided credentials file
  ###
  $serviceCredential = Get-HadoopUserCredentials -credentialsHash @{"username" = $username; "password" = $password}
  $username = $serviceCredential.UserName
  Write-Output "Username: $username"

  Write-Output "Creating service $service"
  ###
  ### Create Service
  ###
  CreateAndConfigureHadoopService $servicename $hdpResourcesDir $hdpResourcesDir $servicecmdpath $serviceCredential
  Write-Output "Done"
}
catch
{
  Write-Output "Failure"
  exit 1
}