From 609dc7b864b3d246d518ba2ad754bce7697b79b0 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Thu, 17 Jan 2019 10:46:53 +0800 Subject: [PATCH] use procutils replace exec --- pkg/appsrv/appsrv.go | 4 + pkg/cloudcommon/storagetypes/storagetypes.go | 2 + pkg/hostman/guestfs/kvmpart.go | 25 ++-- pkg/hostman/guestfs/localfs.go | 3 +- pkg/hostman/guestman/guesthandler.go | 2 +- pkg/hostman/guestman/guestman.go | 4 +- pkg/hostman/guestman/guesttasks.go | 18 +-- pkg/hostman/guestman/qemu-kvm.go | 27 ++-- pkg/hostman/hostinfo/hostbridge/hostbridge.go | 49 ++++--- pkg/hostman/hostinfo/hostinfo.go | 120 ++++++++++-------- pkg/hostman/hostinfo/hostinfohelper.go | 13 +- pkg/hostman/hostutils/hostutils.go | 4 +- pkg/hostman/options/options.go | 2 +- pkg/hostman/service/host_services.go | 17 ++- pkg/hostman/storageman/core.go | 2 +- pkg/hostman/storageman/disklocal.go | 60 ++++----- pkg/hostman/storageman/diskutils.go | 8 +- pkg/hostman/storageman/imagecachemanager.go | 4 +- pkg/hostman/storageman/storagebase.go | 4 +- pkg/hostman/storageman/storagelocal.go | 17 ++- pkg/util/cgrouputils/cgrouputils.go | 16 ++- pkg/util/fileutils2/fileutils.go | 21 +-- pkg/util/fuseutils/fuseutils.go | 14 +- pkg/util/netutils2/netutils.go | 15 ++- pkg/util/netutils2/netutils_test.go | 5 + pkg/util/ovsutils/ovsutils.go | 13 +- pkg/util/procutils/procutils.go | 18 ++- pkg/util/timeutils2/timeutils.go | 2 + .../x/pkg/util/netutils/netutils_test.go | 23 ++++ 29 files changed, 307 insertions(+), 205 deletions(-) create mode 100644 vendor/yunion.io/x/pkg/util/netutils/netutils_test.go diff --git a/pkg/appsrv/appsrv.go b/pkg/appsrv/appsrv.go index f19fa97e1e..9491bd13e0 100644 --- a/pkg/appsrv/appsrv.go +++ b/pkg/appsrv/appsrv.go @@ -336,6 +336,10 @@ func (app *Application) ListenAndServe(addr string) { } } +func (app *Application) IsInServe() bool { + return app.server != nil +} + func (app *Application) ShowDown(ctx context.Context) error { if app.server != nil { return app.server.Shutdown(ctx) diff --git a/pkg/cloudcommon/storagetypes/storagetypes.go b/pkg/cloudcommon/storagetypes/storagetypes.go index 5084f9c0bf..4779914227 100644 --- a/pkg/cloudcommon/storagetypes/storagetypes.go +++ b/pkg/cloudcommon/storagetypes/storagetypes.go @@ -13,4 +13,6 @@ var ( DISK_TYPE_ROTATE = "rotate" DISK_TYPE_SSD = "ssd" + + Local = []string{STORAGE_LOCAL, STORAGE_BAREMETAL, STORAGE_NAS} ) diff --git a/pkg/hostman/guestfs/kvmpart.go b/pkg/hostman/guestfs/kvmpart.go index 986ebefd6e..8e1a3c57ae 100644 --- a/pkg/hostman/guestfs/kvmpart.go +++ b/pkg/hostman/guestfs/kvmpart.go @@ -3,7 +3,6 @@ package guestfs import ( "fmt" "os" - "os/exec" "strings" "time" @@ -11,6 +10,7 @@ import ( "yunion.io/x/pkg/utils" "yunion.io/x/onecloud/pkg/util/fileutils2" + "yunion.io/x/onecloud/pkg/util/procutils" ) type SKVMGuestDiskPartition struct { @@ -68,7 +68,7 @@ func (p *SKVMGuestDiskPartition) Mount() bool { } func (p *SKVMGuestDiskPartition) mount(readonly bool) error { - if err := exec.Command("mkdir", "-p", p.mountPath).Run(); err != nil { + if _, err := procutils.NewCommand("mkdir", "-p", p.mountPath).Run(); err != nil { return err } var cmds = []string{"mount", "-t"} @@ -90,7 +90,8 @@ func (p *SKVMGuestDiskPartition) mount(readonly bool) error { cmds = append(cmds, "-o", opt) } cmds = append(cmds, p.partDev, p.mountPath) - return exec.Command(cmds[0], cmds[1:]...).Run() + _, err := procutils.NewCommand(cmds[0], cmds[1:]...).Run() + return err } func (p *SKVMGuestDiskPartition) fsck() error { @@ -107,11 +108,11 @@ func (p *SKVMGuestDiskPartition) fsck() error { fixCmd = []string{"ntfsfix", p.partDev} } if len(checkCmd) > 0 { - _, err := exec.Command(checkCmd[0], checkCmd[1:]...).Output() + _, err := procutils.NewCommand(checkCmd[0], checkCmd[1:]...).Run() if err != nil { log.Warningf("FS %s dirty, try to repair ...", p.partDev) for i := 0; i < 3; i++ { - _, err := exec.Command(fixCmd[0], fixCmd[1:]...).Output() + _, err := procutils.NewCommand(fixCmd[0], fixCmd[1:]...).Run() if err == nil { break } else { @@ -137,7 +138,7 @@ func (p *SKVMGuestDiskPartition) IsMounted() bool { if _, err := os.Stat(p.mountPath); os.IsNotExist(err) { return false } - err := exec.Command("mountpoint", p.mountPath).Run() + _, err := procutils.NewCommand("mountpoint", p.mountPath).Run() if err == nil { return true } else { @@ -151,9 +152,9 @@ func (p *SKVMGuestDiskPartition) Umount() bool { var tries = 0 for tries < 10 { tries += 1 - err := exec.Command("umount", p.mountPath).Run() + _, err := procutils.NewCommand("umount", p.mountPath).Run() if err == nil { - exec.Command("blockdev", "--flushbufs", p.partDev).Run() + procutils.NewCommand("blockdev", "--flushbufs", p.partDev).Run() os.Remove(p.mountPath) return true } else { @@ -179,7 +180,7 @@ func (p *SKVMGuestDiskPartition) Zerofree() { func (p *SKVMGuestDiskPartition) zerofreeSwap() { uuids := fileutils2.GetDevUuid(p.partDev) - err := exec.Command("shred", "-n", "0", "-z", p.partDev).Run() + _, err := procutils.NewCommand("shred", "-n", "0", "-z", p.partDev).Run() if err != nil { log.Errorf("zerofree swap error: %s", err) return @@ -189,14 +190,14 @@ func (p *SKVMGuestDiskPartition) zerofreeSwap() { cmd = append(cmd, "-U", uuid) } cmd = append(cmd, p.partDev) - err = exec.Command(cmd[0], cmd[1:]...).Run() + _, err = procutils.NewCommand(cmd[0], cmd[1:]...).Run() if err != nil { log.Errorf("zerofree swap error: %s", err) } } func (p *SKVMGuestDiskPartition) zerofreeExt() { - err := exec.Command("zerofree", p.partDev).Run() + _, err := procutils.NewCommand("zerofree", p.partDev).Run() if err != nil { log.Errorf("zerofree ext error: %s", err) return @@ -204,7 +205,7 @@ func (p *SKVMGuestDiskPartition) zerofreeExt() { } func (p *SKVMGuestDiskPartition) zerofreeNtfs() { - err := exec.Command("ntfswipe", "-f", "-l", "-m", "-p", "-s", "-q", + _, err := procutils.NewCommand("ntfswipe", "-f", "-l", "-m", "-p", "-s", "-q", p.partDev).Run() if err != nil { log.Errorf("zerofree ntfs error: %s", err) diff --git a/pkg/hostman/guestfs/localfs.go b/pkg/hostman/guestfs/localfs.go index 7e8542f184..36d34e4956 100644 --- a/pkg/hostman/guestfs/localfs.go +++ b/pkg/hostman/guestfs/localfs.go @@ -12,6 +12,7 @@ import ( "yunion.io/x/log" "yunion.io/x/onecloud/pkg/util/fileutils2" + "yunion.io/x/onecloud/pkg/util/procutils" ) type SLocalGuestFS struct { @@ -187,7 +188,7 @@ func (f *SLocalGuestFS) Chmod(sPath string, mode uint32, caseInsensitive bool) e } func (f *SLocalGuestFS) UserAdd(user string, caseInsensitive bool) error { - output, err := exec.Command("chroot", f.mountPath, "useradd", "-m", "-s", "/bin/bash", user).Output() + output, err := procutils.NewCommand("chroot", f.mountPath, "useradd", "-m", "-s", "/bin/bash", user).Run() if err != nil { log.Errorf("Useradd fail: %s", err) return err diff --git a/pkg/hostman/guestman/guesthandler.go b/pkg/hostman/guestman/guesthandler.go index 498b08968a..9712739bec 100644 --- a/pkg/hostman/guestman/guesthandler.go +++ b/pkg/hostman/guestman/guesthandler.go @@ -30,7 +30,7 @@ func AddGuestTaskHandler(prefix string, app *appsrv.Application) { auth.Authenticate(cpusetBalance)) app.AddHandler("POST", - fmt.Sprintf("%s/%s/servers//", prefix, keyWord), + fmt.Sprintf("%s/%s//", prefix, keyWord), auth.Authenticate(guestActions)) app.AddHandler("DELETE", diff --git a/pkg/hostman/guestman/guestman.go b/pkg/hostman/guestman/guestman.go index db1617377a..75d7a66780 100644 --- a/pkg/hostman/guestman/guestman.go +++ b/pkg/hostman/guestman/guestman.go @@ -5,7 +5,6 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "path" "strings" "sync" @@ -26,6 +25,7 @@ import ( "yunion.io/x/onecloud/pkg/util/cgrouputils" "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/netutils2" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/timeutils2" ) @@ -432,7 +432,7 @@ func (m *SGuestManager) DestPrepareMigrate(ctx context.Context, params interface // prepare disk snapshot dir if len(snapshots) > 0 && !fileutils2.Exists(disk.GetSnapshotDir()) { - err := exec.Command("mkdir", "-p", disk.GetSnapshotDir()).Run() + _, err := procutils.NewCommand("mkdir", "-p", disk.GetSnapshotDir()).Run() if err != nil { return nil, err } diff --git a/pkg/hostman/guestman/guesttasks.go b/pkg/hostman/guestman/guesttasks.go index d788357a0a..f16f7d2e8f 100644 --- a/pkg/hostman/guestman/guesttasks.go +++ b/pkg/hostman/guestman/guesttasks.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "os/exec" "path" "regexp" "strings" @@ -18,6 +17,7 @@ import ( "yunion.io/x/onecloud/pkg/hostman/options" "yunion.io/x/onecloud/pkg/hostman/storageman" "yunion.io/x/onecloud/pkg/util/fileutils2" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/qemuimg" "yunion.io/x/onecloud/pkg/util/timeutils2" ) @@ -765,7 +765,7 @@ func (s *SGuestDiskSnapshotTask) onReloadBlkdevSucc(res string) { func (s *SGuestDiskSnapshotTask) onSnapshotBlkdevFail(string) { snapshotDir := s.disk.GetSnapshotDir() snapshotPath := path.Join(snapshotDir, s.snapshotId) - err := exec.Command("rm", "-rf", snapshotPath).Run() + _, err := procutils.NewCommand("rm", "-rf", snapshotPath).Run() if err != nil { log.Errorln(err) } @@ -831,17 +831,17 @@ func (s *SGuestSnapshotDeleteTask) doDiskConvert() error { } s.tmpPath = snapshotPath + ".swap" - if err := exec.Command("mv", "-f", snapshotPath, s.tmpPath).Run(); err != nil { + if _, err := procutils.NewCommand("mv", "-f", snapshotPath, s.tmpPath).Run(); err != nil { log.Errorln(err) if fileutils2.Exists(s.tmpPath) { - exec.Command("mv", "-f", s.tmpPath, snapshotPath).Run() + procutils.NewCommand("mv", "-f", s.tmpPath, snapshotPath).Run() } return err } - if err := exec.Command("mv", "-f", convertedDisk, snapshotPath).Run(); err != nil { + if _, err := procutils.NewCommand("mv", "-f", convertedDisk, snapshotPath).Run(); err != nil { log.Errorln(err) if fileutils2.Exists(s.tmpPath) { - exec.Command("mv", "-f", s.tmpPath, snapshotPath).Run() + procutils.NewCommand("mv", "-f", s.tmpPath, snapshotPath).Run() } return err } @@ -863,7 +863,7 @@ func (s *SGuestSnapshotDeleteTask) onReloadBlkdevSucc(err string) { func (s *SGuestSnapshotDeleteTask) onSnapshotBlkdevFail(res string) { snapshotPath := path.Join(s.disk.GetSnapshotDir(), s.convertSnapshot) - if err := exec.Command("rm", "-f", s.tmpPath, snapshotPath).Run(); err != nil { + if _, err := procutils.NewCommand("rm", "-f", s.tmpPath, snapshotPath).Run(); err != nil { log.Errorln(err) } s.taskFailed("Reload blkdev failed") @@ -872,14 +872,14 @@ func (s *SGuestSnapshotDeleteTask) onSnapshotBlkdevFail(res string) { func (s *SGuestSnapshotDeleteTask) onResumeSucc(res string) { log.Infof("guest do new snapshot task resume succ %s", res) if len(s.tmpPath) > 0 { - err := exec.Command("rm", "-f", s.tmpPath).Run() + _, err := procutils.NewCommand("rm", "-f", s.tmpPath).Run() if err != nil { log.Errorln(err) } } if !s.pendingDelete { snapshotDir := s.disk.GetSnapshotDir() - exec.Command("rm", "-f", path.Join(snapshotDir, s.deleteSnapshot)) + procutils.NewCommand("rm", "-f", path.Join(snapshotDir, s.deleteSnapshot)) } hostutils.TaskComplete(s.ctx, jsonutils.NewDict(jsonutils.NewPair("deleted", jsonutils.JSONTrue))) diff --git a/pkg/hostman/guestman/qemu-kvm.go b/pkg/hostman/guestman/qemu-kvm.go index c3c6f9c92b..31d87d43dd 100644 --- a/pkg/hostman/guestman/qemu-kvm.go +++ b/pkg/hostman/guestman/qemu-kvm.go @@ -6,7 +6,6 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "path" "strconv" "strings" @@ -29,6 +28,7 @@ import ( "yunion.io/x/onecloud/pkg/util/cgrouputils" "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/netutils2" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/timeutils2" "yunion.io/x/onecloud/pkg/util/version" ) @@ -85,7 +85,8 @@ func (s *SKVMGuestInstance) HomeDir() string { } func (s *SKVMGuestInstance) PrepareDir() error { - return exec.Command("mkdir", "-p", s.HomeDir()).Run() + _, err := procutils.NewCommand("mkdir", "-p", s.HomeDir()).Run() + return err } func (s *SKVMGuestInstance) GetPidFilePath() string { @@ -263,6 +264,9 @@ func (s *SKVMGuestInstance) GetStopScriptPath() string { } func (s *SKVMGuestInstance) ImportServer(pendingDelete bool) { + s.manager.Servers[s.GetId()] = s + delete(s.manager.CandidateServers, s.GetId()) + if s.IsDirtyShotdown() && !pendingDelete { log.Infof("Server dirty shotdown %s", s.GetName()) if jsonutils.QueryBoolean(s.Desc, "is_master", false) || @@ -513,13 +517,13 @@ func (s *SKVMGuestInstance) StartDelete(ctx context.Context, migrated bool) erro func (s *SKVMGuestInstance) ForceStop() bool { s.ExitCleanup(true) if s.IsRunning() { - err := exec.Command("kill", "-9", fmt.Sprintf("%d", s.GetPid())).Run() + _, err := procutils.NewCommand("kill", "-9", fmt.Sprintf("%d", s.GetPid())).Run() if err != nil { log.Errorln(err) return false } for _, f := range s.GetCleanFiles() { - err := exec.Command("rm", "-f", f).Run() + _, err := procutils.NewCommand("rm", "-f", f).Run() if err != nil { log.Errorln(err) return false @@ -575,7 +579,8 @@ func (s *SKVMGuestInstance) Delete(ctx context.Context, migrated bool) error { if err := s.delTmpDisks(ctx, migrated); err != nil { return err } - return exec.Command("rm", "-rf", s.HomeDir()).Run() + _, err := procutils.NewCommand("rm", "-rf", s.HomeDir()).Run() + return err } func (s *SKVMGuestInstance) Stop() bool { @@ -588,7 +593,7 @@ func (s *SKVMGuestInstance) Stop() bool { } func (s *SKVMGuestInstance) scriptStart() error { - err := exec.Command("sh", s.GetStartScriptPath()).Run() + _, err := procutils.NewCommand("sh", s.GetStartScriptPath()).Run() if err != nil { s.scriptStop() return err @@ -597,7 +602,7 @@ func (s *SKVMGuestInstance) scriptStart() error { } func (s *SKVMGuestInstance) scriptStop() bool { - err := exec.Command("sh", s.GetStopScriptPath()).Run() + _, err := procutils.NewCommand("sh", s.GetStopScriptPath()).Run() if err != nil { log.Errorln(err) return false @@ -968,16 +973,16 @@ func (s *SKVMGuestInstance) ListStateFilePaths() []string { // 好像不用了 func (s *SKVMGuestInstance) CleanStatefiles() { for _, stateFile := range s.ListStateFilePaths() { - if err := exec.Command("mountpoint", stateFile).Run(); err == nil { - if err = exec.Command("umount", stateFile).Run(); err != nil { + if _, err := procutils.NewCommand("mountpoint", stateFile).Run(); err == nil { + if _, err = procutils.NewCommand("umount", stateFile).Run(); err != nil { log.Errorln(err) } } - if err := exec.Command("rm", "-rf", stateFile).Run(); err != nil { + if _, err := procutils.NewCommand("rm", "-rf", stateFile).Run(); err != nil { log.Errorln(err) } } - if err := exec.Command("rm", "-rf", s.GetFuseTmpPath()); err != nil { + if _, err := procutils.NewCommand("rm", "-rf", s.GetFuseTmpPath()).Run(); err != nil { log.Errorln(err) } } diff --git a/pkg/hostman/hostinfo/hostbridge/hostbridge.go b/pkg/hostman/hostinfo/hostbridge/hostbridge.go index fa70f7a1c8..ae7cd4bcbb 100644 --- a/pkg/hostman/hostinfo/hostbridge/hostbridge.go +++ b/pkg/hostman/hostinfo/hostbridge/hostbridge.go @@ -4,7 +4,6 @@ import ( "fmt" "net" "os" - "os/exec" "strings" "syscall" @@ -17,6 +16,7 @@ import ( "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/netutils2" "yunion.io/x/onecloud/pkg/util/ovsutils" + "yunion.io/x/onecloud/pkg/util/procutils" ) type IBridgeDriver interface { @@ -71,7 +71,7 @@ func (d *SBaseBridgeDriver) BringupInterface() error { if options.HostOptions.TunnelPaddingBytes > 0 { cmd = append(cmd, "mtu", fmt.Sprintf("%d", options.HostOptions.TunnelPaddingBytes)) } - if err := exec.Command(cmd[0], cmd[1:]...).Run(); err != nil { + if _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run(); err != nil { return err } } @@ -139,12 +139,12 @@ func (d *SBaseBridgeDriver) SetupAddresses(mask net.IPMask) error { if options.HostOptions.TunnelPaddingBytes > 0 { cmd = append(cmd, "mtu", fmt.Sprintf("%d", options.HostOptions.TunnelPaddingBytes+1500)) } - if err := exec.Command(cmd[0], cmd[1:]...).Run(); err != nil { + if _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run(); err != nil { log.Errorln(err) return fmt.Errorf("Failed to bring up bridge %s", d.bridge) } if d.inter != nil { - if err := exec.Command("ifconfig", d.inter.String(), "0", "up").Run(); err != nil { + if _, err := procutils.NewCommand("ifconfig", d.inter.String(), "0", "up").Run(); err != nil { log.Errorln(err) return fmt.Errorf("Failed to bring up interface %s", d.inter) } @@ -156,13 +156,13 @@ func (d *SBaseBridgeDriver) SetupSlaveAddresses(slaveAddrs [][]string) error { for _, slaveAddr := range slaveAddrs { cmd := []string{"ip", "address", "del", fmt.Sprintf("%s/%s", slaveAddr[0], slaveAddr[1]), "dev", d.inter.String()} - if err := exec.Command(cmd[0], cmd[1:]...).Run(); err != nil { + if _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run(); err != nil { log.Errorln("Failed to remove slave address from interface %s: %s", d.inter, err) } cmd = []string{"ip", "address", "add", fmt.Sprintf("%s/%s", slaveAddr[0], slaveAddr[1]), "dev", d.bridge.String()} - if err := exec.Command(cmd[0], cmd[1:]...).Run(); err != nil { + if _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run(); err != nil { return fmt.Errorf("Failed to remove slave address from interface %s: %s", d.bridge, err) } } @@ -177,7 +177,7 @@ func (d *SBaseBridgeDriver) SetupRoutes(routes [][]string) error { } else { cmd = []string{"route", "add", "-net", r[0], "netmask", r[2], "gw", r[1], "dev", d.bridge.String()} } - if err := exec.Command(cmd[0], cmd[1:]...).Run(); err != nil { + if _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run(); err != nil { log.Errorln(err) return fmt.Errorf("Failed to add slave address to bridge %s", d.bridge) } @@ -190,7 +190,7 @@ type SOVSBridgeDriver struct { } func (o *SOVSBridgeDriver) Exists() bool { - data, err := exec.Command("ovs-vsctl", "list-br").Output() + data, err := procutils.NewCommand("ovs-vsctl", "list-br").Run() if err != nil { log.Errorln(err) return false @@ -204,7 +204,7 @@ func (o *SOVSBridgeDriver) Exists() bool { } func (o *SOVSBridgeDriver) Interfaces() []string { - data, err := exec.Command("ovs-vsctl", "list-ifaces", o.bridge.String()).Output() + data, err := procutils.NewCommand("ovs-vsctl", "list-ifaces", o.bridge.String()).Run() if err != nil { log.Errorln(err) return nil @@ -259,7 +259,8 @@ func (o *SOVSBridgeDriver) Setup() error { func (o *SOVSBridgeDriver) SetupBridgeDev() error { if !o.Exists() { - return exec.Command("ovs-vsctl", "--", "--may-exist", "add-br", o.bridge.String()).Run() + _, err := procutils.NewCommand("ovs-vsctl", "--", "--may-exist", "add-br", o.bridge.String()).Run() + return err } return nil } @@ -395,8 +396,9 @@ func (o *SOVSBridgeDriver) AddFlow(cond string, priority int, actions string) st } func (o *SOVSBridgeDriver) DoAddFlow(cond string, pri int, actions, swt string) error { - return exec.Command("ovs-ofctl", "add-flow", swt, + _, err := procutils.NewCommand("ovs-ofctl", "add-flow", swt, fmt.Sprintf("%s priority=%d actions=%s", cond, pri, actions)).Run() + return err } func (o *SOVSBridgeDriver) DelFlow(cond string) string { @@ -429,12 +431,14 @@ func (o *SOVSBridgeDriver) RegisterHostlocalServer(mac, ip string) error { if !options.HostOptions.EnableOpenflowController { metadataPort := o.GetMetadataServerPort() if err := o.DoAddFlow("table=0 ipv6", 20000, "drop", o.bridge.String()); err != nil { + log.Errorln(err) return err } if err := o.DoAddFlow("table=0 tcp nw_dst=169.254.169.254 tp_dst=80", 10000, fmt.Sprintf("mod_dl_dst:%s,mod_nw_dst:%s,mod_tp_dst:%d,local", mac, ip, metadataPort), o.bridge.String()); err != nil { + log.Errorln(err) return err } log.Infof("OVS: metadata server %s:%d", ip, metadataPort) @@ -447,19 +451,24 @@ func (o *SOVSBridgeDriver) RegisterHostlocalServer(mac, ip string) error { return err } k8sCidr = fmt.Sprintf("%s/%d", addr, mask) + log.Infof("OVS: Kubernetes cluster IP range: %s", k8sCidr) err = o.DoAddFlow(fmt.Sprintf("table=0 ip,nw_dst=%s", k8sCidr), 10050, fmt.Sprintf("mod_dl_dst:%s,local", mac), o.bridge.String()) if err != nil { + log.Errorln(err) return err } - err = o.DoAddFlow("table=0", 0, "resubmit(,1)", o.bridge.String()) - if err != nil { - return err - } - err = o.DoAddFlow("table=1", 0, "normal", o.bridge.String()) - if err != nil { - return err - } + } + + err := o.DoAddFlow("table=0", 0, "resubmit(,1)", o.bridge.String()) + if err != nil { + log.Errorln(err) + return err + } + err = o.DoAddFlow("table=1", 0, "normal", o.bridge.String()) + if err != nil { + log.Errorln(err) + return err } } return nil @@ -468,7 +477,7 @@ func (o *SOVSBridgeDriver) RegisterHostlocalServer(mac, ip string) error { func (o *SOVSBridgeDriver) ovsSetParams(params map[string]map[string]string) { for tbl, tblval := range params { for k, v := range tblval { - exec.Command("ovs-vsctl", "set", tbl, o.bridge.String(), + procutils.NewCommand("ovs-vsctl", "set", tbl, o.bridge.String(), fmt.Sprintf("%s=%s", k, v)).Run() } } diff --git a/pkg/hostman/hostinfo/hostinfo.go b/pkg/hostman/hostinfo/hostinfo.go index 10a94bddc4..85995ed2e4 100644 --- a/pkg/hostman/hostinfo/hostinfo.go +++ b/pkg/hostman/hostinfo/hostinfo.go @@ -29,6 +29,7 @@ import ( "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/httputils" "yunion.io/x/onecloud/pkg/util/netutils2" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/qemutils" "yunion.io/x/onecloud/pkg/util/sysutils" "yunion.io/x/onecloud/pkg/util/timeutils2" @@ -167,12 +168,12 @@ func (h *SHostInfo) prepareEnv() error { return fmt.Errorf("Option report_interval must no longer than 5 min") } - _, err := exec.Command("mkdir", "-p", options.HostOptions.ServersPath).Output() + _, err := procutils.NewCommand("mkdir", "-p", options.HostOptions.ServersPath).Run() if err != nil { return fmt.Errorf("Failed to create path %s", options.HostOptions.ServersPath) } - _, err = exec.Command(qemutils.GetQemu(""), "-version").Output() + _, err = procutils.NewCommand(qemutils.GetQemu(""), "-version").Run() if err != nil { return fmt.Errorf("Qemu/Kvm not installed") } @@ -192,11 +193,11 @@ func (h *SHostInfo) prepareEnv() error { ioParams["queue/iosched/quantum"] = "32" } fileutils2.ChangeAllBlkdevsParams(ioParams) - _, err = exec.Command("modprobe", "tun").Output() + _, err = procutils.NewCommand("modprobe", "tun").Run() if err != nil { return fmt.Errorf("Failed to activate tun/tap device") } - _, err = exec.Command("modprobe", "vhost_net").Output() + _, err = procutils.NewCommand("modprobe", "vhost_net").Run() if err != nil { e := err.(*exec.ExitError) log.Errorln(e.Stderr) @@ -205,12 +206,12 @@ func (h *SHostInfo) prepareEnv() error { return fmt.Errorf("Cannot initialize control group subsystem") } - _, err = exec.Command("rmmod", "nbd").Output() + _, err = procutils.NewCommand("rmmod", "nbd").Run() if err != nil { e := err.(*exec.ExitError) log.Errorln(e.Stderr) } - _, err = exec.Command("modprobe", "nbd", "max_part=16").Output() + _, err = procutils.NewCommand("modprobe", "nbd", "max_part=16").Run() if err != nil { e := err.(*exec.ExitError) log.Errorf("Failed to activate nbd device: %s", e.Stderr) @@ -256,7 +257,7 @@ func (h *SHostInfo) prepareEnv() error { } func (h *SHostInfo) detectHostInfo() error { - output, err := exec.Command("dmidecode", "-t", "1").Output() + output, err := procutils.NewCommand("dmidecode", "-t", "1").Run() if err != nil { return err } @@ -277,11 +278,12 @@ func (h *SHostInfo) detectHostInfo() error { h.detectiveStorageSystem() - if options.HostOptions.CheckSystemServices { - if err := h.checkSystemServices(); err != nil { - return err - } - } + // TODO + // if options.HostOptions.CheckSystemServices { + // if err := h.checkSystemServices(); err != nil { + // return err + // } + // } return nil } @@ -350,11 +352,10 @@ func (h *SHostInfo) EnableNativeHugepages() error { h.setSysConfig(k, v) } preAllocPagesNum := h.GetMemory()/h.Mem.GetHugepagesizeMb() + 1 - cmd := timeutils2.CommandWithTimeout(1, "sh", "-c", fmt.Sprintf("echo %d > /proc/sys/vm/nr_hugepages", preAllocPagesNum)) - _, err := cmd.Output() + err := timeutils2.CommandWithTimeout(1, "sh", "-c", fmt.Sprintf("echo %d > /proc/sys/vm/nr_hugepages", preAllocPagesNum)).Run() if err != nil { log.Errorln(err) - _, err = exec.Command("sh", "-c", "echo 0 > /proc/sys/vm/nr_hugepages").Output() + _, err = procutils.NewCommand("sh", "-c", "echo 0 > /proc/sys/vm/nr_hugepages").Run() if err != nil { log.Warningf(err.Error()) } @@ -406,7 +407,7 @@ func (h *SHostInfo) TuneSystem() { func (h *SHostInfo) resetIptables() error { for _, tbl := range []string{"filter", "nat", "mangle"} { - err := exec.Command("iptables", "-t", tbl, "-F").Run() + _, err := procutils.NewCommand("iptables", "-t", tbl, "-F").Run() if err != nil { return fmt.Errorf("Fail to clean NAT iptable: %s", err) } @@ -439,7 +440,7 @@ func (h *SHostInfo) modprobeKvmModule(name string, remove, nest bool) bool { if nest { params = append(params, "nested=1") } - if err := exec.Command(params[0], params[1:]...).Run(); err != nil { + if _, err := procutils.NewCommand(params[0], params[1:]...).Run(); err != nil { return false } return true @@ -472,7 +473,7 @@ func (h *SHostInfo) _detectiveNestSupport() string { } func (h *SHostInfo) _isNestSupport(name string) bool { - output, err := exec.Command("modinfo", name).Output() + output, err := procutils.NewCommand("modinfo", name).Run() if err != nil { log.Errorln(err) return false @@ -543,7 +544,7 @@ func (h *SHostInfo) getModuleParameter(name, moduel string) string { } func (h *SHostInfo) checkKvmModuleInstall(name string) bool { - output, err := exec.Command("lsmod").Output() + output, err := procutils.NewCommand("lsmod").Run() if err != nil { log.Errorln(err) return false @@ -558,14 +559,14 @@ func (h *SHostInfo) checkKvmModuleInstall(name string) bool { } func (h *SHostInfo) detectiveOsDist() { - files, err := exec.Command("ls", "/etc/*elease").Output() + files, err := procutils.NewCommand("sh", "-c", "ls /etc/*elease").Run() if err != nil { log.Errorln(err) return } re := regexp.MustCompile(`(.+) release ([\d.]+)[^(]*(?:\((.+)\))?`) - for _, file := range strings.Split(string(files), " ") { - content, err := fileutils2.FileGetContents(path.Join("/etc", file)) + for _, file := range strings.Split(string(files), "\n") { + content, err := fileutils2.FileGetContents(file) if err != nil { continue } @@ -576,13 +577,14 @@ func (h *SHostInfo) detectiveOsDist() { break } } + log.Infof("DetectiveOsDist %s %s", h.sysinfo.OsDistribution, h.sysinfo.OsVersion) if len(h.sysinfo.OsDistribution) == 0 { log.Errorln("Failed to detect distribution info") } } func (h *SHostInfo) detectiveKernelVersion() { - out, err := exec.Command("uname", "-r").Output() + out, err := procutils.NewCommand("uname", "-r").Run() if err != nil { log.Errorln(err) } @@ -601,15 +603,17 @@ func (h *SHostInfo) detectiveSyssoftwareInfo() error { func (h *SHostInfo) detectiveQemuVersion() error { cmd := qemutils.GetQemu(options.HostOptions.DefaultQemuVersion) - version, err := exec.Command(cmd, "--version").Output() + version, err := procutils.NewCommand(cmd, "--version").Run() if err != nil { log.Errorln(err) return err } else { - re := regexp.MustCompile(`(?i)(?<=version\s)[\d.]+`) - v := re.FindStringSubmatch(string(version)) + versions := strings.Split(string(version), "\n") + parts := strings.Split(versions[0], " ") + v := parts[len(parts)-1] if len(v) > 0 { - h.sysinfo.QemuVersion = v[0] + log.Infof("Detect qemu version is %s", v) + h.sysinfo.QemuVersion = v } else { return fmt.Errorf("Failed to detect qemu version") } @@ -618,14 +622,16 @@ func (h *SHostInfo) detectiveQemuVersion() error { } func (h *SHostInfo) detectiveOvsVersion() { - version, err := exec.Command("ovs-vsctl", "--version").Output() + version, err := procutils.NewCommand("ovs-vsctl", "--version").Run() if err != nil { log.Errorln(err) } else { - re := regexp.MustCompile(`'(?i)(?<=\(open vswitch\)\s)[\d.]+'`) - v := re.FindStringSubmatch(string(version)) + versions := strings.Split(string(version), "\n") + parts := strings.Split(versions[0], " ") + v := parts[len(parts)-1] if len(v) > 0 { - h.sysinfo.OvsVersion = v[0] + log.Infof("Detect OVS version is %s", v) + h.sysinfo.OvsVersion = v } else { log.Errorln("Failed to detect ovs version") } @@ -759,7 +765,7 @@ func (h *SHostInfo) getHostInfo(zoneId string) { func (h *SHostInfo) setHostname(name string) { h.FullName = name - err := exec.Command("hostnamectl", "set-hostname", name).Run() + _, err := procutils.NewCommand("hostnamectl", "set-hostname", name).Run() if err != nil { log.Errorln("Fail to set system hostname: %s", err) } @@ -780,7 +786,7 @@ func (h *SHostInfo) getSysInfo() *SSysInfo { func (h *SHostInfo) updateHostRecord(hostId string) { var method, url string - if len(hostId) > 0 { + if len(hostId) == 0 { method = "POST" url = fmt.Sprintf("/zones/%s/hosts", h.ZoneId) } else { @@ -977,7 +983,7 @@ func (h *SHostInfo) doSyncNicInfo(nic *SNIC) { content := jsonutils.NewDict() content.Set("bridge", jsonutils.NewString(nic.Bridge)) content.Set("interface", jsonutils.NewString(nic.Inter)) - _, err := modules.Hostwires.Patch(h.GetSession(), + _, err := modules.Hostwires.Update(h.GetSession(), h.HostId, nic.Network, content) if err != nil { log.Errorln(err) @@ -1051,7 +1057,7 @@ func (h *SHostInfo) getStorageInfo() { params := jsonutils.NewDict() params.Set("details", jsonutils.JSONTrue) params.Set("limit", jsonutils.NewInt(0)) - res, err := modules.Hoststorages.ListAscendent( + res, err := modules.Hoststorages.ListDescendent( h.GetSession(), h.HostId, params) if err != nil { @@ -1065,26 +1071,35 @@ func (h *SHostInfo) getStorageInfo() { func (h *SHostInfo) onGetStorageInfoSucc(hoststorages []jsonutils.JSONObject) { var detachStorages = []jsonutils.JSONObject{} storageManager := storageman.GetManager() + for _, hs := range hoststorages { + storagetype, _ := hs.GetString("storage_type") mountPoint, _ := hs.GetString("mount_point") storagecacheId, _ := hs.GetString("storagecache_id") - storagetype, _ := hs.GetString("storage_type") imagecachePath, _ := hs.GetString("imagecache_path") storageId, _ := hs.GetString("storage_id") storageName, _ := hs.GetString("storage") storageConf, _ := hs.Get("storage_conf") - storage := storageManager.NewSharedStorageInstance(mountPoint, storagetype) - if storage != nil { - storageManager.Storages = append(storageManager.Storages, storage) - } - storageManager.InitSharedStorageImageCache(storagetype, - storagecacheId, imagecachePath, storage) - storage = storageManager.GetStorageByPath(mountPoint) - if storage != nil { - storage.SetStorageInfo(storageId, storageName, storageConf) - } else if storagetype != storagetypes.STORAGE_BAREMETAL { - detachStorages = append(detachStorages, hs) + if !utils.IsInStringArray(storagetype, storagetypes.Local) { + storage := storageManager.NewSharedStorageInstance(mountPoint, storagetype) + if storage != nil { + storageManager.Storages = append(storageManager.Storages, storage) + storageManager.InitSharedStorageImageCache( + storagetype, storagecacheId, imagecachePath, storage) + storage.SetStorageInfo(storageId, storageName, storageConf) + } + } else { + // Storage type local + storage := storageManager.GetStorageByPath(mountPoint) + if storage != nil { + storage.SetStorageInfo(storageId, storageName, storageConf) + } else { + // XXX hack: storage type baremetal is a converted host,reserve storage + if storagetype != storagetypes.STORAGE_BAREMETAL { + detachStorages = append(detachStorages, hs) + } + } } } @@ -1204,19 +1219,25 @@ func (h *SHostInfo) StartPinger() { func (h *SHostInfo) save() error { if h.saved { return nil + } else { + h.saved = true } - h.saved = true + if err := h.registerHostlocalServer(); err != nil { return err } // TODO XXX >>> ??? // file put content - return h.setupBridges() + if err := h.setupBridges(); err != nil { + return err + } + return nil } func (h *SHostInfo) setupBridges() error { for _, n := range h.Nics { if err := n.BridgeDev.WarmupConfig(); err != nil { + log.Errorln(err) return err } } @@ -1236,7 +1257,6 @@ func (h *SHostInfo) registerHostlocalServer() error { err := n.BridgeDev.RegisterHostlocalServer(mac, ip) if err != nil { - log.Errorln(err) return err } } diff --git a/pkg/hostman/hostinfo/hostinfohelper.go b/pkg/hostman/hostinfo/hostinfohelper.go index 5506fe9e3f..0e4a038303 100644 --- a/pkg/hostman/hostinfo/hostinfohelper.go +++ b/pkg/hostman/hostinfo/hostinfohelper.go @@ -4,7 +4,6 @@ import ( "bufio" "context" "os" - "os/exec" "regexp" "strconv" "strings" @@ -25,6 +24,7 @@ import ( "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/netutils2" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/sysutils" ) @@ -65,7 +65,7 @@ func DetectCpuInfo() (*SCPUInfo, error) { log.Errorln(err) return nil, err } - bret, err := exec.Command("dmidecode", "-t", "4").Output() + bret, err := procutils.NewCommand("dmidecode", "-t", "4").Run() if err != nil { log.Errorln(err) return nil, err @@ -129,7 +129,7 @@ func DetectMemoryInfo() (*SMemory, error) { smem.Total = int(info.Total / 1024 / 1024) smem.Free = int(info.Available / 1024 / 1024) smem.Used = smem.Total - smem.Free - ret, err := exec.Command("dmidecode", "-t", "17").Output() + ret, err := procutils.NewCommand("dmidecode", "-t", "17").Run() if err != nil { return nil, err } @@ -182,7 +182,7 @@ func (n *SNIC) EnableDHCPRelay() bool { log.Errorln(err) return false } - if options.HostOptions.DhcpRelay != nil && netutils.IsExitAddress(v4Ip) { + if len(options.HostOptions.GoDhcpRelay) > 0 && !netutils.IsExitAddress(v4Ip) { return true } else { return false @@ -213,6 +213,7 @@ func NewNIC(desc string) (*SNIC, error) { } nic.Bandwidth = 1000 + log.Infof("IP %s/%s/%s", nic.Ip, nic.Bridge, nic.Inter) // 这是干啥呢 ??? if len(nic.Ip) > 0 { var max, wait = 30, 0 @@ -257,10 +258,10 @@ func NewNIC(desc string) (*SNIC, error) { var dhcpRelay []string if nic.EnableDHCPRelay() { - dhcpRelay = options.HostOptions.DhcpRelay + dhcpRelay = options.HostOptions.GoDhcpRelay } nic.dhcpServer = hostdhcp.NewGuestDHCPServer(nic.Bridge, dhcpRelay) - nic.dhcpServer.Start() + go nic.dhcpServer.Start() return nic, nil } diff --git a/pkg/hostman/hostutils/hostutils.go b/pkg/hostman/hostutils/hostutils.go index 7c650a39ff..24b8ea5831 100644 --- a/pkg/hostman/hostutils/hostutils.go +++ b/pkg/hostman/hostutils/hostutils.go @@ -87,11 +87,9 @@ func RemoteStoragecacheCacheImage(ctx context.Context, storagecacheId, imageId, } func UpdateServerStatus(ctx context.Context, sid, status string) (jsonutils.JSONObject, error) { - var body = jsonutils.NewDict() var stats = jsonutils.NewDict() stats.Set("status", jsonutils.NewString(status)) - body.Set("server", stats) - return modules.Servers.PerformAction(GetComputeSession(ctx), sid, "status", body) + return modules.Servers.PerformAction(GetComputeSession(ctx), sid, "status", stats) } func ResponseOk(ctx context.Context, w http.ResponseWriter) { diff --git a/pkg/hostman/options/options.go b/pkg/hostman/options/options.go index 47ce2ca907..4bd2d60576 100644 --- a/pkg/hostman/options/options.go +++ b/pkg/hostman/options/options.go @@ -52,7 +52,7 @@ type SHostOptions struct { DefaultQemuVersion string `help:"Default qemu version" default:"2.9.1"` // dhcp_relay = ('10.168.222.236', 67) => dhcp_relay = ['10.168.222.236', '67'] - DhcpRelay []string `help:"DHCP relay upstream"` + GoDhcpRelay []string `help:"DHCP relay upstream"` TunnelPaddingBytes int64 `help:"Specify tunnel padding bytes" default:"0"` CheckSystemServices bool `help:"Check system services (ntpd, telegraf) on startup" default:"true"` diff --git a/pkg/hostman/service/host_services.go b/pkg/hostman/service/host_services.go index cc19f99e0b..44ae00ff79 100644 --- a/pkg/hostman/service/host_services.go +++ b/pkg/hostman/service/host_services.go @@ -5,6 +5,7 @@ import ( "os" "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/appsrv" "yunion.io/x/onecloud/pkg/cloudcommon" "yunion.io/x/onecloud/pkg/cloudcommon/service" @@ -25,11 +26,12 @@ type SHostService struct { func (host *SHostService) StartService() { cloudcommon.ParseOptions(&options.HostOptions, os.Args, "host.conf", "host") + // disable rbac + options.HostOptions.EnableRbac = false + app := cloudcommon.InitApp(&options.HostOptions.CommonOptions, false) host.TrapSignals(func() { host.quitSignalHandler(app) }) - // isolatedman.Init() - hostInstance := hostinfo.Instance() if err := hostInstance.Init(); err != nil { log.Fatalf(err.Error()) @@ -59,12 +61,15 @@ func (host *SHostService) StartService() { } func (host *SHostService) quitSignalHandler(app *appsrv.Application) { - log.Infof("Received quit signal") - err := app.ShowDown(context.Background()) - if err != nil { - log.Errorln(err.Error()) + if app.IsInServe() { + err := app.ShowDown(context.Background()) + if err != nil { + log.Errorln(err.Error()) + } } + hostutils.GetWorkManager().Stop() + os.Exit(0) } func (host *SHostService) initHandlers(app *appsrv.Application) { diff --git a/pkg/hostman/storageman/core.go b/pkg/hostman/storageman/core.go index fd7051ebe3..2ab42d65dc 100644 --- a/pkg/hostman/storageman/core.go +++ b/pkg/hostman/storageman/core.go @@ -132,7 +132,7 @@ func (s *SStorageManager) initLocalStorageImagecache() error { return err } } - if len(cachePath) == 0 { + if len(cachePath) > 0 { s.LocalStorageImagecacheManager = NewLocalImageCacheManager(s, cachePath, limit, true, "") return nil } else { diff --git a/pkg/hostman/storageman/disklocal.go b/pkg/hostman/storageman/disklocal.go index 21021cc682..4cc7639be4 100644 --- a/pkg/hostman/storageman/disklocal.go +++ b/pkg/hostman/storageman/disklocal.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "os/exec" "path" "yunion.io/x/jsonutils" @@ -17,6 +16,7 @@ import ( "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/fuseutils" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/qemuimg" "yunion.io/x/pkg/utils" ) @@ -71,10 +71,10 @@ func (d *SLocalDisk) Probe() error { func (d *SLocalDisk) UmountFuseImage() { mntPath := path.Join(d.Storage.GetFuseMountPath(), d.Id) - if err := exec.Command("umount", mntPath).Run(); err != nil { + if _, err := procutils.NewCommand("umount", mntPath).Run(); err != nil { log.Errorln(err) } - if err := exec.Command("rm", "-rf", mntPath); err != nil { + if _, err := procutils.NewCommand("rm", "-rf", mntPath).Run(); err != nil { log.Errorln(err) } } @@ -301,10 +301,10 @@ func (d *SLocalDisk) GetDiskSetupScripts(diskIndex int) string { func (d *SLocalDisk) PostCreateFromImageFuse() { mntPath := path.Join(d.Storage.GetFuseMountPath(), d.Id) - if err := exec.Command("umount", mntPath).Run(); err != nil { + if _, err := procutils.NewCommand("umount", mntPath).Run(); err != nil { log.Errorln(err) } - if err := exec.Command("rm", "-rf", mntPath).Run(); err != nil { + if _, err := procutils.NewCommand("rm", "-rf", mntPath).Run(); err != nil { log.Errorln(err) } } @@ -312,14 +312,14 @@ func (d *SLocalDisk) PostCreateFromImageFuse() { func (d *SLocalDisk) CreateSnapshot(snapshotId string) error { snapshotDir := d.GetSnapshotDir() if !fileutils2.Exists(snapshotDir) { - err := exec.Command("mkdir", "-p", snapshotDir).Run() + _, err := procutils.NewCommand("mkdir", "-p", snapshotDir).Run() if err != nil { log.Errorln(err) return err } } snapshotPath := path.Join(snapshotDir, snapshotId) - err := exec.Command("mv", "-f", d.getPath(), snapshotPath).Run() + _, err := procutils.NewCommand("mv", "-f", d.getPath(), snapshotPath).Run() if err != nil { log.Errorln(err) return err @@ -327,12 +327,12 @@ func (d *SLocalDisk) CreateSnapshot(snapshotId string) error { img, err := qemuimg.NewQemuImage(d.getPath()) if err != nil { log.Errorln(err) - exec.Command("mv", "-f", snapshotPath, d.getPath()).Run() + procutils.NewCommand("mv", "-f", snapshotPath, d.getPath()).Run() return err } if err := img.CreateQcow2(0, false, snapshotPath); err != nil { log.Errorf("Snapshot create image error %s", err) - exec.Command("mv", "-f", snapshotPath, d.getPath()).Run() + procutils.NewCommand("mv", "-f", snapshotPath, d.getPath()).Run() return err } return nil @@ -342,7 +342,7 @@ func (d *SLocalDisk) DeleteSnapshot(snapshotId, convertSnapshot string, pendingD snapshotDir := d.GetSnapshotDir() if len(convertSnapshot) > 0 { if !fileutils2.Exists(snapshotDir) { - err := exec.Command("mkdir", "-p", snapshotDir).Run() + _, err := procutils.NewCommand("mkdir", "-p", snapshotDir).Run() if err != nil { log.Errorln(err) return err @@ -351,7 +351,7 @@ func (d *SLocalDisk) DeleteSnapshot(snapshotId, convertSnapshot string, pendingD convertSnapshotPath := path.Join(snapshotDir, convertSnapshot) output := convertSnapshotPath + ".tmp" if fileutils2.Exists(output) { - exec.Command("rm", "-f", output).Run() + procutils.NewCommand("rm", "-f", output).Run() } img, err := qemuimg.NewQemuImage(convertSnapshotPath) if err != nil { @@ -360,19 +360,19 @@ func (d *SLocalDisk) DeleteSnapshot(snapshotId, convertSnapshot string, pendingD } if err = img.Convert2Qcow2To(output, true); err != nil { log.Errorln(err) - exec.Command("rm", "-f", output).Run() + procutils.NewCommand("rm", "-f", output).Run() return err } - if err = exec.Command("rm", "-f", convertSnapshotPath).Run(); err != nil { + if _, err = procutils.NewCommand("rm", "-f", convertSnapshotPath).Run(); err != nil { log.Errorln(err) return err } - if err = exec.Command("mv", "-f", output, convertSnapshotPath).Run(); err != nil { + if _, err = procutils.NewCommand("mv", "-f", output, convertSnapshotPath).Run(); err != nil { log.Errorln(err) return err } if !pendingDelete { - err = exec.Command("rm", "-f", path.Join(snapshotDir, snapshotId)).Run() + _, err = procutils.NewCommand("rm", "-f", path.Join(snapshotDir, snapshotId)).Run() if err != nil { log.Errorln(err) return err @@ -380,7 +380,7 @@ func (d *SLocalDisk) DeleteSnapshot(snapshotId, convertSnapshot string, pendingD } return nil } else { - err := exec.Command("rm", "-f", path.Join(snapshotDir, snapshotId)).Run() + _, err := procutils.NewCommand("rm", "-f", path.Join(snapshotDir, snapshotId)).Run() if err != nil { log.Errorln(err) return err @@ -398,14 +398,14 @@ func (d *SLocalDisk) PrepareSaveToGlance(ctx context.Context, params interface{} return nil, err } destDir := d.Storage.GetImgsaveBackupPath() - if err := exec.Command("mkdir", "-p", destDir).Run(); err != nil { + if _, err := procutils.NewCommand("mkdir", "-p", destDir).Run(); err != nil { log.Errorln(err) return nil, err } backupPath := path.Join(destDir, fmt.Sprintf("%s.%s", d.Id, appctx.AppContextTaskId(ctx))) - if err := exec.Command("cp", "--sparse=always", "-f", d.GetPath(), backupPath).Run(); err != nil { + if _, err := procutils.NewCommand("cp", "--sparse=always", "-f", d.GetPath(), backupPath).Run(); err != nil { log.Errorln(err) - exec.Command("rm", "-f", backupPath).Run() + procutils.NewCommand("rm", "-f", backupPath).Run() return nil, err } return jsonutils.NewDict(jsonutils.NewPair("backup", jsonutils.NewString(backupPath))), nil @@ -420,7 +420,7 @@ func (d *SLocalDisk) ResetFromSnapshot(ctx context.Context, params interface{}) snapshotDir := d.GetSnapshotDir() snapshotPath := path.Join(snapshotDir, resetParams.SnapshotId) diskTmpPath := d.GetPath() + "_reset.tmp" - if err := exec.Command("mv", "-f", d.GetPath(), diskTmpPath).Run(); err != nil { + if _, err := procutils.NewCommand("mv", "-f", d.GetPath(), diskTmpPath).Run(); err != nil { log.Errorln(err) return nil, err } @@ -428,22 +428,23 @@ func (d *SLocalDisk) ResetFromSnapshot(ctx context.Context, params interface{}) img, err := qemuimg.NewQemuImage(d.GetPath()) if err != nil { log.Errorln(err) - exec.Command("mv", "-f", diskTmpPath, d.GetPath()).Run() + procutils.NewCommand("mv", "-f", diskTmpPath, d.GetPath()).Run() return nil, err } if err := img.CreateQcow2(0, false, snapshotPath); err != nil { log.Errorln(err) - exec.Command("mv", "-f", diskTmpPath, d.GetPath()).Run() + procutils.NewCommand("mv", "-f", diskTmpPath, d.GetPath()).Run() return nil, err } } else { - if err := exec.Command("cp", "-f", snapshotPath, d.GetPath()).Run(); err != nil { + if _, err := procutils.NewCommand("cp", "-f", snapshotPath, d.GetPath()).Run(); err != nil { log.Errorln(err) - exec.Command("mv", "-f", diskTmpPath, d.GetPath()).Run() + procutils.NewCommand("mv", "-f", diskTmpPath, d.GetPath()).Run() return nil, err } } - return nil, exec.Command("rm", "-f", diskTmpPath).Run() + _, err := procutils.NewCommand("rm", "-f", diskTmpPath).Run() + return nil, err } func (d *SLocalDisk) CleanupSnapshots(ctx context.Context, params interface{}) (jsonutils.JSONObject, error) { @@ -465,8 +466,8 @@ func (d *SLocalDisk) CleanupSnapshots(ctx context.Context, params interface{}) ( log.Errorln(err) return nil, err } - if exec.Command("mv", "-f", output, snapshotPath).Run(); err != nil { - exec.Command("rm", "-f", output).Run() + if procutils.NewCommand("mv", "-f", output, snapshotPath).Run(); err != nil { + procutils.NewCommand("rm", "-f", output).Run() log.Errorln(err) return nil, err } @@ -474,7 +475,7 @@ func (d *SLocalDisk) CleanupSnapshots(ctx context.Context, params interface{}) ( for _, snapshotId := range cleanupParams.DeleteSnapshots { snapId, _ := snapshotId.GetString() - if err := exec.Command("rm", "-f", path.Join(snapshotDir, snapId)).Run(); err != nil { + if _, err := procutils.NewCommand("rm", "-f", path.Join(snapshotDir, snapId)).Run(); err != nil { log.Errorln(err) return nil, err } @@ -485,7 +486,8 @@ func (d *SLocalDisk) CleanupSnapshots(ctx context.Context, params interface{}) ( func (d *SLocalDisk) DeleteAllSnapshot() error { snapshotDir := d.GetSnapshotDir() log.Infof("Delete disk(%s) snapshot dir %s", d.Id, snapshotDir) - return exec.Command("rm", "-rf", snapshotDir).Run() + _, err := procutils.NewCommand("rm", "-rf", snapshotDir).Run() + return err } func (d *SLocalDisk) PrepareMigrate(liveMigrate bool) (string, error) { diff --git a/pkg/hostman/storageman/diskutils.go b/pkg/hostman/storageman/diskutils.go index a982477c87..0117d99c8b 100644 --- a/pkg/hostman/storageman/diskutils.go +++ b/pkg/hostman/storageman/diskutils.go @@ -3,7 +3,6 @@ package storageman import ( "fmt" "io/ioutil" - "os/exec" "path" "path/filepath" "strings" @@ -15,6 +14,7 @@ import ( "yunion.io/x/onecloud/pkg/hostman/guestfs/fsdriver" "yunion.io/x/onecloud/pkg/hostman/storageman/nbd" "yunion.io/x/onecloud/pkg/util/fileutils2" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/qemutils" ) @@ -46,7 +46,7 @@ func (d *SKVMGuestDisk) Connect() bool { } else { cmd = []string{qemutils.GetQemuNbd(), "-c", d.nbdDev, d.imagePath} } - _, err := exec.Command(cmd[0], cmd[1:]...).Output() + _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run() if err != nil { log.Errorln(err.Error()) return false @@ -67,7 +67,7 @@ func (d *SKVMGuestDisk) Connect() bool { } func (d *SKVMGuestDisk) getImageFormat() string { - lines, err := exec.Command(qemutils.GetQemuImg(), "info", d.imagePath).Output() + lines, err := procutils.NewCommand(qemutils.GetQemuImg(), "info", d.imagePath).Run() if err != nil { return "" } @@ -107,7 +107,7 @@ func (d *SKVMGuestDisk) setupLVMS() error { func (d *SKVMGuestDisk) Disconnect() bool { if len(d.nbdDev) > 0 { // TODO?? PutdownLVMS ?? - err := exec.Command(qemutils.GetQemuNbd(), "-d", d.nbdDev).Run() + _, err := procutils.NewCommand(qemutils.GetQemuNbd(), "-d", d.nbdDev).Run() if err != nil { log.Errorln(err.Error()) return false diff --git a/pkg/hostman/storageman/imagecachemanager.go b/pkg/hostman/storageman/imagecachemanager.go index 7011e6087b..5dfb6bb164 100644 --- a/pkg/hostman/storageman/imagecachemanager.go +++ b/pkg/hostman/storageman/imagecachemanager.go @@ -5,11 +5,11 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "sync" "yunion.io/x/jsonutils" "yunion.io/x/onecloud/pkg/hostman/hostutils" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/pkg/util/regutils" ) @@ -63,7 +63,7 @@ func NewLocalImageCacheManager(manager *SStorageManager, cachePath string, limit imageCacheManager.cachedImages = make(map[string]IImageCache, 0) imageCacheManager.mutex = new(sync.Mutex) if _, err := os.Stat(cachePath); os.IsNotExist(err) { - exec.Command("mkdir", "-p", cachePath).Run() + procutils.NewCommand("mkdir", "-p", cachePath).Run() } imageCacheManager.loadCache() return imageCacheManager diff --git a/pkg/hostman/storageman/storagebase.go b/pkg/hostman/storageman/storagebase.go index 15f1c1ca95..a9524b5698 100644 --- a/pkg/hostman/storageman/storagebase.go +++ b/pkg/hostman/storageman/storagebase.go @@ -121,7 +121,9 @@ func (s *SBaseStorage) GetTotalSizeMb() int { func (s *SBaseStorage) SetStorageInfo(storageId, storageName string, conf jsonutils.JSONObject) { s.StorageId = storageId s.StorageName = storageName - s.StorageConf = conf.(*jsonutils.JSONDict) + if dconf, ok := conf.(*jsonutils.JSONDict); ok { + s.StorageConf = dconf + } } func (s *SBaseStorage) RemoveDisk(d IDisk) { diff --git a/pkg/hostman/storageman/storagelocal.go b/pkg/hostman/storageman/storagelocal.go index 528bfc738d..aa315f96d6 100644 --- a/pkg/hostman/storageman/storagelocal.go +++ b/pkg/hostman/storageman/storagelocal.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "os/exec" "path" "time" @@ -17,6 +16,7 @@ import ( "yunion.io/x/onecloud/pkg/hostman/storageman/remotefile" "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/util/fileutils2" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/qemuimg" "yunion.io/x/pkg/util/timeutils" ) @@ -121,7 +121,7 @@ func (s *SLocalStorage) StartSnapshotRecycle() { func (s *SLocalStorage) Accessible() bool { if !fileutils2.Exists(s.Path) { - if err := exec.Command("mkdir", "-p", s.Path).Run(); err != nil { + if _, err := procutils.NewCommand("mkdir", "-p", s.Path).Run(); err != nil { log.Errorln(err) } } @@ -138,12 +138,14 @@ func (s *SLocalStorage) DeleteDiskfile(diskpath string) error { destDir = s.getRecyclePath() destFile = fmt.Sprintf("%s.%d", path.Base(diskpath), time.Now().Unix()) ) - if err := exec.Command("mkdir", "-p", destDir).Run(); err != nil { + if _, err := procutils.NewCommand("mkdir", "-p", destDir).Run(); err != nil { return err } - return exec.Command("mv", "-f", diskpath, path.Join(destDir, destFile)).Run() + _, err := procutils.NewCommand("mv", "-f", diskpath, path.Join(destDir, destFile)).Run() + return err } else { - return exec.Command("rm", "-rf", diskpath).Run() + _, err := procutils.NewCommand("rm", "-rf", diskpath).Run() + return err } } @@ -177,10 +179,11 @@ func (s *SLocalStorage) SaveToGlance(ctx context.Context, params interface{}) (j imagecacheManager := s.Manager.LocalStorageImagecacheManager if len(imagecacheManager.GetId()) > 0 { - return nil, exec.Command("rm", "-f", imagePath).Run() + _, err := procutils.NewCommand("rm", "-f", imagePath).Run() + return nil, err } else { dstPath := path.Join(imagecacheManager.GetPath(), imageId) - if err := exec.Command("mv", imagePath, dstPath).Run(); err != nil { + if _, err := procutils.NewCommand("mv", imagePath, dstPath).Run(); err != nil { log.Errorf("Fail to move saved image to cache: %s", err) } imagecacheManager.LoadImageCache(imageId) diff --git a/pkg/util/cgrouputils/cgrouputils.go b/pkg/util/cgrouputils/cgrouputils.go index 051e3d00f0..498e20a006 100644 --- a/pkg/util/cgrouputils/cgrouputils.go +++ b/pkg/util/cgrouputils/cgrouputils.go @@ -5,7 +5,6 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "path" "path/filepath" "regexp" @@ -14,6 +13,7 @@ import ( "yunion.io/x/log" "yunion.io/x/onecloud/pkg/util/fileutils2" + "yunion.io/x/onecloud/pkg/util/procutils" ) const ( @@ -66,7 +66,8 @@ func getGroupPath() string { } func CgroupIsMounted() bool { - return exec.Command("mountpoint", cgroupsPath).Run() == nil + _, err := procutils.NewCommand("mountpoint", cgroupsPath).Run() + return err == nil } func ModuleIsMounted(module string) bool { @@ -81,7 +82,8 @@ func ModuleIsMounted(module string) bool { log.Errorln(err) } } - return exec.Command("mountpoint", fullPath).Run() == nil + _, err := procutils.NewCommand("mountpoint", fullPath).Run() + return err == nil } func RootTaskPath(module string) string { @@ -299,11 +301,11 @@ func (c *CGroupTask) PushPid(pid string, isRoot bool) { func (c *CGroupTask) init() bool { if !CgroupIsMounted() { if !fileutils2.Exists(cgroupsPath) { - if err := exec.Command("mkdir", "-p", cgroupsPath).Run(); err != nil { + if _, err := procutils.NewCommand("mkdir", "-p", cgroupsPath).Run(); err != nil { log.Errorln(err) } } - if err := exec.Command("mount", "-t", "tmpfs", "-o", "uid=0,gid=0,mode=0755", + if _, err := procutils.NewCommand("mount", "-t", "tmpfs", "-o", "uid=0,gid=0,mode=0755", "cgroup", cgroupsPath).Run(); err != nil { log.Errorln(err) return false @@ -326,13 +328,13 @@ func (c *CGroupTask) init() bool { if !ModuleIsMounted(module) { moduleDir := path.Join(cgroupsPath, module) if !fileutils2.Exists(moduleDir) { - if err := exec.Command("mkdir", moduleDir).Run(); err != nil { + if _, err := procutils.NewCommand("mkdir", moduleDir).Run(); err != nil { log.Errorln(err) return false } } log.Errorln(module) - if err := exec.Command("mount", "-t", "cgroup", "-o", + if _, err := procutils.NewCommand("mount", "-t", "cgroup", "-o", module, module, moduleDir).Run(); err != nil { log.Errorln(err) return false diff --git a/pkg/util/fileutils2/fileutils.go b/pkg/util/fileutils2/fileutils.go index 39efddb1e9..3fe6c950ff 100644 --- a/pkg/util/fileutils2/fileutils.go +++ b/pkg/util/fileutils2/fileutils.go @@ -16,6 +16,7 @@ import ( "golang.org/x/sys/unix" "yunion.io/x/log" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/regutils2" "yunion.io/x/pkg/utils" ) @@ -96,7 +97,7 @@ func FilePutContents(filename string, content string, modAppend bool) error { func IsBlockDevMounted(dev string) bool { devPath := "/dev/" + dev - mounts, err := exec.Command("mount").Output() + mounts, err := procutils.NewCommand("mount").Run() if err != nil { return false } @@ -413,7 +414,7 @@ func GetDevSector512Count(dev string) int { // TODO test func ResizeDiskFs(diskPath string, sizeMb int) error { var cmds = []string{"parted", "-a", "none", "-s", diskPath, "--", "unit", "s", "print"} - lines, err := exec.Command(cmds[0], cmds[1:]...).Output() + lines, err := procutils.NewCommand(cmds[0], cmds[1:]...).Run() if err != nil { log.Errorf("resize disk fs fail: %s", err) return err @@ -483,7 +484,7 @@ func ResizeDiskFs(diskPath string, sizeMb int) error { if len(part[1]) > 0 { cmds = append(cmds, "set", part[0], "boot", "on") } - err := exec.Command(cmds[0], cmds[1:]...).Run() + _, err := procutils.NewCommand(cmds[0], cmds[1:]...).Run() if err != nil { log.Errorln(err) return err @@ -500,7 +501,7 @@ func ResizeDiskFs(diskPath string, sizeMb int) error { func FsckExtFs(fpath string) bool { cmd := []string{"e2fsck", "-f", "-p", fpath} - if err := exec.Command(cmd[0], cmd[1:]...).Run(); err != nil { + if _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run(); err != nil { log.Errorln(err) return false } @@ -508,7 +509,7 @@ func FsckExtFs(fpath string) bool { } func FsckXfsFs(fpath string) bool { - if err := exec.Command("xfs_check", fpath).Run(); err != nil { + if _, err := procutils.NewCommand("xfs_check", fpath).Run(); err != nil { log.Errorln(err) exec.Command("xfs_repair", fpath).Run() return false @@ -537,7 +538,7 @@ func ResizePartitionFs(fpath, fs string) error { cmds = [][]string{{"resize2fs", fpath}} } else if fs == "xfs" { var tmpPoint = fmt.Sprintf("/tmp/%s", strings.Replace(fpath, "/", "_", -1)) - if err := exec.Command("mountpoint", tmpPoint).Run(); err == nil { + if _, err := procutils.NewCommand("mountpoint", tmpPoint).Run(); err == nil { err = exec.Command("umount", "-f", tmpPoint).Run() if err != nil { log.Errorln(err) @@ -557,7 +558,7 @@ func ResizePartitionFs(fpath, fs string) error { if len(cmds) > 0 { for _, cmd := range cmds { - err := exec.Command(cmd[0], cmd[1:]...).Run() + _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run() if err != nil { log.Errorln(err) return err @@ -568,7 +569,7 @@ func ResizePartitionFs(fpath, fs string) error { } func GetDevUuid(dev string) map[string]string { - lines, err := exec.Command("blkid", dev).Output() + lines, err := procutils.NewCommand("blkid", dev).Run() if err != nil { return nil } @@ -597,7 +598,7 @@ func GetDevOfPath(spath string) string { log.Errorln(err) return "" } - lines, err := exec.Command("mount").Output() + lines, err := procutils.NewCommand("mount").Run() if err != nil { log.Errorln(err) return "" @@ -630,7 +631,7 @@ func GetDevId(spath string) string { if len(dev) == 0 { return "" } - devInfo, err := exec.Command("ls", "-l", dev).Output() + devInfo, err := procutils.NewCommand("ls", "-l", dev).Run() if err != nil { log.Errorln(err) return "" diff --git a/pkg/util/fuseutils/fuseutils.go b/pkg/util/fuseutils/fuseutils.go index ef33ab3dad..8d1d2fbad5 100644 --- a/pkg/util/fuseutils/fuseutils.go +++ b/pkg/util/fuseutils/fuseutils.go @@ -3,13 +3,13 @@ package fuseutils import ( "fmt" "os" - "os/exec" "path" "strings" "time" "yunion.io/x/log" "yunion.io/x/onecloud/pkg/util/fileutils2" + "yunion.io/x/onecloud/pkg/util/procutils" ) const DEFAULT_BLOCKSIZE = 8 @@ -23,18 +23,18 @@ func MountFusefs(fetcherfsPath, url, tmpdir, token, mntpath string, blocksize in } // is mounted - if err := exec.Command("mountpoint", mntpath).Run(); err == nil { - exec.Command("umount", mntpath).Run() + if _, err := procutils.NewCommand("mountpoint", mntpath).Run(); err == nil { + procutils.NewCommand("umount", mntpath).Run() } if !fileutils2.Exists(tmpdir) { - if err := exec.Command("mkdir", "-p", tmpdir).Run(); err != nil { + if _, err := procutils.NewCommand("mkdir", "-p", tmpdir).Run(); err != nil { return err } } if !fileutils2.Exists(mntpath) { - if err := exec.Command("mkdir", "-p", mntpath).Run(); err != nil { + if _, err := procutils.NewCommand("mkdir", "-p", mntpath).Run(); err != nil { return err } } @@ -46,10 +46,10 @@ func MountFusefs(fetcherfsPath, url, tmpdir, token, mntpath string, blocksize in var cmd = []string{fetcherfsPath, "-s", "-p", opts, mntpath} log.Infof("%s", strings.Join(cmd, " ")) - err := exec.Command(cmd[0], cmd[1:]...).Run() + _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run() if err != nil { log.Errorf("Mount fetcherfs filed: %s", err) - exec.Command("umount", mntpath).Run() + procutils.NewCommand("umount", mntpath).Run() return err } diff --git a/pkg/util/netutils2/netutils.go b/pkg/util/netutils2/netutils.go index d5bd6a0ddf..ca8bb3085f 100644 --- a/pkg/util/netutils2/netutils.go +++ b/pkg/util/netutils2/netutils.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "net" - "os/exec" "reflect" "regexp" "strconv" @@ -17,6 +16,7 @@ import ( "yunion.io/x/pkg/util/regutils" "yunion.io/x/onecloud/pkg/cloudcommon/types" + "yunion.io/x/onecloud/pkg/util/procutils" "yunion.io/x/onecloud/pkg/util/regutils2" ) @@ -200,7 +200,7 @@ func (n *SNetInterface) FetchConfig() { n.Mac = inter.HardwareAddr.String() addrs, err := inter.Addrs() - if err != nil { + if err == nil { for _, addr := range addrs { if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { if ipnet.IP.To4() != nil { @@ -224,13 +224,13 @@ func (n *SNetInterface) FetchConfig() { } func (n *SNetInterface) DisableGso() { - err := exec.Command( + _, err := procutils.NewCommand( "ethtool", "-K", n.name, "tso", "off", "gso", "off", "gro", "off", "tx", "off", "rx", "off", "sg", "off").Run() if err != nil { - log.Errorln(err) + log.Errorln("DisableGso: ", err) } } @@ -254,7 +254,7 @@ func GetSecretInterfaceAddress() (string, []byte) { } func (n *SNetInterface) GetRoutes(gwOnly bool) [][]string { - output, err := exec.Command("route", "-n").Output() + output, err := procutils.NewCommand("route", "-n").Run() if err != nil { return nil } @@ -288,7 +288,7 @@ func (n *SNetInterface) getAddresses(output []string) [][]string { } func (n *SNetInterface) GetAddresses() [][]string { - output, err := exec.Command("ip", "address", "show", "dev", n.name).Output() + output, err := procutils.NewCommand("ip", "address", "show", "dev", n.name).Run() if err != nil { log.Errorln(err) return nil @@ -351,6 +351,7 @@ func Netmask2Len(mask string) int { for _, d := range data { if d != "0" { nle := netmask2len(d) + log.Errorln(d) if nle < 0 { return -1 } @@ -367,7 +368,7 @@ func PrefixSplit(pref string) (string, int, error) { if slash > 0 { ip := pref[:slash] mask := pref[slash+1:] - if regutils.MatchIPAddr(ip) { + if regutils.MatchIPAddr(mask) { intMask = Netmask2Len(mask) } else { intMask, err = strconv.Atoi(mask) diff --git a/pkg/util/netutils2/netutils_test.go b/pkg/util/netutils2/netutils_test.go index 3899b08b60..bbc0629f3c 100644 --- a/pkg/util/netutils2/netutils_test.go +++ b/pkg/util/netutils2/netutils_test.go @@ -158,3 +158,8 @@ func TestFormatMac(t *testing.T) { }) } } + +func TestNewNetInterface(t *testing.T) { + n := NewNetInterface("br0") + t.Logf("NetInterface: %s %s %s %s", n.name, n.Addr, n.Mask.String(), n.Mac) +} diff --git a/pkg/util/ovsutils/ovsutils.go b/pkg/util/ovsutils/ovsutils.go index 26da622e21..dfd08a5de2 100644 --- a/pkg/util/ovsutils/ovsutils.go +++ b/pkg/util/ovsutils/ovsutils.go @@ -1,17 +1,18 @@ package ovsutils import ( - "os/exec" "regexp" "strings" "yunion.io/x/log" - "yunion.io/x/onecloud/pkg/util/regutils2" "yunion.io/x/pkg/utils" + + "yunion.io/x/onecloud/pkg/util/procutils" + "yunion.io/x/onecloud/pkg/util/regutils2" ) func GetDbPorts(brname string) []string { - output, err := exec.Command("ovs-vsctl", "list-ifaces", brname).Output() + output, err := procutils.NewCommand("ovs-vsctl", "list-ifaces", brname).Run() if err != nil { log.Errorln(err) return nil @@ -28,7 +29,7 @@ func GetDbPorts(brname string) []string { } func GetDpPorts(brname string) []string { - output, err := exec.Command("ovs-dpctl", "show").Output() + output, err := procutils.NewCommand("ovs-dpctl", "show").Run() if err != nil { log.Errorln(err) return nil @@ -49,7 +50,7 @@ func GetDpPorts(brname string) []string { } func GetBridges() []string { - output, err := exec.Command("ovs_vsctl", "list-br").Output() + output, err := procutils.NewCommand("ovs-vsctl", "list-br").Run() if err != nil { log.Errorln(err) return nil @@ -67,7 +68,7 @@ func GetBridges() []string { func RemovePortFromBridge(brname, port string) { log.Infof("remove_port_from_bridge %s %s", brname, port) - if err := exec.Command("ovs-vsctl", "del-port", brname, port).Run(); err != nil { + if _, err := procutils.NewCommand("ovs-vsctl", "del-port", brname, port).Run(); err != nil { log.Errorln(err) } } diff --git a/pkg/util/procutils/procutils.go b/pkg/util/procutils/procutils.go index 7e7a5dbe01..339999c915 100644 --- a/pkg/util/procutils/procutils.go +++ b/pkg/util/procutils/procutils.go @@ -42,8 +42,18 @@ func Run(name string, args ...string) ([]string, error) { return ParseOutput(ret), nil } +// Doesn't have timeout func (c *Command) Run() ([]byte, error) { - output, err := RunCommand(c.Path, c.Args...) + output, err := RunCommandWithoutTimeout(c.Path, c.Args...) + if err != nil { + log.Errorf("Execute command %q , error: %v , output: %s", c, err, string(output)) + } + return output, err +} + +// Have default timeout 3 * time.Second +func (c *Command) RunWithTimeout() ([]byte, error) { + output, err := RunCommandWithTimeout(c.Path, c.Args...) if err != nil { log.Errorf("Execute command %q , error: %v , output: %s", c, err, string(output)) } @@ -64,7 +74,11 @@ func (c *Command) String() string { return strings.Join(ss, " ") } -func RunCommand(name string, args ...string) ([]byte, error) { +func RunCommandWithoutTimeout(name string, args ...string) ([]byte, error) { + return RunCommandWithContext(context.Background(), name, args...) +} + +func RunCommandWithTimeout(name string, args ...string) ([]byte, error) { ctx, cancel := context.WithTimeout(context.Background(), Timeout) defer cancel() return RunCommandWithContext(ctx, name, args...) diff --git a/pkg/util/timeutils2/timeutils.go b/pkg/util/timeutils2/timeutils.go index dd228efb0d..060f8bd668 100644 --- a/pkg/util/timeutils2/timeutils.go +++ b/pkg/util/timeutils2/timeutils.go @@ -3,6 +3,7 @@ package timeutils2 import ( "fmt" "os/exec" + "runtime/debug" "time" "yunion.io/x/log" @@ -13,6 +14,7 @@ func AddTimeout(second time.Duration, callback func()) { defer func() { if r := recover(); r != nil { log.Errorln(r) + debug.PrintStack() } }() diff --git a/vendor/yunion.io/x/pkg/util/netutils/netutils_test.go b/vendor/yunion.io/x/pkg/util/netutils/netutils_test.go new file mode 100644 index 0000000000..6fb0bacf56 --- /dev/null +++ b/vendor/yunion.io/x/pkg/util/netutils/netutils_test.go @@ -0,0 +1,23 @@ +package netutils + +import "testing" + +func TestIsExitAddress(t *testing.T) { + type args struct { + addr IPV4Addr + } + tests := []struct { + name string + args args + want bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsExitAddress(tt.args.addr); got != tt.want { + t.Errorf("IsExitAddress() = %v, want %v", got, tt.want) + } + }) + } +}