Пример #1
0
func (s *DatamodelSuite) TestReserveRouterPortAndUpdateTrie(c *C) {
	Zk.RecursiveDelete(helper.GetBaseRouterPortsPath(true))
	Zk.RecursiveDelete(helper.GetBaseRouterPortsPath(false))
	Zk.RecursiveDelete(helper.GetBaseLockPath())
	Zk.RecursiveDelete("/atlantis/router")
	CreateRouterPaths()
	CreateRouterPortsPaths()
	CreateLockPaths()

	MinRouterPort = uint16(65533)
	MaxRouterPort = uint16(65535)

	helper.SetRouterRoot(true)
	port, created, err := ReserveRouterPortAndUpdateTrie(true, "app", "sha", "env")
	c.Assert(err, IsNil)
	c.Assert(created, Equals, true)
	c.Assert(port, Equals, "65533")
	trie, err := routerzk.GetTrie(Zk.Conn, helper.GetAppEnvTrieName("app", "env"))
	c.Assert(err, IsNil)
	c.Assert(len(trie.Rules), Equals, 1)
	port, created, err = ReserveRouterPortAndUpdateTrie(true, "app", "sha2", "env")
	c.Assert(err, IsNil)
	c.Assert(created, Equals, false)
	c.Assert(port, Equals, "65533")
	trie, err = routerzk.GetTrie(Zk.Conn, helper.GetAppEnvTrieName("app", "env"))
	c.Assert(err, IsNil)
	c.Assert(len(trie.Rules), Equals, 2)
}
func (s *DeployHelperSuite) TestResolveDepValues(c *C) {
	datamodel.Zk.RecursiveDelete(helper.GetBaseEnvPath())
	datamodel.Zk.RecursiveDelete(helper.GetBaseRouterPath(true))
	datamodel.Zk.RecursiveDelete(helper.GetBaseRouterPath(false))
	datamodel.Zk.RecursiveDelete(helper.GetBaseRouterPortsPath(true))
	datamodel.Zk.RecursiveDelete(helper.GetBaseRouterPortsPath(false))
	datamodel.CreateEnvPath()
	datamodel.CreateRouterPaths()
	datamodel.Router(true, "dev", "somehost", "1.2.3.4").Save()
	zkEnv := datamodel.Env("root")
	err := zkEnv.Save()
	c.Assert(err, IsNil)
	deps, err := ResolveDepValues("app", zkEnv, []string{"hello-go"}, false, &Task{})
	c.Assert(err, Not(IsNil))
	_, err = datamodel.CreateInstance("hello-go", "1234567890", "root", "myhost")
	c.Assert(err, IsNil)
	_, err = datamodel.CreateOrUpdateApp(false, false, "app", "ssh://github.com/ooyala/apo", "/", "*****@*****.**")
	c.Assert(err, IsNil)
	zkApp, err := datamodel.CreateOrUpdateApp(false, true, "hello-go", "ssh://github.com/ooyala/hello-go", "/", "*****@*****.**")
	c.Assert(err, IsNil)
	c.Assert(zkApp.AddDependerAppData(&DependerAppData{Name: "app", DependerEnvData: map[string]*DependerEnvData{"root": &DependerEnvData{Name: "root"}}}), IsNil)
	deps, err = ResolveDepValues("app", zkEnv, []string{"hello-go"}, false, &Task{})
	c.Assert(err, IsNil)
	c.Assert(deps["dev1"]["hello-go"].DataMap["address"], Equals, fmt.Sprintf("internal-router.1.%s.suffix.com:%d", Region, datamodel.MinRouterPort))
	deps, err = ResolveDepValues("app", zkEnv, []string{"hello-go"}, true, &Task{})
	c.Assert(err, IsNil)
	c.Assert(deps["dev1"]["hello-go"].EncryptedData, Not(Equals), "")
	c.Assert(deps["dev1"]["hello-go"].DataMap, IsNil)
	scrypto.DecryptAppDep(deps["dev1"]["hello-go"])
	c.Assert(deps["dev1"]["hello-go"].DataMap, Not(IsNil))
	c.Assert(deps["dev1"]["hello-go"].DataMap["address"], Equals, fmt.Sprintf("internal-router.1.%s.suffix.com:%d", Region, datamodel.MinRouterPort))
}
Пример #3
0
func GetRouterPorts(internal bool) (zr *ZkRouterPorts) {
	zr = &ZkRouterPorts{}
	err := getJson(helper.GetBaseRouterPortsPath(internal), zr)
	if err != nil || zr == nil {
		zr = &ZkRouterPorts{
			Internal:  internal,
			PortMap:   map[string]types.AppEnv{},
			AppEnvMap: map[string]string{},
		}
		zr.save()
	} else if zr.PortMap == nil || zr.AppEnvMap == nil {
		zr.Internal = internal
		zr.PortMap = map[string]types.AppEnv{}
		zr.AppEnvMap = map[string]string{}
		zr.save()
	}
	return zr
}
Пример #4
0
func CreateRouterPortsPaths() {
	Zk.Touch(helper.GetBaseRouterPortsPath(true))
	Zk.Touch(helper.GetBaseRouterPortsPath(false))
}
Пример #5
0
func (s *DatamodelSuite) TestRouterPorts(c *C) {
	// test internal
	Zk.RecursiveDelete(helper.GetBaseRouterPortsPath(true))
	Zk.RecursiveDelete(helper.GetBaseRouterPortsPath(false))
	Zk.RecursiveDelete(helper.GetBaseLockPath())
	CreateRouterPortsPaths()
	CreateLockPaths()

	MinRouterPort = uint16(65533)
	MaxRouterPort = uint16(65535)

	c.Assert(HasRouterPortForAppEnv(true, "app", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(false, "app", "env"), Equals, false)

	appPort, err := reserveRouterPort(true, "app", "env")
	c.Assert(err, IsNil)
	appPort2, err := reserveRouterPort(true, "app", "env")
	c.Assert(err, IsNil)
	c.Assert(appPort, Equals, appPort2)
	c.Assert(HasRouterPortForAppEnv(true, "app", "env"), Equals, true)
	c.Assert(HasRouterPortForAppEnv(false, "app", "env"), Equals, false)

	app2Port, err := reserveRouterPort(true, "app2", "env")
	c.Assert(err, IsNil)
	c.Assert(appPort, Not(Equals), app2Port)
	c.Assert(HasRouterPortForAppEnv(true, "app2", "env"), Equals, true)
	c.Assert(HasRouterPortForAppEnv(false, "app2", "env"), Equals, false)

	_, err = reserveRouterPort(true, "app3", "env2")
	c.Assert(err, IsNil)
	c.Assert(HasRouterPortForAppEnv(true, "app3", "env2"), Equals, true)
	c.Assert(HasRouterPortForAppEnv(false, "app3", "env2"), Equals, false)

	_, err = reserveRouterPort(true, "app4", "env3")
	c.Assert(err, Not(IsNil))

	err = ReclaimRouterPortsForEnv(true, "env")
	c.Assert(err, IsNil)
	c.Assert(HasRouterPortForAppEnv(true, "app", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(true, "app2", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(true, "app3", "env2"), Equals, true)
	err = ReclaimRouterPortsForApp(true, "app3")
	c.Assert(err, IsNil)
	c.Assert(HasRouterPortForAppEnv(true, "app", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(true, "app2", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(true, "app3", "env2"), Equals, false)
	_, err = reserveRouterPort(true, "app4", "env2")
	c.Assert(err, IsNil)
	c.Assert(HasRouterPortForAppEnv(true, "app4", "env2"), Equals, true)
	c.Assert(HasRouterPortForAppEnv(false, "app4", "env2"), Equals, false)

	// test external
	Zk.RecursiveDelete(helper.GetBaseRouterPortsPath(true))
	Zk.RecursiveDelete(helper.GetBaseRouterPortsPath(false))
	Zk.RecursiveDelete(helper.GetBaseLockPath())
	CreateRouterPortsPaths()
	CreateLockPaths()

	MinRouterPort = uint16(65533)
	MaxRouterPort = uint16(65535)

	c.Assert(HasRouterPortForAppEnv(true, "app", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(false, "app", "env"), Equals, false)

	appPort, err = reserveRouterPort(false, "app", "env")
	c.Assert(err, IsNil)
	appPort2, err = reserveRouterPort(false, "app", "env")
	c.Assert(err, IsNil)
	c.Assert(appPort, Equals, appPort2)
	c.Assert(HasRouterPortForAppEnv(false, "app", "env"), Equals, true)
	c.Assert(HasRouterPortForAppEnv(true, "app", "env"), Equals, false)

	app2Port, err = reserveRouterPort(false, "app2", "env")
	c.Assert(err, IsNil)
	c.Assert(appPort, Not(Equals), app2Port)
	c.Assert(HasRouterPortForAppEnv(false, "app2", "env"), Equals, true)
	c.Assert(HasRouterPortForAppEnv(true, "app2", "env"), Equals, false)

	_, err = reserveRouterPort(false, "app3", "env2")
	c.Assert(err, IsNil)
	c.Assert(HasRouterPortForAppEnv(false, "app3", "env2"), Equals, true)
	c.Assert(HasRouterPortForAppEnv(true, "app3", "env2"), Equals, false)

	_, err = reserveRouterPort(false, "app4", "env3")
	c.Assert(err, Not(IsNil))

	err = ReclaimRouterPortsForEnv(false, "env")
	c.Assert(err, IsNil)
	c.Assert(HasRouterPortForAppEnv(false, "app", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(false, "app2", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(false, "app3", "env2"), Equals, true)
	err = ReclaimRouterPortsForApp(false, "app3")
	c.Assert(err, IsNil)
	c.Assert(HasRouterPortForAppEnv(false, "app", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(false, "app2", "env"), Equals, false)
	c.Assert(HasRouterPortForAppEnv(false, "app3", "env2"), Equals, false)
	_, err = reserveRouterPort(false, "app4", "env2")
	c.Assert(err, IsNil)
	c.Assert(HasRouterPortForAppEnv(false, "app4", "env2"), Equals, true)
	c.Assert(HasRouterPortForAppEnv(true, "app4", "env2"), Equals, false)
}
Пример #6
0
func (r *ZkRouterPorts) path() string {
	return helper.GetBaseRouterPortsPath(r.Internal)
}