urbit.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package docker
  2. // start up urbits
  3. import (
  4. "fmt"
  5. "goseg/config"
  6. "goseg/defaults"
  7. "goseg/structs"
  8. "io/ioutil"
  9. "path/filepath"
  10. "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/api/types/mount"
  12. )
  13. // load existing urbits from config json
  14. func LoadUrbits() error {
  15. logger.Info("Loading Urbit ships")
  16. // Loop through pier list
  17. conf := config.Conf()
  18. for _, pier := range conf.Piers {
  19. logger.Info(fmt.Sprintf("Loading pier %s", pier))
  20. // load json into struct
  21. err := config.LoadUrbitConfig(pier)
  22. if err != nil {
  23. logger.Error(fmt.Sprintf("Error loading %s config: %v", pier, err))
  24. continue
  25. }
  26. shipConf := config.UrbitConf(pier)
  27. // don't bootstrap if it's busted
  28. if shipConf.BootStatus != "noboot" {
  29. info, err := StartContainer(pier, "vere")
  30. if err != nil {
  31. logger.Error(fmt.Sprintf("Error starting %s: %v", pier, err))
  32. continue
  33. }
  34. config.UpdateContainerState(pier, info)
  35. }
  36. }
  37. return nil
  38. }
  39. // urbit container config builder
  40. func urbitContainerConf(containerName string) (container.Config, container.HostConfig, error) {
  41. var containerConfig container.Config
  42. var hostConfig container.HostConfig
  43. var scriptContent string
  44. // construct the container metadata from version server info
  45. containerInfo, err := GetLatestContainerInfo("vere")
  46. if err != nil {
  47. return containerConfig, hostConfig, err
  48. }
  49. desiredImage := fmt.Sprintf("%s:%s@sha256:%s", containerInfo["repo"], containerInfo["tag"], containerInfo["hash"])
  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, hostConfig, 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. case "noboot":
  71. return containerConfig, hostConfig, fmt.Errorf("%s marked noboot!",containerName)
  72. default:
  73. return containerConfig, hostConfig, fmt.Errorf("Unknown action: %s", act)
  74. }
  75. // reset ship status to boot for next time
  76. if act != "boot" {
  77. updateUrbitConf := shipConf
  78. updateUrbitConf.BootStatus = "boot"
  79. var newConfig map[string]structs.UrbitDocker
  80. newConfig[containerName] = updateUrbitConf
  81. err = config.UpdateUrbitConfig(newConfig)
  82. if err != nil {
  83. logger.Warn("Unable to reset %s boot script!", containerName)
  84. }
  85. }
  86. // write the script
  87. scriptPath := filepath.Join(config.DockerDir, containerName, "_data", containerName, "start_urbit.sh")
  88. err = ioutil.WriteFile(scriptPath, []byte(scriptContent), 0755) // make the script executable
  89. if err != nil {
  90. return containerConfig, hostConfig, fmt.Errorf("Failed to write script: %v", err)
  91. }
  92. // gather boot option values
  93. shipName := shipConf.PierName
  94. loomValue := string(shipConf.LoomSize)
  95. dirnameValue := shipConf.PierName
  96. var devMode string
  97. if shipConf.DevMode == true {
  98. devMode = "True"
  99. } else {
  100. devMode = "False"
  101. }
  102. var httpPort string
  103. var amesPort string
  104. var network string
  105. if shipConf.Network == "wireguard" {
  106. httpPort = string(shipConf.WgHTTPPort)
  107. amesPort = string(shipConf.WgAmesPort)
  108. network = "container:wireguard"
  109. } else {
  110. httpPort = string(shipConf.HTTPPort)
  111. amesPort = string(shipConf.AmesPort)
  112. network = "default"
  113. }
  114. // finally construct the container config structs
  115. containerConfig = container.Config{
  116. Image: desiredImage,
  117. Entrypoint: []string{scriptPath, shipName, "--loom=" + loomValue, "--dirname=" + dirnameValue, "--dev-mode="+ devMode, "--http-port=" + httpPort, "--port=" + amesPort},
  118. }
  119. mounts := []mount.Mount{
  120. {
  121. Type: mount.TypeBind,
  122. Source: shipName,
  123. Target: "/data",
  124. },
  125. }
  126. hostConfig = container.HostConfig{
  127. NetworkMode: container.NetworkMode(network),
  128. Mounts: mounts,
  129. }
  130. return containerConfig, hostConfig, nil
  131. }