| 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 settings | 5 package settings |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "strings" | |
| 10 | 9 |
| 11 "github.com/golang/protobuf/proto" | |
| 12 ds "github.com/luci/gae/service/datastore" | 10 ds "github.com/luci/gae/service/datastore" |
| 13 "github.com/luci/gae/service/info" | 11 "github.com/luci/gae/service/info" |
| 14 "github.com/luci/luci-go/common/config" | |
| 15 "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" |
| 14 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" |
| 16 milocfg "github.com/luci/luci-go/milo/common/config" | 15 milocfg "github.com/luci/luci-go/milo/common/config" |
| 17 "github.com/luci/luci-go/server/router" | 16 "github.com/luci/luci-go/server/router" |
| 18 | 17 |
| 18 "github.com/golang/protobuf/proto" |
| 19 "golang.org/x/net/context" | 19 "golang.org/x/net/context" |
| 20 ) | 20 ) |
| 21 | 21 |
| 22 // Project is a LUCI project. | 22 // Project is a LUCI project. |
| 23 type Project struct { | 23 type Project struct { |
| 24 // The ID of the project, as per self defined. This is not the luci-cfg | 24 // The ID of the project, as per self defined. This is not the luci-cfg |
| 25 // name. | 25 // name. |
| 26 ID string `gae:"$id"` | 26 ID string `gae:"$id"` |
| 27 // The luci-cfg name of the project. | 27 // The luci-cfg name of the project. |
| 28 Name string | 28 Name string |
| (...skipping 11 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 logging.Infof(c, "Successfully completed") | 41 logging.Infof(c, "Successfully completed") |
| 42 h.WriteHeader(200) | 42 h.WriteHeader(200) |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Update internal configuration based off luci-cfg. | 45 // Update internal configuration based off luci-cfg. |
| 46 // update updates Milo's configuration based off luci config. This includes | 46 // update updates Milo's configuration based off luci config. This includes |
| 47 // scanning through all project and extract all console configs. | 47 // scanning through all project and extract all console configs. |
| 48 func Update(c context.Context) error { | 48 func Update(c context.Context) error { |
| 49 cfgName := info.AppID(c) + ".cfg" | 49 cfgName := info.AppID(c) + ".cfg" |
| 50 » cfgs, err := config.GetProjectConfigs(c, cfgName, false) | 50 |
| 51 » if err != nil { | 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 { |
| 52 logging.WithError(err).Errorf(c, "Encountered error while gettin
g project config for %s", cfgName) | 56 logging.WithError(err).Errorf(c, "Encountered error while gettin
g project config for %s", cfgName) |
| 53 return err | 57 return err |
| 54 } | 58 } |
| 59 |
| 55 // A map of project ID to project. | 60 // A map of project ID to project. |
| 56 projects := map[string]*Project{} | 61 projects := map[string]*Project{} |
| 57 » for _, cfg := range cfgs { | 62 » for i, proj := range configs { |
| 58 » » pathParts := strings.SplitN(cfg.ConfigSet, "/", 2) | 63 » » projectName, _, _ := metas[i].ConfigSet.SplitProject() |
| 59 » » if len(pathParts) != 2 { | 64 |
| 60 » » » return fmt.Errorf("Invalid config path: %s", cfg.Path) | 65 » » logging.Infof(c, "Prossing %s", projectName) |
| 61 » » } | |
| 62 » » name := pathParts[1] | |
| 63 » » proj := milocfg.Project{} | |
| 64 » » logging.Infof(c, "Prossing %s", name) | |
| 65 » » if err = proto.UnmarshalText(cfg.Content, &proj); err != nil { | |
| 66 » » » logging.WithError(err).Errorf( | |
| 67 » » » » c, "Encountered error while processing %s. Conf
ig:\n%s", name, cfg.Content) | |
| 68 » » » return err | |
| 69 » » } | |
| 70 if dup, ok := projects[proj.ID]; ok { | 66 if dup, ok := projects[proj.ID]; ok { |
| 71 return fmt.Errorf( | 67 return fmt.Errorf( |
| 72 » » » » "Duplicate project ID: %s. (%s and %s)", proj.ID
, dup.Name, name) | 68 » » » » "Duplicate project ID: %s. (%s and %s)", proj.ID
, dup.Name, projectName) |
| 73 } | 69 } |
| 74 p := &Project{ | 70 p := &Project{ |
| 75 ID: proj.ID, | 71 ID: proj.ID, |
| 76 » » » Name: name, | 72 » » » Name: string(projectName), |
| 77 } | 73 } |
| 78 projects[proj.ID] = p | 74 projects[proj.ID] = p |
| 79 » » p.Data, err = proto.Marshal(&proj) | 75 |
| 76 » » var err error |
| 77 » » p.Data, err = proto.Marshal(proj) |
| 80 if err != nil { | 78 if err != nil { |
| 81 return err | 79 return err |
| 82 } | 80 } |
| 83 } | 81 } |
| 84 | 82 |
| 85 // Now load all the data into the datastore. | 83 // Now load all the data into the datastore. |
| 86 projs := make([]*Project, 0, len(projects)) | 84 projs := make([]*Project, 0, len(projects)) |
| 87 for _, proj := range projects { | 85 for _, proj := range projects { |
| 88 projs = append(projs, proj) | 86 projs = append(projs, proj) |
| 89 } | 87 } |
| 90 » err = ds.Put(c, projs) | 88 » if err := ds.Put(c, projs); err != nil { |
| 91 » if err != nil { | |
| 92 return err | 89 return err |
| 93 } | 90 } |
| 94 | 91 |
| 95 // Delete entries that no longer exist. | 92 // Delete entries that no longer exist. |
| 96 q := ds.NewQuery("Project").KeysOnly(true) | 93 q := ds.NewQuery("Project").KeysOnly(true) |
| 97 allProjs := []Project{} | 94 allProjs := []Project{} |
| 98 ds.GetAll(c, q, &allProjs) | 95 ds.GetAll(c, q, &allProjs) |
| 99 toDelete := []Project{} | 96 toDelete := []Project{} |
| 100 for _, proj := range allProjs { | 97 for _, proj := range allProjs { |
| 101 if _, ok := projects[proj.ID]; !ok { | 98 if _, ok := projects[proj.ID]; !ok { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 if err != nil { | 145 if err != nil { |
| 149 return nil, err | 146 return nil, err |
| 150 } | 147 } |
| 151 for _, cs := range p.Consoles { | 148 for _, cs := range p.Consoles { |
| 152 if cs.Name == consoleName { | 149 if cs.Name == consoleName { |
| 153 return cs, nil | 150 return cs, nil |
| 154 } | 151 } |
| 155 } | 152 } |
| 156 return nil, fmt.Errorf("Console %s not found in project %s", consoleName
, projName) | 153 return nil, fmt.Errorf("Console %s not found in project %s", consoleName
, projName) |
| 157 } | 154 } |
| OLD | NEW |