func updatePlacements(view db.Database, spec stitch.Stitch) { stitchPlacements := toDBPlacements(spec.QueryPlacements()) key := func(val interface{}) interface{} { pVal := val.(db.Placement) return struct { tl string rule db.PlacementRule }{pVal.TargetLabel, pVal.Rule} } _, addSet, removeSet := join.HashJoin(stitchPlacements, db.PlacementSlice(view.SelectFromPlacement(nil)), key, key) for _, toAddIntf := range addSet { toAdd := toAddIntf.(db.Placement) newPlacement := view.InsertPlacement() newPlacement.TargetLabel = toAdd.TargetLabel newPlacement.Rule = toAdd.Rule view.Commit(newPlacement) } for _, toRemove := range removeSet { view.Remove(toRemove.(db.Placement)) } }
func updateConnections(view db.Database, spec stitch.Stitch) { scs, vcs := stitch.ConnectionSlice(spec.QueryConnections()), view.SelectFromConnection(nil) dbcKey := func(val interface{}) interface{} { c := val.(db.Connection) return stitch.Connection{ From: c.From, To: c.To, MinPort: c.MinPort, MaxPort: c.MaxPort, } } pairs, stitchs, dbcs := join.HashJoin(scs, db.ConnectionSlice(vcs), nil, dbcKey) for _, dbc := range dbcs { view.Remove(dbc.(db.Connection)) } for _, stitchc := range stitchs { pairs = append(pairs, join.Pair{L: stitchc, R: view.InsertConnection()}) } for _, pair := range pairs { stitchc := pair.L.(stitch.Connection) dbc := pair.R.(db.Connection) dbc.From = stitchc.From dbc.To = stitchc.To dbc.MinPort = stitchc.MinPort dbc.MaxPort = stitchc.MaxPort view.Commit(dbc) } }
func machineTxn(view db.Database, stitch stitch.Stitch) error { // XXX: How best to deal with machines that don't specify enough information? maxPrice, _ := stitch.QueryFloat("MaxPrice") stitchMachines := toDBMachine(stitch.QueryMachines(), maxPrice) dbMachines := view.SelectFromMachine(nil) scoreFun := func(left, right interface{}) int { stitchMachine := left.(db.Machine) dbMachine := right.(db.Machine) switch { case dbMachine.Provider != stitchMachine.Provider: return -1 case dbMachine.Region != stitchMachine.Region: return -1 case dbMachine.Size != "" && stitchMachine.Size != dbMachine.Size: return -1 case dbMachine.Role != db.None && dbMachine.Role != stitchMachine.Role: return -1 case dbMachine.DiskSize != stitchMachine.DiskSize: return -1 case dbMachine.PrivateIP == "": return 2 case dbMachine.PublicIP == "": return 1 default: return 0 } } pairs, bootList, terminateList := join.Join(stitchMachines, dbMachines, scoreFun) for _, toTerminate := range terminateList { toTerminate := toTerminate.(db.Machine) view.Remove(toTerminate) } for _, bootSet := range bootList { bootSet := bootSet.(db.Machine) pairs = append(pairs, join.Pair{L: bootSet, R: view.InsertMachine()}) } for _, pair := range pairs { stitchMachine := pair.L.(db.Machine) dbMachine := pair.R.(db.Machine) dbMachine.Role = stitchMachine.Role dbMachine.Size = stitchMachine.Size dbMachine.DiskSize = stitchMachine.DiskSize dbMachine.Provider = stitchMachine.Provider dbMachine.Region = stitchMachine.Region dbMachine.SSHKeys = stitchMachine.SSHKeys view.Commit(dbMachine) } return nil }
func clusterTxn(view db.Database, stitch stitch.Stitch) error { namespace := stitch.QueryString("Namespace") if namespace == "" { namespace = "DEFAULT_NAMESPACE" msg := "policy did not specify 'Namespace', defaulting to '%s'" log.Warn(fmt.Sprintf(msg, namespace)) } cluster, err := view.GetCluster() if err != nil { cluster = view.InsertCluster() } cluster.Namespace = namespace cluster.Spec = stitch.String() view.Commit(cluster) return nil }
func updateStitch(t *testing.T, conn db.Conn, stitch stitch.Stitch) { conn.Transact(func(view db.Database) error { cluster, err := view.GetCluster() if err != nil { cluster = view.InsertCluster() } cluster.Spec = stitch.String() view.Commit(cluster) return nil }) assert.Nil(t, conn.Transact(updateTxn)) }
func queryContainers(spec stitch.Stitch) []db.Container { containers := map[int]*db.Container{} for _, c := range spec.QueryContainers() { containers[c.ID] = &db.Container{ Command: c.Command, Image: c.Image, Env: c.Env, } } for label, ids := range spec.QueryLabels() { for _, id := range ids { containers[id].Labels = append(containers[id].Labels, label) } } var ret []db.Container for _, c := range containers { ret = append(ret, *c) } return ret }
func aclTxn(view db.Database, stitch stitch.Stitch) error { cluster, err := view.GetCluster() if err != nil { return err } machines := view.SelectFromMachine(func(m db.Machine) bool { return m.PublicIP != "" }) acls := resolveACLs(stitch.QueryStrSlice("AdminACL")) for _, m := range machines { acls = append(acls, m.PublicIP+"/32") } sort.Strings(acls) cluster.ACLs = acls view.Commit(cluster) return nil }