-
Notifications
You must be signed in to change notification settings - Fork 230
/
os_linux.go
181 lines (152 loc) · 4.87 KB
/
os_linux.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
// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package platform
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/Azure/azure-container-networking/log"
)
const (
// CNMRuntimePath is the path where CNM state files are stored.
CNMRuntimePath = "/var/lib/azure-network/"
// CNIRuntimePath is the path where CNI state files are stored.
CNIRuntimePath = "/var/run/"
// CNILockPath is the path where CNI lock files are stored.
CNILockPath = "/var/run/azure-vnet/"
// CNIStateFilePath is the path to the CNI state file
CNIStateFilePath = "/var/run/azure-vnet.json"
// CNIIpamStatePath is the name of IPAM state file
CNIIpamStatePath = "/var/run/azure-vnet-ipam.json"
// CNIBinaryPath is the path to the CNI binary
CNIBinaryPath = "/opt/cni/bin/azure-vnet"
// CNSRuntimePath is the path where CNS state files are stored.
CNSRuntimePath = "/var/run/"
// CNI runtime path on a Kubernetes cluster
K8SCNIRuntimePath = "/opt/cni/bin"
// Network configuration file path on a Kubernetes cluster
K8SNetConfigPath = "/etc/cni/net.d"
// NPMRuntimePath is the path where NPM logging files are stored.
NPMRuntimePath = "/var/run/"
// DNCRuntimePath is the path where DNC logging files are stored.
DNCRuntimePath = "/var/run/"
// This file contains OS details
osReleaseFile = "/etc/os-release"
)
// GetOSInfo returns OS version information.
func GetOSInfo() string {
info, err := os.ReadFile("/proc/version")
if err != nil {
return "unknown"
}
return string(info)
}
func GetProcessSupport() error {
p := NewExecClient()
cmd := fmt.Sprintf("ps -p %v -o comm=", os.Getpid())
_, err := p.ExecuteCommand(cmd)
return err
}
// GetLastRebootTime returns the last time the system rebooted.
func GetLastRebootTime() (time.Time, error) {
// Query last reboot time.
out, err := exec.Command("uptime", "-s").Output()
if err != nil {
log.Printf("Failed to query uptime, err:%v", err)
return time.Time{}.UTC(), err
}
// Parse the output.
layout := "2006-01-02 15:04:05"
rebootTime, err := time.ParseInLocation(layout, string(out[:len(out)-1]), time.Local)
if err != nil {
log.Printf("Failed to parse uptime, err:%v", err)
return time.Time{}.UTC(), err
}
return rebootTime.UTC(), nil
}
func (p *execClient) ExecuteCommand(command string) (string, error) {
log.Printf("[Azure-Utils] %s", command)
var stderr bytes.Buffer
var out bytes.Buffer
// Create a new context and add a timeout to it
ctx, cancel := context.WithTimeout(context.Background(), p.Timeout)
defer cancel() // The cancel should be deferred so resources are cleaned up
cmd := exec.CommandContext(ctx, "sh", "-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 {
p := NewExecClient()
cmd := fmt.Sprintf("iptables -t nat -A POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d %v -j MASQUERADE",
subnet)
_, err := p.ExecuteCommand(cmd)
if err != nil {
log.Printf("SNAT Iptable rule was not set")
return err
}
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) {
return false, nil
}
func KillProcessByName(processName string) error {
p := NewExecClient()
cmd := fmt.Sprintf("pkill -f %v", processName)
_, err := p.ExecuteCommand(cmd)
return err
}
// SetSdnRemoteArpMacAddress sets the regkey for SDNRemoteArpMacAddress needed for multitenancy
// This operation is specific to windows OS
func SetSdnRemoteArpMacAddress() error {
return nil
}
func GetOSDetails() (map[string]string, error) {
linesArr, err := ReadFileByLines(osReleaseFile)
if err != nil || len(linesArr) <= 0 {
return nil, err
}
osInfoArr := make(map[string]string)
for i := range linesArr {
s := strings.Split(linesArr[i], "=")
if len(s) == 2 {
osInfoArr[s[0]] = strings.TrimSuffix(s[1], "\n")
}
}
return osInfoArr, nil
}
func GetProcessNameByID(pidstr string) (string, error) {
p := NewExecClient()
pidstr = strings.Trim(pidstr, "\n")
cmd := fmt.Sprintf("ps -p %s -o comm=", pidstr)
out, err := p.ExecuteCommand(cmd)
if err != nil {
log.Printf("GetProcessNameByID returned error: %v", err)
return "", err
}
out = strings.Trim(out, "\n")
out = strings.TrimSpace(out)
return out, nil
}
func PrintDependencyPackageDetails() {
p := NewExecClient()
out, err := p.ExecuteCommand("iptables --version")
out = strings.TrimSuffix(out, "\n")
log.Printf("[cni-net] iptable version:%s, err:%v", out, err)
out, err = p.ExecuteCommand("ebtables --version")
out = strings.TrimSuffix(out, "\n")
log.Printf("[cni-net] ebtable version %s, err:%v", out, err)
}
func ReplaceFile(source, destination string) error {
return os.Rename(source, destination)
}