-
Notifications
You must be signed in to change notification settings - Fork 24
/
info.go
128 lines (114 loc) · 3.28 KB
/
info.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
package info
import (
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"
guuid "github.com/google/uuid"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
infov1 "github.com/fluxninja/aperture/v2/api/gen/proto/go/aperture/info/v1"
)
// Default build-time variables. These values are overridden via ldflags.
var (
// Version is the version of the aperture process.
Version = "0.0.1"
// Service is the name of the aperture process.
Service = "unknown"
// BuildHost is the hostname of the machine that built the application.
BuildHost = "unknown"
// BuildOS is the operating system that built the application.
BuildOS = "unknown"
// BuildTime is the time when the application was built.
BuildTime = "unknown"
// GitBranch is the git branch that the application was built from.
GitBranch = "unknown"
// GitCommitHash is the git commit hash that the application was built from.
GitCommitHash = "unknown"
// Prefix is the prefix for the aperture service application.
Prefix = "aperture"
// Hostname is the hostname of the machine that is running the process.
Hostname = "unknown"
// LocalIP is the IP of the service.
LocalIP = "unknown"
// UUID is the unique identifier for the process.
UUID = "unknown"
// Extensions is the list of extensions that are enabled.
Extensions = []string{}
)
var (
mutex sync.Mutex
versionInfo infov1.VersionInfo
processInfo infov1.ProcessInfo
)
func init() {
if Service == "unknown" {
service := filepath.Base(os.Args[0])
// remove dots from service name if it has them
Service = strings.ReplaceAll(service, ".", "-")
}
versionInfo.Version = Version
versionInfo.Service = Service
versionInfo.BuildHost = BuildHost
versionInfo.BuildOs = BuildOS
versionInfo.BuildTime = BuildTime
versionInfo.GitBranch = GitBranch
versionInfo.GitCommitHash = GitCommitHash
// ProcessInfo
processInfo.StartTime = timestamppb.Now()
// Hostname
hostname, err := os.Hostname()
if err != nil {
hostname = getLocalIP()
}
if hostname != "" {
Hostname = hostname
}
LocalIP = getLocalIP()
// generate UUID
UUID = guuid.NewString()
}
// GetVersionInfo returns the version info for the application.
func GetVersionInfo() *infov1.VersionInfo {
mutex.Lock()
defer mutex.Unlock()
return proto.Clone(&versionInfo).(*infov1.VersionInfo)
}
// GetProcessInfo returns the process info for the application.
func GetProcessInfo() *infov1.ProcessInfo {
mutex.Lock()
defer mutex.Unlock()
processInfo.Extensions = Extensions
// reset uptime
processInfo.Uptime = durationpb.New(time.Since(processInfo.StartTime.AsTime()))
return (proto.Clone(&processInfo)).(*infov1.ProcessInfo)
}
// GetHostInfo returns the host info for the application.
func GetHostInfo() *infov1.HostInfo {
mutex.Lock()
defer mutex.Unlock()
return &infov1.HostInfo{
Hostname: Hostname,
Uuid: UUID,
LocalIp: LocalIP,
}
}
// getLocalIP gets local ip address.
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}