| 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 "time" | 9 "time" |
| 10 | 10 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 } | 103 } |
| 104 if msg, ok := item.(ServiceConfig); ok { | 104 if msg, ok := item.(ServiceConfig); ok { |
| 105 logging.Infof(c, "loaded config entry from %s", msg.LastUpdated.
Format(time.RFC3339)) | 105 logging.Infof(c, "loaded config entry from %s", msg.LastUpdated.
Format(time.RFC3339)) |
| 106 return &msg, nil | 106 return &msg, nil |
| 107 } | 107 } |
| 108 return nil, fmt.Errorf("could not load service config %#v", item) | 108 return nil, fmt.Errorf("could not load service config %#v", item) |
| 109 } | 109 } |
| 110 | 110 |
| 111 // UpdateServiceConfig fetches the service config from luci-config | 111 // UpdateServiceConfig fetches the service config from luci-config |
| 112 // and then stores a snapshot of the configuration in datastore. | 112 // and then stores a snapshot of the configuration in datastore. |
| 113 func UpdateServiceConfig(c context.Context) error { | 113 func UpdateServiceConfig(c context.Context) (*config.Settings, error) { |
| 114 // Load the settings from luci-config. | 114 // Load the settings from luci-config. |
| 115 cs := string(cfgclient.CurrentServiceConfigSet(c)) | 115 cs := string(cfgclient.CurrentServiceConfigSet(c)) |
| 116 // Acquire the raw config client. | 116 // Acquire the raw config client. |
| 117 lucicfg := backend.Get(c).GetConfigInterface(c, backend.AsService) | 117 lucicfg := backend.Get(c).GetConfigInterface(c, backend.AsService) |
| 118 // Our global config name is called settings.cfg. | 118 // Our global config name is called settings.cfg. |
| 119 cfg, err := lucicfg.GetConfig(c, cs, "settings.cfg", false) | 119 cfg, err := lucicfg.GetConfig(c, cs, "settings.cfg", false) |
| 120 if err != nil { | 120 if err != nil { |
| 121 » » return fmt.Errorf("could not load settings.cfg from luci-config:
%s", err) | 121 » » return nil, fmt.Errorf("could not load settings.cfg from luci-co
nfig: %s", err) |
| 122 } | 122 } |
| 123 settings := &config.Settings{} | 123 settings := &config.Settings{} |
| 124 err = proto.UnmarshalText(cfg.Content, settings) | 124 err = proto.UnmarshalText(cfg.Content, settings) |
| 125 if err != nil { | 125 if err != nil { |
| 126 » » return fmt.Errorf("could not unmarshal proto from luci-config:\n
%s", cfg.Content) | 126 » » return nil, fmt.Errorf( |
| 127 » » » "could not unmarshal proto from luci-config:\n%s", cfg.C
ontent) |
| 127 } | 128 } |
| 128 newConfig := ServiceConfig{ | 129 newConfig := ServiceConfig{ |
| 129 ID: ServiceConfigID, | 130 ID: ServiceConfigID, |
| 130 Text: cfg.Content, | 131 Text: cfg.Content, |
| 131 Revision: cfg.Revision, | 132 Revision: cfg.Revision, |
| 132 LastUpdated: time.Now().UTC(), | 133 LastUpdated: time.Now().UTC(), |
| 133 } | 134 } |
| 134 newConfig.Data, err = proto.Marshal(settings) | 135 newConfig.Data, err = proto.Marshal(settings) |
| 135 if err != nil { | 136 if err != nil { |
| 136 » » return fmt.Errorf("could not marshal proto into binary\n%s", new
Config.Text) | 137 » » return nil, fmt.Errorf("could not marshal proto into binary\n%s"
, newConfig.Text) |
| 137 } | 138 } |
| 138 | 139 |
| 139 // Do the revision check & swap in a datastore transaction. | 140 // Do the revision check & swap in a datastore transaction. |
| 140 err = datastore.RunInTransaction(c, func(c context.Context) error { | 141 err = datastore.RunInTransaction(c, func(c context.Context) error { |
| 141 oldConfig := ServiceConfig{ID: ServiceConfigID} | 142 oldConfig := ServiceConfig{ID: ServiceConfigID} |
| 142 err := datastore.Get(c, &oldConfig) | 143 err := datastore.Get(c, &oldConfig) |
| 143 switch err { | 144 switch err { |
| 144 case datastore.ErrNoSuchEntity: | 145 case datastore.ErrNoSuchEntity: |
| 145 // Might be the first time this has run. | 146 // Might be the first time this has run. |
| 146 logging.WithError(err).Warningf(c, "No existing service
config.") | 147 logging.WithError(err).Warningf(c, "No existing service
config.") |
| 147 case nil: | 148 case nil: |
| 148 // Continue | 149 // Continue |
| 149 default: | 150 default: |
| 150 return fmt.Errorf("could not load existing config: %s",
err) | 151 return fmt.Errorf("could not load existing config: %s",
err) |
| 151 } | 152 } |
| 152 // Check to see if we need to update | 153 // Check to see if we need to update |
| 153 if oldConfig.Revision == newConfig.Revision { | 154 if oldConfig.Revision == newConfig.Revision { |
| 154 logging.Infof(c, "revisions matched (%s), no need to upd
ate", oldConfig.Revision) | 155 logging.Infof(c, "revisions matched (%s), no need to upd
ate", oldConfig.Revision) |
| 155 return nil | 156 return nil |
| 156 } | 157 } |
| 157 logging.Infof(c, "revisions differ (old %s, new %s), updating", | 158 logging.Infof(c, "revisions differ (old %s, new %s), updating", |
| 158 oldConfig.Revision, newConfig.Revision) | 159 oldConfig.Revision, newConfig.Revision) |
| 159 return datastore.Put(c, &newConfig) | 160 return datastore.Put(c, &newConfig) |
| 160 }, nil) | 161 }, nil) |
| 161 | 162 |
| 162 if err != nil { | 163 if err != nil { |
| 163 » » return fmt.Errorf("failed to update config entry in transaction"
, err) | 164 » » return nil, fmt.Errorf("failed to update config entry in transac
tion", err) |
| 164 } | 165 } |
| 165 logging.Infof(c, "successfully updated to new config") | 166 logging.Infof(c, "successfully updated to new config") |
| 166 | 167 |
| 167 » return nil | 168 » return settings, nil |
| 168 } | 169 } |
| 169 | 170 |
| 170 // UpdateProjectConfigs internal project configuration based off luci-config. | 171 // UpdateProjectConfigs internal project configuration based off luci-config. |
| 171 // update updates Milo's configuration based off luci config. This includes | 172 // update updates Milo's configuration based off luci config. This includes |
| 172 // scanning through all project and extract all console configs. | 173 // scanning through all project and extract all console configs. |
| 173 func UpdateProjectConfigs(c context.Context) error { | 174 func UpdateProjectConfigs(c context.Context) error { |
| 174 cfgName := info.AppID(c) + ".cfg" | 175 cfgName := info.AppID(c) + ".cfg" |
| 175 | 176 |
| 176 var ( | 177 var ( |
| 177 configs []*config.Project | 178 configs []*config.Project |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 if err != nil { | 272 if err != nil { |
| 272 return nil, err | 273 return nil, err |
| 273 } | 274 } |
| 274 for _, cs := range p.Consoles { | 275 for _, cs := range p.Consoles { |
| 275 if cs.Name == consoleName { | 276 if cs.Name == consoleName { |
| 276 return cs, nil | 277 return cs, nil |
| 277 } | 278 } |
| 278 } | 279 } |
| 279 return nil, fmt.Errorf("Console %s not found in project %s", consoleName
, projName) | 280 return nil, fmt.Errorf("Console %s not found in project %s", consoleName
, projName) |
| 280 } | 281 } |
| OLD | NEW |