func (self *ContainerManager) Manage(host string, container *config.Container) error { if config.NotIn(self.UsedContainerNames[host], container.Name()) { self.UsedContainerNames[host] = append(self.UsedContainerNames[host], container.Name()) } err := self.Launch(host, container) if err != nil { return err } return self.StopExtra(host, container) }
func (self *ContainerManager) StopExtra(host string, cont *config.Container) error { runningContainers, err := self.GetRunningContainersByName(host, cont.Name()) if err != nil { return err } containersToStop := len(runningContainers) - cont.GetCount() vClient := self.Clients[host] if containersToStop > 0 { for i := 0; i < containersToStop; i++ { c := runningContainers[len(runningContainers)-1-i] err = vClient.Stop(c.ContainerId) if err != nil { return err } fmt.Println("stopped " + c.String()) } } return nil }
func (self *ContainerManager) Launch(host string, cont *config.Container) error { runningContainers, err := self.GetRunningContainersByName(host, cont.Name()) if err != nil { return err } containersToLaunch := cont.GetCount() - len(runningContainers) vClient := self.Clients[host] if containersToLaunch > 0 { for i := 0; i < containersToLaunch; i++ { variables := make(map[string]string) for _, link := range cont.Links { if _, ok := self.EnvVariables[link]; ok { variables[link] = self.EnvVariables[link] } } if cont.Variables != nil { for _, variable := range cont.Variables { values := strings.Split(variable, ":") if len(values) == 2 { variables[values[0]] = self.VariableValue(values[1]) } } } serverContainer, err := vClient.Run(cont.Name(), cont.Image, cont.Tag, cont.ImageId, variables, cont.DNS, cont.Volumes, cont.Command, cont.Dockerfile) if err != nil { return err } if _, ok := self.EnvVariables[cont.Name()]; !ok { self.EnvVariables[cont.Name()] = serverContainer.Ip } fmt.Println(serverContainer) err = self.Expose(host, cont, serverContainer) if err != nil { return err } } } else if len(runningContainers) > 0 { for _, rc := range runningContainers { err = self.Expose(host, cont, rc) if err != nil { return err } } if _, ok := self.EnvVariables[cont.Name()]; !ok { self.EnvVariables[cont.Name()] = runningContainers[0].Ip } } return nil }