| 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 common |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 | 9 |
| 10 ds "github.com/luci/gae/service/datastore" | 10 ds "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 » milocfg "github.com/luci/luci-go/milo/common/config" | 15 » "github.com/luci/luci-go/milo/common/config" |
| 16 » "github.com/luci/luci-go/server/router" | |
| 17 | 16 |
| 18 "github.com/golang/protobuf/proto" | 17 "github.com/golang/protobuf/proto" |
| 19 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
| 20 ) | 19 ) |
| 21 | 20 |
| 22 // Project is a LUCI project. | 21 // Project is a LUCI project. |
| 23 type Project struct { | 22 type Project struct { |
| 24 // 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 |
| 25 // name. | 24 // name. |
| 26 ID string `gae:"$id"` | 25 ID string `gae:"$id"` |
| 27 // The luci-cfg name of the project. | 26 // The luci-cfg name of the project. |
| 28 Name string | 27 Name string |
| 29 // The Project data in protobuf binary format. | 28 // The Project data in protobuf binary format. |
| 30 Data []byte `gae:",noindex"` | 29 Data []byte `gae:",noindex"` |
| 31 } | 30 } |
| 32 | 31 |
| 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. | 32 // Update internal configuration based off luci-cfg. |
| 46 // update updates Milo's configuration based off luci config. This includes | 33 // update updates Milo's configuration based off luci config. This includes |
| 47 // scanning through all project and extract all console configs. | 34 // scanning through all project and extract all console configs. |
| 48 func Update(c context.Context) error { | 35 func Update(c context.Context) error { |
| 49 cfgName := info.AppID(c) + ".cfg" | 36 cfgName := info.AppID(c) + ".cfg" |
| 50 | 37 |
| 51 var ( | 38 var ( |
| 52 » » configs []*milocfg.Project | 39 » » configs []*config.Project |
| 53 metas []*cfgclient.Meta | 40 metas []*cfgclient.Meta |
| 54 ) | 41 ) |
| 55 if err := cfgclient.Projects(c, cfgclient.AsService, cfgName, textproto.
Slice(&configs), &metas); err != nil { | 42 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) | 43 logging.WithError(err).Errorf(c, "Encountered error while gettin
g project config for %s", cfgName) |
| 57 return err | 44 return err |
| 58 } | 45 } |
| 59 | 46 |
| 60 // A map of project ID to project. | 47 // A map of project ID to project. |
| 61 projects := map[string]*Project{} | 48 projects := map[string]*Project{} |
| 62 for i, proj := range configs { | 49 for i, proj := range configs { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 if _, ok := projects[proj.ID]; !ok { | 85 if _, ok := projects[proj.ID]; !ok { |
| 99 toDelete = append(toDelete, proj) | 86 toDelete = append(toDelete, proj) |
| 100 } | 87 } |
| 101 } | 88 } |
| 102 ds.Delete(c, toDelete) | 89 ds.Delete(c, toDelete) |
| 103 | 90 |
| 104 return nil | 91 return nil |
| 105 } | 92 } |
| 106 | 93 |
| 107 // GetAllProjects returns all registered projects. | 94 // GetAllProjects returns all registered projects. |
| 108 func GetAllProjects(c context.Context) ([]*milocfg.Project, error) { | 95 func GetAllProjects(c context.Context) ([]*config.Project, error) { |
| 109 q := ds.NewQuery("Project") | 96 q := ds.NewQuery("Project") |
| 110 q.Order("ID") | 97 q.Order("ID") |
| 111 | 98 |
| 112 ps := []*Project{} | 99 ps := []*Project{} |
| 113 err := ds.GetAll(c, q, &ps) | 100 err := ds.GetAll(c, q, &ps) |
| 114 if err != nil { | 101 if err != nil { |
| 115 return nil, err | 102 return nil, err |
| 116 } | 103 } |
| 117 » results := make([]*milocfg.Project, len(ps)) | 104 » results := make([]*config.Project, len(ps)) |
| 118 for i, p := range ps { | 105 for i, p := range ps { |
| 119 » » results[i] = &milocfg.Project{} | 106 » » results[i] = &config.Project{} |
| 120 if err := proto.Unmarshal(p.Data, results[i]); err != nil { | 107 if err := proto.Unmarshal(p.Data, results[i]); err != nil { |
| 121 return nil, err | 108 return nil, err |
| 122 } | 109 } |
| 123 } | 110 } |
| 124 return results, nil | 111 return results, nil |
| 125 } | 112 } |
| 126 | 113 |
| 127 // GetProject returns the requested project. | 114 // GetProject returns the requested project. |
| 128 func GetProject(c context.Context, projName string) (*milocfg.Project, error) { | 115 func GetProject(c context.Context, projName string) (*config.Project, error) { |
| 129 // Next, Try datastore | 116 // Next, Try datastore |
| 130 p := Project{ID: projName} | 117 p := Project{ID: projName} |
| 131 if err := ds.Get(c, &p); err != nil { | 118 if err := ds.Get(c, &p); err != nil { |
| 132 return nil, err | 119 return nil, err |
| 133 } | 120 } |
| 134 » mp := milocfg.Project{} | 121 » mp := config.Project{} |
| 135 if err := proto.Unmarshal(p.Data, &mp); err != nil { | 122 if err := proto.Unmarshal(p.Data, &mp); err != nil { |
| 136 return nil, err | 123 return nil, err |
| 137 } | 124 } |
| 138 | 125 |
| 139 return &mp, nil | 126 return &mp, nil |
| 140 } | 127 } |
| 141 | 128 |
| 142 // GetConsole returns the requested console instance. | 129 // GetConsole returns the requested console instance. |
| 143 func GetConsole(c context.Context, projName, consoleName string) (*milocfg.Conso
le, error) { | 130 func GetConsole(c context.Context, projName, consoleName string) (*config.Consol
e, error) { |
| 144 p, err := GetProject(c, projName) | 131 p, err := GetProject(c, projName) |
| 145 if err != nil { | 132 if err != nil { |
| 146 return nil, err | 133 return nil, err |
| 147 } | 134 } |
| 148 for _, cs := range p.Consoles { | 135 for _, cs := range p.Consoles { |
| 149 if cs.Name == consoleName { | 136 if cs.Name == consoleName { |
| 150 return cs, nil | 137 return cs, nil |
| 151 } | 138 } |
| 152 } | 139 } |
| 153 return nil, fmt.Errorf("Console %s not found in project %s", consoleName
, projName) | 140 return nil, fmt.Errorf("Console %s not found in project %s", consoleName
, projName) |
| 154 } | 141 } |
| OLD | NEW |