func GetByContainerId(containerId string) (*Container, error) { db := storage.GetStorage() ip, err := db.Get(containerId) if err != nil { return nil, err } return GetByIp(ip) }
func FreeIp(ip string) error { if ip == "" { return errors.New("no ip to free") } db := storage.GetStorage() container, err := GetByIp(ip) if err == nil { db.Delete(container.ContainerId) } return db.Delete(ip) }
func (self *Host) Persist() error { db := storage.GetStorage() jsonBytes, err := json.Marshal(self) if err != nil { return err } err = db.Set(self.InternalIp, string(jsonBytes)) if err != nil { return err } return nil }
func GetByIp(ip string) (*Container, error) { db := storage.GetStorage() jsonString, err := db.Get(ip) if err != nil { return nil, err } var container *Container err = json.Unmarshal([]byte(jsonString), &container) if err != nil { return nil, err } return container, nil }
func GetByIp(ip string) (*Host, error) { db := storage.GetStorage() jsonString, err := db.Get(ip) if err != nil { return nil, err } var host *Host err = json.Unmarshal([]byte(jsonString), &host) if err != nil { return nil, err } return host, nil }
func GetHosts() ([]*Host, error) { db := storage.GetStorage() ips, err := db.Keys("10.0.1.") if err != nil { return nil, err } hosts := []*Host{} for _, ip := range ips { host, err := GetByIp(ip) if err != nil { return nil, err } if host.ExternalIp != "" && host.InternalIp != "" { hosts = append(hosts, host) } } return hosts, nil }
func Persist(c *Container) error { jsonBytes, err := json.Marshal(c) if err != nil { return err } if c.Ip == "" { return errors.New("Can't persist container that is not running -" + c.String()) } db := storage.GetStorage() err = db.Set(c.Ip, string(jsonBytes)) if err != nil { return err } err = db.Set(c.ContainerId, c.Ip) if err != nil { return err } return nil }
func ReserveIp() (string, error) { db := storage.GetStorage() counter := 0 ip := "" for { counter++ ip = "10.0.1." + strconv.Itoa(counter) ok, err := db.KeyPresent(ip) if err != nil { return "", err } if ok == false { err = db.Set(ip, "reserved") if err != nil { return "", err } break } } return ip, nil }
func GetContainers(check bool) ([]*Container, error) { db := storage.GetStorage() ips, err := db.Keys("10.0.1.") if err != nil { return nil, err } containers := []*Container{} for _, ip := range ips { container, err := GetByIp(ip) if err != nil { return nil, err } if !check { if container.Name != "" { containers = append(containers, container) } } else { if container.Check() { containers = append(containers, container) } } } return containers, nil }