Files
my-rook-config/pkg/daemon/ceph/mon/daemon.go
T
Blaine Gardner 3731f06176 Ceph: Refactor mon config/keyring to cephconfig
Create a cephconfig module in Ceph's daemon pkg source, and refactor the
config and keyring generation that exists in the mon package into the
new cephconfig package. The config/keyring generation code is used by
most all daemons and not just mon, so a new package is a more
appropriate place for this.

Signed-off-by: Blaine Gardner <blaine.gardner@suse.com>
2018-09-05 10:17:11 -06:00

163 lines
5.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 mon provides methods for configuring and running Ceph mons.
package mon
import (
"fmt"
"net"
"os"
"strings"
"github.com/rook/rook/pkg/clusterd"
cephconfig "github.com/rook/rook/pkg/daemon/ceph/config"
"github.com/rook/rook/pkg/util"
)
const (
// DefaultPort is the default port Ceph mons use to communicate amongst themselves.
DefaultPort = 6790
)
// Config contains the necessary parameters Rook needs to know to set up a mon for a Ceph cluster.
type Config struct {
Name string
Cluster *cephconfig.ClusterInfo
isDaemon bool
Port int32
}
// FlattenMonEndpoints returns a comma-delimited string of all mons and endpoints in the form
// <mon-name>=<mon-endpoint>
func FlattenMonEndpoints(mons map[string]*cephconfig.MonInfo) string {
endpoints := []string{}
for _, m := range mons {
endpoints = append(endpoints, fmt.Sprintf("%s=%s", m.Name, m.Endpoint))
}
return strings.Join(endpoints, ",")
}
// ParseMonEndpoints parses a flattened representation of mons and endpoints in the form
// <mon-name>=<mon-endpoint> and returns a list of Ceph mon configs.
func ParseMonEndpoints(input string) map[string]*cephconfig.MonInfo {
logger.Infof("parsing mon endpoints: %s", input)
mons := map[string]*cephconfig.MonInfo{}
rawMons := strings.Split(input, ",")
for _, rawMon := range rawMons {
parts := strings.Split(rawMon, "=")
if len(parts) != 2 {
logger.Warningf("ignoring invalid monitor %s", rawMon)
continue
}
mons[parts[0]] = &cephconfig.MonInfo{Name: parts[0], Endpoint: parts[1]}
}
return mons
}
// Run generates configuration files for a Ceph mon and then runs a mon daemon with the config
func Run(context *clusterd.Context, config *Config) error {
configFile, monDataDir, err := generateConfigFiles(context, config)
if err != nil {
return fmt.Errorf("failed to generate mon config files. %+v", err)
}
err = startMon(context, config, configFile, monDataDir)
if err != nil {
return fmt.Errorf("failed to run mon. %+v", err)
}
return err
}
func generateConfigFiles(context *clusterd.Context, config *Config) (string, string, error) {
// write the keyring to disk
if err := writeMonKeyring(context, config.Cluster, config.Name); err != nil {
return "", "", err
}
// write the config file to disk
confFilePath, err := generateConnectionConfigFile(context, config.Cluster, GetMonRunDirPath(context.ConfigDir, config.Name),
"admin", getMonKeyringPath(context.ConfigDir, config.Name))
if err != nil {
return "", "", err
}
// create monitor data dir
monDataDir := getMonDataDirPath(context.ConfigDir, config.Name)
if err := os.MkdirAll(monDataDir, 0744); err != nil {
logger.Warningf("failed to create monitor data directory at %s: %+v", monDataDir, err)
}
// write the kv_backend file to force ceph to use rocksdb for the MON store
if err := writeBackendFile(monDataDir, "rocksdb"); err != nil {
return "", "", err
}
return confFilePath, monDataDir, nil
}
func startMon(context *clusterd.Context, config *Config, confFilePath, monDataDir string) error {
// call mon --mkfs in a child process
logger.Infof("initializing mon")
// generate the monmap
monmapPath, err := generateMonMap(context, config.Cluster, GetMonRunDirPath(context.ConfigDir, config.Name))
if err != nil {
return err
}
monNameArg := fmt.Sprintf("--name=mon.%s", config.Name)
keyringPath := getMonKeyringPath(context.ConfigDir, config.Name)
err = context.Executor.ExecuteCommand(
false,
fmt.Sprintf("mkfs-%s", config.Name),
"ceph-mon",
"--mkfs",
monNameArg,
fmt.Sprintf("--cluster=%s", config.Cluster.Name),
fmt.Sprintf("--mon-data=%s", monDataDir),
fmt.Sprintf("--conf=%s", confFilePath),
fmt.Sprintf("--keyring=%s", keyringPath),
fmt.Sprintf("--monmap=%s", monmapPath))
if err != nil {
return fmt.Errorf("failed mon %s --mkfs: %+v", config.Name, err)
}
// start the monitor daemon in the foreground with the given config
logger.Infof("starting mon")
util.WriteFileToLog(logger, confFilePath)
args := []string{
"--foreground",
monNameArg,
fmt.Sprintf("--cluster=%s", config.Cluster.Name),
fmt.Sprintf("--mon-data=%s", monDataDir),
fmt.Sprintf("--conf=%s", confFilePath),
fmt.Sprintf("--keyring=%s", keyringPath),
fmt.Sprintf("--public-addr=%s", joinHostPort(context.NetworkInfo.PublicAddr, config.Port)),
fmt.Sprintf("--public-bind-addr=%s", joinHostPort(context.NetworkInfo.ClusterAddr, config.Port)),
}
if err = context.Executor.ExecuteCommand(false, config.Name, "ceph-mon", args...); err != nil {
return fmt.Errorf("failed to start mon: %+v", err)
}
return nil
}
func joinHostPort(host string, port int32) string {
return net.JoinHostPort(host, fmt.Sprintf("%d", port))
}