mirror of
https://github.com/netbirdio/libdetectcloud.git
synced 2026-05-22 17:09:55 -07:00
Add containers
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
# detectcloud
|
||||
# libdetectcloud
|
||||
|
||||
http.Client timeout is set to `120ms`. Sometimes hitting the metadata service to fast will return unknown instead of the cloud provider detected.
|
||||
http.Client timeout is set to `300ms`. Sometimes hitting the metadata service to fast will return empty instead of the cloud provider detected.
|
||||
|
||||
package main
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitlab.com/taskfitio/lib/detectcloud"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
// detectcloud.Detect() will return a string
|
||||
// Amazon Web Services, Microsoft Azure, Digital Ocean
|
||||
// Google Compute Engine, OpenStack, SoftLayer, Vultr
|
||||
// unknown
|
||||
|
||||
// detectcloud.Detect() will return an empty string or
|
||||
// Amazon Web Services, Microsoft Azure, Digital Ocean
|
||||
// Google Compute Engine, OpenStack, SoftLayer, Vultr
|
||||
// K8S Container, Container
|
||||
|
||||
fmt.Println(detectcloud.Detect())
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// https://azure.microsoft.com/en-us/blog/what-just-happened-to-my-vm-in-vm-metadata-service/
|
||||
func detectAzure() string {
|
||||
resp, err := hc.Get("http://169.254.169.254/metadata/v1/InstanceInfo")
|
||||
if err == nil && resp.StatusCode == http.StatusOK {
|
||||
|
||||
@@ -7,17 +7,18 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var hc = &http.Client{Timeout: 120 * time.Millisecond}
|
||||
var hc = &http.Client{Timeout: 300 * time.Millisecond}
|
||||
|
||||
// Clouds type
|
||||
type Clouds struct {
|
||||
Aws string
|
||||
Azure string
|
||||
Do string
|
||||
Gce string
|
||||
Ost string
|
||||
Sl string
|
||||
Vr string
|
||||
Aws string
|
||||
Azure string
|
||||
Do string
|
||||
Gce string
|
||||
Ost string
|
||||
Sl string
|
||||
Vr string
|
||||
Container string
|
||||
}
|
||||
|
||||
// Detect function
|
||||
@@ -25,7 +26,7 @@ func Detect() string {
|
||||
if runtime.GOOS != "darwin" {
|
||||
var c Clouds
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(7)
|
||||
wg.Add(8)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
c.Aws = detectAWS()
|
||||
@@ -54,7 +55,12 @@ func Detect() string {
|
||||
defer wg.Done()
|
||||
c.Vr = detectVultr()
|
||||
}()
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
c.Container = detectContainer()
|
||||
}()
|
||||
wg.Wait()
|
||||
|
||||
if c.Aws != "" {
|
||||
return c.Aws
|
||||
}
|
||||
@@ -76,6 +82,9 @@ func Detect() string {
|
||||
if c.Vr != "" {
|
||||
return c.Vr
|
||||
}
|
||||
if c.Container != "" {
|
||||
return c.Container
|
||||
}
|
||||
}
|
||||
return "unknown"
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// https://developers.digitalocean.com/documentation/metadata/#metadata-in-json
|
||||
func detectDigitalOcean() string {
|
||||
resp, err := hc.Get("http://169.254.169.254/metadata/v1.json")
|
||||
if err == nil && resp.StatusCode == http.StatusOK {
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// https://cloud.google.com/compute/docs/storing-retrieving-metadata#endpoints
|
||||
// curl "http://metadata.google.internal/computeMetadata/v1/instance/tags" -H "Metadata-Flavor: Google"
|
||||
func detectGCE() string {
|
||||
r, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/tags", nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
module github.com/yeticloud/libdetectcloud
|
||||
|
||||
go 1.14
|
||||
|
||||
require github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e
|
||||
go 1.15
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e h1:vUmf0yezR0y7jJ5pceLHthLaYf4bA5T14B6q39S4q2Q=
|
||||
github.com/digitalocean/go-smbios v0.0.0-20180907143718-390a4f403a8e/go.mod h1:YTIHhz/QFSYnu/EhlF2SpU2Uk+32abacUYA5ZPljz1A=
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
package libdetectcloud
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/digitalocean/go-smbios/smbios"
|
||||
)
|
||||
|
||||
// BIOS type
|
||||
type BIOS struct {
|
||||
Manufacturer string
|
||||
ProductName string
|
||||
SerialNumber string
|
||||
IsVM bool
|
||||
Hypervisor string
|
||||
}
|
||||
|
||||
var hypervisors = map[string][]string{
|
||||
"VMWare": []string{"vmware", "vm ware"},
|
||||
"Xen": []string{"xen"},
|
||||
"Hyper-V": []string{"microsoft", "hyperv", "hyper-v", "hyper v"},
|
||||
"VirtualBox": []string{"virtual box", "virtual-box", "virtualbox"},
|
||||
"KVM": []string{"virt-manager", "virt manager", "virtmanager", "hvm", "kvm"},
|
||||
"Parallels": []string{"parallels"},
|
||||
}
|
||||
|
||||
//BIOSInfo function gets information from the SMBIOS
|
||||
func BIOSInfo() (*BIOS, error) {
|
||||
rc, _, err := smbios.Stream()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Be sure to close the stream!
|
||||
defer rc.Close()
|
||||
|
||||
// Decode SMBIOS structures from the stream.
|
||||
d := smbios.NewDecoder(rc)
|
||||
ss, err := d.Decode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
info := []string{}
|
||||
for _, i := range ss {
|
||||
if i.Header.Type == 1 {
|
||||
info = i.Strings
|
||||
}
|
||||
}
|
||||
b := &BIOS{
|
||||
Manufacturer: info[0],
|
||||
ProductName: info[1],
|
||||
SerialNumber: info[3],
|
||||
}
|
||||
for h, s := range hypervisors {
|
||||
for _, i := range s {
|
||||
re, err := regexp.Compile(`(?igm)` + i)
|
||||
if err != nil {
|
||||
return b, nil
|
||||
}
|
||||
if re.MatchString(b.Manufacturer) || re.MatchString(b.ProductName) {
|
||||
b.IsVM = true
|
||||
b.Hypervisor = h
|
||||
return b, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package libdetectcloud
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func detectContainer() string {
|
||||
b, err := ioutil.ReadFile("/proc/self/cgroup")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
fc := string(b)
|
||||
kube := strings.Contains(fc, "kube")
|
||||
container := strings.Contains(fc, "containerd")
|
||||
|
||||
if kube {
|
||||
return "K8S Container"
|
||||
}
|
||||
|
||||
if container {
|
||||
return "Container"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// https://sldn.softlayer.com/blog/jarteche/getting-started-user-data-and-post-provisioning-scripts
|
||||
// https://github.com/bodenr/cci/wiki/SL-user-metadata
|
||||
func detectSoftlayer() string {
|
||||
resp, err := hc.Get("https://api.service.softlayer.com/rest/v3/SoftLayer_Resource_Metadata/UserMetadata.txt")
|
||||
if err == nil && (resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNotFound) {
|
||||
|
||||
Reference in New Issue
Block a user