InstallHubot.psm1

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Defines the values for the resource's Ensure property.
enum Ensure
{
    # The resource must be absent.
    Absent
    # The resource must be present.
    Present
}

class HubotHelpers
{
    [string] RefreshPathVariable ()
    {
        $updatedPath = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
        return $updatedPath
    }

    [bool] CheckPathExists ([string]$Path)
    {
        if (Test-Path -Path $Path)
        {
            Write-Verbose "Directory $($Path) exists."
            return $true
        }
        else
        {
            Write-Verbose "Directory $($Path) exists."
            return $false
        }
    }

    [PSCustomObject] RunProcess([string]$FilePath, [string]$ArgumentList, [string]$WorkingDirectory)
    {
        $env:Path = [HubotHelpers]::new().RefreshPathVariable()

        $pinfo = New-Object System.Diagnostics.ProcessStartInfo

        if (-not([string]::IsNullOrEmpty($WorkingDirectory)))
        {
            $pinfo.WorkingDirectory = $WorkingDirectory
        }

        $pinfo.FileName = $FilePath
        $pinfo.RedirectStandardError = $true
        $pinfo.RedirectStandardOutput = $true
        $pinfo.UseShellExecute = $false
        $pinfo.StandardOutputEncoding = [System.Text.Encoding]::Unicode
        $pinfo.StandardErrorEncoding = [System.Text.Encoding]::Unicode
        $pinfo.Arguments = $ArgumentList
        $p = New-Object System.Diagnostics.Process
        $p.StartInfo = $pinfo
        $p.Start() | Out-Null
        $p.WaitForExit()
        $stdout = $p.StandardOutput.ReadToEnd()
        $stderr = $p.StandardError.ReadToEnd()

        $output = @{}
        $output.filepath = $FilePath
        $output.arg = $ArgumentList
        $output.workingdirectory = $WorkingDirectory
        $output.stdout = $stdout
        $output.stderr = $stderr
        $output.exitcode = $p.ExitCode

        $returnObj =  New-Object -Property $output -TypeName PSCustomObject
        Write-Verbose $returnObj.stdout
        return $returnObj
    }

    [string] GetNSSMPath ()
    {
        if (Test-Path -Path 'C:\nssm')
        {
            # get latest version installed
            $path = ((Get-ChildItem -Path C:\nssm\*\win64\nssm.exe)[-1]).FullName
            Write-Verbose "Found nssm at $($path)"
            return $path
        }
        else
        {
            throw 'NSSM folder cannot be found at C:\nssm'
        } 
    }
}

[DscResource()]
class HubotInstall
{

    # A DSC resource must define at least one key property.
    [DscProperty(Key)]
    [string]$BotPath

    [DscProperty(Mandatory)]
    [Ensure]$Ensure

    [DscProperty(NotConfigurable)]
    [String]$NodeModulesPath

    # Gets the resource's current state.
    [HubotInstall] Get()
    {
        $GetObject = [HubotInstall]::new()
        $GetObject.BotPath = $this.BotPath
        $GetObject.Ensure = $this.Ensure
        $GetObject.NodeModulesPath = Join-Path -Path $this.BotPath -ChildPath 'node_modules'


        if([HubotHelpers]::new().CheckPathExists($GetObject.NodeModulesPath))
        {
            $GetObject.Ensure = [Ensure]::Present
        }
        else
        {
            $GetObject.Ensure = [Ensure]::Absent
        }


        return $GetObject
    }

    # Sets the desired state of the resource.
    [void] Set()
    {
        $Helpers = [HubotHelpers]::new()
        
        $env:Path = $Helpers.RefreshPathVariable()              

        if (!($Helpers.CheckPathExists($this.BotPath)))
        {
            throw "The path $($this.BotPath) must exist and contain a Hubot installation in it. You can clone one from here: https://github.com/MattHodge/HubotWindows"
        }

        if (Get-Command -CommandType Application -Name npm -ErrorAction SilentlyContinue)
        {
            $npmPath = (Get-Command -CommandType Application -Name npm)[0].Source
        }
        else
        {
            throw "npm cannot be found. Cannot continue."
        }


        if ($this.Ensure -eq [Ensure]::Present)
        {
            $npmCmd = 'install'
        }
        else
        {
            $npmCmd = 'uninstall'
        }

        Write-Verbose -Message "$($npmCmd)ing CoffeeScript at $($this.BotPath)"

        Start-Process -FilePath $npmPath -ArgumentList "$($npmCmd) coffee-script" -Wait

        Write-Verbose "$($npmCmd)ing all required npm modules"

        Start-Process -FilePath $npmPath -ArgumentList $npmCmd -Wait

        if ($this.Ensure -eq [Ensure]::Absent)
        {
            Remove-Item -Path $this.NodeModulesPath -Force
        }
    }

    # Tests if the resource is in the desired state.
    [bool] Test()
    {
        $TestObject = $This.Get()

        # present case
        if ($TestObject.Ensure -eq [Ensure]::Present)
        {
            return $true
        }
        # absent case
        else
        {
            return $false
        }
    }
}

[DscResource()]
class HubotInstallService
{

    # Path where the Hubot is located
    [DscProperty(Key)]
    [string]$BotPath

    # Name for the Hubot service
    [DscProperty(Mandatory)]
    [string]$ServiceName

    # Credential to run the service under
    [DscProperty()]
    [PSCredential]$Credential

    # Bot adapter for Hubot to be used. Used as a paramater to start the server (-a $botadapter)
    [DscProperty(Mandatory)]
    [string]$BotAdapter

    [DscProperty(Mandatory)]
    [Ensure]$Ensure

    [DscProperty(NotConfigurable)]
    [Boolean]$State_ServiceExists

    [DscProperty(NotConfigurable)]
    [Boolean]$State_ServiceRunning

    [DscProperty(NotConfigurable)]
    [string]$NSSMAppParameters

    [DscProperty(NotConfigurable)]
    [string[]]$NSSMCmdsToRun

    [DscProperty(NotConfigurable)]
    [string]$BotLoggingPath

    [DscProperty(NotConfigurable)]
    [Boolean]$State_NSSMAppParameters

    # Gets the resource's current state.
    [HubotInstallService] Get()
    {
        $Helpers = [HubotHelpers]::new()
        
        $GetObject = [HubotInstallService]::new()
        $GetObject.BotPath = $this.BotPath
        $GetObject.Ensure = $this.Ensure
        $GetObject.ServiceName = $this.ServiceName
        $GetObject.Credential = $this.Credential
        $GetObject.BotAdapter = $this.BotAdapter
        $GetObject.NSSMAppParameters = "/c .\bin\hubot.cmd -a $($this.BotAdapter)"

        # set default states to save having nested if statements
        $GetObject.State_ServiceExists = $false
        $GetObject.State_ServiceRunning = $false
        $GetObject.State_NSSMAppParameters = $false

        if (Get-Service -Name $this.ServiceName -ErrorAction SilentlyContinue)
        {
            $GetObject.State_ServiceExists = $true

            # Check service is running
            if ((Get-Service -Name $this.ServiceName).Status -eq 'Running')
            {
                $GetObject.State_ServiceRunning = $true
            }

            $nssmPath = $Helpers.GetNSSMPath()

            # check if appparams set correctly
            $currentAppParams = ($Helpers.RunProcess($nssmPath,"get $($this.ServiceName) AppParameters",$null)).stdout

            # need to use trim to remove white spaces
            if ([string]$currentAppParams.Trim() -eq [string]$GetObject.NSSMAppParameters)
            {
                $GetObject.State_NSSMAppParameters = $true
            }
        }

        # get the bot logging path
        $GetObject.BotLoggingPath = Join-Path -Path $GetObject.BotPath -ChildPath 'Logs'

        # build an array of NSSM Commands to execute based upon user input
        $GetObject.NSSMCmdsToRun = @(
            "install $($this.ServiceName) cmd.exe"
            "set $($this.ServiceName) AppDirectory $($GetObject.BotPath)"
            "set $($this.ServiceName) AppParameters ""/c .\bin\hubot.cmd -a $($GetObject.BotAdapter)"""
            "set $($this.ServiceName) AppStdout ""$($GetObject.BotLoggingPath)\$($GetObject.ServiceName)_log.txt"""
            "set $($this.ServiceName) AppStderr ""$($GetObject.BotLoggingPath)\$($GetObject.ServiceName)_error.txt"""
            "set $($this.ServiceName) AppDirectory $($GetObject.BotPath)"
            "set $($this.ServiceName) AppRotateFiles 1"
            "set $($this.ServiceName) AppRotateOnline 1"
            "set $($this.ServiceName) AppRotateSeconds 86400"
            "set $($this.ServiceName) Description Hubot Service"
            "set $($this.ServiceName) Start SERVICE_AUTO_START"
        )

        # if a credetial is passed with no password assume LocalSystem
        if ([string]::IsNullOrEmpty($GetObject.Credential))
        {
            Write-Verbose "No credential passed, using LocalSystem."
            $GetObject.NSSMCmdsToRun += "set $($GetObject.ServiceName) ObjectName LocalSystem"
        }
        # if a credential is passed with a password
        else
        {
            Write-Verbose "Credential passed, using username $($GetObject.Credential.UserName)."
            $GetObject.NSSMCmdsToRun += "set $($GetObject.ServiceName) ObjectName .\$($GetObject.Credential.UserName) $($GetObject.Credential.GetNetworkCredential().Password)"
        }

        return $GetObject
    }

    [void] Set()
    {
        $Helpers = [HubotHelpers]::new()

        $env:Path = $Helpers.RefreshPathVariable()

        $TestObject = $This.Get()
        
        $nssmPath = $Helpers.GetNSSMPath()

        if ($this.Ensure -eq [Ensure]::Present)
        {
            
            if ($TestObject.State_ServiceExists)
            {
                Write-Verbose "Removing old service"
                Stop-Service -Name $this.ServiceName -Force
                $Helpers.RunProcess($nssmPath,"remove $($this.ServiceName) confirm",$null) | Out-Null
            }

            # Create bot logging path
            Write-Verbose "Creating bot logging path at $($TestObject.BotLoggingPath)"
            New-Item -Path $TestObject.BotLoggingPath -Force -ItemType Directory

            ForEach ($cmd in $TestObject.NSSMCmdsToRun)
            {               
                $Helpers.RunProcess($nssmPath,$cmd,$null) | Out-Null
            }
            
            Start-Service -Name $this.ServiceName
        }
        else
        {
            Write-Verbose "Removing Bot Service $($this.ServiceName)"
            Stop-Service -Name $this.ServiceName -Force -ErrorAction SilentlyContinue
            $Helpers.RunProcess($nssmPath,"remove $($this.ServiceName) confirm",$null) | Out-Null
        }
    }

    # Tests if the resource is in the desired state.
    [bool] Test()
    {
        $TestObject = $This.Get()

        # present case
        if ($this.Ensure -eq [Ensure]::Present)
        {
            # If any of the possible states for the service are false, not in desired state
            return (-not($TestObject.psobject.Properties.Where({$PSItem.Name -like 'State_*'}).Value -contains $false))
        }
        # absent case
        else
        {
            return (-not($TestObject.State_ServiceExists))
        }
    }
}