2017-02-07 14:47:35 -08:00
|
|
|
/*
|
|
|
|
|
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 mds
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"path"
|
2017-10-05 10:00:19 -07:00
|
|
|
"strconv"
|
2017-02-07 14:47:35 -08:00
|
|
|
|
2017-10-17 09:33:37 -07:00
|
|
|
"github.com/coreos/pkg/capnslog"
|
2017-02-07 14:47:35 -08:00
|
|
|
"github.com/rook/rook/pkg/clusterd"
|
2017-12-04 08:33:19 -08:00
|
|
|
"github.com/rook/rook/pkg/daemon/ceph/client"
|
|
|
|
|
"github.com/rook/rook/pkg/daemon/ceph/mon"
|
2017-02-07 14:47:35 -08:00
|
|
|
"github.com/rook/rook/pkg/util"
|
2017-10-05 10:00:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
keyringTemplate = `
|
|
|
|
|
[mds.%s]
|
|
|
|
|
key = %s
|
|
|
|
|
caps mon = "allow profile mds"
|
|
|
|
|
caps osd = "allow *"
|
|
|
|
|
caps mds = "allow"
|
|
|
|
|
`
|
2017-02-07 14:47:35 -08:00
|
|
|
)
|
|
|
|
|
|
2017-10-17 09:33:37 -07:00
|
|
|
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "cephmds")
|
|
|
|
|
|
2017-02-07 14:47:35 -08:00
|
|
|
type Config struct {
|
2017-10-05 10:00:19 -07:00
|
|
|
FilesystemID string
|
|
|
|
|
ID string
|
|
|
|
|
ActiveStandby bool
|
|
|
|
|
ClusterInfo *mon.ClusterInfo
|
2017-02-07 14:47:35 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-02 13:44:54 -07:00
|
|
|
func Run(context *clusterd.Context, config *Config) error {
|
2017-02-07 14:47:35 -08:00
|
|
|
|
|
|
|
|
err := generateConfigFiles(context, config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to generate mds config files. %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:00:19 -07:00
|
|
|
err = startMDS(context, config)
|
2017-02-07 14:47:35 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to run mds. %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 13:44:54 -07:00
|
|
|
func generateConfigFiles(context *clusterd.Context, config *Config) error {
|
|
|
|
|
// write the latest config to the config dir
|
|
|
|
|
if err := mon.GenerateAdminConnectionConfig(context, config.ClusterInfo); err != nil {
|
|
|
|
|
return fmt.Errorf("failed to write connection config. %+v", err)
|
|
|
|
|
}
|
2017-02-07 14:47:35 -08:00
|
|
|
|
2017-10-05 10:00:19 -07:00
|
|
|
// Create a keyring for the new mds instance. Each pod will have its own keyring.
|
|
|
|
|
// If the instance fails, the operator will need to clean up the unused keyring.
|
|
|
|
|
keyring, err := createKeyring(context, config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create mds keyring. %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settings := map[string]string{
|
|
|
|
|
"mds_standby_for_fscid": config.FilesystemID,
|
|
|
|
|
"mds_standby_replay": strconv.FormatBool(config.ActiveStandby),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyringPath := getMDSKeyringPath(context.ConfigDir)
|
|
|
|
|
_, err = mon.GenerateConfigFile(context, config.ClusterInfo, getMDSConfDir(context.ConfigDir),
|
2017-10-25 15:20:50 -07:00
|
|
|
fmt.Sprintf("mds.%s", config.ID), keyringPath, nil, settings)
|
2017-02-07 14:47:35 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create mds config file. %+v", err)
|
|
|
|
|
}
|
|
|
|
|
keyringEval := func(key string) string {
|
2017-10-05 10:00:19 -07:00
|
|
|
r := fmt.Sprintf(keyringTemplate, config.ID, key)
|
|
|
|
|
logger.Infof("keyring: %s", r)
|
|
|
|
|
return r
|
2017-02-07 14:47:35 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:00:19 -07:00
|
|
|
err = mon.WriteKeyring(keyringPath, keyring, keyringEval)
|
2017-02-07 14:47:35 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to create mds keyring. %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:00:19 -07:00
|
|
|
func startMDS(context *clusterd.Context, config *Config) error {
|
2017-02-07 14:47:35 -08:00
|
|
|
|
|
|
|
|
// start the mds daemon in the foreground with the given config
|
|
|
|
|
logger.Infof("starting mds %s", config.ID)
|
|
|
|
|
|
2017-10-05 10:00:19 -07:00
|
|
|
confFile := getMDSConfFilePath(context.ConfigDir, config.ClusterInfo.Name)
|
2017-02-07 14:47:35 -08:00
|
|
|
util.WriteFileToLog(logger, confFile)
|
|
|
|
|
|
|
|
|
|
mdsNameArg := fmt.Sprintf("--name=mds.%s", config.ID)
|
|
|
|
|
args := []string{
|
|
|
|
|
"--foreground",
|
|
|
|
|
mdsNameArg,
|
2017-10-05 10:00:19 -07:00
|
|
|
"-i", config.ID,
|
2017-02-07 14:47:35 -08:00
|
|
|
fmt.Sprintf("--cluster=%s", config.ClusterInfo.Name),
|
|
|
|
|
fmt.Sprintf("--conf=%s", confFile),
|
2017-10-05 10:00:19 -07:00
|
|
|
fmt.Sprintf("--keyring=%s", getMDSKeyringPath(context.ConfigDir)),
|
2017-02-07 14:47:35 -08:00
|
|
|
}
|
|
|
|
|
|
2017-06-02 13:44:54 -07:00
|
|
|
name := fmt.Sprintf("mds%s", config.ID)
|
2017-10-26 15:21:53 -07:00
|
|
|
if err := context.Executor.ExecuteCommand(false, name, "ceph-mds", args...); err != nil {
|
2017-10-05 10:00:19 -07:00
|
|
|
return fmt.Errorf("failed to start mds. %+v", err)
|
2017-02-07 14:47:35 -08:00
|
|
|
}
|
2017-10-05 10:00:19 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getMDSConfDir(dir string) string {
|
|
|
|
|
return path.Join(dir, "mds")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getMDSConfFilePath(dir, clusterName string) string {
|
|
|
|
|
return path.Join(getMDSConfDir(dir), fmt.Sprintf("%s.config", clusterName))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getMDSKeyringPath(dir string) string {
|
|
|
|
|
return path.Join(getMDSConfDir(dir), "keyring")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create a keyring for the mds client with a limited set of privileges
|
|
|
|
|
func createKeyring(context *clusterd.Context, config *Config) (string, error) {
|
|
|
|
|
access := []string{"osd", "allow *", "mds", "allow", "mon", "allow profile mds"}
|
|
|
|
|
username := fmt.Sprintf("mds.%s", config.ID)
|
|
|
|
|
|
|
|
|
|
// get-or-create-key for the user account
|
|
|
|
|
keyring, err := client.AuthGetOrCreateKey(context, config.ClusterInfo.Name, username, access)
|
2017-02-07 14:47:35 -08:00
|
|
|
if err != nil {
|
2017-10-05 10:00:19 -07:00
|
|
|
return "", fmt.Errorf("failed to get or create mds auth key for %s. %+v", username, err)
|
2017-02-07 14:47:35 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:00:19 -07:00
|
|
|
return keyring, nil
|
2017-02-07 14:47:35 -08:00
|
|
|
}
|