-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
158 lines (129 loc) · 4.73 KB
/
daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
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 rgw
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"github.com/coreos/pkg/capnslog"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/daemon/ceph/mon"
"github.com/rook/rook/pkg/util"
)
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "cephrgw")
type Config struct {
Name string
Host string
Port int
SecurePort int
Keyring string
CertificatePath string
ClusterInfo *mon.ClusterInfo
}
func Run(context *clusterd.Context, config *Config) error {
err := generateConfigFiles(context, config)
if err != nil {
return fmt.Errorf("failed to generate rgw config files. %+v", err)
}
err = startRGW(context, config)
if err != nil {
return fmt.Errorf("failed to run rgw. %+v", err)
}
return err
}
func portString(config *Config) string {
var portString string
if config.Port != 0 {
portString = strconv.Itoa(config.Port)
}
if config.SecurePort != 0 && config.CertificatePath != "" {
var separator string
if config.Port != 0 {
separator = "+"
}
// the suffix is intended to be appended to the end of the rgw_frontends arg, immediately after the port.
// with ssl enabled, the port number must end with the letter s.
portString = fmt.Sprintf("%s%s%ds ssl_certificate=%s", portString, separator, config.SecurePort, config.CertificatePath)
}
return portString
}
func generateConfigFiles(context *clusterd.Context, config *Config) error {
// create the rgw data directory
dataDir := path.Join(getRGWConfDir(context.ConfigDir), "data")
if err := os.MkdirAll(dataDir, 0744); err != nil {
logger.Warningf("failed to create data directory %s: %+v", dataDir, err)
}
settings := map[string]string{
"host": config.Host,
"rgw data": dataDir,
"rgw log nonexistent bucket": "true",
"rgw intent log object name utc": "true",
"rgw enable usage log": "true",
"rgw_frontends": fmt.Sprintf("civetweb port=%s", portString(config)),
"rgw_zone": config.Name,
"rgw_zonegroup": config.Name,
}
_, err := mon.GenerateConfigFile(context, config.ClusterInfo, getRGWConfDir(context.ConfigDir),
"client.radosgw.gateway", getRGWKeyringPath(context.ConfigDir), nil, settings)
if err != nil {
return fmt.Errorf("failed to create config file. %+v", err)
}
keyringEval := func(key string) string {
return fmt.Sprintf(keyringTemplate, key)
}
// create rgw config
err = mon.WriteKeyring(getRGWKeyringPath(context.ConfigDir), config.Keyring, keyringEval)
if err != nil {
return fmt.Errorf("failed to save keyring. %+v", err)
}
// write the mime types config
mimeTypesPath := getMimeTypesPath(context.ConfigDir)
logger.Debugf("Writing mime types to: %s", mimeTypesPath)
if err := ioutil.WriteFile(mimeTypesPath, []byte(mimeTypes), 0644); err != nil {
return fmt.Errorf("failed to write mime types to %s: %+v", mimeTypesPath, err)
}
return nil
}
func startRGW(context *clusterd.Context, config *Config) (err error) {
// start the monitor daemon in the foreground with the given config
logger.Infof("starting rgw")
confFile := getRGWConfFilePath(context.ConfigDir, config.ClusterInfo.Name)
util.WriteFileToLog(logger, confFile)
rgwNameArg := "--name=client.radosgw.gateway"
args := []string{
"--foreground",
rgwNameArg,
fmt.Sprintf("--cluster=%s", config.ClusterInfo.Name),
fmt.Sprintf("--conf=%s", confFile),
fmt.Sprintf("--keyring=%s", getRGWKeyringPath(context.ConfigDir)),
fmt.Sprintf("--rgw-mime-types-file=%s", getMimeTypesPath(context.ConfigDir)),
}
if err = context.Executor.ExecuteCommand(false, "rgw", "radosgw", args...); err != nil {
return fmt.Errorf("failed to start rgw: %+v", err)
}
return nil
}
func getRGWConfFilePath(configDir, clusterName string) string {
return path.Join(getRGWConfDir(configDir), fmt.Sprintf("%s.config", clusterName))
}
func getRGWConfDir(configDir string) string {
return path.Join(configDir, "rgw")
}
func getRGWKeyringPath(configDir string) string {
return path.Join(getRGWConfDir(configDir), "keyring")
}
func getMimeTypesPath(configDir string) string {
return path.Join(getRGWConfDir(configDir), "mime.types")
}