Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The LUCI Authors. | 1 // Copyright 2016 The LUCI Authors. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 "github.com/luci/luci-go/common/logging" | 27 "github.com/luci/luci-go/common/logging" |
| 28 "github.com/luci/luci-go/luci_config/server/cfgclient" | 28 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 29 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" | 29 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 30 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" | 30 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" |
| 31 | 31 |
| 32 "github.com/luci/luci-go/milo/api/config" | 32 "github.com/luci/luci-go/milo/api/config" |
| 33 ) | 33 ) |
| 34 | 34 |
| 35 // Project is a LUCI project. | 35 // Project is a LUCI project. |
| 36 type Project struct { | 36 type Project struct { |
| 37 » // The ID of the project, as per self defined. This is not the luci-con fig | 37 » // ID of the project, as per self defined. This is the luci-config name . |
| 38 » // name. | |
| 39 ID string `gae:"$id"` | 38 ID string `gae:"$id"` |
| 40 » // The luci-config name of the project. | 39 » // Data is the Project data in protobuf binary format. |
| 41 » Name string | |
| 42 » // The Project data in protobuf binary format. | |
| 43 Data []byte `gae:",noindex"` | 40 Data []byte `gae:",noindex"` |
| 41 // Revision is the latest revision we have of this project's config. | |
| 42 Revision string | |
| 44 } | 43 } |
| 45 | 44 |
| 46 // The key for the service config entity in datastore. | 45 // The key for the service config entity in datastore. |
| 47 const ServiceConfigID = "service_config" | 46 const ServiceConfigID = "service_config" |
| 48 | 47 |
| 49 // ServiceConfig is a container for the instance's service config. | 48 // ServiceConfig is a container for the instance's service config. |
| 50 type ServiceConfig struct { | 49 type ServiceConfig struct { |
| 51 // ID is the datastore key. This should be static, as there should only be | 50 // ID is the datastore key. This should be static, as there should only be |
| 52 // one service config. | 51 // one service config. |
| 53 ID string `gae:"$id"` | 52 ID string `gae:"$id"` |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 // UpdateProjectConfigs internal project configuration based off luci-config. | 180 // UpdateProjectConfigs internal project configuration based off luci-config. |
| 182 // update updates Milo's configuration based off luci config. This includes | 181 // update updates Milo's configuration based off luci config. This includes |
| 183 // scanning through all project and extract all console configs. | 182 // scanning through all project and extract all console configs. |
| 184 func UpdateProjectConfigs(c context.Context) error { | 183 func UpdateProjectConfigs(c context.Context) error { |
| 185 cfgName := info.AppID(c) + ".cfg" | 184 cfgName := info.AppID(c) + ".cfg" |
| 186 | 185 |
| 187 var ( | 186 var ( |
| 188 configs []*config.Project | 187 configs []*config.Project |
| 189 metas []*cfgclient.Meta | 188 metas []*cfgclient.Meta |
| 190 ) | 189 ) |
| 190 | |
| 191 logging.Debugf(c, "fetching configs for %s", cfgName) | 191 logging.Debugf(c, "fetching configs for %s", cfgName) |
| 192 if err := cfgclient.Projects(c, cfgclient.AsService, cfgName, textproto. Slice(&configs), &metas); err != nil { | 192 if err := cfgclient.Projects(c, cfgclient.AsService, cfgName, textproto. Slice(&configs), &metas); err != nil { |
|
iannucci
2017/07/15 00:45:53
var merr MultiError
if ! nil {
merr = err.(Mult
Ryan Tseng
2017/07/15 00:53:57
Done.
| |
| 193 logging.WithError(err).Errorf(c, "Encountered error while gettin g project config for %s", cfgName) | 193 logging.WithError(err).Errorf(c, "Encountered error while gettin g project config for %s", cfgName) |
| 194 » » return err | 194 » » // We can continue with the ones that work still. |
| 195 } | 195 } |
| 196 | 196 |
| 197 // A map of project ID to project. | 197 // A map of project ID to project. |
| 198 projects := map[string]*Project{} | 198 projects := map[string]*Project{} |
| 199 for i, proj := range configs { | 199 for i, proj := range configs { |
| 200 » » projectName, _, _ := metas[i].ConfigSet.SplitProject() | 200 » » meta := metas[i] |
| 201 » » projectName, _, _ := meta.ConfigSet.SplitProject() | |
| 201 | 202 |
| 202 logging.Infof(c, "Prossing %s", projectName) | 203 logging.Infof(c, "Prossing %s", projectName) |
| 203 » » if dup, ok := projects[proj.ID]; ok { | 204 » » p := &Project{ |
| 204 » » » return fmt.Errorf( | 205 » » » ID: string(projectName), |
| 205 » » » » "Duplicate project ID: %s. (%s and %s)", proj.ID , dup.Name, projectName) | 206 » » » Revision: meta.Revision, |
| 206 } | 207 } |
| 207 p := &Project{ | |
| 208 ID: proj.ID, | |
| 209 Name: string(projectName), | |
| 210 } | |
| 211 projects[proj.ID] = p | |
| 212 | 208 |
| 213 var err error | 209 var err error |
| 214 p.Data, err = proto.Marshal(proj) | 210 p.Data, err = proto.Marshal(proj) |
| 215 if err != nil { | 211 if err != nil { |
| 216 » » » return err | 212 » » » logging.WithError(err).Errorf(c, "Encountered error whil e processing project %s", projectName) |
| 213 » » » // Set this to nil to signal this project exists but we don't want to update it. | |
| 214 » » » projects[proj.ID] = nil | |
| 215 » » » continue | |
| 217 } | 216 } |
| 217 projects[proj.ID] = p | |
| 218 } | 218 } |
| 219 | 219 |
| 220 // Now load all the data into the datastore. | 220 // Now load all the data into the datastore. |
| 221 projs := make([]*Project, 0, len(projects)) | 221 projs := make([]*Project, 0, len(projects)) |
| 222 for _, proj := range projects { | 222 for _, proj := range projects { |
| 223 » » projs = append(projs, proj) | 223 » » if proj != nil { |
| 224 » » » projs = append(projs, proj) | |
| 225 » » } | |
| 224 } | 226 } |
| 225 if err := datastore.Put(c, projs); err != nil { | 227 if err := datastore.Put(c, projs); err != nil { |
| 226 return err | 228 return err |
| 227 } | 229 } |
| 228 | 230 |
| 229 // Delete entries that no longer exist. | 231 // Delete entries that no longer exist. |
| 230 q := datastore.NewQuery("Project").KeysOnly(true) | 232 q := datastore.NewQuery("Project").KeysOnly(true) |
| 231 allProjs := []Project{} | 233 allProjs := []Project{} |
| 232 datastore.GetAll(c, q, &allProjs) | 234 datastore.GetAll(c, q, &allProjs) |
| 233 toDelete := []Project{} | 235 toDelete := []Project{} |
| 234 for _, proj := range allProjs { | 236 for _, proj := range allProjs { |
| 235 if _, ok := projects[proj.ID]; !ok { | 237 if _, ok := projects[proj.ID]; !ok { |
| 236 toDelete = append(toDelete, proj) | 238 toDelete = append(toDelete, proj) |
| 237 } | 239 } |
| 238 } | 240 } |
| 239 » datastore.Delete(c, toDelete) | 241 » return datastore.Delete(c, toDelete) |
| 240 | |
| 241 » return nil | |
| 242 } | 242 } |
| 243 | 243 |
| 244 // GetAllProjects returns all registered projects. | 244 // GetAllProjects returns all registered projects. |
| 245 func GetAllProjects(c context.Context) ([]*config.Project, error) { | 245 func GetAllProjects(c context.Context) ([]*config.Project, error) { |
| 246 q := datastore.NewQuery("Project") | 246 q := datastore.NewQuery("Project") |
| 247 q.Order("ID") | 247 q.Order("ID") |
| 248 | 248 |
| 249 ps := []*Project{} | 249 ps := []*Project{} |
| 250 err := datastore.GetAll(c, q, &ps) | 250 err := datastore.GetAll(c, q, &ps) |
| 251 if err != nil { | 251 if err != nil { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 282 if err != nil { | 282 if err != nil { |
| 283 return nil, err | 283 return nil, err |
| 284 } | 284 } |
| 285 for _, cs := range p.Consoles { | 285 for _, cs := range p.Consoles { |
| 286 if cs.Name == consoleName { | 286 if cs.Name == consoleName { |
| 287 return cs, nil | 287 return cs, nil |
| 288 } | 288 } |
| 289 } | 289 } |
| 290 return nil, fmt.Errorf("Console %s not found in project %s", consoleName , projName) | 290 return nil, fmt.Errorf("Console %s not found in project %s", consoleName , projName) |
| 291 } | 291 } |
| OLD | NEW |