func NewContainerManager(s3Bucket, s3Prefix, appname string, proxy *Proxy, cfgFile string, rport int) (*ContainerManager, error) { cm := &ContainerManager{ S3Bucket: s3Bucket, S3Prefix: s3Prefix, AppName: appname, Proxy: proxy, RegistryPort: rport, s3client: s3.NewFromEnv(), containers: map[string]int{}, containerEnv: map[string]string{}, } if cfgFile != "" { err := cm.readConfigFile(cfgFile) if err != nil { return nil, err } } var e error cm.dockerHost, e = dockerclient.New("127.0.0.1", 4243) if e != nil { return nil, e } return cm, func() error { err := cm.runLatest() switch err { case nil, ErrorLaunchConfigNotFound: return nil default: return err } }() }
func (cc *currentImage) Run() error { s3client := s3.NewFromEnv() cfg, err := wunderproxy.LoadCurrentLaunchConfig(s3client, cc.S3Bucket, cc.S3Prefix) if err != nil { return err } fmt.Println(cfg.ContainerConfig.Image) return nil }
func (cm *ContainerManager) StartContainer(hash string) (string, int, error) { cm.s3client = s3.NewFromEnv() cfg, e := LoadLaunchConfig(cm.s3client, cm.S3Bucket, cm.S3Prefix, hash) if e != nil { return "", -1, e } containerId, port, err := cm.execute(cfg) if err != nil { return "", -1, err } cm.currentContainer = containerId cm.containers[hash] = port return containerId, port, nil }
func (cm *ContainerManager) SwitchContainer(hash string) (string, int, error) { cm.s3client = s3.NewFromEnv() port, ok := cm.containers[hash] if !ok { return "", -1, fmt.Errorf("don't know about a container with revision %q", hash) } cm.Proxy.Update(fmt.Sprintf("localhost:%d", port)) // Stop old containers. Ignore errors ... they'll get visible soon enough. go func() { logger.Printf("stopping old containers") err := cm.StopOldContainers() if err != nil { logger.Printf("error stopping old containers: %s", err) } }() return cm.currentContainer, port, nil }