github.com/openimsdk/tools@v0.0.49/utils/mageutil/bssc.go (about) 1 package mageutil 2 3 import ( 4 "fmt" 5 "github.com/magefile/mage/sh" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "strings" 10 "sync" 11 ) 12 13 // CheckAndReportBinariesStatus checks the running status of all binary files and reports it. 14 func CheckAndReportBinariesStatus() { 15 InitForSSC() 16 err := CheckBinariesRunning() 17 if err != nil { 18 PrintRed("Some programs are not running properly:") 19 PrintRedNoTimeStamp(err.Error()) 20 return 21 } 22 PrintGreen("All services are running normally.") 23 PrintBlue("Display details of the ports listened to by the service:") 24 PrintListenedPortsByBinaries() 25 } 26 27 // StopAndCheckBinaries stops all binary processes and checks if they have all stopped. 28 func StopAndCheckBinaries() { 29 InitForSSC() 30 KillExistBinaries() 31 err := CheckBinariesStop() 32 if err != nil { 33 PrintRed("Some services have not been stopped, details are as follows:" + err.Error()) 34 return 35 } 36 PrintGreen("All services have been stopped") 37 } 38 39 // StartToolsAndServices starts the process for tools and services. 40 func StartToolsAndServices() { 41 InitForSSC() 42 PrintBlue("Starting tools primarily involves component verification and other preparatory tasks.") 43 if err := StartTools(); err != nil { 44 PrintRed("Some tools failed to start, details are as follows, abort start") 45 PrintRedNoTimeStamp(err.Error()) 46 return 47 } 48 PrintGreen("All tools executed successfully") 49 50 KillExistBinaries() 51 err := CheckBinariesStop() 52 if err != nil { 53 PrintRed("Some services running, details are as follows, abort start " + err.Error()) 54 return 55 } 56 PrintBlue("Starting services involves multiple RPCs and APIs and may take some time. Please be patient") 57 err = StartBinaries() 58 if err != nil { 59 PrintRed("Failed to start all binaries") 60 PrintRedNoTimeStamp(err.Error()) 61 return 62 } 63 CheckAndReportBinariesStatus() 64 } 65 66 // CompileForPlatform Main compile function 67 func CompileForPlatform(platform string) { 68 69 PrintBlue(fmt.Sprintf("Compiling cmd for %s...", platform)) 70 71 cmdCompiledDirs := compileDir(filepath.Join(rootDirPath, "cmd"), platformsOutputBase, platform) 72 73 PrintBlue(fmt.Sprintf("Compiling tools for %s...", platform)) 74 toolsCompiledDirs := compileDir(filepath.Join(rootDirPath, "tools"), toolsOutputBase, platform) 75 createStartConfigYML(cmdCompiledDirs, toolsCompiledDirs) 76 77 } 78 79 func createStartConfigYML(cmdDirs, toolsDirs []string) { 80 configPath := filepath.Join(rootDirPath, "start-config.yml") 81 82 if _, err := os.Stat(configPath); !os.IsNotExist(err) { 83 PrintBlue("start-config.yml already exists, skipping creation.") 84 return 85 } 86 87 var content strings.Builder 88 content.WriteString("serviceBinaries:\n") 89 for _, dir := range cmdDirs { 90 content.WriteString(fmt.Sprintf(" %s: 1\n", dir)) 91 } 92 content.WriteString("toolBinaries:\n") 93 for _, dir := range toolsDirs { 94 content.WriteString(fmt.Sprintf(" - %s\n", dir)) 95 } 96 content.WriteString("maxFileDescriptors: 10000\n") 97 98 err := ioutil.WriteFile(configPath, []byte(content.String()), 0644) 99 if err != nil { 100 PrintRed("Failed to create start-config.yml: " + err.Error()) 101 return 102 } 103 PrintGreen("start-config.yml created successfully.") 104 } 105 106 func compileDir(sourceDir, outputBase, platform string) []string { 107 var compiledDirs []string 108 var mu sync.Mutex 109 targetOS, targetArch := strings.Split(platform, "_")[0], strings.Split(platform, "_")[1] 110 outputDir := filepath.Join(outputBase, targetOS, targetArch) 111 112 if err := os.MkdirAll(outputDir, 0755); err != nil { 113 fmt.Printf("Failed to create directory %s: %v\n", outputDir, err) 114 os.Exit(1) 115 } 116 117 var wg sync.WaitGroup 118 errors := make(chan error, 1) 119 sem := make(chan struct{}, 4) 120 121 err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { 122 if err != nil { 123 return err 124 } 125 if info.IsDir() || filepath.Base(path) != "main.go" { 126 return nil 127 } 128 129 wg.Add(1) 130 go func() { 131 sem <- struct{}{} 132 defer wg.Done() 133 defer func() { <-sem }() 134 135 dir := filepath.Dir(path) 136 dirName := filepath.Base(dir) 137 outputFileName := dirName 138 if targetOS == "windows" { 139 outputFileName += ".exe" 140 } 141 142 PrintBlue(fmt.Sprintf("Compiling dir: %s for platform: %s binary: %s ...", dirName, platform, outputFileName)) 143 err := sh.RunWith(map[string]string{"GOOS": targetOS, "GOARCH": targetArch}, "go", "build", "-o", filepath.Join(outputDir, outputFileName), filepath.Join(dir, "main.go")) 144 if err != nil { 145 errors <- fmt.Errorf("failed to compile %s for %s: %v", dirName, platform, err) 146 PrintRed("Compilation aborted. " + fmt.Sprintf("failed to compile %s for %s: %v", dirName, platform, err)) 147 os.Exit(1) 148 return 149 } 150 PrintGreen(fmt.Sprintf("Successfully compiled. dir: %s for platform: %s binary: %s", dirName, platform, outputFileName)) 151 mu.Lock() 152 compiledDirs = append(compiledDirs, dirName) 153 mu.Unlock() 154 }() 155 156 return nil 157 }) 158 159 if err != nil { 160 fmt.Println("Error walking through directories:", err) 161 os.Exit(1) 162 } 163 164 wg.Wait() 165 close(errors) 166 167 // Check for errors 168 if err, ok := <-errors; ok { 169 fmt.Println(err) 170 os.Exit(1) 171 } 172 return compiledDirs 173 } 174 175 // compileDir compiles Go programs in a specified directory, appending .exe extension for output files on Windows platform 176 //func compileDir(sourceDir, outputBase, platform string) { 177 // targetOS, targetArch := strings.Split(platform, "_")[0], strings.Split(platform, "_")[1] 178 // outputDir := filepath.Join(outputBase, targetOS, targetArch) 179 // 180 // if err := os.MkdirAll(outputDir, 0755); err != nil { 181 // fmt.Printf("Failed to create directory %s: %v\n", outputDir, err) 182 // os.Exit(1) 183 // } 184 // 185 // var wg sync.WaitGroup 186 // errors := make(chan error, 1) 187 // 188 // err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { 189 // if err != nil { 190 // return err 191 // } 192 // if info.IsDir() || filepath.Base(path) != "main.go" { 193 // return nil 194 // } 195 // 196 // wg.Add(1) 197 // go func() { 198 // defer wg.Done() 199 // 200 // dir := filepath.Dir(path) 201 // dirName := filepath.Base(dir) 202 // outputFileName := dirName 203 // if targetOS == "windows" { 204 // outputFileName += ".exe" 205 // } 206 // 207 // PrintBlue(fmt.Sprintf("Compiling dir: %s for platform: %s binary: %s ...", dirName, platform, outputFileName)) 208 // err := sh.RunWith(map[string]string{"GOOS": targetOS, "GOARCH": targetArch}, "go", "build", "-o", filepath.Join(outputDir, outputFileName), filepath.Join(dir, "main.go")) 209 // if err != nil { 210 // errors <- fmt.Errorf("failed to compile %s for %s: %v", dirName, platform, err) 211 // PrintRed("Compilation aborted. " + fmt.Sprintf("failed to compile %s for %s: %v", dirName, platform, err)) 212 // os.Exit(1) 213 // return 214 // } 215 // PrintGreen(fmt.Sprintf("Compiling dir: %s for platform: %s binary: %s ...", dirName, platform, outputFileName)) 216 // }() 217 // 218 // return nil 219 // }) 220 // 221 // if err != nil { 222 // fmt.Println("Error walking through directories:", err) 223 // os.Exit(1) 224 // } 225 // 226 // wg.Wait() 227 // close(errors) 228 // 229 // // Check for errors 230 // if err, ok := <-errors; ok { 231 // fmt.Println(err) 232 // fmt.Println("Compilation aborted.") 233 // os.Exit(1) 234 // } 235 //}