2017-02-24 04:32:13 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/autopilot"
|
2017-02-24 21:08:49 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2017-02-24 04:32:13 +00:00
|
|
|
)
|
|
|
|
|
2017-11-29 01:03:34 +00:00
|
|
|
// autopilotConfigTableSchema returns a new table schema used for storing
|
|
|
|
// the autopilot configuration
|
|
|
|
func autopilotConfigTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: "autopilot-config",
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
"id": &memdb.IndexSchema{
|
|
|
|
Name: "id",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.ConditionalIndex{
|
|
|
|
Conditional: func(obj interface{}) (bool, error) { return true, nil },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registerSchema(autopilotConfigTableSchema)
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
// Autopilot is used to pull the autopilot config from the snapshot.
|
2017-12-12 00:38:52 +00:00
|
|
|
func (s *Snapshot) Autopilot() (*autopilot.Config, error) {
|
2017-03-21 23:36:44 +00:00
|
|
|
c, err := s.tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
config, ok := c.(*autopilot.Config)
|
2017-03-21 23:36:44 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Autopilot is used when restoring from a snapshot.
|
2017-12-12 00:38:52 +00:00
|
|
|
func (s *Restore) Autopilot(config *autopilot.Config) error {
|
2017-03-21 23:36:44 +00:00
|
|
|
if err := s.tx.Insert("autopilot-config", config); err != nil {
|
|
|
|
return fmt.Errorf("failed restoring autopilot config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 04:32:13 +00:00
|
|
|
// AutopilotConfig is used to get the current Autopilot configuration.
|
2017-12-12 00:38:52 +00:00
|
|
|
func (s *Store) AutopilotConfig() (uint64, *autopilot.Config, error) {
|
2017-02-24 04:32:13 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Get the autopilot config
|
|
|
|
c, err := tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
2017-02-24 21:08:49 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed autopilot config lookup: %s", err)
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
config, ok := c.(*autopilot.Config)
|
2017-02-24 04:32:13 +00:00
|
|
|
if !ok {
|
2017-02-24 21:08:49 +00:00
|
|
|
return 0, nil, nil
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
return config.ModifyIndex, config, nil
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
// AutopilotSetConfig is used to set the current Autopilot configuration.
|
2017-12-12 00:38:52 +00:00
|
|
|
func (s *Store) AutopilotSetConfig(idx uint64, config *autopilot.Config) error {
|
2017-02-24 04:32:13 +00:00
|
|
|
tx := s.db.Txn(true)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
s.autopilotSetConfigTxn(idx, tx, config)
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotCASConfig is used to try updating the Autopilot configuration with a
|
|
|
|
// given Raft index. If the CAS index specified is not equal to the last observed index
|
|
|
|
// for the config, then the call is a noop,
|
2017-12-12 00:38:52 +00:00
|
|
|
func (s *Store) AutopilotCASConfig(idx, cidx uint64, config *autopilot.Config) (bool, error) {
|
2017-02-24 21:08:49 +00:00
|
|
|
tx := s.db.Txn(true)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Check for an existing config
|
|
|
|
existing, err := tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed autopilot config lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the existing index does not match the provided CAS
|
|
|
|
// index arg, then we shouldn't update anything and can safely
|
|
|
|
// return early here.
|
2017-12-12 00:38:52 +00:00
|
|
|
e, ok := existing.(*autopilot.Config)
|
2017-02-24 21:08:49 +00:00
|
|
|
if !ok || e.ModifyIndex != cidx {
|
|
|
|
return false, nil
|
2017-02-24 04:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 21:08:49 +00:00
|
|
|
s.autopilotSetConfigTxn(idx, tx, config)
|
|
|
|
|
2017-02-24 04:32:13 +00:00
|
|
|
tx.Commit()
|
2017-02-24 21:08:49 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
func (s *Store) autopilotSetConfigTxn(idx uint64, tx *memdb.Txn, config *autopilot.Config) error {
|
2017-02-24 21:08:49 +00:00
|
|
|
// Check for an existing config
|
|
|
|
existing, err := tx.First("autopilot-config", "id")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed autopilot config lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the indexes.
|
|
|
|
if existing != nil {
|
2017-12-12 00:38:52 +00:00
|
|
|
config.CreateIndex = existing.(*autopilot.Config).CreateIndex
|
2017-02-24 21:08:49 +00:00
|
|
|
} else {
|
|
|
|
config.CreateIndex = idx
|
|
|
|
}
|
|
|
|
config.ModifyIndex = idx
|
|
|
|
|
|
|
|
if err := tx.Insert("autopilot-config", config); err != nil {
|
|
|
|
return fmt.Errorf("failed updating autopilot config: %s", err)
|
|
|
|
}
|
2017-02-24 04:32:13 +00:00
|
|
|
return nil
|
|
|
|
}
|