166 lines
4.2 KiB
Go
166 lines
4.2 KiB
Go
/*
|
|
Copyright 2016 The Rook Authors. All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
package mgr
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
|
|
etcd "github.com/coreos/etcd/client"
|
|
ctx "golang.org/x/net/context"
|
|
|
|
"github.com/rook/rook/pkg/ceph/mon"
|
|
"github.com/rook/rook/pkg/clusterd"
|
|
"github.com/rook/rook/pkg/util"
|
|
"github.com/rook/rook/pkg/util/proc"
|
|
)
|
|
|
|
type agent struct {
|
|
context *clusterd.Context
|
|
mgrProc *proc.MonitoredProc
|
|
}
|
|
|
|
func NewAgent() *agent {
|
|
return &agent{}
|
|
}
|
|
|
|
func (a *agent) Name() string {
|
|
return MgrKey
|
|
}
|
|
|
|
// set the desired state in etcd
|
|
func (a *agent) Initialize(context *clusterd.Context) error {
|
|
a.context = context
|
|
return nil
|
|
}
|
|
|
|
// configure cephmgr on the local node if it is in the desired state
|
|
func (a *agent) ConfigureLocalService(context *clusterd.Context) error {
|
|
// check if the mgr is in desired state on this node
|
|
desired, err := getMgrState(context.EtcdClient, context.NodeID, false)
|
|
if (err != nil && util.IsEtcdKeyNotFound(err)) || (err == nil && !desired) {
|
|
// the mgr is not in the desired state on this node, so ensure it is not running
|
|
return a.DestroyLocalService(context)
|
|
} else if err != nil {
|
|
return fmt.Errorf("failed to load mgr state. %+v", err)
|
|
}
|
|
|
|
// load cluster info
|
|
cluster, err := mon.LoadClusterInfo(context.EtcdClient)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load cluster info. %+v", err)
|
|
}
|
|
|
|
// start cephmgr
|
|
err = a.startMgr(context, cluster)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to start mgr process. %+v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *agent) DestroyLocalService(context *clusterd.Context) error {
|
|
if a.mgrProc == nil {
|
|
logger.Debugf("no need to stop mgr that is not running")
|
|
return nil
|
|
}
|
|
|
|
if err := a.mgrProc.Stop(); err != nil {
|
|
return fmt.Errorf("failed to stop mgr. %+v", err)
|
|
}
|
|
|
|
logger.Infof("stopped ceph mgr")
|
|
a.mgrProc = nil
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *agent) startMgr(context *clusterd.Context, cluster *mon.ClusterInfo) error {
|
|
|
|
// retrieve the keyring
|
|
val, err := context.EtcdClient.Get(ctx.Background(), getKeyringKey(), nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to retrieve the keyring")
|
|
}
|
|
keyring := val.Node.Value
|
|
|
|
mgrName := "1"
|
|
config := &Config{Keyring: keyring, ClusterInfo: cluster, Name: mgrName}
|
|
err = generateConfigFiles(context, config)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to generate mgr config files. %+v", err)
|
|
}
|
|
|
|
mgrProc, err := startMgr(context, config)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to start mgr daemon: %+v", err)
|
|
}
|
|
|
|
if mgrProc != nil {
|
|
// if the process was already running Start will return nil in which case we don't want to overwrite it
|
|
a.mgrProc = mgrProc
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getMgrState(etcdClient etcd.KeysAPI, nodeID string, applied bool) (bool, error) {
|
|
key := path.Join(getMgrNodeKey(nodeID, applied), "state")
|
|
res, err := etcdClient.Get(ctx.Background(), key, nil)
|
|
if err != nil {
|
|
if util.IsEtcdKeyNotFound(err) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
|
|
return res.Node.Value == "1", nil
|
|
}
|
|
|
|
func setMgrState(etcdClient etcd.KeysAPI, nodeID string, applied bool) error {
|
|
key := path.Join(getMgrNodeKey(nodeID, applied), "state")
|
|
_, err := etcdClient.Set(ctx.Background(), key, "1", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func removeMgrState(etcdClient etcd.KeysAPI, nodeID string, applied bool) error {
|
|
key := path.Join(getMgrNodeKey(nodeID, applied))
|
|
_, err := etcdClient.Delete(ctx.Background(), key, &etcd.DeleteOptions{Dir: true, Recursive: true})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getMgrNodesKey(applied bool) string {
|
|
a := clusterd.DesiredKey
|
|
if applied {
|
|
a = clusterd.AppliedKey
|
|
}
|
|
|
|
return path.Join(mon.CephKey, MgrKey, a, "node")
|
|
}
|
|
|
|
func getMgrNodeKey(nodeID string, applied bool) string {
|
|
return path.Join(getMgrNodesKey(applied), nodeID)
|
|
}
|