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 "errors" | |
| 8 "fmt" | 9 "fmt" |
| 9 | 10 |
| 10 » ds "github.com/luci/gae/service/datastore" | 11 » "github.com/luci/gae/service/datastore" |
| 11 "github.com/luci/gae/service/info" | 12 "github.com/luci/gae/service/info" |
| 12 "github.com/luci/luci-go/common/logging" | 13 "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" |
| 14 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" | 15 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" |
| 15 "github.com/luci/luci-go/milo/common/config" | 16 "github.com/luci/luci-go/milo/common/config" |
| 16 | 17 |
| 17 "github.com/golang/protobuf/proto" | 18 "github.com/golang/protobuf/proto" |
| 18 "golang.org/x/net/context" | 19 "golang.org/x/net/context" |
| 19 ) | 20 ) |
| 20 | 21 |
| 21 // Project is a LUCI project. | 22 // Project is a LUCI project. |
| 22 type Project struct { | 23 type Project struct { |
| 23 // 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 |
| 24 // name. | 25 // name. |
| 25 ID string `gae:"$id"` | 26 ID string `gae:"$id"` |
| 26 // The luci-cfg name of the project. | 27 // The luci-cfg name of the project. |
| 27 Name string | 28 Name string |
| 28 // The Project data in protobuf binary format. | 29 // The Project data in protobuf binary format. |
| 29 Data []byte `gae:",noindex"` | 30 Data []byte `gae:",noindex"` |
| 30 } | 31 } |
| 31 | 32 |
| 32 // Update internal configuration based off luci-cfg. | 33 var errNotConfigured = errors.New("milo instance is missing settings.cfg") |
| 34 | |
| 35 // GetServiceConfig returns the service (aka global) config for the current | |
| 36 // instance of Milo. | |
| 37 func GetSettings(c context.Context) (*config.Settings, error) { | |
| 38 » cs := cfgclient.CurrentServiceConfigSet(c) | |
| 39 » var content string | |
| 40 » // Our global config name is called settings.cfg. | |
| 41 » err := cfgclient.Get(c, cfgclient.AsService, cs, "settings.cfg", cfgclie nt.String(&content), nil) | |
|
nodir
2017/03/20 21:41:32
this API call should be used in serving path. Luci
hinoka
2017/03/20 22:19:26
Done.
| |
| 42 » switch err { | |
| 43 » case cfgclient.ErrNoConfig: | |
| 44 » » return nil, errNotConfigured | |
|
nodir
2017/03/20 21:41:32
this will cause http500s on all requests, I assume
hinoka
2017/03/20 22:19:26
Done.
| |
| 45 » case nil: | |
| 46 » » // continue | |
| 47 » default: | |
| 48 » » return nil, err | |
| 49 » } | |
| 50 » msg := &config.Settings{} | |
| 51 » err = proto.UnmarshalText(content, msg) | |
|
nodir
2017/03/20 21:41:32
make sure the config is stored in binary so we can
hinoka
2017/03/20 22:19:26
Done.
| |
| 52 » return msg, err | |
| 53 } | |
| 54 | |
| 55 // UpdateProjectConfigs internal project configuration based off luci-cfg. | |
| 33 // update updates Milo's configuration based off luci config. This includes | 56 // update updates Milo's configuration based off luci config. This includes |
| 34 // scanning through all project and extract all console configs. | 57 // scanning through all project and extract all console configs. |
| 35 func Update(c context.Context) error { | 58 func UpdateProjectConfigs(c context.Context) error { |
| 36 cfgName := info.AppID(c) + ".cfg" | 59 cfgName := info.AppID(c) + ".cfg" |
| 37 | 60 |
| 38 var ( | 61 var ( |
| 39 configs []*config.Project | 62 configs []*config.Project |
| 40 metas []*cfgclient.Meta | 63 metas []*cfgclient.Meta |
| 41 ) | 64 ) |
| 42 if err := cfgclient.Projects(c, cfgclient.AsService, cfgName, textproto. Slice(&configs), &metas); err != nil { | 65 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) | 66 logging.WithError(err).Errorf(c, "Encountered error while gettin g project config for %s", cfgName) |
| 44 return err | 67 return err |
| 45 } | 68 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 65 if err != nil { | 88 if err != nil { |
| 66 return err | 89 return err |
| 67 } | 90 } |
| 68 } | 91 } |
| 69 | 92 |
| 70 // Now load all the data into the datastore. | 93 // Now load all the data into the datastore. |
| 71 projs := make([]*Project, 0, len(projects)) | 94 projs := make([]*Project, 0, len(projects)) |
| 72 for _, proj := range projects { | 95 for _, proj := range projects { |
| 73 projs = append(projs, proj) | 96 projs = append(projs, proj) |
| 74 } | 97 } |
| 75 » if err := ds.Put(c, projs); err != nil { | 98 » if err := datastore.Put(c, projs); err != nil { |
| 76 return err | 99 return err |
| 77 } | 100 } |
| 78 | 101 |
| 79 // Delete entries that no longer exist. | 102 // Delete entries that no longer exist. |
| 80 » q := ds.NewQuery("Project").KeysOnly(true) | 103 » q := datastore.NewQuery("Project").KeysOnly(true) |
| 81 allProjs := []Project{} | 104 allProjs := []Project{} |
| 82 » ds.GetAll(c, q, &allProjs) | 105 » datastore.GetAll(c, q, &allProjs) |
| 83 toDelete := []Project{} | 106 toDelete := []Project{} |
| 84 for _, proj := range allProjs { | 107 for _, proj := range allProjs { |
| 85 if _, ok := projects[proj.ID]; !ok { | 108 if _, ok := projects[proj.ID]; !ok { |
| 86 toDelete = append(toDelete, proj) | 109 toDelete = append(toDelete, proj) |
| 87 } | 110 } |
| 88 } | 111 } |
| 89 » ds.Delete(c, toDelete) | 112 » datastore.Delete(c, toDelete) |
| 90 | 113 |
| 91 return nil | 114 return nil |
| 92 } | 115 } |
| 93 | 116 |
| 94 // GetAllProjects returns all registered projects. | 117 // GetAllProjects returns all registered projects. |
| 95 func GetAllProjects(c context.Context) ([]*config.Project, error) { | 118 func GetAllProjects(c context.Context) ([]*config.Project, error) { |
| 96 » q := ds.NewQuery("Project") | 119 » q := datastore.NewQuery("Project") |
| 97 q.Order("ID") | 120 q.Order("ID") |
| 98 | 121 |
| 99 ps := []*Project{} | 122 ps := []*Project{} |
| 100 » err := ds.GetAll(c, q, &ps) | 123 » err := datastore.GetAll(c, q, &ps) |
| 101 if err != nil { | 124 if err != nil { |
| 102 return nil, err | 125 return nil, err |
| 103 } | 126 } |
| 104 results := make([]*config.Project, len(ps)) | 127 results := make([]*config.Project, len(ps)) |
| 105 for i, p := range ps { | 128 for i, p := range ps { |
| 106 results[i] = &config.Project{} | 129 results[i] = &config.Project{} |
| 107 if err := proto.Unmarshal(p.Data, results[i]); err != nil { | 130 if err := proto.Unmarshal(p.Data, results[i]); err != nil { |
| 108 return nil, err | 131 return nil, err |
| 109 } | 132 } |
| 110 } | 133 } |
| 111 return results, nil | 134 return results, nil |
| 112 } | 135 } |
| 113 | 136 |
| 114 // GetProject returns the requested project. | 137 // GetProject returns the requested project. |
| 115 func GetProject(c context.Context, projName string) (*config.Project, error) { | 138 func GetProject(c context.Context, projName string) (*config.Project, error) { |
| 116 // Next, Try datastore | 139 // Next, Try datastore |
| 117 p := Project{ID: projName} | 140 p := Project{ID: projName} |
| 118 » if err := ds.Get(c, &p); err != nil { | 141 » if err := datastore.Get(c, &p); err != nil { |
| 119 return nil, err | 142 return nil, err |
| 120 } | 143 } |
| 121 mp := config.Project{} | 144 mp := config.Project{} |
| 122 if err := proto.Unmarshal(p.Data, &mp); err != nil { | 145 if err := proto.Unmarshal(p.Data, &mp); err != nil { |
| 123 return nil, err | 146 return nil, err |
| 124 } | 147 } |
| 125 | 148 |
| 126 return &mp, nil | 149 return &mp, nil |
| 127 } | 150 } |
| 128 | 151 |
| 129 // GetConsole returns the requested console instance. | 152 // GetConsole returns the requested console instance. |
| 130 func GetConsole(c context.Context, projName, consoleName string) (*config.Consol e, error) { | 153 func GetConsole(c context.Context, projName, consoleName string) (*config.Consol e, error) { |
| 131 p, err := GetProject(c, projName) | 154 p, err := GetProject(c, projName) |
| 132 if err != nil { | 155 if err != nil { |
| 133 return nil, err | 156 return nil, err |
| 134 } | 157 } |
| 135 for _, cs := range p.Consoles { | 158 for _, cs := range p.Consoles { |
| 136 if cs.Name == consoleName { | 159 if cs.Name == consoleName { |
| 137 return cs, nil | 160 return cs, nil |
| 138 } | 161 } |
| 139 } | 162 } |
| 140 return nil, fmt.Errorf("Console %s not found in project %s", consoleName , projName) | 163 return nil, fmt.Errorf("Console %s not found in project %s", consoleName , projName) |
| 141 } | 164 } |
| OLD | NEW |