예제 #1
0
파일: handlers.go 프로젝트: pedrosnk/tsuru
func nodeContainerInfo(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	pools, err := listContextValues(t, permission.PermNodecontainerRead, true)
	if err != nil {
		return err
	}
	name := r.URL.Query().Get(":name")
	configMap, err := nodecontainer.LoadNodeContainersForPools(name)
	if err != nil {
		return err
	}
	if pools != nil {
		poolMap := map[string]struct{}{}
		for _, p := range pools {
			poolMap[p] = struct{}{}
		}
		for poolName := range configMap {
			if poolName == "" {
				continue
			}
			if _, ok := poolMap[poolName]; !ok {
				delete(configMap, poolName)
			}
		}
	}
	w.Header().Set("Content-Type", "application/json")
	return json.NewEncoder(w).Encode(configMap)
}
예제 #2
0
func (s *S) TestMigrateBSEnvs(c *check.C) {
	conn, err := db.Conn()
	c.Assert(err, check.IsNil)
	defer conn.Close()
	entries, err := nodecontainer.LoadNodeContainersForPools(nodecontainer.BsDefaultName)
	c.Assert(err, check.IsNil)
	c.Assert(entries, check.DeepEquals, map[string]nodecontainer.NodeContainerConfig{
		"": {},
	})
	coll := conn.Collection("bsconfig")
	err = coll.Insert(bson.M{
		"_id":   "bs",
		"image": "tsuru/bs@shacabum",
		"token": "999",
		"envs": []bson.M{
			{"name": "FOO", "value": "1"},
		},
		"pools": []bson.M{},
	})
	c.Assert(err, check.IsNil)
	err = migrateBSEnvs()
	c.Assert(err, check.IsNil)
	entries, err = nodecontainer.LoadNodeContainersForPools(nodecontainer.BsDefaultName)
	c.Assert(err, check.IsNil)
	defaultEntry := entries[""]
	c.Assert(defaultEntry.Config.Env, check.HasLen, 5)
	c.Assert(defaultEntry.Config.Env[0], check.Matches, `TSURU_TOKEN=\w{40}`)
	defaultEntry.Config.Env = defaultEntry.Config.Env[1:]
	entries[""] = defaultEntry
	expected := map[string]nodecontainer.NodeContainerConfig{
		"": {Name: "big-sibling", PinnedImage: "tsuru/bs@shacabum", Config: docker.Config{
			Image: "tsuru/bs:v1",
			Env: []string{
				"TSURU_ENDPOINT=http://tsuru.server:8080/",
				"HOST_PROC=/prochost",
				"SYSLOG_LISTEN_ADDRESS=udp://0.0.0.0:1514",
				"FOO=1",
			},
		}, HostConfig: docker.HostConfig{
			RestartPolicy: docker.AlwaysRestart(),
			Privileged:    true,
			NetworkMode:   "host",
			Binds:         []string{"/proc:/prochost:ro"},
		}},
	}
	c.Assert(entries, check.DeepEquals, expected)
	err = coll.UpdateId("bs", bson.M{
		"$set": bson.M{"pools": []bson.M{
			{"name": "p1", "envs": []bson.M{{"name": "A", "value": "x"}}},
			{"name": "p2", "envs": []bson.M{{"name": "A", "value": "y"}}},
			{"name": "p3", "envs": []bson.M{{"name": "B", "value": "z"}, {"name": "FOO", "value": "2"}}},
		}},
	})
	c.Assert(err, check.IsNil)
	err = migrateBSEnvs()
	c.Assert(err, check.IsNil)
	entries, err = nodecontainer.LoadNodeContainersForPoolsMerge(nodecontainer.BsDefaultName, true)
	c.Assert(err, check.IsNil)
	for k, v := range entries {
		v.Config.Env = v.Config.Env[1:]
		entries[k] = v
	}
	expectedBase := expected[""]
	expectedP1 := expectedBase
	expectedP2 := expectedBase
	expectedP3 := expectedBase
	expectedBase.Config.Env = append(expectedBase.Config.Env, "FOO=1")
	baseEnvs := append([]string{}, expectedBase.Config.Env...)
	expectedP1.Config.Env = append(baseEnvs, "A=x")
	expectedP2.Config.Env = append(baseEnvs, "A=y")
	expectedP3.Config.Env = append(baseEnvs, "B=z", "FOO=2")
	c.Assert(entries[""], check.DeepEquals, expectedBase)
	c.Assert(entries["p1"], check.DeepEquals, expectedP1)
	c.Assert(entries["p2"], check.DeepEquals, expectedP2)
	c.Assert(entries["p3"], check.DeepEquals, expectedP3)
}