2020-05-15 15:09:01 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2020-08-24 02:09:13 +00:00
|
|
|
"github.com/shirou/gopsutil/host"
|
2020-05-15 15:09:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const bridgeKernelModuleName = "bridge"
|
|
|
|
|
|
|
|
func (f *BridgeFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
|
|
|
|
if err := f.checkKMod(bridgeKernelModuleName); err != nil {
|
|
|
|
f.logger.Warn("failed to detect bridge kernel module, bridge network mode disabled", "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.NodeResources = &structs.NodeResources{
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
Mode: "bridge",
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 15:53:10 +00:00
|
|
|
NodeNetworks: []*structs.NodeNetworkResource{
|
|
|
|
{
|
|
|
|
Mode: "bridge",
|
|
|
|
Device: req.Config.BridgeNetworkName,
|
|
|
|
},
|
|
|
|
},
|
2020-05-15 15:09:01 +00:00
|
|
|
}
|
|
|
|
resp.Detected = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BridgeFingerprint) checkKMod(mod string) error {
|
2020-08-24 03:24:16 +00:00
|
|
|
hostInfo, err := host.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dynErr := f.checkKModFile(mod, "/proc/modules", fmt.Sprintf("%s\\s+.*$", mod))
|
2020-08-24 02:09:13 +00:00
|
|
|
if dynErr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-24 03:24:16 +00:00
|
|
|
builtinErr := f.checkKModFile(mod,
|
|
|
|
fmt.Sprintf("/lib/modules/%s/modules.builtin", hostInfo.KernelVersion),
|
|
|
|
fmt.Sprintf(".+\\/%s.ko$", mod))
|
2020-08-24 02:09:13 +00:00
|
|
|
if builtinErr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("%v, %v", dynErr, builtinErr)
|
|
|
|
}
|
|
|
|
|
2020-08-24 03:24:16 +00:00
|
|
|
func (f *BridgeFingerprint) checkKModFile(mod, fileName, pattern string) error {
|
2020-08-24 02:09:13 +00:00
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not read %s: %v", fileName, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
if matched, err := regexp.MatchString(pattern, scanner.Text()); matched {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("could not parse %s: %v", fileName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 03:24:16 +00:00
|
|
|
return fmt.Errorf("could not detect kernel module %s", mod)
|
2020-05-15 15:09:01 +00:00
|
|
|
}
|