Files
my-rook-config/pkg/ceph/mon/daemon.go
T
Travis Nielsen c122562a7d Rook refactor to call external ceph tools instead of embedded ceph
Remove all cgo from the code base, and start the transformation to calling external ceph tools to generate config and start daemons.
2017-06-20 20:57:45 -07:00

154 lines
4.4 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
import (
"fmt"
"os"
"strings"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/util"
)
const (
Port = 6790
)
type Config struct {
Name string
Cluster *ClusterInfo
isDaemon bool
}
func NewConfig(name string, cluster *ClusterInfo, isDaemon bool) *Config {
return &Config{Name: name, Cluster: cluster, isDaemon: isDaemon}
}
func FlattenMonEndpoints(mons map[string]*CephMonitorConfig) string {
endpoints := []string{}
for _, m := range mons {
endpoints = append(endpoints, fmt.Sprintf("%s=%s", m.Name, m.Endpoint))
}
return strings.Join(endpoints, ",")
}
func ParseMonEndpoints(input string) map[string]*CephMonitorConfig {
logger.Infof("parsing mon endpoints: %s", input)
mons := map[string]*CephMonitorConfig{}
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]] = &CephMonitorConfig{Name: parts[0], Endpoint: parts[1]}
}
return mons
}
func ToCephMon(name, ip string) *CephMonitorConfig {
return &CephMonitorConfig{Name: name, Endpoint: fmt.Sprintf("%s:%d", ip, Port)}
}
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 latest config to the config dir
if err := GenerateAdminConnectionConfig(context, config.Cluster); err != nil {
return "", "", fmt.Errorf("failed to write connection config. %+v", err)
}
// 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")
monNameArg := fmt.Sprintf("--name=mon.%s", config.Name)
keyringPath := getMonKeyringPath(context.ConfigDir, config.Name)
err := context.ProcMan.Run(
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))
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),
}
err = context.ProcMan.Run(config.Name, "ceph-mon", args...)
if err != nil {
return fmt.Errorf("failed to start mon: %+v", err)
}
return nil
}