func updateConfig(conn db.Conn, configPath string) error { pathStr, _ := os.LookupEnv(quiltPath) if pathStr == "" { pathStr = stitch.GetQuiltPath() } f, err := util.Open(configPath) if err != nil { f, err = util.Open(filepath.Join(pathStr, configPath)) if err != nil { return err } } defer f.Close() sc := scanner.Scanner{ Position: scanner.Position{ Filename: configPath, }, } spec, err := stitch.New(*sc.Init(bufio.NewReader(f)), pathStr, false) if err != nil { return err } return engine.UpdatePolicy(conn, spec) }
func checkConfig(content string) error { reader := strings.NewReader(content) var sc scanner.Scanner _, err := stitch.New(*sc.Init(reader), "", false) if err != nil { return err } return nil }
func prog(t *testing.T, code string) stitch.Stitch { var sc scanner.Scanner result, err := stitch.New(*sc.Init(strings.NewReader(code)), "", false) if err != nil { t.Error(err.Error()) return stitch.Stitch{} } return result }
func configRunOnce(configPath string, quiltPath string) error { f, err := util.Open(configPath) if err != nil { return err } defer f.Close() var sc scanner.Scanner _, err = stitch.New(*sc.Init(bufio.NewReader(f)), quiltPath, false) if err != nil { return err } return nil }
func stop(conn db.Conn, namespace string) { specStr := "(define AdminACL (list))" if namespace != "" { specStr += fmt.Sprintf(` (define Namespace "%s")`, namespace) } var sc scanner.Scanner spec, err := stitch.New(*sc.Init(strings.NewReader(specStr)), "", false) if err != nil { panic(err) } err = engine.UpdatePolicy(conn, spec) if err != nil { panic(err) } }
// Main is the main function for inspect tool. Helps visualize stitches. func Main(opts []string) { if arglen := len(opts); arglen < 3 { fmt.Println("not enough arguments: ", arglen-1) usage() } configPath := opts[1] f, err := os.Open(configPath) if err != nil { fmt.Println(err) os.Exit(1) } defer f.Close() sc := scanner.Scanner{ Position: scanner.Position{ Filename: configPath, }, } pathStr, _ := os.LookupEnv(quiltPath) spec, err := stitch.New(*sc.Init(bufio.NewReader(f)), pathStr, false) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } graph, err := stitch.InitializeGraph(spec) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } switch opts[2] { case "pdf": fallthrough case "ascii": viz(configPath, spec, graph, opts[2]) default: usage() } }
func updatePolicy(view db.Database, role db.Role, spec string) { var sc scanner.Scanner compiled, err := stitch.New(*sc.Init(strings.NewReader(spec)), "", false) if err != nil { log.WithError(err).Warn("Invalid spec.") return } updateConnections(view, compiled) if role == db.Master { // This must happen after `updateConnections` because we generate // placement rules based on whether there are incoming connections from // public internet. updatePlacements(view, compiled) // The container table is aspirational -- it's the set of containers that // should exist. In the workers, however, the container table is just // what's running locally. That's why we only sync the database // containers on the master. updateContainers(view, compiled) } }
func testConnectionTxn(conn db.Conn, spec string) string { var connections []db.Connection conn.Transact(func(view db.Database) error { updatePolicy(view, db.Master, spec) connections = view.SelectFromConnection(nil) return nil }) var sc scanner.Scanner compiled, err := stitch.New(*sc.Init(strings.NewReader(spec)), "", false) if err != nil { return err.Error() } exp := compiled.QueryConnections() for _, e := range exp { found := false for i, c := range connections { if e.From == c.From && e.To == c.To && e.MinPort == c.MinPort && e.MaxPort == c.MaxPort { connections = append( connections[:i], connections[i+1:]...) found = true break } } if found == false { return fmt.Sprintf("Missing expected connection: %v", e) } } if len(connections) > 0 { return spew.Sprintf("Unexpected connections: %s", connections) } return "" }
func testContainerTxn(conn db.Conn, spec string) string { var containers []db.Container conn.Transact(func(view db.Database) error { updatePolicy(view, db.Master, spec) containers = view.SelectFromContainer(nil) return nil }) var sc scanner.Scanner compiled, err := stitch.New(*sc.Init(strings.NewReader(spec)), "", false) if err != nil { return err.Error() } for _, e := range queryContainers(compiled) { found := false for i, c := range containers { if e.Image == c.Image && reflect.DeepEqual(e.Command, c.Command) && util.EditDistance(c.Labels, e.Labels) == 0 { containers = append(containers[:i], containers[i+1:]...) found = true break } } if found == false { return fmt.Sprintf("Missing expected label set: %v\n%v", e, containers) } } if len(containers) > 0 { return spew.Sprintf("Unexpected containers: %s", containers) } return "" }
func initSpec(src string) (stitch.Stitch, error) { var sc scanner.Scanner spec, err := stitch.New(*sc.Init(strings.NewReader(src)), "../specs", false) return spec, err }