Esempio n. 1
0
func updateWorker(view db.Database, self db.Minion, store Store,
	etcdData storeData) {

	var containers []storeContainer
	for _, etcdc := range etcdData.containers {
		if etcdc.Minion == self.PrivateIP {
			containers = append(containers, etcdc)
		}
	}

	pairs, dbcs, etcdcs := join.Join(view.SelectFromContainer(nil), containers,
		func(left, right interface{}) int {
			dbc := left.(db.Container)
			l := storeContainer{
				StitchID: dbc.StitchID,
				Minion:   dbc.Minion,
				Image:    dbc.Image,
				Command:  dbc.Command,
				Env:      dbc.Env,
				Labels:   dbc.Labels,
			}
			return containerJoinScore(l, right.(storeContainer))
		})

	for _, i := range dbcs {
		dbc := i.(db.Container)
		view.Remove(dbc)
	}

	for _, etcdc := range etcdcs {
		pairs = append(pairs, join.Pair{
			L: view.InsertContainer(),
			R: etcdc,
		})
	}

	for _, pair := range pairs {
		dbc := pair.L.(db.Container)
		etcdc := pair.R.(storeContainer)

		dbc.StitchID = etcdc.StitchID
		dbc.Minion = etcdc.Minion
		dbc.Image = etcdc.Image
		dbc.Command = etcdc.Command
		dbc.Env = etcdc.Env
		dbc.Labels = etcdc.Labels

		view.Commit(dbc)
	}

	updateContainerIP(view.SelectFromContainer(nil), self.PrivateIP, store)
}
Esempio n. 2
0
func testReadContainerTransact(t *testing.T, view db.Database) {
	minion := view.InsertMinion()
	minion.Role = db.Worker
	minion.Self = true
	view.Commit(minion)

	for _, id := range []string{"a", "b"} {
		container := view.InsertContainer()
		container.DockerID = id
		view.Commit(container)
	}

	container := view.InsertContainer()
	container.DockerID = "c"
	container.IP = "junk"
	view.Commit(container)

	dir := directory(map[string]map[string]string{
		"a": {"IP": "1.0.0.0", "Labels": `["e"]`},
		"b": {"IP": "2.0.0.0", "Labels": `["e", "f"]`},
	})

	readContainerTransact(view, dir)

	ipMap := map[string]string{}
	labelMap := map[string][]string{}
	for _, c := range view.SelectFromContainer(nil) {
		ipMap[c.DockerID] = c.IP
		labelMap[c.DockerID] = c.Labels
	}

	expIPMap := map[string]string{
		"a": "1.0.0.0",
		"b": "2.0.0.0",
		"c": "",
	}
	if !eq(ipMap, expIPMap) {
		t.Error(spew.Sprintf("Found %s, Expected: %s", ipMap, expIPMap))
	}

	expLabelMap := map[string][]string{
		"a": {"e"},
		"b": {"e", "f"},
		"c": nil,
	}

	if !eq(labelMap, expLabelMap) {
		t.Error(spew.Sprintf("Found %s, Expected: %s", ipMap, expIPMap))
	}
}
Esempio n. 3
0
func updateContainers(view db.Database, spec stitch.Stitch) {
	score := func(l, r interface{}) int {
		left := l.(db.Container)
		right := r.(db.Container)

		if left.Image != right.Image ||
			!util.StrSliceEqual(left.Command, right.Command) ||
			!util.StrStrMapEqual(left.Env, right.Env) {
			return -1
		}

		score := util.EditDistance(left.Labels, right.Labels)
		if left.StitchID != right.StitchID {
			score++
		}
		return score
	}

	pairs, news, dbcs := join.Join(queryContainers(spec),
		view.SelectFromContainer(nil), score)

	for _, dbc := range dbcs {
		view.Remove(dbc.(db.Container))
	}

	for _, new := range news {
		pairs = append(pairs, join.Pair{L: new, R: view.InsertContainer()})
	}

	for _, pair := range pairs {
		newc := pair.L.(db.Container)
		dbc := pair.R.(db.Container)

		// By sorting the labels we prevent the database from getting confused
		// when their order is non deterministic.
		dbc.Labels = newc.Labels
		sort.Sort(sort.StringSlice(dbc.Labels))

		dbc.Command = newc.Command
		dbc.Image = newc.Image
		dbc.Env = newc.Env
		dbc.StitchID = newc.StitchID
		view.Commit(dbc)
	}
}
Esempio n. 4
0
func (sv *supervisor) runAppTransact(view db.Database,
	dkcsArgs []docker.Container) []string {

	var tearDowns []string

	dbKey := func(val interface{}) interface{} {
		return val.(db.Container).DockerID
	}
	dkKey := func(val interface{}) interface{} {
		return val.(docker.Container).ID
	}

	pairs, dbcs, dkcs := join.HashJoin(db.ContainerSlice(
		view.SelectFromContainer(nil)),
		docker.ContainerSlice(dkcsArgs), dbKey, dkKey)

	for _, iface := range dbcs {
		dbc := iface.(db.Container)

		tearDowns = append(tearDowns, dbc.DockerID)
		view.Remove(dbc)
	}

	for _, dkc := range dkcs {
		pairs = append(pairs, join.Pair{L: view.InsertContainer(), R: dkc})
	}

	for _, pair := range pairs {
		dbc := pair.L.(db.Container)
		dkc := pair.R.(docker.Container)

		dbc.DockerID = dkc.ID
		dbc.Pid = dkc.Pid
		dbc.Image = dkc.Image
		dbc.Command = append([]string{dkc.Path}, dkc.Args...)
		view.Commit(dbc)
	}

	return tearDowns
}
Esempio n. 5
0
func testUpdateWorkerDBC(t *testing.T, view db.Database) {
	minion := view.InsertMinion()
	minion.Role = db.Worker
	minion.Self = true
	view.Commit(minion)

	for id := 1; id < 3; id++ {
		container := view.InsertContainer()
		container.StitchID = id
		container.IP = fmt.Sprintf("10.1.0.%d", id-1)
		container.Minion = "1.2.3.4"
		container.Command = []string{"echo", "hi"}
		container.Env = map[string]string{"GOPATH": "~"}
		view.Commit(container)
	}

	cs := storeContainerSlice{
		{
			StitchID: 1,
			Command:  []string{"echo", "hi"},
			Labels:   []string{"red", "blue"},
			Env:      map[string]string{"GOPATH": "~"},
			Minion:   "1.2.3.4",
		}, {
			StitchID: 2,
			Command:  []string{"echo", "hi"},
			Labels:   []string{"blue", "green"},
			Env:      map[string]string{"GOPATH": "~"},
			Minion:   "1.2.3.4",
		}, {
			StitchID: 3,
			Command:  []string{"echo", "bye"},
			Labels:   []string{"blue", "green"},
			Env:      map[string]string{"GOPATH": "~"},
			Minion:   "1.2.3.5",
		},
	}

	store := newTestMock()

	jsonCS, _ := json.Marshal(cs)
	err := store.Set(containerStore, string(jsonCS), 0)
	assert.Nil(t, err)

	jsonNull, _ := json.Marshal(map[string]string{})
	minionDirKey := path.Join(nodeStore, "1.2.3.4")
	err = store.Set(path.Join(minionDirKey, minionIPStore), string(jsonNull), 0)
	assert.Nil(t, err)

	updateWorker(view, db.Minion{PrivateIP: "1.2.3.4",
		Subnet: "10.1.0.0"}, store, storeData{containers: cs})

	ipMap := map[int]string{}
	labelMap := map[int][]string{}
	commandMap := map[string][]string{}
	envMap := map[string]map[string]string{}
	for _, c := range view.SelectFromContainer(nil) {
		ipMap[c.StitchID] = c.IP
		labelMap[c.StitchID] = c.Labels
		commandMap[c.DockerID] = c.Command
		envMap[c.DockerID] = c.Env
	}

	expIPMap := map[int]string{
		1: "10.1.0.0",
		2: "10.1.0.1",
	}

	assert.Equal(t, expIPMap, ipMap)

	resultMap := map[string]string{}
	storeIPs, _ := store.Get(path.Join(minionDirKey, minionIPStore))
	json.Unmarshal([]byte(storeIPs), &resultMap)

	for id, ip := range resultMap {
		sid, _ := strconv.Atoi(id)
		if otherIP, ok := ipMap[sid]; !ok || ip != otherIP {
			t.Fatalf("IPs did not match: %s vs %s", ip, otherIP)
		}
	}

	expLabelMap := map[int][]string{
		1: {"red", "blue"},
		2: {"blue", "green"},
	}

	assert.Equal(t, expLabelMap, labelMap)
}