Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add node ip and sn into daemon labels #1134

Merged
merged 1 commit into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/alibaba/pouch/internal"
"github.com/alibaba/pouch/network/mode"
"github.com/alibaba/pouch/pkg/meta"
"github.com/alibaba/pouch/pkg/system"

"github.com/gorilla/mux"
"github.com/pkg/errors"
Expand Down Expand Up @@ -167,6 +168,10 @@ func (d *Daemon) Run() error {
}
d.containerMgr = containerMgr

if err := d.addSystemLabels(); err != nil {
return err
}

d.server = server.Server{
Config: d.config,
ContainerMgr: containerMgr,
Expand Down Expand Up @@ -269,6 +274,27 @@ func (d *Daemon) ShutdownPlugin() error {
return nil
}

// addSystemLabels adds some system labels to daemon's config.
// Currently, pouchd add node ip and serial number to pouchd with the format:
// node_ip=192.168.0.1
// SN=xxxxx
func (d *Daemon) addSystemLabels() error {
d.config.Lock()
defer d.config.Unlock()
if d.config.Labels == nil {
d.config.Labels = make([]string, 0)
}
// get node IP
nodeIP := system.GetNodeIP()
d.config.Labels = append(d.config.Labels, fmt.Sprintf("node_ip=%s", nodeIP))

// get serial number
serialNo := system.GetSerialNumber()
d.config.Labels = append(d.config.Labels, fmt.Sprintf("SN=%s", serialNo))

return nil
}

// RunCriService start cri service if pouchd is specified with --enable-cri.
func (d *Daemon) RunCriService(stopCh chan error) {
var err error
Expand Down
36 changes: 36 additions & 0 deletions pkg/system/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package system

import (
"bufio"
"bytes"
"os"
"os/exec"
"strings"
"time"
)

// GetSerialNumber gets serial number or a machine.
func GetSerialNumber() string {
var sn string
if b, e := exec.Command("dmidecode", "-s", "system-serial-number").CombinedOutput(); e == nil {
scanner := bufio.NewScanner(bytes.NewReader(b))
for scanner.Scan() {
sn = scanner.Text()
}
}
if len(strings.Fields(sn)) != 0 {
sn = strings.Fields(sn)[0]
}
for i := 0; i < 10; i++ {
if _, ex := os.Stat("/usr/alisys/dragoon/libexec/armory/bin/armoryinfo"); ex == nil {
if b, e := exec.Command("/usr/alisys/dragoon/libexec/armory/bin/armoryinfo", "sn").CombinedOutput(); e == nil {
sn = strings.TrimSpace(string(b))
}
}
if sn != "" {
break
}
time.Sleep(100 * time.Millisecond)
}
return sn
}
26 changes: 26 additions & 0 deletions pkg/system/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package system

import (
"bufio"
"bytes"
"net"
"os/exec"
)

// GetNodeIP fetches node ip via command hostname.
// If it fails to get this, return empty string directly.
func GetNodeIP() string {
output, err := exec.Command("hostname", "-i").CombinedOutput()
if err != nil {
return ""
}

scanner := bufio.NewScanner(bytes.NewReader(output))
for scanner.Scan() {
ip := scanner.Text()
if net.ParseIP(ip) != nil {
return ip
}
}
return ""
}
14 changes: 13 additions & 1 deletion test/api_daemon_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func (suite *APIDaemonUpdateSuite) TestUpdateDaemon(c *check.C) {
err = request.DecodeBody(&info, resp.Body)
c.Assert(err, check.IsNil)

c.Assert(info.Labels, check.DeepEquals, labels)
for _, label := range labels {
isContained := false
for _, infoLabel := range info.Labels {
if infoLabel == label {
isContained = true
break
}
}
if !isContained {
c.Fatalf("label %s should be in labels in info API", label)
}
}
//c.Assert(info.Labels, check.DeepEquals, labels)
// TODO: add checking image proxy
}