Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package common | 5 package common |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 | 9 |
| 10 » ds "github.com/luci/gae/service/datastore" | 10 » "github.com/luci/gae/service/datastore" |
| 11 "github.com/luci/gae/service/info" | 11 "github.com/luci/gae/service/info" |
| 12 "github.com/luci/luci-go/common/logging" | 12 "github.com/luci/luci-go/common/logging" |
| 13 "github.com/luci/luci-go/luci_config/server/cfgclient" | 13 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 14 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" | 14 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" |
| 15 "github.com/luci/luci-go/milo/common/config" | 15 "github.com/luci/luci-go/milo/common/config" |
| 16 | 16 |
| 17 "github.com/golang/protobuf/proto" | 17 "github.com/golang/protobuf/proto" |
| 18 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
| 19 ) | 19 ) |
| 20 | 20 |
| 21 // Project is a LUCI project. | 21 // Project is a LUCI project. |
| 22 type Project struct { | 22 type Project struct { |
| 23 // The ID of the project, as per self defined. This is not the luci-cfg | 23 // The ID of the project, as per self defined. This is not the luci-cfg |
| 24 // name. | 24 // name. |
| 25 ID string `gae:"$id"` | 25 ID string `gae:"$id"` |
| 26 // The luci-cfg name of the project. | 26 // The luci-cfg name of the project. |
| 27 Name string | 27 Name string |
| 28 // The Project data in protobuf binary format. | 28 // The Project data in protobuf binary format. |
| 29 Data []byte `gae:",noindex"` | 29 Data []byte `gae:",noindex"` |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Update internal configuration based off luci-cfg. | 32 // GetServiceConfig returns the service (aka global) config for the current |
| 33 // instance of Milo. | |
| 34 func GetSettings(c context.Context) (*config.Settings, error) { | |
| 35 » cs := cfgclient.CurrentServiceConfigSet(c) | |
| 36 » msg := &config.Settings{} | |
| 37 » // Our global config name is called settings.cfg. | |
| 38 » err := cfgclient.Get(c, cfgclient.AsService, cs, "settings.cfg", textpro to.Message(msg), nil) | |
| 39 » switch err { | |
| 40 » case cfgclient.ErrNoConfig: | |
| 41 » » // Just warn very heavily in the logs, but don't 500, instead re turn an | |
| 42 » » // empty config. | |
| 43 » » logging.WithError(err).Errorf(c, "settings.cfg does not exist") | |
| 44 » » msg.Buildbot = &config.Settings_Buildbot{} | |
| 45 » » return msg, nil | |
|
nodir
2017/03/21 01:06:51
err = nil
in case we add more code at L51
hinoka
2017/03/21 04:54:53
Done.
| |
| 46 » case nil: | |
| 47 » » // continue | |
| 48 » default: | |
| 49 » » return nil, err | |
| 50 » } | |
| 51 » return msg, err | |
| 52 } | |
| 53 | |
| 54 // UpdateProjectConfigs internal project configuration based off luci-cfg. | |
| 33 // update updates Milo's configuration based off luci config. This includes | 55 // update updates Milo's configuration based off luci config. This includes |
| 34 // scanning through all project and extract all console configs. | 56 // scanning through all project and extract all console configs. |
| 35 func Update(c context.Context) error { | 57 func UpdateProjectConfigs(c context.Context) error { |
| 36 cfgName := info.AppID(c) + ".cfg" | 58 cfgName := info.AppID(c) + ".cfg" |
| 37 | 59 |
| 38 var ( | 60 var ( |
| 39 configs []*config.Project | 61 configs []*config.Project |
| 40 metas []*cfgclient.Meta | 62 metas []*cfgclient.Meta |
| 41 ) | 63 ) |
| 42 if err := cfgclient.Projects(c, cfgclient.AsService, cfgName, textproto. Slice(&configs), &metas); err != nil { | 64 if err := cfgclient.Projects(c, cfgclient.AsService, cfgName, textproto. Slice(&configs), &metas); err != nil { |
| 43 logging.WithError(err).Errorf(c, "Encountered error while gettin g project config for %s", cfgName) | 65 logging.WithError(err).Errorf(c, "Encountered error while gettin g project config for %s", cfgName) |
| 44 return err | 66 return err |
| 45 } | 67 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 65 if err != nil { | 87 if err != nil { |
| 66 return err | 88 return err |
| 67 } | 89 } |
| 68 } | 90 } |
| 69 | 91 |
| 70 // Now load all the data into the datastore. | 92 // Now load all the data into the datastore. |
| 71 projs := make([]*Project, 0, len(projects)) | 93 projs := make([]*Project, 0, len(projects)) |
| 72 for _, proj := range projects { | 94 for _, proj := range projects { |
| 73 projs = append(projs, proj) | 95 projs = append(projs, proj) |
| 74 } | 96 } |
| 75 » if err := ds.Put(c, projs); err != nil { | 97 » if err := datastore.Put(c, projs); err != nil { |
| 76 return err | 98 return err |
| 77 } | 99 } |
| 78 | 100 |
| 79 // Delete entries that no longer exist. | 101 // Delete entries that no longer exist. |
| 80 » q := ds.NewQuery("Project").KeysOnly(true) | 102 » q := datastore.NewQuery("Project").KeysOnly(true) |
| 81 allProjs := []Project{} | 103 allProjs := []Project{} |
| 82 » ds.GetAll(c, q, &allProjs) | 104 » datastore.GetAll(c, q, &allProjs) |
| 83 toDelete := []Project{} | 105 toDelete := []Project{} |
| 84 for _, proj := range allProjs { | 106 for _, proj := range allProjs { |
| 85 if _, ok := projects[proj.ID]; !ok { | 107 if _, ok := projects[proj.ID]; !ok { |
| 86 toDelete = append(toDelete, proj) | 108 toDelete = append(toDelete, proj) |
| 87 } | 109 } |
| 88 } | 110 } |
| 89 » ds.Delete(c, toDelete) | 111 » datastore.Delete(c, toDelete) |
| 90 | 112 |
| 91 return nil | 113 return nil |
| 92 } | 114 } |
| 93 | 115 |
| 94 // GetAllProjects returns all registered projects. | 116 // GetAllProjects returns all registered projects. |
| 95 func GetAllProjects(c context.Context) ([]*config.Project, error) { | 117 func GetAllProjects(c context.Context) ([]*config.Project, error) { |
| 96 » q := ds.NewQuery("Project") | 118 » q := datastore.NewQuery("Project") |
| 97 q.Order("ID") | 119 q.Order("ID") |
| 98 | 120 |
| 99 ps := []*Project{} | 121 ps := []*Project{} |
| 100 » err := ds.GetAll(c, q, &ps) | 122 » err := datastore.GetAll(c, q, &ps) |
| 101 if err != nil { | 123 if err != nil { |
| 102 return nil, err | 124 return nil, err |
| 103 } | 125 } |
| 104 results := make([]*config.Project, len(ps)) | 126 results := make([]*config.Project, len(ps)) |
| 105 for i, p := range ps { | 127 for i, p := range ps { |
| 106 results[i] = &config.Project{} | 128 results[i] = &config.Project{} |
| 107 if err := proto.Unmarshal(p.Data, results[i]); err != nil { | 129 if err := proto.Unmarshal(p.Data, results[i]); err != nil { |
| 108 return nil, err | 130 return nil, err |
| 109 } | 131 } |
| 110 } | 132 } |
| 111 return results, nil | 133 return results, nil |
| 112 } | 134 } |
| 113 | 135 |
| 114 // GetProject returns the requested project. | 136 // GetProject returns the requested project. |
| 115 func GetProject(c context.Context, projName string) (*config.Project, error) { | 137 func GetProject(c context.Context, projName string) (*config.Project, error) { |
| 116 // Next, Try datastore | 138 // Next, Try datastore |
| 117 p := Project{ID: projName} | 139 p := Project{ID: projName} |
| 118 » if err := ds.Get(c, &p); err != nil { | 140 » if err := datastore.Get(c, &p); err != nil { |
| 119 return nil, err | 141 return nil, err |
| 120 } | 142 } |
| 121 mp := config.Project{} | 143 mp := config.Project{} |
| 122 if err := proto.Unmarshal(p.Data, &mp); err != nil { | 144 if err := proto.Unmarshal(p.Data, &mp); err != nil { |
| 123 return nil, err | 145 return nil, err |
| 124 } | 146 } |
| 125 | 147 |
| 126 return &mp, nil | 148 return &mp, nil |
| 127 } | 149 } |
| 128 | 150 |
| 129 // GetConsole returns the requested console instance. | 151 // GetConsole returns the requested console instance. |
| 130 func GetConsole(c context.Context, projName, consoleName string) (*config.Consol e, error) { | 152 func GetConsole(c context.Context, projName, consoleName string) (*config.Consol e, error) { |
| 131 p, err := GetProject(c, projName) | 153 p, err := GetProject(c, projName) |
| 132 if err != nil { | 154 if err != nil { |
| 133 return nil, err | 155 return nil, err |
| 134 } | 156 } |
| 135 for _, cs := range p.Consoles { | 157 for _, cs := range p.Consoles { |
| 136 if cs.Name == consoleName { | 158 if cs.Name == consoleName { |
| 137 return cs, nil | 159 return cs, nil |
| 138 } | 160 } |
| 139 } | 161 } |
| 140 return nil, fmt.Errorf("Console %s not found in project %s", consoleName , projName) | 162 return nil, fmt.Errorf("Console %s not found in project %s", consoleName , projName) |
| 141 } | 163 } |
| OLD | NEW |