diff --git a/cmd/climc/shell/pprof.go b/cmd/climc/shell/pprof.go index 327884b412..eba267cf6b 100644 --- a/cmd/climc/shell/pprof.go +++ b/cmd/climc/shell/pprof.go @@ -79,10 +79,11 @@ func init() { }, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) signalutils.StartTrap() - cmd := procutils.NewCommand("go", "tool") - cmd.Args = append(cmd.Args, args...) - cmd.Args = append(cmd.Args, tempfile) - if _, err := cmd.Run(); err != nil { + argv := []string{"tool"} + argv = append(argv, args...) + argv = append(argv, tempfile) + cmd := procutils.NewCommand("go", argv...) + if err := cmd.Run(); err != nil { return err } return nil diff --git a/cmd/executor-server/main.go b/cmd/executor-server/main.go new file mode 100644 index 0000000000..c1f8479736 --- /dev/null +++ b/cmd/executor-server/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "flag" + "net" + "os" + "strings" + + "google.golang.org/grpc" + + "yunion.io/x/executor/apis" + "yunion.io/x/executor/server" + "yunion.io/x/log" + + "yunion.io/x/onecloud/pkg/util/sysutils" +) + +var socketPath string + +func init() { + flag.StringVar(&socketPath, "socket-path", "/var/run/exec.sock", "execute service listen socket path") +} + +func main() { + if !sysutils.IsRootPermission() { + log.Fatalln("executor must run on root permission") + } + Serve() +} + +type SExecuteService struct { +} + +func NewExecuteService() *SExecuteService { + return &SExecuteService{} +} + +func (s *SExecuteService) fixPathEnv() error { + var paths = []string{ + "/usr/local/sbin", + "/usr/local/bin", + "/sbin", + "/bin", + "/usr/sbin", + "/usr/bin", + } + return os.Setenv("PATH", strings.Join(paths, ":")) +} + +func (s *SExecuteService) prepareEnv() error { + if err := s.fixPathEnv(); err != nil { + return err + } + return nil +} + +func (s *SExecuteService) runService() { + grpcServer := grpc.NewServer() + apis.RegisterExecutorServer(grpcServer, &server.Executor{}) + if _, err := os.Stat(socketPath); !os.IsNotExist(err) { + // socket file already exist, remove first + if err := os.Remove(socketPath); err != nil { + log.Fatalln(err) + } + } + + listener, err := net.Listen("unix", socketPath) + if err != nil { + log.Fatalln(err) + } + defer listener.Close() + log.Infof("Init net listener on %s succ", socketPath) + err = grpcServer.Serve(listener) + if err != nil { + log.Fatalln(err) + } +} + +func (s *SExecuteService) initService() { + if len(socketPath) == 0 { + log.Fatalf("missing socket path") + } + if err := s.prepareEnv(); err != nil { + log.Fatalln(err) + } +} + +func (s *SExecuteService) Run() { + s.initService() + s.runService() +} + +func Serve() { + NewExecuteService().Run() +} diff --git a/go.mod b/go.mod index 09922426bf..aa9af3d41a 100644 --- a/go.mod +++ b/go.mod @@ -132,6 +132,7 @@ require ( k8s.io/client-go v9.0.0+incompatible k8s.io/klog v0.1.0 // indirect k8s.io/kubernetes v1.12.3 + yunion.io/x/executor v0.0.0-20191202093616-92e2e6119257 yunion.io/x/jsonutils v0.0.0-20191005115334-bb1c187fc0e7 yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d yunion.io/x/pkg v0.0.0-20191121110824-e03b47b93fe0 diff --git a/go.sum b/go.sum index 5302af177a..77f4924d32 100644 --- a/go.sum +++ b/go.sum @@ -608,6 +608,7 @@ google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107/go.mod h1:VzzqZJRn google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= google.golang.org/grpc v1.19.0 h1:cfg4PD8YEdSFnm7qLV4++93WcmhH2nIUhMjhdCvl3j8= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.22.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.23.1 h1:q4XQuHFC6I28BKZpo6IYyb3mNO+l7lSOxRuYTCiDfXk= google.golang.org/grpc v1.23.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= @@ -653,6 +654,8 @@ k8s.io/kubernetes v1.12.3 h1:5GPfYyyBylqcZUqL+ApYpYTm2IYjH56JUewYC0GbetU= k8s.io/kubernetes v1.12.3/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk= sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= +yunion.io/x/executor v0.0.0-20191202093616-92e2e6119257 h1:LZ6eC1uoLDAp7NQlaCy+DKnF31Mgd3LDKlIithziEmY= +yunion.io/x/executor v0.0.0-20191202093616-92e2e6119257/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws= yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051 h1:vtZw2iwGrsARNSwRTREGjmr2BWPdxbmXVkb3kI1qu28= yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634= yunion.io/x/jsonutils v0.0.0-20191005115334-bb1c187fc0e7 h1:9NcHs2OFMyN8O8SmJ3sFm5AJSVXP3u1N3avJsbyX3RI= @@ -663,6 +666,7 @@ yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d h1:59zrDL7Ft+hDukguJRmLr/Gdu/ yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d/go.mod h1:LC6f/4FozL0iaAbnFt2eDX9jlsyo3WiOUPm03d7+U4U= yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf h1:OsKC+2ghZHwp+Ztm/MwKlLKKRiE7QcPG8eTp0GmsHbg= yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= +yunion.io/x/pkg v0.0.0-20190628082551-f4033ba2ea30/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/pkg v0.0.0-20191121110824-e03b47b93fe0 h1:gMENCnVkKO5BlwtbKSKW5liEeHk8FHDxxcFXZcdhlxk= yunion.io/x/pkg v0.0.0-20191121110824-e03b47b93fe0/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo= diff --git a/pkg/baremetal/manager.go b/pkg/baremetal/manager.go index a5d5dba550..da6be323aa 100644 --- a/pkg/baremetal/manager.go +++ b/pkg/baremetal/manager.go @@ -1679,11 +1679,11 @@ func (b *SBaremetalInstance) SendNicInfo(nic *types.SNicDevInfo, idx int, nicTyp } func bindMount(src, dst string) error { - _, err := procutils.NewCommand("touch", dst).Run() + err := procutils.NewCommand("touch", dst).Run() if err != nil { return errors.Wrapf(err, "touch %s", dst) } - _, err = procutils.NewCommand("mount", "-o", "ro,bind", src, dst).Run() + err = procutils.NewCommand("mount", "-o", "ro,bind", src, dst).Run() if err != nil { return errors.Wrapf(err, "mount %s %s", src, dst) } @@ -1691,7 +1691,7 @@ func bindMount(src, dst string) error { } func unbindMount(dst string) error { - _, err := procutils.NewCommand("umount", dst).Run() + err := procutils.NewCommand("umount", dst).Run() if err != nil { return errors.Wrapf(err, "umount %s", dst) } @@ -1760,7 +1760,7 @@ func (b *SBaremetalInstance) GenerateBootISO() error { for _, f := range []string{ "isolinux.bin", } { - _, err = procutils.NewCommand("cp", filepath.Join(o.Options.TftpRoot, f), filepath.Join(isoLinDir, f)).Run() + err = procutils.NewCommand("cp", filepath.Join(o.Options.TftpRoot, f), filepath.Join(isoLinDir, f)).Run() if err != nil { return errors.Wrapf(err, "cp %s", f) } @@ -1782,7 +1782,7 @@ func (b *SBaremetalInstance) GenerateBootISO() error { "-o", b.getBootIsoImagePath(), isoDir, } - _, err = procutils.NewCommand("mkisofs", args...).Run() + err = procutils.NewCommand("mkisofs", args...).Run() if err != nil { return errors.Wrap(err, "procutils.NewCommand mkisofs") } diff --git a/pkg/baremetal/utils/ipmitool/ipmitool.go b/pkg/baremetal/utils/ipmitool/ipmitool.go index 2cd6031aca..706c8e0b02 100644 --- a/pkg/baremetal/utils/ipmitool/ipmitool.go +++ b/pkg/baremetal/utils/ipmitool/ipmitool.go @@ -131,7 +131,7 @@ func (ipmi *LanPlusIPMI) GetCommand(args ...string) *procutils.Command { func (ipmi *LanPlusIPMI) ExecuteCommand(args ...string) ([]string, error) { cmd := ipmi.GetCommand(args...) log.Debugf("[LanPlusIPMI] execute command: %s", cmd.String()) - out, err := cmd.Run() + out, err := cmd.Output() if err != nil { return nil, err } diff --git a/pkg/cloudcommon/service/services.go b/pkg/cloudcommon/service/services.go index 3355079179..48d49f5df3 100644 --- a/pkg/cloudcommon/service/services.go +++ b/pkg/cloudcommon/service/services.go @@ -69,7 +69,7 @@ func (s *SServiceBase) CreatePid() error { s.O.PidFile = absPath pidDir := filepath.Dir(s.O.PidFile) if !fileutils2.Exists(pidDir) { - output, err := procutils.NewCommand("mkdir", "-p", pidDir).Run() + output, err := procutils.NewCommand("mkdir", "-p", pidDir).Output() if err != nil { return fmt.Errorf("Make pid dir %s failed: %s", pidDir, output) } diff --git a/pkg/hostman/diskutils/diskutils.go b/pkg/hostman/diskutils/diskutils.go index a2700cd8fb..65305d2540 100644 --- a/pkg/hostman/diskutils/diskutils.go +++ b/pkg/hostman/diskutils/diskutils.go @@ -96,7 +96,7 @@ func (d *SKVMGuestDisk) connect() bool { } else { cmd = []string{qemutils.GetQemuNbd(), "-c", d.nbdDev, d.imagePath} } - _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Run() + _, err := procutils.NewCommand(cmd[0], cmd[1:]...).Output() if err != nil { log.Errorln(err.Error()) return false @@ -134,7 +134,7 @@ func (d *SKVMGuestDisk) Connect() bool { } func (d *SKVMGuestDisk) getImageFormat() string { - lines, err := procutils.NewCommand(qemutils.GetQemuImg(), "info", d.imagePath).Run() + lines, err := procutils.NewCommand(qemutils.GetQemuImg(), "info", d.imagePath).Output() if err != nil { return "" } @@ -228,7 +228,7 @@ func (d *SKVMGuestDisk) LvmDisconnectNotify() { func (d *SKVMGuestDisk) setupLVMS() (bool, error) { // Scan all devices and send the metadata to lvmetad - output, err := procutils.NewCommand("pvscan", "--cache").Run() + output, err := procutils.NewCommand("pvscan", "--cache").Output() if err != nil { log.Errorf("pvscan error %s", output) return false, err @@ -278,7 +278,7 @@ func (d *SKVMGuestDisk) Disconnect() bool { } func (d *SKVMGuestDisk) disconnect() bool { - _, err := procutils.NewCommand(qemutils.GetQemuNbd(), "-d", d.nbdDev).Run() + _, err := procutils.NewCommand(qemutils.GetQemuNbd(), "-d", d.nbdDev).Output() if err != nil { log.Errorln(err.Error()) return false @@ -342,15 +342,15 @@ func (d *SKVMGuestDisk) UmountKvmRootfs(fd fsdriver.IRootFsDriver) { } func (d *SKVMGuestDisk) MakePartition(fs string) error { - return fileutils2.Mkpartition(d.nbdDev, fs) + return Mkpartition(d.nbdDev, fs) } func (d *SKVMGuestDisk) FormatPartition(fs, uuid string) error { - return fileutils2.FormatPartition(fmt.Sprintf("%sp1", d.nbdDev), fs, uuid) + return FormatPartition(fmt.Sprintf("%sp1", d.nbdDev), fs, uuid) } func (d *SKVMGuestDisk) ResizePartition() error { - return fileutils2.ResizeDiskFs(d.nbdDev, 0) + return ResizeDiskFs(d.nbdDev, 0) } func (d *SKVMGuestDisk) Zerofree() { diff --git a/pkg/hostman/diskutils/fsutils.go b/pkg/hostman/diskutils/fsutils.go new file mode 100644 index 0000000000..529560c736 --- /dev/null +++ b/pkg/hostman/diskutils/fsutils.go @@ -0,0 +1,363 @@ +package diskutils + +import ( + "fmt" + "io" + "io/ioutil" + "path" + "regexp" + "strconv" + "strings" + + "github.com/pkg/errors" + + "yunion.io/x/log" + "yunion.io/x/pkg/utils" + + "yunion.io/x/onecloud/pkg/util/fileutils2" + "yunion.io/x/onecloud/pkg/util/procutils" + "yunion.io/x/onecloud/pkg/util/regutils2" +) + +func IsPartedFsString(fsstr string) bool { + return utils.IsInStringArray(strings.ToLower(fsstr), []string{ + "ext2", "ext3", "ext4", "xfs", + "fat16", "fat32", + "hfs", "hfs+", "hfsx", + "linux-swap", "linux-swap(v1)", + "ntfs", "reiserfs", "ufs", "btrfs", + }) +} + +func ParseDiskPartition(dev string, lines []byte) ([][]string, string) { + var ( + parts = [][]string{} + label string + labelPartten = regexp.MustCompile(`Partition Table:\s+(?P