main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "flag"
  17. "net"
  18. "os"
  19. "strings"
  20. "google.golang.org/grpc"
  21. "yunion.io/x/executor/apis"
  22. "yunion.io/x/executor/server"
  23. "yunion.io/x/log"
  24. "yunion.io/x/onecloud/pkg/util/sysutils"
  25. )
  26. var socketPath string
  27. func init() {
  28. flag.StringVar(&socketPath, "socket-path", "/var/run/exec.sock", "execute service listen socket path")
  29. }
  30. func main() {
  31. if !sysutils.IsRootPermission() {
  32. log.Fatalln("executor must run on root permission")
  33. }
  34. Serve()
  35. }
  36. type SExecuteService struct {
  37. }
  38. func NewExecuteService() *SExecuteService {
  39. return &SExecuteService{}
  40. }
  41. func (s *SExecuteService) fixPathEnv() error {
  42. var paths = []string{
  43. "/usr/local/sbin",
  44. "/usr/local/bin",
  45. "/sbin",
  46. "/bin",
  47. "/usr/sbin",
  48. "/usr/bin",
  49. }
  50. return os.Setenv("PATH", strings.Join(paths, ":"))
  51. }
  52. func (s *SExecuteService) prepareEnv() error {
  53. if err := s.fixPathEnv(); err != nil {
  54. return err
  55. }
  56. return nil
  57. }
  58. func (s *SExecuteService) runService() {
  59. grpcServer := grpc.NewServer()
  60. apis.RegisterExecutorServer(grpcServer, &server.Executor{})
  61. if _, err := os.Stat(socketPath); !os.IsNotExist(err) {
  62. conn, err := net.Dial("unix", socketPath)
  63. if err == nil {
  64. conn.Close()
  65. log.Fatalf("socket %s already listening", socketPath)
  66. }
  67. // socket file already exist, remove first
  68. if err := os.Remove(socketPath); err != nil {
  69. log.Fatalln(err)
  70. }
  71. }
  72. listener, err := net.Listen("unix", socketPath)
  73. if err != nil {
  74. log.Fatalln(err)
  75. }
  76. defer listener.Close()
  77. log.Infof("Init net listener on %s succ", socketPath)
  78. err = grpcServer.Serve(listener)
  79. if err != nil {
  80. log.Fatalln(err)
  81. }
  82. }
  83. func (s *SExecuteService) initService() {
  84. if len(socketPath) == 0 {
  85. log.Fatalf("missing socket path")
  86. }
  87. if err := s.prepareEnv(); err != nil {
  88. log.Fatalln(err)
  89. }
  90. }
  91. func (s *SExecuteService) Run() {
  92. s.initService()
  93. s.runService()
  94. }
  95. func Serve() {
  96. NewExecuteService().Run()
  97. }