func (zi *ZkInstance) Delete() (bool, error) { // true if this was the last instance of app+sha+env var ( last bool err error err2 error list []string ) // try to get the data (its ok if we can't) dataErr := getJson(zi.dataPath(), zi) err = Zk.RecursiveDelete(zi.dataPath()) err2 = Zk.RecursiveDelete(zi.path()) if err != nil { return last, err } if err2 != nil { return last, err2 } // check if we can delete parent directories if list, err = ListInstances(zi.App, zi.Sha, zi.Env); err != nil { log.Printf("Warning: clean up fail during instance delete: %s", err) return last, nil // this is extra, no need to return the error if we couldn't get them } else if list != nil && len(list) > 0 { return last, nil } last = true Zk.RecursiveDelete(helper.GetBaseInstancePath(zi.App, zi.Sha, zi.Env)) if dataErr != nil { log.Printf("Warning: could not fetch data to clean up pool: %s", err) } if list, err = ListAppEnvs(zi.App, zi.Sha); err != nil { log.Printf("Warning: clean up fail during instance delete: %s", err) return last, nil // this is extra, no need to return the error if we couldn't get them } else if list != nil && len(list) > 0 { return last, nil } Zk.RecursiveDelete(helper.GetBaseInstancePath(zi.App, zi.Sha)) // no need to kill pools, they should have been cleaned up when we deleted the instances if list, err = ListShas(zi.App); err != nil { log.Printf("Warning: clean up fail during instance delete: %s", err) return last, nil // this is extra, no need to return the error if we couldn't get them } else if list != nil && len(list) > 0 { return last, nil } Zk.RecursiveDelete(helper.GetBaseInstancePath(zi.App)) // no need to kill pools, they should have been cleaned up when we deleted the instances return last, nil }
func ListInstances(app, sha, env string) (instances []string, err error) { instances, _, err = Zk.VisibleChildren(helper.GetBaseInstancePath(app, sha, env)) if err != nil { log.Printf("Error getting list of instances. Error: %s.", err.Error()) } if instances == nil { log.Println("No instances found. Returning empty list.") instances = []string{} } return }
func ListAppEnvs(app, sha string) (envs []string, err error) { envs, _, err = Zk.VisibleChildren(helper.GetBaseInstancePath(app, sha)) if err != nil { log.Printf("Error getting list of shas. Error: %s.", err.Error()) } if envs == nil { log.Println("No shas found. Returning empty list.") envs = []string{} } return }
func ListApps() (apps []string, err error) { apps, _, err = Zk.VisibleChildren(helper.GetBaseInstancePath()) if err != nil { log.Printf("Error getting list of apps. Error: %s.", err.Error()) } if apps == nil { log.Println("No apps found. Returning empty list.") apps = []string{} } return }
func (s *DatamodelSuite) TestInstance(c *C) { Zk.RecursiveDelete(helper.GetBaseInstancePath()) externalInst, err := CreateInstance(app, sha, env, host) c.Assert(err, IsNil) c.Assert(externalInst.ID, Not(Equals), "") c.Assert(externalInst.App, Equals, app) c.Assert(externalInst.Sha, Equals, sha) c.Assert(externalInst.Host, Equals, host) c.Assert(externalInst.SetPort(uint16(1337)), IsNil) otherExtInst, err := GetInstance(externalInst.ID) c.Assert(*otherExtInst, Equals, *externalInst) last, err := externalInst.Delete() c.Assert(last, Equals, true) c.Assert(err, IsNil) }
func CreateInstancePaths() { Zk.Touch(helper.GetBaseInstancePath()) Zk.Touch(helper.GetBaseInstanceDataPath()) }
func (s *DatamodelSuite) TestRouterExternalPool(c *C) { Zk.RecursiveDelete("/atlantis/router") Zk.RecursiveDelete("/atlantis/apps") Zk.RecursiveDelete(helper.GetBaseInstancePath()) CreateRouterPaths() CreateAppPath() // fake register app CreateOrUpdateApp(false, false, app, "ssh://[email protected]/app", "/", "*****@*****.**") CreateOrUpdateApp(false, false, "app2", "ssh://[email protected]/app", "/", "*****@*****.**") // do tests instance, err := CreateInstance(app, sha, env, host+"-1") c.Assert(err, IsNil) instance.SetPort(uint16(1337)) instance2, err := CreateInstance(app, sha, env, host+"-2") c.Assert(err, IsNil) instance2.SetPort(uint16(1338)) c.Assert(AddToPool([]string{instance.ID, instance2.ID}), IsNil) theName := helper.CreatePoolName(app, sha, env) helper.SetRouterRoot(false) thePool, err := routerzk.GetPool(Zk.Conn, theName) c.Assert(err, IsNil) c.Assert(thePool.Name, Equals, theName) c.Assert(thePool.Config.HealthzEvery, Not(Equals), "") c.Assert(thePool.Config.HealthzTimeout, Not(Equals), "") c.Assert(thePool.Config.RequestTimeout, Not(Equals), "") c.Assert(thePool.Hosts, DeepEquals, map[string]config.Host{host + "-1:1337": config.Host{Address: host + "-1:1337"}, host + "-2:1338": config.Host{Address: host + "-2:1338"}}) newInstance, err := CreateInstance("app2", "sha1", "env1", host+"-1") c.Assert(err, IsNil) newInstance.SetPort(uint16(1339)) newInstance2, err := CreateInstance(app, sha, env, host+"-3") c.Assert(err, IsNil) newInstance2.SetPort(uint16(1340)) c.Assert(DeleteFromPool([]string{instance2.ID}), IsNil) instance2.Delete() c.Assert(AddToPool([]string{newInstance.ID, newInstance2.ID}), IsNil) helper.SetRouterRoot(false) thePool, err = routerzk.GetPool(Zk.Conn, theName) c.Assert(err, IsNil) c.Assert(thePool.Name, Equals, theName) c.Assert(thePool.Config.HealthzEvery, Not(Equals), "") c.Assert(thePool.Config.HealthzTimeout, Not(Equals), "") c.Assert(thePool.Config.RequestTimeout, Not(Equals), "") c.Assert(thePool.Hosts, DeepEquals, map[string]config.Host{host + "-1:1337": config.Host{Address: host + "-1:1337"}, host + "-3:1340": config.Host{Address: host + "-3:1340"}}) helper.SetRouterRoot(false) thePool2, err := routerzk.GetPool(Zk.Conn, helper.CreatePoolName("app2", "sha1", "env1")) c.Assert(err, IsNil) c.Assert(thePool2.Name, Equals, helper.CreatePoolName("app2", "sha1", "env1")) c.Assert(thePool2.Config.HealthzEvery, Not(Equals), "") c.Assert(thePool2.Config.HealthzTimeout, Not(Equals), "") c.Assert(thePool2.Config.RequestTimeout, Not(Equals), "") c.Assert(thePool2.Hosts, DeepEquals, map[string]config.Host{host + "-1:1339": config.Host{Address: host + "-1:1339"}}) helper.SetRouterRoot(false) pools, err := routerzk.ListPools(Zk.Conn) c.Assert(err, IsNil) sort.Strings(pools) c.Assert(pools, DeepEquals, []string{thePool2.Name, thePool.Name}) c.Assert(DeleteFromPool([]string{instance.ID, newInstance.ID, newInstance2.ID}), IsNil) instance.Delete() newInstance.Delete() newInstance2.Delete() helper.SetRouterRoot(false) thePool, err = routerzk.GetPool(Zk.Conn, theName) c.Assert(err, Not(IsNil)) }
func (s *DatamodelSuite) TestInstanceListers(c *C) { Zk.RecursiveDelete(helper.GetBaseInstancePath()) apps := []string{"app1", "app2", "app3"} appsMap := map[string][]string{} appsMap["app1"] = []string{"app1sha1", "app1sha2", "app1sha3"} appsMap["app2"] = []string{"app2sha1"} appsMap["app3"] = []string{"app3sha1"} shas := map[string]map[string][]string{} shas["app1sha1"] = map[string][]string{"env1": []string{"", ""}} shas["app1sha2"] = map[string][]string{"env2": []string{""}} shas["app1sha3"] = map[string][]string{"env1": []string{"", "", ""}, "env2": []string{""}} shas["app2sha1"] = map[string][]string{"env2": []string{""}} shas["app3sha1"] = map[string][]string{"env3": []string{""}} envs := map[string][]string{} envs["app1sha1"] = []string{"env1"} envs["app1sha2"] = []string{"env2"} envs["app1sha3"] = []string{"env1", "env2"} envs["app2sha1"] = []string{"env2"} envs["app3sha1"] = []string{"env3"} instances := []*ZkInstance{} for _, app := range apps { for _, sha := range appsMap[app] { for env, hosts := range shas[sha] { for i, _ := range hosts { inst, err := CreateInstance(app, sha, env, fmt.Sprintf("%s-%d", host, i)) c.Assert(err, IsNil) shas[sha][env][i] = inst.ID instances = append(instances, inst) } sort.Strings(shas[sha][env]) } } } getApps, err := ListApps() c.Assert(err, IsNil) sort.Strings(getApps) c.Assert(getApps, DeepEquals, apps) for _, app := range getApps { getShas, err := ListShas(app) c.Assert(err, IsNil) sort.Strings(getShas) c.Assert(getShas, DeepEquals, appsMap[app]) for _, sha := range getShas { getEnvs, err := ListAppEnvs(app, sha) c.Assert(err, IsNil) sort.Strings(getEnvs) c.Assert(getEnvs, DeepEquals, envs[sha]) for _, env := range getEnvs { getInsts, err := ListInstances(app, sha, env) c.Assert(err, IsNil) sort.Strings(getInsts) c.Assert(getInsts, DeepEquals, shas[sha][env]) } } } for _, inst := range instances { _, err = inst.Delete() c.Assert(err, IsNil) } getApps, err = ListApps() c.Assert(err, IsNil) sort.Strings(getApps) c.Assert(getApps, DeepEquals, []string{}) }
func (zi *ZkInstance) path() string { return helper.GetBaseInstancePath(zi.App, zi.Sha, zi.Env, zi.ID) }