minio.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/api/types/container"
  5. "github.com/docker/docker/api/types/mount"
  6. "goseg/config"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. )
  11. func LoadMC() error {
  12. logger.Info("Loading MC container")
  13. confPath := filepath.Join(config.BasePath, "settings", "mc.json")
  14. _, err := os.Open(confPath)
  15. if err != nil {
  16. // create a default if it doesn't exist
  17. err = config.CreateDefaultMcConf()
  18. if err != nil {
  19. // error if we can't create it
  20. errmsg := fmt.Sprintf("Unable to create MC config! %v", err)
  21. logger.Error(errmsg)
  22. }
  23. }
  24. logger.Info("Running MC")
  25. info, err := StartContainer("mc", "miniomc")
  26. if err != nil {
  27. logger.Error(fmt.Sprintf("Error starting MC: %v", err))
  28. return err
  29. }
  30. config.UpdateContainerState("mc", info)
  31. return nil
  32. }
  33. // iterate through each ship and create a minio
  34. // version stuff is offloaded to version server struct
  35. func LoadMinIOs() error {
  36. logger.Info("Loading MinIO containers")
  37. conf := config.Conf()
  38. for _, pier := range conf.Piers {
  39. // uConf := config.UrbitConf(pier)
  40. label := "minio_" + pier
  41. info, err := StartContainer(label, "minio")
  42. if err != nil {
  43. logger.Error(fmt.Sprintf("Error starting %s Minio: %v", pier, err))
  44. return err
  45. }
  46. config.UpdateContainerState(label, info)
  47. }
  48. return nil
  49. }
  50. // minio container config builder
  51. func minioContainerConf(containerName string) (container.Config, container.HostConfig, error) {
  52. var containerConfig container.Config
  53. var hostConfig container.HostConfig
  54. shipName := strings.Split(containerName, "_")[1]
  55. err := config.LoadUrbitConfig(shipName)
  56. if err != nil {
  57. errmsg := fmt.Errorf("Error loading %s config: %v", shipName, err)
  58. return containerConfig, hostConfig, errmsg
  59. }
  60. shipConf := config.UrbitConf(shipName)
  61. // construct the container metadata from version server info
  62. containerInfo, err := GetLatestContainerInfo("minio")
  63. if err != nil {
  64. return containerConfig, hostConfig, err
  65. }
  66. desiredImage := fmt.Sprintf("%s:%s@sha256:%s", containerInfo["repo"], containerInfo["tag"], containerInfo["hash"])
  67. command := fmt.Sprintf("server /data --console-address :%s --address :%s", string(shipConf.WgConsolePort), string(shipConf.WgS3Port))
  68. environment := []string{
  69. fmt.Sprintf("MINIO_ROOT_USER=%s", shipName),
  70. fmt.Sprintf("MINIO_ROOT_PASSWORD=%s", shipConf.MinioPassword),
  71. fmt.Sprintf("MINIO_DOMAIN=s3.%s", shipConf.WgURL),
  72. fmt.Sprintf("MINIO_SERVER_URL=https://s3.%s", shipConf.WgURL),
  73. }
  74. mounts := []mount.Mount{
  75. {
  76. Type: mount.TypeBind,
  77. Source: shipName,
  78. Target: "/data",
  79. },
  80. }
  81. containerConfig = container.Config{
  82. Image: desiredImage,
  83. Cmd: []string{command},
  84. Env: environment,
  85. }
  86. // always on wg nw
  87. hostConfig = container.HostConfig{
  88. NetworkMode: "container:wireguard",
  89. Mounts: mounts,
  90. }
  91. return containerConfig, hostConfig, nil
  92. }
  93. // miniomc container config builder
  94. func mcContainerConf() (container.Config, container.HostConfig, error) {
  95. var containerConfig container.Config
  96. var hostConfig container.HostConfig
  97. // construct the container metadata from version server info
  98. containerInfo, err := GetLatestContainerInfo("miniomc")
  99. if err != nil {
  100. return containerConfig, hostConfig, err
  101. }
  102. desiredTag := containerInfo["tag"]
  103. desiredHash := containerInfo["hash"]
  104. desiredRepo := containerInfo["repo"]
  105. desiredImage := fmt.Sprintf("%s:%s@sha256:%s", desiredRepo, desiredTag, desiredHash)
  106. // construct the container config struct
  107. containerConfig = container.Config{
  108. Image: desiredImage,
  109. Entrypoint: []string{"/bin/bash"},
  110. Tty: true,
  111. OpenStdin: true,
  112. }
  113. // always on wg nw
  114. hostConfig = container.HostConfig{
  115. NetworkMode: "container:wireguard",
  116. }
  117. return containerConfig, hostConfig, nil
  118. }