From c8fff1d44fc12f9751922ef38cb4f1bc81facae6 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Wed, 4 Dec 2019 11:35:43 +0800 Subject: [PATCH] add rpm file, add socket used check --- .../lib/systemd/system/yunion-executor.service | 18 ++++++++++++++++++ build/executor-server/vars | 2 ++ cmd/executor-server/main.go | 6 ++++++ pkg/hostman/guestman/qemu-kvm.go | 4 ++-- .../hostdeployer/deployserver/deployserver.go | 2 +- pkg/hostman/hostinfo/hostinfo.go | 6 +++--- pkg/hostman/storageman/imagecache_rbd.go | 3 ++- pkg/hostman/storageman/storage_rbd.go | 2 +- pkg/hostman/system_service/systemd.go | 18 +++++++++++------- pkg/util/procutils/executor.go | 10 +++++++--- pkg/util/procutils/procutils.go | 13 +++++++++++-- pkg/util/qemuimg/init.go | 2 +- pkg/util/qemuimg/qemuimg.go | 10 +++++----- 13 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 build/executor-server/root/usr/lib/systemd/system/yunion-executor.service create mode 100644 build/executor-server/vars diff --git a/build/executor-server/root/usr/lib/systemd/system/yunion-executor.service b/build/executor-server/root/usr/lib/systemd/system/yunion-executor.service new file mode 100644 index 0000000000..867bc3f90c --- /dev/null +++ b/build/executor-server/root/usr/lib/systemd/system/yunion-executor.service @@ -0,0 +1,18 @@ +[Unit] +Description=Yunion Command Executor +Documentation=https://docs.yunion.cn + +[Service] +Type=simple +User=root +Group=root +ExecStart=/opt/yunion/bin/executor-server +WorkingDirectory=/opt/yunion/bin +KillMode=process +Restart=always +RestartSec=30 +LimitNOFILE=500000 +LimitNPROC=500000 + +[Install] +WantedBy=multi-user.target diff --git a/build/executor-server/vars b/build/executor-server/vars new file mode 100644 index 0000000000..e7767102f5 --- /dev/null +++ b/build/executor-server/vars @@ -0,0 +1,2 @@ +DESCRIPTION="Yunion Executor Server Command Line Utility" +SERVICE="yes" \ No newline at end of file diff --git a/cmd/executor-server/main.go b/cmd/executor-server/main.go index c1f8479736..f3356873a1 100644 --- a/cmd/executor-server/main.go +++ b/cmd/executor-server/main.go @@ -58,6 +58,12 @@ func (s *SExecuteService) runService() { grpcServer := grpc.NewServer() apis.RegisterExecutorServer(grpcServer, &server.Executor{}) if _, err := os.Stat(socketPath); !os.IsNotExist(err) { + conn, err := net.Dial("unix", socketPath) + if err == nil { + conn.Close() + log.Fatalf("socket %s already listening", socketPath) + } + // socket file already exist, remove first if err := os.Remove(socketPath); err != nil { log.Fatalln(err) diff --git a/pkg/hostman/guestman/qemu-kvm.go b/pkg/hostman/guestman/qemu-kvm.go index 9f041cb3e6..a14249813a 100644 --- a/pkg/hostman/guestman/qemu-kvm.go +++ b/pkg/hostman/guestman/qemu-kvm.go @@ -916,7 +916,7 @@ func (s *SKVMGuestInstance) Stop() bool { } func (s *SKVMGuestInstance) scriptStart() error { - output, err := procutils.NewCommand("sh", s.GetStartScriptPath()).Output() + output, err := procutils.NewRemoteCommandAsFarAsPossible("sh", s.GetStartScriptPath()).Output() if err != nil { s.scriptStop() return fmt.Errorf("Start VM Failed %s %s", output, err) @@ -925,7 +925,7 @@ func (s *SKVMGuestInstance) scriptStart() error { } func (s *SKVMGuestInstance) scriptStop() bool { - _, err := procutils.NewCommand("sh", s.GetStopScriptPath()).Output() + _, err := procutils.NewRemoteCommandAsFarAsPossible("sh", s.GetStopScriptPath()).Output() if err != nil { log.Errorln(err) return false diff --git a/pkg/hostman/hostdeployer/deployserver/deployserver.go b/pkg/hostman/hostdeployer/deployserver/deployserver.go index 73cc2fad14..dfc41b75da 100644 --- a/pkg/hostman/hostdeployer/deployserver/deployserver.go +++ b/pkg/hostman/hostdeployer/deployserver/deployserver.go @@ -209,12 +209,12 @@ func (s *SDeployService) RunService() { func (s *SDeployService) FixPathEnv() error { var paths = []string{ + "/usr/bin", "/usr/local/sbin", "/usr/local/bin", "/sbin", "/bin", "/usr/sbin", - "/usr/bin", } return os.Setenv("PATH", strings.Join(paths, ":")) } diff --git a/pkg/hostman/hostinfo/hostinfo.go b/pkg/hostman/hostinfo/hostinfo.go index a34895593b..0b67ec0c2a 100644 --- a/pkg/hostman/hostinfo/hostinfo.go +++ b/pkg/hostman/hostinfo/hostinfo.go @@ -253,12 +253,12 @@ func (h *SHostInfo) prepareEnv() error { return fmt.Errorf("Qemu not installed") } - _, err = procutils.NewCommand(qemutils.GetQemu(""), "-version").Output() + _, err = procutils.NewRemoteCommandAsFarAsPossible(qemutils.GetQemu(""), "-version").Output() if err != nil { return fmt.Errorf("Qemu/Kvm not installed") } - _, err = procutils.NewCommand("/sbin/ethtool", "-h").Output() + _, err = procutils.NewCommand("ethtool", "-h").Output() if err != nil { return fmt.Errorf("Ethtool not installed") } @@ -547,7 +547,7 @@ func (h *SHostInfo) detectiveSyssoftwareInfo() error { func (h *SHostInfo) detectiveQemuVersion() error { cmd := qemutils.GetQemu(options.HostOptions.DefaultQemuVersion) - version, err := procutils.NewCommand(cmd, "--version").Output() + version, err := procutils.NewRemoteCommandAsFarAsPossible(cmd, "--version").Output() if err != nil { log.Errorln(err) return err diff --git a/pkg/hostman/storageman/imagecache_rbd.go b/pkg/hostman/storageman/imagecache_rbd.go index a5653cd777..b5bcc336ce 100644 --- a/pkg/hostman/storageman/imagecache_rbd.go +++ b/pkg/hostman/storageman/imagecache_rbd.go @@ -76,7 +76,8 @@ func (r *SRbdImageCache) Acquire(ctx context.Context, zone, srcUrl, format strin r.imageName = localImageCache.GetName() if !r.Load() { log.Debugf("convert local image %s to rbd pool %s", r.imageId, r.Manager.GetPath()) - err := procutils.NewCommand(qemutils.GetQemuImg(), "convert", "-O", "raw", localImageCache.GetPath(), r.GetPath()).Run() + err := procutils.NewRemoteCommandAsFarAsPossible(qemutils.GetQemuImg(), + "convert", "-O", "raw", localImageCache.GetPath(), r.GetPath()).Run() if err != nil { log.Errorf("failed to convert image %s", options.HostOptions.ServersPath) return false diff --git a/pkg/hostman/storageman/storage_rbd.go b/pkg/hostman/storageman/storage_rbd.go index 2872005611..f29d934ce1 100644 --- a/pkg/hostman/storageman/storage_rbd.go +++ b/pkg/hostman/storageman/storage_rbd.go @@ -658,7 +658,7 @@ func (s *SRbdStorage) saveToGlance(ctx context.Context, imageId, imagePath strin format = options.HostOptions.DefaultImageSaveFormat } - err = procutils.NewCommand(qemutils.GetQemuImg(), + err = procutils.NewRemoteCommandAsFarAsPossible(qemutils.GetQemuImg(), "convert", "-f", "raw", "-O", format, imagePath, tmpImageFile).Run() if err != nil { return err diff --git a/pkg/hostman/system_service/systemd.go b/pkg/hostman/system_service/systemd.go index 3223460947..778d72fd45 100644 --- a/pkg/hostman/system_service/systemd.go +++ b/pkg/hostman/system_service/systemd.go @@ -27,29 +27,33 @@ var ( type SSystemdServiceManager struct { } +func systemctl(args ...string) *procutils.Command { + return procutils.NewRemoteCommandAsFarAsPossible("systemctl", args...) +} + func (manager *SSystemdServiceManager) Detect() bool { - return procutils.NewCommand("systemctl", "--version").Run() == nil + return systemctl("--version").Run() == nil } func (manager *SSystemdServiceManager) Start(srvname string) error { - return procutils.NewCommand("systemctl", "restart", srvname).Run() + return systemctl("restart", srvname).Run() } func (manager *SSystemdServiceManager) Enable(srvname string) error { - return procutils.NewCommand("systemctl", "enable", srvname).Run() + return systemctl("enable", srvname).Run() } func (manager *SSystemdServiceManager) Stop(srvname string) error { - return procutils.NewCommand("systemctl", "stop", srvname).Run() + return systemctl("stop", srvname).Run() } func (manager *SSystemdServiceManager) Disable(srvname string) error { - return procutils.NewCommand("systemctl", "disable", srvname).Run() + return systemctl("disable", srvname).Run() } func (manager *SSystemdServiceManager) GetStatus(srvname string) SServiceStatus { - res, _ := procutils.NewCommand("systemctl", "status", srvname).Output() - res2, _ := procutils.NewCommand("systemctl", "is-enabled", srvname).Output() + res, _ := systemctl("status", srvname).Output() + res2, _ := systemctl("is-enabled", srvname).Output() return parseSystemdStatus(string(res), string(res2), srvname) } diff --git a/pkg/util/procutils/executor.go b/pkg/util/procutils/executor.go index c669f1fd51..c0238659b9 100644 --- a/pkg/util/procutils/executor.go +++ b/pkg/util/procutils/executor.go @@ -9,14 +9,18 @@ import ( "yunion.io/x/executor/client" ) -var execInstance Executor +var ( + execInstance Executor + localExecutor = new(defaultExecutor) + _remoteExecutor = new(remoteExecutor) +) func init() { - execInstance = new(defaultExecutor) + execInstance = localExecutor } func SetRemoteExecutor() { - execInstance = new(remoteExecutor) + execInstance = _remoteExecutor } type Cmd interface { diff --git a/pkg/util/procutils/procutils.go b/pkg/util/procutils/procutils.go index 6944e2f5e9..cb17bd7a43 100644 --- a/pkg/util/procutils/procutils.go +++ b/pkg/util/procutils/procutils.go @@ -38,7 +38,7 @@ func NewCommand(name string, args ...string) *Command { return &Command{ path: name, args: args, - cmd: execInstance.Command(name, args...), + cmd: localExecutor.Command(name, args...), } } @@ -46,7 +46,16 @@ func NewCommandContext(ctx context.Context, name string, args ...string) *Comman return &Command{ path: name, args: args, - cmd: execInstance.CommandContext(ctx, name, args...), + cmd: localExecutor.CommandContext(ctx, name, args...), + } +} + +// exec remote command as far as possible +func NewRemoteCommandAsFarAsPossible(name string, args ...string) *Command { + return &Command{ + path: name, + args: args, + cmd: execInstance.Command(name, args...), } } diff --git a/pkg/util/qemuimg/init.go b/pkg/util/qemuimg/init.go index cc955d9237..34bcfe5d11 100644 --- a/pkg/util/qemuimg/init.go +++ b/pkg/util/qemuimg/init.go @@ -36,7 +36,7 @@ var ( ) func getQemuImgVersion() string { - out, err := procutils.NewCommand(qemutils.GetQemuImg(), "--version").Output() + out, err := procutils.NewRemoteCommandAsFarAsPossible(qemutils.GetQemuImg(), "--version").Output() if err != nil { log.Errorf("check qemu-img version fail %s", out) return "" diff --git a/pkg/util/qemuimg/qemuimg.go b/pkg/util/qemuimg/qemuimg.go index da5926bcf4..3e1bd6921b 100644 --- a/pkg/util/qemuimg/qemuimg.go +++ b/pkg/util/qemuimg/qemuimg.go @@ -111,7 +111,7 @@ func (img *SQemuImage) parse() error { img.ActualSizeBytes = fileInfo.Size() } } - cmd := procutils.NewCommand(qemutils.GetQemuImg(), "info", img.Path) + cmd := procutils.NewRemoteCommandAsFarAsPossible(qemutils.GetQemuImg(), "info", img.Path) var stdin io.WriteCloser var err error @@ -234,7 +234,7 @@ func (img *SQemuImage) doConvert(name string, format TImageFormat, options []str } cmdline = append(cmdline, img.Path, name) log.Infof("XXXX qemu-img command: %s", cmdline) - cmd := procutils.NewCommand("ionice", cmdline...) + cmd := procutils.NewRemoteCommandAsFarAsPossible("ionice", cmdline...) var stdin io.WriteCloser var err error if len(img.Password) > 0 || len(password) > 0 { @@ -443,7 +443,7 @@ func (img *SQemuImage) create(sizeMB int, format TImageFormat, options []string) if sizeMB > 0 { args = append(args, fmt.Sprintf("%dM", sizeMB)) } - cmd := procutils.NewCommand("ionice", args...) + cmd := procutils.NewRemoteCommandAsFarAsPossible("ionice", args...) output, err := cmd.Output() if err != nil { log.Errorf("%v create error %s %s", args, output, err) @@ -490,7 +490,7 @@ func (img *SQemuImage) Resize(sizeMB int) error { if !img.IsValid() { return fmt.Errorf("self is not valid") } - cmd := procutils.NewCommand("ionice", "-c", strconv.Itoa(int(img.IoLevel)), + cmd := procutils.NewRemoteCommandAsFarAsPossible("ionice", "-c", strconv.Itoa(int(img.IoLevel)), qemutils.GetQemuImg(), "resize", img.Path, fmt.Sprintf("%dM", sizeMB)) err := cmd.Run() if err != nil { @@ -510,7 +510,7 @@ func (img *SQemuImage) Rebase(backPath string, force bool) error { args = append(args, "-u") } args = append(args, "-b", backPath, img.Path) - cmd := procutils.NewCommand("ionice", args...) + cmd := procutils.NewRemoteCommandAsFarAsPossible("ionice", args...) err := cmd.Run() if err != nil { log.Errorf("rebase fail %s", err)