| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package docker
- // start up urbits
- import (
- "fmt"
- "goseg/config"
- "goseg/defaults"
- "path/filepath"
- "io/ioutil"
- "github.com/docker/docker/api/types/container"
- )
- // load existing urbits from config json
- func LoadUrbits() error {
- logger.Info("Loading Urbit ships")
- // Loop through pier list
- conf := config.Conf()
- for _, pier := range conf.Piers {
- logger.Info(fmt.Sprintf("Loading pier %s", pier))
- // load json into struct
- err := config.LoadUrbitConfig(pier)
- if err != nil {
- logger.Error(fmt.Sprintf("Error loading %s config: %v", pier, err))
- continue
- }
- shipConf := config.UrbitConf(pier)
- // don't bootstrap if it's busted
- if shipConf.BootStatus != "noboot" {
- info, err := StartContainer(pier, "vere")
- if err != nil {
- logger.Error(fmt.Sprintf("Error starting %s: %v", pier, err))
- continue
- }
- config.UpdateContainerState(pier, info)
- }
- }
- return nil
- }
- // urbit container config builder
- func urbitContainerConf(containerName string) (container.Config, error) {
- var containerConfig container.Config
- var scriptContent string
- // construct the container metadata from version server info
- containerInfo, err := GetLatestContainerInfo("vere")
- if err != nil {
- return containerConfig, err
- }
- desiredTag := containerInfo["tag"]
- desiredHash := containerInfo["hash"]
- desiredRepo := containerInfo["repo"]
- desiredImage := fmt.Sprintf("%s:%s@sha256:%s", desiredRepo, desiredTag, desiredHash)
- // reload urbit conf from disk
- err = config.LoadUrbitConfig(containerName)
- if err != nil {
- errmsg := fmt.Errorf("Error loading %s config: %v", containerName, err)
- return containerConfig, errmsg
- }
- // put in memory
- shipConf := config.UrbitConf(containerName)
- // todo: this BootStatus doesnt actually have anythin to do with pack and meld right now
- act := shipConf.BootStatus
- // get the correct startup script based on act
- switch act {
- case "boot":
- scriptContent = defaults.StartScript
- case "pack":
- scriptContent = defaults.PackScript
- case "meld":
- scriptContent = defaults.MeldScript
- case "prep":
- scriptContent = defaults.PrepScript
- default:
- return containerConfig, fmt.Errorf("Unknown action: %s", act)
- }
- // write the script
- scriptPath := filepath.Join(config.DockerDir, containerName, "_data", containerName, "start_urbit.sh")
- err = ioutil.WriteFile(scriptPath, []byte(scriptContent), 0755) // make the script executable
- if err != nil {
- return containerConfig, fmt.Errorf("Failed to write script: %v", err)
- }
- // gather boot option values
- shipName := shipConf.PierName
- loomValue := string(shipConf.LoomSize)
- dirnameValue := shipConf.PierName
- var httpPort string
- var amesPort string
- if shipConf.Network == "wireguard" {
- httpPort = string(shipConf.WgHTTPPort)
- amesPort = string(shipConf.WgAmesPort)
- } else {
- httpPort = string(shipConf.HTTPPort)
- amesPort = string(shipConf.AmesPort)
- }
- // finally construct the container config struct
- containerConfig = container.Config{
- Image: desiredImage,
- Entrypoint: []string{scriptPath, shipName, "--loom="+loomValue, "--dirname="+dirnameValue, "--http-port="+httpPort, "--ames-port="+amesPort},
- }
- return containerConfig, nil
- }
|