1
0

main.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //go:generate go run .
  2. package main
  3. import (
  4. "archive/tar"
  5. "archive/zip"
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "runtime"
  15. "github.com/spf13/afero"
  16. "github.com/spf13/afero/zipfs"
  17. "github.com/uozi-tech/cosy/logger"
  18. "github.com/ulikunitz/xz"
  19. )
  20. // GitHubRelease represents the structure of GitHub's release API response
  21. type GitHubRelease struct {
  22. TagName string `json:"tag_name"`
  23. }
  24. const (
  25. githubAPIURL = "https://cloud.nginxui.com/https://api.github.com/repos/go-acme/lego/releases/latest"
  26. configDir = "internal/cert/config"
  27. )
  28. func main() {
  29. logger.Init("release")
  30. _, file, _, ok := runtime.Caller(0)
  31. if !ok {
  32. logger.Error("Unable to get the current file")
  33. return
  34. }
  35. basePath := filepath.Join(filepath.Dir(file), "../../")
  36. // Get the latest release tag
  37. tag, err := getLatestReleaseTag()
  38. if err != nil {
  39. logger.Errorf("Error getting latest release tag: %v\n", err)
  40. os.Exit(1)
  41. }
  42. logger.Infof("Latest release tag: %s", tag)
  43. zipFile, err := downloadAndExtract(tag)
  44. if err != nil {
  45. logger.Errorf("Error downloading and extracting: %v\n", err)
  46. os.Exit(1)
  47. }
  48. if err := copyTomlFiles(zipFile, basePath, tag); err != nil {
  49. logger.Errorf("Error copying TOML files: %v\n", err)
  50. os.Exit(1)
  51. }
  52. if err := compressConfigs(basePath); err != nil {
  53. logger.Errorf("Error compressing configs: %v\n", err)
  54. os.Exit(1)
  55. }
  56. logger.Info("Successfully updated and compressed provider config")
  57. }
  58. // getLatestReleaseTag fetches the latest release tag from GitHub API
  59. func getLatestReleaseTag() (string, error) {
  60. logger.Info("Fetching latest release tag...")
  61. req, err := http.NewRequest("GET", githubAPIURL, nil)
  62. if err != nil {
  63. return "", err
  64. }
  65. // Add User-Agent header to avoid GitHub API limitations
  66. req.Header.Set("User-Agent", "NGINX-UI-LegoConfigure")
  67. client := &http.Client{}
  68. resp, err := client.Do(req)
  69. if err != nil {
  70. return "", err
  71. }
  72. defer resp.Body.Close()
  73. if resp.StatusCode != http.StatusOK {
  74. return "", fmt.Errorf("bad status from GitHub API: %s", resp.Status)
  75. }
  76. var release GitHubRelease
  77. if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
  78. return "", err
  79. }
  80. if release.TagName == "" {
  81. return "", fmt.Errorf("no tag name found in the latest release")
  82. }
  83. return release.TagName, nil
  84. }
  85. // downloadAndExtract downloads the lego repository for a specific tag and extracts it
  86. func downloadAndExtract(tag string) (string, error) {
  87. downloadURL := fmt.Sprintf("https://cloud.nginxui.com/https://github.com/go-acme/lego/archive/refs/tags/%s.zip", tag)
  88. // Download the file
  89. logger.Infof("Downloading lego repository for tag %s...", tag)
  90. resp, err := http.Get(downloadURL)
  91. if err != nil {
  92. return "", err
  93. }
  94. defer resp.Body.Close()
  95. if resp.StatusCode != http.StatusOK {
  96. return "", fmt.Errorf("bad status: %s", resp.Status)
  97. }
  98. // Create the file
  99. out, err := os.CreateTemp("", "lego-"+tag+".zip")
  100. if err != nil {
  101. return "", err
  102. }
  103. defer out.Close()
  104. // Write the body to file
  105. _, err = io.Copy(out, resp.Body)
  106. if err != nil {
  107. return "", err
  108. }
  109. return out.Name(), nil
  110. }
  111. func copyTomlFiles(zipFile, basePath, tag string) error {
  112. // Open the zip file
  113. logger.Info("Extracting files...")
  114. zipReader, err := zip.OpenReader(zipFile)
  115. if err != nil {
  116. return err
  117. }
  118. defer zipReader.Close()
  119. // Extract files
  120. tag = strings.TrimPrefix(tag, "v")
  121. zfs := zipfs.New(&zipReader.Reader)
  122. afero.Walk(zfs, "./lego-"+tag+"/providers", func(path string, info os.FileInfo, err error) error {
  123. if info == nil || info.IsDir() {
  124. return nil
  125. }
  126. if !strings.HasSuffix(info.Name(), ".toml") {
  127. return nil
  128. }
  129. if err != nil {
  130. return err
  131. }
  132. data, err := afero.ReadFile(zfs, path)
  133. if err != nil {
  134. return err
  135. }
  136. // Write to the destination file
  137. destPath := filepath.Join(basePath, configDir, info.Name())
  138. if err := os.WriteFile(destPath, data, 0644); err != nil {
  139. return err
  140. }
  141. logger.Infof("Copied: %s", info.Name())
  142. return nil
  143. })
  144. // Clean up zip file
  145. return os.Remove(zipFile)
  146. }
  147. // compressConfigs compresses all TOML files into a single XZ archive
  148. func compressConfigs(basePath string) error {
  149. logger.Info("Compressing config files...")
  150. configDir := filepath.Join(basePath, "internal/cert/config")
  151. // Create buffer for tar data
  152. var tarBuffer bytes.Buffer
  153. tarWriter := tar.NewWriter(&tarBuffer)
  154. // Walk through TOML files and add to tar
  155. err := filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
  156. if err != nil {
  157. return err
  158. }
  159. if !strings.HasSuffix(info.Name(), ".toml") {
  160. return nil
  161. }
  162. // Read file content
  163. data, err := os.ReadFile(path)
  164. if err != nil {
  165. return err
  166. }
  167. // Create tar header
  168. header := &tar.Header{
  169. Name: info.Name(),
  170. Mode: 0644,
  171. Size: int64(len(data)),
  172. }
  173. // Write header and data to tar
  174. if err := tarWriter.WriteHeader(header); err != nil {
  175. return err
  176. }
  177. if _, err := tarWriter.Write(data); err != nil {
  178. return err
  179. }
  180. logger.Infof("Added to archive: %s", info.Name())
  181. return nil
  182. })
  183. if err != nil {
  184. return err
  185. }
  186. if err := tarWriter.Close(); err != nil {
  187. return err
  188. }
  189. // Compress with XZ
  190. var compressedBuffer bytes.Buffer
  191. xzWriter, err := xz.NewWriter(&compressedBuffer)
  192. if err != nil {
  193. return err
  194. }
  195. if _, err := xzWriter.Write(tarBuffer.Bytes()); err != nil {
  196. return err
  197. }
  198. if err := xzWriter.Close(); err != nil {
  199. return err
  200. }
  201. // Write compressed data to config.tar.xz
  202. compressedPath := filepath.Join(configDir, "config.tar.xz")
  203. if err := os.WriteFile(compressedPath, compressedBuffer.Bytes(), 0644); err != nil {
  204. return err
  205. }
  206. // Remove individual TOML files
  207. err = filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
  208. if err != nil {
  209. return err
  210. }
  211. if strings.HasSuffix(info.Name(), ".toml") {
  212. if err := os.Remove(path); err != nil {
  213. return err
  214. }
  215. logger.Infof("Removed: %s", info.Name())
  216. }
  217. return nil
  218. })
  219. if err != nil {
  220. return err
  221. }
  222. logger.Infof("Compressed config saved to: %s", compressedPath)
  223. return nil
  224. }