-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathos_windows.go
120 lines (94 loc) · 3.29 KB
/
os_windows.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
// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package platform
import (
"bytes"
"fmt"
"os/exec"
"strings"
"time"
"github.com/Azure/azure-container-networking/log"
)
const (
// CNMRuntimePath is the path where CNM state files are stored.
CNMRuntimePath = ""
// CNIRuntimePath is the path where CNI state files are stored.
CNIRuntimePath = ""
// CNI runtime path on a Kubernetes cluster
K8SCNIRuntimePath = "C:\\k\\azurecni\\bin"
// Network configuration file path on a Kubernetes cluster
K8SNetConfigPath = "C:\\k\\azurecni\\netconf"
// CNSRuntimePath is the path where CNS state files are stored.
CNSRuntimePath = ""
// NPMRuntimePath is the path where NPM state files are stored.
NPMRuntimePath = ""
// DNCRuntimePath is the path where DNC state files are stored.
DNCRuntimePath = ""
)
// GetOSInfo returns OS version information.
func GetOSInfo() string {
return "windows"
}
// GetLastRebootTime returns the last time the system rebooted.
func GetLastRebootTime() (time.Time, error) {
out, err := exec.Command("cmd", "/c", "wmic os get lastbootuptime").Output()
if err != nil {
log.Printf("Failed to query wmic os get lastbootuptime, err: %v", err)
return time.Time{}.UTC(), err
}
lastBootupTime := strings.Split(strings.TrimSpace(string(out)), "\n")
if strings.TrimSpace(lastBootupTime[0]) != "LastBootUpTime" || len(lastBootupTime) != 2 {
log.Printf("Failed to retrieve boot time")
return time.Time{}.UTC(), fmt.Errorf("Failed to retrieve boot time with 'wmic os get lastbootuptime'")
}
systemBootupTime := strings.Split(lastBootupTime[1], ".")[0]
// The systembootuptime is in the format YYYYMMDDHHMMSS
bootYear := systemBootupTime[0:4]
bootMonth := systemBootupTime[4:6]
bootDay := systemBootupTime[6:8]
bootHour := systemBootupTime[8:10]
bootMin := systemBootupTime[10:12]
bootSec := systemBootupTime[12:14]
systemBootTime := bootYear + "-" + bootMonth + "-" + bootDay + " " + bootHour + ":" + bootMin + ":" + bootSec
log.Printf("Formatted Boot time: %s", systemBootTime)
// Parse the boot time.
layout := "2006-01-02 15:04:05"
rebootTime, err := time.ParseInLocation(layout, systemBootTime, time.Local)
if err != nil {
log.Printf("Failed to parse boot time, err:%v", err)
return time.Time{}.UTC(), err
}
return rebootTime.UTC(), nil
}
func ExecuteCommand(command string) (string, error) {
log.Printf("[Azure-Utils] %s", command)
var stderr bytes.Buffer
var out bytes.Buffer
cmd := exec.Command("cmd", "/c", command)
cmd.Stderr = &stderr
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("%s:%s", err.Error(), stderr.String())
}
return out.String(), nil
}
func SetOutboundSNAT(subnet string) error {
return nil
}
// ClearNetworkConfiguration clears the azure-vnet.json contents.
// This will be called only when reboot is detected - This is windows specific
func ClearNetworkConfiguration() (bool, error) {
jsonStore := CNIRuntimePath + "azure-vnet.json"
log.Printf("Deleting the json store %s", jsonStore)
cmd := exec.Command("cmd", "/c", "del", jsonStore)
if err := cmd.Run(); err != nil {
log.Printf("Error deleting the json store %s", jsonStore)
return true, err
}
return true, nil
}
func KillProcessByName(processName string) {
cmd := fmt.Sprintf("taskkill /IM %v /F", processName)
ExecuteCommand(cmd)
}