Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package settings | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "strings" | |
| 10 | |
| 11 "github.com/golang/protobuf/proto" | |
| 12 "github.com/luci/gae/service/datastore" | |
| 13 "github.com/luci/gae/service/info" | |
| 14 "github.com/luci/luci-go/common/config" | |
| 15 "github.com/luci/luci-go/common/logging" | |
| 16 milocfg "github.com/luci/luci-go/milo/common/config" | |
| 17 "github.com/luci/luci-go/server/router" | |
| 18 "golang.org/x/net/context" | |
| 19 ) | |
| 20 | |
| 21 type Project struct { | |
| 22 // The ID of the project, as per self defined. This is not the luci-cfg | |
| 23 // name. | |
| 24 ID string `gae:"$id"` | |
| 25 // The luci-cfg name of the project. | |
| 26 Name string | |
| 27 // The Project data in protobuf binary format. | |
| 28 Data []byte `gae:",noindex"` | |
| 29 } | |
| 30 | |
| 31 func UpdateHandler(ctx *router.Context) { | |
| 32 c, h := ctx.Context, ctx.Writer | |
| 33 err := update(c) | |
| 34 if err != nil { | |
| 35 logging.WithError(err).Errorf(c, "Update Handler encountered err or") | |
| 36 h.WriteHeader(500) | |
| 37 } | |
| 38 logging.Infof(c, "Successfully completed") | |
| 39 h.WriteHeader(200) | |
| 40 } | |
| 41 | |
| 42 // Update internal configuration based off luci-cfg. | |
| 43 // update updates Milo's configuration based off luci config. This includes | |
| 44 // scanning through all project and extract all console configs. | |
| 45 func update(c context.Context) error { | |
| 46 cfgName := info.Get(c).AppID() + ".cfg" | |
| 47 cfgs, err := config.GetProjectConfigs(c, cfgName, false) | |
| 48 if err != nil { | |
| 49 return err | |
| 50 } | |
| 51 // A map of project ID to project. | |
| 52 projects := map[string]*Project{} | |
| 53 for _, cfg := range cfgs { | |
| 54 pathParts := strings.SplitN(cfg.ConfigSet, "/", 2) | |
| 55 if len(pathParts) != 2 { | |
| 56 return fmt.Errorf("Invalid config path: %s", cfg.Path) | |
| 57 } | |
| 58 name := pathParts[1] | |
| 59 proj := milocfg.Project{} | |
| 60 logging.Infof(c, "Prossing %s", name) | |
| 61 if err = proto.UnmarshalText(cfg.Content, &proj); err != nil { | |
| 62 return err | |
| 63 } | |
| 64 if dup, ok := projects[proj.ID]; ok { | |
| 65 return fmt.Errorf( | |
| 66 "Duplicate project ID: %s. (%s and %s)", proj.ID , dup.Name, name) | |
| 67 } | |
| 68 p := &Project{ | |
| 69 ID: proj.ID, | |
| 70 Name: name, | |
| 71 } | |
| 72 projects[proj.ID] = p | |
| 73 p.Data, err = proto.Marshal(&proj) | |
| 74 if err != nil { | |
| 75 return err | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 // Now load all the data into the datastore. | |
| 80 ds := datastore.Get(c) | |
| 81 projs := make([]*Project, 0, len(projects)) | |
| 82 for _, proj := range projects { | |
| 83 projs = append(projs, proj) | |
| 84 } | |
| 85 err = ds.Put(projs) | |
| 86 if err != nil { | |
| 87 return err | |
| 88 } | |
| 89 | |
| 90 // Delete entries that no longer exist. | |
| 91 q := datastore.NewQuery("Project").KeysOnly(true) | |
| 92 allProjs := []Project{} | |
| 93 ds.GetAll(q, &allProjs) | |
| 94 toDelete := []Project{} | |
| 95 for _, proj := range allProjs { | |
| 96 if _, ok := projects[proj.ID]; !ok { | |
| 97 toDelete = append(toDelete, proj) | |
| 98 } | |
| 99 } | |
| 100 ds.Delete(toDelete) | |
| 101 | |
| 102 return nil | |
| 103 } | |
| 104 | |
| 105 func GetProject(c context.Context, projName string) (*milocfg.Project, error) { | |
| 106 // TODO(hinoka): First, get it from Memcache | |
|
estaab
2016/08/18 23:19:37
Doesn't gae have an automatic memcaching layer? Ma
Ryan Tseng
2016/08/18 23:55:30
Yeah it does... removing now.
| |
| 107 | |
| 108 // Next, Try datastore | |
| 109 ds := datastore.Get(c) | |
| 110 p := Project{ID: projName} | |
| 111 if err := ds.Get(&p); err != nil { | |
| 112 return nil, err | |
| 113 } | |
| 114 mp := milocfg.Project{} | |
| 115 if err := proto.Unmarshal(p.Data, &mp); err != nil { | |
| 116 return nil, err | |
| 117 } | |
| 118 // TODO(hinoka): Save it in memcache | |
| 119 | |
| 120 return &mp, nil | |
| 121 } | |
| 122 | |
| 123 func GetConsole(c context.Context, projName, consoleName string) (*milocfg.Conso le, error) { | |
| 124 p, err := GetProject(c, projName) | |
| 125 if err != nil { | |
| 126 return nil, err | |
| 127 } | |
| 128 for _, cs := range p.Consoles { | |
| 129 if cs.Name == consoleName { | |
| 130 return cs, nil | |
| 131 } | |
| 132 } | |
| 133 return nil, fmt.Errorf("Console %s not found in project %s", consoleName , projName) | |
| 134 } | |
| OLD | NEW |