urbit.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package docker
  2. // start up urbits
  3. import (
  4. "fmt"
  5. "goseg/config"
  6. "goseg/defaults"
  7. "io/ioutil"
  8. "path/filepath"
  9. "github.com/docker/docker/api/types/container"
  10. )
  11. // load existing urbits from config json
  12. func LoadUrbits() error {
  13. logger.Info("Loading Urbit ships")
  14. // Loop through pier list
  15. conf := config.Conf()
  16. for _, pier := range conf.Piers {
  17. logger.Info(fmt.Sprintf("Loading pier %s", pier))
  18. // load json into struct
  19. err := config.LoadUrbitConfig(pier)
  20. if err != nil {
  21. logger.Error(fmt.Sprintf("Error loading %s config: %v", pier, err))
  22. continue
  23. }
  24. shipConf := config.UrbitConf(pier)
  25. // don't bootstrap if it's busted
  26. if shipConf.BootStatus != "noboot" {
  27. info, err := StartContainer(pier, "vere")
  28. if err != nil {
  29. logger.Error(fmt.Sprintf("Error starting %s: %v", pier, err))
  30. continue
  31. }
  32. config.UpdateContainerState(pier, info)
  33. }
  34. }
  35. return nil
  36. }
  37. // urbit container config builder
  38. func urbitContainerConf(containerName string) (container.Config, error) {
  39. var containerConfig container.Config
  40. var scriptContent string
  41. // construct the container metadata from version server info
  42. containerInfo, err := GetLatestContainerInfo("vere")
  43. if err != nil {
  44. return containerConfig, err
  45. }
  46. desiredTag := containerInfo["tag"]
  47. desiredHash := containerInfo["hash"]
  48. desiredRepo := containerInfo["repo"]
  49. desiredImage := fmt.Sprintf("%s:%s@sha256:%s", desiredRepo, desiredTag, desiredHash)
  50. // reload urbit conf from disk
  51. err = config.LoadUrbitConfig(containerName)
  52. if err != nil {
  53. errmsg := fmt.Errorf("Error loading %s config: %v", containerName, err)
  54. return containerConfig, errmsg
  55. }
  56. // put in memory
  57. shipConf := config.UrbitConf(containerName)
  58. // todo: this BootStatus doesnt actually have anythin to do with pack and meld right now
  59. act := shipConf.BootStatus
  60. // get the correct startup script based on act
  61. switch act {
  62. case "boot":
  63. scriptContent = defaults.StartScript
  64. case "pack":
  65. scriptContent = defaults.PackScript
  66. case "meld":
  67. scriptContent = defaults.MeldScript
  68. case "prep":
  69. scriptContent = defaults.PrepScript
  70. default:
  71. return containerConfig, fmt.Errorf("Unknown action: %s", act)
  72. }
  73. // write the script
  74. scriptPath := filepath.Join(config.DockerDir, containerName, "_data", containerName, "start_urbit.sh")
  75. err = ioutil.WriteFile(scriptPath, []byte(scriptContent), 0755) // make the script executable
  76. if err != nil {
  77. return containerConfig, fmt.Errorf("Failed to write script: %v", err)
  78. }
  79. // gather boot option values
  80. shipName := shipConf.PierName
  81. loomValue := string(shipConf.LoomSize)
  82. dirnameValue := shipConf.PierName
  83. var httpPort string
  84. var amesPort string
  85. if shipConf.Network == "wireguard" {
  86. httpPort = string(shipConf.WgHTTPPort)
  87. amesPort = string(shipConf.WgAmesPort)
  88. } else {
  89. httpPort = string(shipConf.HTTPPort)
  90. amesPort = string(shipConf.AmesPort)
  91. }
  92. // finally construct the container config struct
  93. containerConfig = container.Config{
  94. Image: desiredImage,
  95. Entrypoint: []string{scriptPath, shipName, "--loom=" + loomValue, "--dirname=" + dirnameValue, "--http-port=" + httpPort, "--ames-port=" + amesPort},
  96. }
  97. return containerConfig, nil
  98. }