Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: milo/appengine/settings/config.go

Issue 2238883003: Milo: Use luci-cfg for defining projects and console view. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: review Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « milo/appengine/frontend/milo.go ('k') | milo/appengine/settings/config_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Next, Try datastore
107 ds := datastore.Get(c)
108 p := Project{ID: projName}
109 if err := ds.Get(&p); err != nil {
110 return nil, err
111 }
112 mp := milocfg.Project{}
113 if err := proto.Unmarshal(p.Data, &mp); err != nil {
114 return nil, err
115 }
116
117 return &mp, nil
118 }
119
120 func GetConsole(c context.Context, projName, consoleName string) (*milocfg.Conso le, error) {
121 p, err := GetProject(c, projName)
122 if err != nil {
123 return nil, err
124 }
125 for _, cs := range p.Consoles {
126 if cs.Name == consoleName {
127 return cs, nil
128 }
129 }
130 return nil, fmt.Errorf("Console %s not found in project %s", consoleName , projName)
131 }
OLDNEW
« no previous file with comments | « milo/appengine/frontend/milo.go ('k') | milo/appengine/settings/config_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698