/
iis.go
820 lines (686 loc) · 24.1 KB
/
iis.go
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
package iis
import (
"bufio"
"bytes"
"encoding/xml"
"fmt"
"math"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
wmi "github.com/StackExchange/wmi"
)
// Application Pool schema given from appcmd.exe
type appCmdAppPool struct {
Name string `xml:"APPPOOL.NAME,attr"`
PipelineMode string `xml:"PipelineMode,attr"`
RuntimeVersion string `xml:"RuntimeVersion,attr"`
State string `xml:"state,attr"`
Add appPoolAdd `xml:"add"`
}
// An Application Pool's inner schema to describe the ApplicationPool given from appcmd.exe
type appPoolAdd struct {
Name string `xml:"name,attr"`
QueueLength int `xml:"queueLength,attr"`
AutoStart bool `xml:"autoStart,attr"`
ProcessModel appPoolProcessModel `xml:"processModel"`
}
// An Application Pool's ProcessModel schema given from appcmd.exe
type appPoolProcessModel struct {
IdentityType string `xml:"identityType,attr"`
Password string `xml:"password,attr"`
Username string `xml:"userName,attr"`
}
// AppCmd message schema used for errors and messages
type appCmdMessage struct {
Message string `xml:"message,attr"`
}
// AppCmd schema for all results
type appCmdResult struct {
AppPools []appCmdAppPool `xml:"APPPOOL"`
Errors []appCmdMessage `xml:"ERROR"`
Sites []appCmdSite `xml:"SITE"`
Statuses []appCmdMessage `xml:"STATUS"`
WorkerProcesses []appCmdWP `xml:"WP"`
XMLName xml.Name `xml:"appcmd"`
}
// Site schema given from appcmd.exe
type appCmdSite struct {
Bindings string `xml:"bindings,attr"`
ID int `xml:"SISTE.ID,attr"`
Name string `xml:"SITE.NAME,attr"`
State string `xml:"state,attr"`
Site site `xml:"site"`
}
// Nested Site schema given from appcmd.exe
type site struct {
Name string `xml:"name,attr"`
ID string `xml:"id,attr"`
Application siteApplication `xml:"application"`
}
// A Site's Application schema given from appcmd.exe
type siteApplication struct {
Path string `xml:"path,attr"`
ApplicationPool string `xml:"applicationPool,attr"`
VDirs []siteVDir `xml:"virtualDirectory"`
}
// A Site's Virtual Directory schema given from appcmd.exe
type siteVDir struct {
Path string `xml:"path,attr"`
PhysicalPath string `xml:"physicalPath,attr"`
}
// Worker Process schema given from appcmd.exe
type appCmdWP struct {
AppPoolName string `xml:"APPPOOL.NAME,attr"`
Name string `xml:"WP.NAME,attr"`
}
// IIS Identity used for an Application Pool
type iisAppPoolIdentity struct {
Identity string `codec:"identity"`
Password string `codec:"username"`
Username string `codec:"password"`
}
// IIS Binding struct to match
type iisBinding struct {
CertHash string `codec:"cert_hash"`
HostName string `codec:"hostname"`
IPAddress string `codec:"ipaddress"`
Port int `codec:"port"`
ResourcePort string `codec:"resource_port"`
Type string `codec:"type"`
}
// Stat fields that are unmarshalled from WMI
type wmiProcessStats struct {
KernelModeTime uint64
UserModeTime uint64
WorkingSetPrivate uint64
}
// Gets the exe version of InetMgr.exe
func getVersion() (string, error) {
cmd := exec.Command("cmd", "/C", `wmic datafile where name='C:\\Windows\\System32\\inetsrv\\InetMgr.exe' get version`)
if out, err := cmd.Output(); err != nil {
return "", fmt.Errorf("Failed to determine version: %v", err)
} else {
if output := strings.Fields(string(out)); len(output) != 2 {
return "", fmt.Errorf("Did not receive proper version formatting")
} else {
return output[1], nil
}
}
}
// Returns if the IIS service is running in Windows Service Controller (SC)
func isIISRunning() (bool, error) {
cmd := exec.Command(`C:\Windows\System32\sc.exe`, "query", "w3svc")
if out, err := cmd.CombinedOutput(); err != nil {
return false, err
} else {
return regexp.MatchString(`STATE.*:.*4.*RUNNING`, string(out))
}
}
// Removes all Application Pools and Sites from IIS
func purgeIIS() error {
if sites, err := getSites(); err != nil {
return err
} else {
for _, site := range sites {
if err = deleteSite(site.Name); err != nil {
return err
}
}
}
if appPools, err := getAppPools(); err != nil {
return err
} else {
for _, appPool := range appPools {
if err = deleteAppPool(appPool.Name); err != nil {
return err
}
}
}
return nil
}
// Starts the IIS service in Windows SC
func startIIS() error {
if isRunning, err := isIISRunning(); err != nil || isRunning {
return err
}
cmd := exec.Command(`C:\Windows\System32\sc.exe`, "start", "w3svc")
if _, err := cmd.Output(); err != nil {
return err
}
return nil
}
// Stops the IIS service in Windows SC
func stopIIS() error {
if isRunning, err := isIISRunning(); err != nil || !isRunning {
return err
}
cmd := exec.Command(`C:\Windows\System32\sc.exe`, "stop", "w3svc")
if _, err := cmd.Output(); err != nil {
return err
}
return nil
}
// Executes appcmd.exe with the given arguments and returns a structured result or error
func executeAppCmd(arg ...string) (appCmdResult, error) {
return executeAppCmdWithInput("", arg...)
}
// Executes appcmd.exe with the given arguments along with an xml path file for input and returns a structured result or error
func executeAppCmdWithInput(importXmlPath string, arg ...string) (appCmdResult, error) {
var result appCmdResult
var cmd *exec.Cmd
arg = append(arg, "/xml")
if importXmlPath != "" {
arg = append([]string{"/C", `C:\Windows\System32\inetsrv\APPCMD.exe`}, append(arg, fmt.Sprintf("/in<%s", importXmlPath))...)
cmd = exec.Command("cmd", arg...)
} else {
cmd = exec.Command(`C:\Windows\System32\inetsrv\APPCMD.exe`, arg...)
}
if out, err := cmd.Output(); err != nil {
// Attempt to parse output for verbose error messages in xml, otherwise return error code
// If an appcmd xml is parsed successfully, then accept that as source of error truth
if xmlErr := xml.Unmarshal(out, &result); xmlErr == nil {
if len(result.Errors) != 0 {
return result, fmt.Errorf(result.Errors[0].Message)
}
return result, nil
}
return result, err
} else {
xml.Unmarshal(out, &result)
return result, nil
}
}
// Applies the Application Pool identity user settings
func applyAppPoolIdentity(appPoolName string, appPoolIdentity iisAppPoolIdentity) error {
properties := []string{"set", "config", "/section:applicationPools"}
if appPoolIdentity.Identity != "" {
properties = append(properties, fmt.Sprintf("/[name='%s'].processModel.identityType:%s", appPoolName, appPoolIdentity.Identity))
}
if appPoolIdentity.Identity == "SpecificUser" && appPoolIdentity.Username != "" && appPoolIdentity.Password != "" {
properties = append(properties, fmt.Sprintf("/[name='%s'].processModel.userName:%s", appPoolName, appPoolIdentity.Username))
properties = append(properties, fmt.Sprintf("/[name='%s'].processModel.password:%s", appPoolName, appPoolIdentity.Password))
}
if _, err := executeAppCmd(properties...); err != nil {
return fmt.Errorf("Failed to set Application Pool identity: %v", err)
}
return nil
}
// Applies a shutdown timeout in secs to ensure worker processes are terminated in a timely fashion
func applyAppPoolShutdownTimeout(appPoolName string, timeout time.Duration) error {
hrs := math.Floor(timeout.Hours())
mins := math.Floor(timeout.Minutes())
secs := math.Floor(timeout.Seconds())
timeoutStr := fmt.Sprintf("%02d:%02d:%02d", int32(hrs), int32(mins), int32(secs))
if _, err := executeAppCmd("set", "config", "/section:applicationPools", fmt.Sprintf("/[name='%s'].processModel.shutdownTimeLimit:%s", appPoolName, timeoutStr)); err != nil {
return fmt.Errorf("Failed to set Application Pool shutdown timeout: %v", err)
}
return nil
}
// Creates an Application Pool with the given name and applies an IIS exported Application Pool xml if a path is provided
func createAppPool(appPoolName string, configPath string) error {
if exists, err := doesAppPoolExist(appPoolName); err != nil || exists {
return err
}
properties := []string{"add", "apppool", fmt.Sprintf("/name:%s", appPoolName)}
if _, err := executeAppCmdWithInput(configPath, properties...); err != nil {
return fmt.Errorf("Failed to create Application Pool: %v", err)
}
return nil
}
// Deletes an Application Pool with the given name
func deleteAppPool(appPoolName string) error {
if exists, err := doesAppPoolExist(appPoolName); err != nil || !exists {
return err
}
if _, err := executeAppCmd("delete", "apppool", appPoolName); err != nil {
return fmt.Errorf("Failed to delete Application Pool: %v", err)
}
return nil
}
// Returns if an Application Pool with the given name exists in IIS
func doesAppPoolExist(appPoolName string) (bool, error) {
if appPool, err := getAppPool(appPoolName, false); err != nil || appPool == nil {
return false, err
}
return true, nil
}
// Returns an Application Pool with the given name
func getAppPool(appPoolName string, allConfigs bool) (*appCmdAppPool, error) {
args := []string{"list", "apppool", appPoolName}
if allConfigs {
args = append(args, "/config:*")
}
if result, err := executeAppCmd(args...); err != nil {
return nil, fmt.Errorf("Failed to get Application Pool: %v", err)
} else if len(result.AppPools) == 0 {
return nil, nil
} else {
return &result.AppPools[0], nil
}
}
// Returns all Application Pools that exist in IIS
func getAppPools() ([]appCmdAppPool, error) {
if result, err := executeAppCmd("list", "apppool"); err != nil {
return nil, fmt.Errorf("Failed to get Application Pools: %v", err)
} else {
return result.AppPools, nil
}
}
// Returns if an Application Pool with the given name is started in IIS
func isAppPoolStarted(appPoolName string) (bool, error) {
if appPool, err := getAppPool(appPoolName, false); err != nil || appPool == nil {
return false, err
} else {
return strings.ToLower(appPool.State) == "started", nil
}
}
// Starts an Application Pool with the given name in IIS
func startAppPool(appPoolName string) error {
if isStarted, err := isAppPoolStarted(appPoolName); err != nil || isStarted {
return err
}
if _, err := executeAppCmd("start", "apppool", appPoolName); err != nil {
return fmt.Errorf("Failed to start Application Pool: %v", err)
}
return nil
}
// Stops an Application Pool with the given name in IIS
func stopAppPool(appPoolName string) error {
if isStarted, err := isAppPoolStarted(appPoolName); err != nil || !isStarted {
return err
}
if _, err := executeAppCmd("stop", "apppool", appPoolName); err != nil {
return fmt.Errorf("Failed to stop Application Pool: %v", err)
}
return nil
}
// Applies the Site bindings
func applySiteBindings(siteName string, bindings []iisBinding) error {
site, err := getSite(siteName, false)
if err != nil {
return err
}
currentBindings, err := site.getBindings()
if err != nil {
return err
}
properties := []string{"set", "site", siteName}
// Compare current bindings with desired bindings
// Remove any bindings that exist in both arrays
var exists bool
cIndex := 0
for _, currentBinding := range currentBindings {
exists = false
for index, binding := range bindings {
if binding.IPAddress == "" {
binding.IPAddress = "*"
}
if currentBinding.Type == binding.Type && currentBinding.IPAddress == binding.IPAddress && currentBinding.Port == binding.Port && currentBinding.HostName == binding.HostName {
exists = true
bindings[index] = bindings[len(bindings)-1]
bindings = bindings[:len(bindings)-1]
break
}
}
if !exists {
currentBindings[cIndex] = currentBinding
cIndex++
}
}
currentBindings = currentBindings[:cIndex]
// Nothing is changed if there are no bindings to update
if len(currentBindings) == 0 && len(bindings) == 0 {
return nil
}
// Remove any bindings that are not desired
for _, binding := range currentBindings {
if binding.Type == "https" {
bindingInfo, err := getSSLCertBinding(binding.IPAddress, binding.Port)
if len(bindingInfo) != 0 {
if err = unbindSSLCert(binding.IPAddress, binding.Port); err != nil {
return err
}
}
}
properties = append(properties, fmt.Sprintf("/-bindings.[protocol='%s',bindingInformation='%s:%d:%s']", binding.Type, binding.IPAddress, binding.Port, binding.HostName))
}
// Add bindings that are desired
for _, binding := range bindings {
if binding.Type == "https" {
if binding.CertHash == "" {
return fmt.Errorf("HTTPS binding used, but no cert hash was supplied!")
}
bindingInfo, err := getSSLCertBinding(binding.IPAddress, binding.Port)
if len(bindingInfo) != 0 && bindingInfo["CertificateHash"] != binding.CertHash {
if err = unbindSSLCert(binding.IPAddress, binding.Port); err != nil {
return err
}
}
if err = bindSSLCert(siteName, binding.IPAddress, binding.Port, binding.CertHash); err != nil {
return err
}
}
if binding.IPAddress == "" {
binding.IPAddress = "*"
}
properties = append(properties, fmt.Sprintf("/+bindings.[protocol='%s',bindingInformation='%s:%d:%s']", binding.Type, binding.IPAddress, binding.Port, binding.HostName))
}
if _, err := executeAppCmd(properties...); err != nil {
return fmt.Errorf("Failed to set Site bindings: %v", err)
}
return nil
}
// Creates a Site with the given name and applies an IIS exported Site xml if a path is provided
func createSite(siteName string, sitePath string, configPath string) error {
if exists, err := doesSiteExist(siteName); err != nil || exists {
return err
}
properties := []string{"add", "site", fmt.Sprintf("/name:%s", siteName), fmt.Sprintf("/physicalPath:%s", sitePath)}
if _, err := executeAppCmdWithInput(configPath, properties...); err != nil {
return fmt.Errorf("Failed to create Site: %v", err)
}
return nil
}
// Deletes a Site with the given name
func deleteSite(siteName string) error {
if exists, err := doesSiteExist(siteName); err != nil || !exists {
return err
}
if _, err := executeAppCmd("delete", "site", siteName); err != nil {
return fmt.Errorf("Failed to delete Site: %v", err)
}
return nil
}
// Returns if a Site with the given name exists in IIS
func doesSiteExist(siteName string) (bool, error) {
if site, err := getSite(siteName, false); err != nil || site == nil {
return false, err
}
return true, nil
}
// Returns IISBindings by parsing a Site's bindings string
func (site *appCmdSite) getBindings() ([]iisBinding, error) {
var currentBindings []iisBinding
if site.Bindings == "" {
return currentBindings, nil
}
bindings := strings.Split(site.Bindings, ",")
for _, binding := range bindings {
var iisBinding iisBinding
slashIndex := strings.Index(binding, "/")
iisBinding.Type = binding[:slashIndex]
bindingInfo := strings.Split(binding[slashIndex+1:], ":")
iisBinding.IPAddress = bindingInfo[0]
if port, err := strconv.Atoi(bindingInfo[1]); err != nil {
return nil, fmt.Errorf("Failed to parse a binding's port")
} else {
iisBinding.Port = port
}
iisBinding.HostName = bindingInfo[2]
currentBindings = append(currentBindings, iisBinding)
}
return currentBindings, nil
}
// Returns a Site with the given name
func getSite(siteName string, allConfigs bool) (*appCmdSite, error) {
args := []string{"list", "site", siteName}
if allConfigs {
args = append(args, "/config:*")
}
if result, err := executeAppCmd(args...); err != nil {
return nil, fmt.Errorf("Failed to get Site: %v", err)
} else if len(result.Sites) == 0 {
return nil, nil
} else {
return &result.Sites[0], nil
}
}
// Returns all Sites that exist in IIS
func getSites() ([]appCmdSite, error) {
if result, err := executeAppCmd("list", "site"); err != nil {
return nil, fmt.Errorf("Failed to get Sites: %v", err)
} else {
return result.Sites, nil
}
}
// Returns if a Site with the given name is started in IIS
func isSiteStarted(siteName string) (bool, error) {
if site, err := getSite(siteName, false); err != nil || site == nil {
return false, err
} else {
return strings.ToLower(site.State) == "started", nil
}
}
// Starts a Site with the given name in IIS
func startSite(siteName string) error {
if isRunning, err := isSiteStarted(siteName); err != nil || isRunning {
return err
}
if _, err := executeAppCmd("start", "site", siteName); err != nil {
return fmt.Errorf("Failed to start Site: %v", err)
}
return nil
}
// Stops a Site with the given name in IIS
func stopSite(siteName string) error {
if isRunning, err := isSiteStarted(siteName); err != nil || !isRunning {
return err
}
if _, err := executeAppCmd("stop", "site", siteName); err != nil {
return fmt.Errorf("Failed to stop Site: %v", err)
}
return nil
}
// Sets a Site's Application Pool to the names given
func applySiteAppPool(siteName string, appPoolName string) error {
if _, err := executeAppCmd("set", "app", fmt.Sprintf("%s/", siteName), fmt.Sprintf("/applicationPool:%s", appPoolName)); err != nil {
return fmt.Errorf("Failed to set Site Application Pool: %v", err)
}
return nil
}
// Applies shutdown timeouts where appropriate to the website
func applyWebsiteShutdownTimeout(webSiteName string, timeout time.Duration) error {
return applyAppPoolShutdownTimeout(webSiteName, timeout)
}
// Creates an Application Pool and Site with the given configuration
func createWebsite(websiteName string, config *TaskConfig) error {
if err := createAppPool(websiteName, config.AppPoolConfigPath); err != nil {
return err
}
if err := applyAppPoolIdentity(websiteName, config.AppPoolIdentity); err != nil {
return err
}
if err := createSite(websiteName, config.Path, config.SiteConfigPath); err != nil {
return err
}
if err := applySiteAppPool(websiteName, websiteName); err != nil {
return err
}
return applySiteBindings(websiteName, config.Bindings)
}
// Deletes an Application Pool and Site with the given name
func deleteWebsite(websiteName string) error {
if err := deleteSite(websiteName); err != nil {
return err
}
return deleteAppPool(websiteName)
}
// Returns if both Application Pool and Site exist with the given name
func doesWebsiteExist(websiteName string) (bool, error) {
if exists, err := doesAppPoolExist(websiteName); err != nil || !exists {
return false, err
}
if exists, err := doesSiteExist(websiteName); err != nil || !exists {
return false, err
}
return true, nil
}
// Returns the ProcessIds of a running Application Pool as string slice
func getWebsiteProcessIdsStr(websiteName string) ([]string, error) {
result, err := executeAppCmd("list", "wp", fmt.Sprintf("/apppool.name:%s", websiteName))
if err != nil {
return nil, fmt.Errorf("Failed to get Website Process Ids: %v", err)
}
var processIds []string
for _, wp := range result.WorkerProcesses {
processIds = append(processIds, wp.Name)
}
return processIds, nil
}
// Returns the ProcessIds of a running Application Pool
func getWebsiteProcessIds(websiteName string) ([]int, error) {
result, err := getWebsiteProcessIdsStr(websiteName)
if err != nil {
return nil, err
}
var processIds []int
for _, id := range result {
newProcessId, err := strconv.Atoi(id)
if err != nil {
return nil, fmt.Errorf("Failed to parse Website Process Ids: %v", err)
}
processIds = append(processIds, newProcessId)
}
return processIds, nil
}
// Gets the WMI CPU and Memory stats of a given website
func getWebsiteStats(websiteName string) (*wmiProcessStats, error) {
// Get a list of process ids tied to the app pool
processIds, err := getWebsiteProcessIdsStr(websiteName)
if err != nil {
return nil, err
}
// No process ids means no stats.
// IIS sites/app pools can be in a state without an actively running process id.
if len(processIds) == 0 {
return &wmiProcessStats{
WorkingSetPrivate: 0,
KernelModeTime: 0,
UserModeTime: 0,
}, nil
}
// Query WMI for cpu stats with the given process ids
var wmiProcesses []wmiProcessStats
if err := wmi.Query(fmt.Sprintf("SELECT KernelModeTime,UserModeTime FROM Win32_Process WHERE ProcessID=%s", strings.Join(processIds, "OR ProcessID=")), &wmiProcesses); err != nil {
return nil, err
}
// Sum up all cpu stats
var stats wmiProcessStats
for _, process := range wmiProcesses {
stats.KernelModeTime += process.KernelModeTime
stats.UserModeTime += process.UserModeTime
}
// Query WMI for memory stats with the given process ids
// We are only using the WorkingSetPrivate for our memory to better align the Windows Task Manager and the RSS field nomad is expecting
if err := wmi.Query(fmt.Sprintf("SELECT WorkingSetPrivate FROM Win32_PerfFormattedData_PerfProc_Process WHERE IDProcess=%s", strings.Join(processIds, "OR IDProcess=")), &wmiProcesses); err != nil {
return nil, err
}
// Sum up all memory stats
for _, process := range wmiProcesses {
stats.WorkingSetPrivate += process.WorkingSetPrivate
}
// Need to multiply cpu stats by one hundred to align with nomad method CpuStats.Percent's expected decimal placement
stats.KernelModeTime *= 100
stats.UserModeTime *= 100
return &stats, nil
}
func isWebsiteStarted(websiteName string) (bool, error) {
if isStarted, err := isAppPoolStarted(websiteName); err != nil || !isStarted {
return false, err
}
if isStarted, err := isSiteStarted(websiteName); err != nil || !isStarted {
return false, err
}
return true, nil
}
// Returns if the Application Pool has running processes or both Application Pool and Site are started with the given name
func isWebsiteRunning(websiteName string) (bool, error) {
processIds, err := getWebsiteProcessIdsStr(websiteName)
if err != nil {
return false, err
}
if len(processIds) != 0 {
return true, nil
}
if isRunning, err := isWebsiteStarted(websiteName); err != nil || !isRunning {
return false, err
}
return true, nil
}
// Starts both Application Pool and Site with the given name
func startWebsite(websiteName string) error {
if err := startAppPool(websiteName); err != nil {
return err
}
return startSite(websiteName)
}
// Stops both Application Pool and Site with the given name
func stopWebsite(websiteName string) error {
if err := stopSite(websiteName); err != nil {
return err
}
return stopAppPool(websiteName)
}
func getNetshIP(ipAddress string) string {
if ipAddress != "" && ipAddress != "*" {
return ipAddress
} else {
return "0.0.0.0"
}
}
// Binds an appid, ip address, and port to a hash of a pre-existing certificate in the cert store for https protocol IIS binding with netsh
func bindSSLCert(appID string, ipAddress string, port int, hash string) error {
if info, err := getSSLCertBinding(ipAddress, port); err != nil {
return err
} else if info["CertificateHash"] == hash {
return nil
}
cmd := exec.Command(`C:\Windows\System32\netsh.exe`, "http", "add", "sslcert", fmt.Sprintf("ipport=%s:%d", getNetshIP(ipAddress), port), fmt.Sprintf("certhash=%s", hash), fmt.Sprintf("appid={%s}", appID))
if err := cmd.Run(); err != nil {
return fmt.Errorf("Failed to install cert! %+v", err)
}
return nil
}
// Gets sslcert binding details from an ip address and port with netsh.
func getSSLCertBinding(ipAddress string, port int) (map[string]string, error) {
cmd := exec.Command(`C:\Windows\System32\netsh.exe`, "http", "show", "sslcert", fmt.Sprintf("%s:%d", getNetshIP(ipAddress), port))
if out, err := cmd.Output(); err != nil {
// Only ignore errors for not being able to find the file specified. SSLBinding doesn't exist in that case
if !strings.Contains(string(out), "The system cannot find the file specified") {
return nil, fmt.Errorf("Failed to read imported certificate! %+v", err)
}
return nil, nil
} else {
result := make(map[string]string)
count := 0
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
count++
if count < 3 {
continue
}
line := scanner.Text()
if strings.Contains(line, ":") {
split := strings.Split(line, ":")
space := regexp.MustCompile(`\s+`)
key := space.ReplaceAllString(split[0], "")
result[key] = strings.TrimSpace(split[1])
}
}
return result, nil
}
}
// Removes ip address and port sslcert binding with netsh
func unbindSSLCert(ipAddress string, port int) error {
if info, err := getSSLCertBinding(ipAddress, port); err != nil || len(info) == 0 {
return err
}
cmd := exec.Command(`C:\Windows\System32\netsh.exe`, "http", "delete", "sslcert", fmt.Sprintf("ipport=%s:%d", getNetshIP(ipAddress), port))
if err := cmd.Run(); err != nil {
return fmt.Errorf("Failed to uninstall cert! %+v", err)
}
return nil
}