| 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 | |
| 10 ds "github.com/luci/gae/service/datastore" | |
| 11 "github.com/luci/gae/service/info" | |
| 12 "github.com/luci/luci-go/common/logging" | |
| 13 "github.com/luci/luci-go/luci_config/server/cfgclient" | |
| 14 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" | |
| 15 milocfg "github.com/luci/luci-go/milo/common/config" | |
| 16 "github.com/luci/luci-go/server/router" | |
| 17 | |
| 18 "github.com/golang/protobuf/proto" | |
| 19 "golang.org/x/net/context" | |
| 20 ) | |
| 21 | |
| 22 // Project is a LUCI project. | |
| 23 type Project struct { | |
| 24 // The ID of the project, as per self defined. This is not the luci-cfg | |
| 25 // name. | |
| 26 ID string `gae:"$id"` | |
| 27 // The luci-cfg name of the project. | |
| 28 Name string | |
| 29 // The Project data in protobuf binary format. | |
| 30 Data []byte `gae:",noindex"` | |
| 31 } | |
| 32 | |
| 33 // UpdateHandler is an HTTP handler that handles configuration update requests. | |
| 34 func UpdateHandler(ctx *router.Context) { | |
| 35 c, h := ctx.Context, ctx.Writer | |
| 36 err := Update(c) | |
| 37 if err != nil { | |
| 38 logging.WithError(err).Errorf(c, "Update Handler encountered err
or") | |
| 39 h.WriteHeader(500) | |
| 40 } | |
| 41 logging.Infof(c, "Successfully completed") | |
| 42 h.WriteHeader(200) | |
| 43 } | |
| 44 | |
| 45 // Update internal configuration based off luci-cfg. | |
| 46 // update updates Milo's configuration based off luci config. This includes | |
| 47 // scanning through all project and extract all console configs. | |
| 48 func Update(c context.Context) error { | |
| 49 cfgName := info.AppID(c) + ".cfg" | |
| 50 | |
| 51 var ( | |
| 52 configs []*milocfg.Project | |
| 53 metas []*cfgclient.Meta | |
| 54 ) | |
| 55 if err := cfgclient.Projects(c, cfgclient.AsService, cfgName, textproto.
Slice(&configs), &metas); err != nil { | |
| 56 logging.WithError(err).Errorf(c, "Encountered error while gettin
g project config for %s", cfgName) | |
| 57 return err | |
| 58 } | |
| 59 | |
| 60 // A map of project ID to project. | |
| 61 projects := map[string]*Project{} | |
| 62 for i, proj := range configs { | |
| 63 projectName, _, _ := metas[i].ConfigSet.SplitProject() | |
| 64 | |
| 65 logging.Infof(c, "Prossing %s", projectName) | |
| 66 if dup, ok := projects[proj.ID]; ok { | |
| 67 return fmt.Errorf( | |
| 68 "Duplicate project ID: %s. (%s and %s)", proj.ID
, dup.Name, projectName) | |
| 69 } | |
| 70 p := &Project{ | |
| 71 ID: proj.ID, | |
| 72 Name: string(projectName), | |
| 73 } | |
| 74 projects[proj.ID] = p | |
| 75 | |
| 76 var err error | |
| 77 p.Data, err = proto.Marshal(proj) | |
| 78 if err != nil { | |
| 79 return err | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 // Now load all the data into the datastore. | |
| 84 projs := make([]*Project, 0, len(projects)) | |
| 85 for _, proj := range projects { | |
| 86 projs = append(projs, proj) | |
| 87 } | |
| 88 if err := ds.Put(c, projs); err != nil { | |
| 89 return err | |
| 90 } | |
| 91 | |
| 92 // Delete entries that no longer exist. | |
| 93 q := ds.NewQuery("Project").KeysOnly(true) | |
| 94 allProjs := []Project{} | |
| 95 ds.GetAll(c, q, &allProjs) | |
| 96 toDelete := []Project{} | |
| 97 for _, proj := range allProjs { | |
| 98 if _, ok := projects[proj.ID]; !ok { | |
| 99 toDelete = append(toDelete, proj) | |
| 100 } | |
| 101 } | |
| 102 ds.Delete(c, toDelete) | |
| 103 | |
| 104 return nil | |
| 105 } | |
| 106 | |
| 107 // GetAllProjects returns all registered projects. | |
| 108 func GetAllProjects(c context.Context) ([]*milocfg.Project, error) { | |
| 109 q := ds.NewQuery("Project") | |
| 110 q.Order("ID") | |
| 111 | |
| 112 ps := []*Project{} | |
| 113 err := ds.GetAll(c, q, &ps) | |
| 114 if err != nil { | |
| 115 return nil, err | |
| 116 } | |
| 117 results := make([]*milocfg.Project, len(ps)) | |
| 118 for i, p := range ps { | |
| 119 results[i] = &milocfg.Project{} | |
| 120 if err := proto.Unmarshal(p.Data, results[i]); err != nil { | |
| 121 return nil, err | |
| 122 } | |
| 123 } | |
| 124 return results, nil | |
| 125 } | |
| 126 | |
| 127 // GetProject returns the requested project. | |
| 128 func GetProject(c context.Context, projName string) (*milocfg.Project, error) { | |
| 129 // Next, Try datastore | |
| 130 p := Project{ID: projName} | |
| 131 if err := ds.Get(c, &p); err != nil { | |
| 132 return nil, err | |
| 133 } | |
| 134 mp := milocfg.Project{} | |
| 135 if err := proto.Unmarshal(p.Data, &mp); err != nil { | |
| 136 return nil, err | |
| 137 } | |
| 138 | |
| 139 return &mp, nil | |
| 140 } | |
| 141 | |
| 142 // GetConsole returns the requested console instance. | |
| 143 func GetConsole(c context.Context, projName, consoleName string) (*milocfg.Conso
le, error) { | |
| 144 p, err := GetProject(c, projName) | |
| 145 if err != nil { | |
| 146 return nil, err | |
| 147 } | |
| 148 for _, cs := range p.Consoles { | |
| 149 if cs.Name == consoleName { | |
| 150 return cs, nil | |
| 151 } | |
| 152 } | |
| 153 return nil, fmt.Errorf("Console %s not found in project %s", consoleName
, projName) | |
| 154 } | |
| OLD | NEW |