@@ -28,6 +28,7 @@ import (
2828 "yunion.io/x/pkg/errors"
2929 "yunion.io/x/pkg/utils"
3030
31+ api "yunion.io/x/onecloud/pkg/apis/compute"
3132 "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
3233 "yunion.io/x/onecloud/pkg/util/fileutils2"
3334 "yunion.io/x/onecloud/pkg/util/procutils"
@@ -40,10 +41,14 @@ type CephClient struct {
4041 cephConf string
4142 keyConf string
4243
43- timeout int
44+ timeout int
45+ persistentConf bool
4446}
4547
4648func (cli * CephClient ) Close () error {
49+ if cli .persistentConf {
50+ return nil
51+ }
4752 if len (cli .keyConf ) > 0 {
4853 os .Remove (cli .keyConf )
4954 }
@@ -236,7 +241,29 @@ func SetCephConfTempDir(dir string) {
236241 cephConfTmpDir = dir
237242}
238243
239- func NewClient (monHost , key , pool string , enableMessengerV2 bool ) (* CephClient , error ) {
244+ func NewClientAndPersistentConf (
245+ monHost , key , pool string , enableMessengerV2 bool ,
246+ radosMonTimeout , radosOsdTimeout , clientMountTimeout int ,
247+ confPath , keyringPath string ,
248+ ) (* CephClient , error ) {
249+ if len (confPath ) == 0 || len (keyringPath ) == 0 {
250+ return nil , errors .Errorf ("empty conf path %s %s" , confPath , keyringPath )
251+ }
252+ return newClient (monHost , key , pool , enableMessengerV2 , radosMonTimeout , radosOsdTimeout , clientMountTimeout , confPath , keyringPath )
253+ }
254+
255+ func NewClient (
256+ monHost , key , pool string , enableMessengerV2 bool ,
257+ radosMonTimeout , radosOsdTimeout , clientMountTimeout int ,
258+ ) (* CephClient , error ) {
259+ return newClient (monHost , key , pool , enableMessengerV2 , radosMonTimeout , radosOsdTimeout , clientMountTimeout , "" , "" )
260+ }
261+
262+ func newClient (
263+ monHost , key , pool string , enableMessengerV2 bool ,
264+ radosMonTimeout , radosOsdTimeout , clientMountTimeout int ,
265+ confPath , keyringPath string ,
266+ ) (* CephClient , error ) {
240267 client := & CephClient {
241268 monHost : monHost ,
242269 key : key ,
@@ -248,9 +275,18 @@ func NewClient(monHost, key, pool string, enableMessengerV2 bool) (*CephClient,
248275 keyring := fmt .Sprintf (`[client.admin]
249276 key = %s
250277` , client .key )
251- client .keyConf , err = writeFile ("ceph.*.keyring" , keyring )
252- if err != nil {
253- return nil , errors .Wrapf (err , "write keyring" )
278+ if len (keyringPath ) == 0 {
279+ client .keyConf , err = writeFile ("ceph.*.keyring" , keyring )
280+ if err != nil {
281+ return nil , errors .Wrapf (err , "write keyring" )
282+ }
283+ } else {
284+ err = fileutils2 .FilePutContents (keyringPath , keyring , false )
285+ if err != nil {
286+ return nil , errors .Wrapf (err , "write keyring to %s" , keyringPath )
287+ }
288+ client .keyConf = keyringPath
289+ client .persistentConf = true
254290 }
255291 }
256292 monHosts := []string {}
@@ -264,13 +300,22 @@ func NewClient(monHost, key, pool string, enableMessengerV2 bool) (*CephClient,
264300 }
265301 }
266302 client .monHost = strings .Join (monHosts , "," )
303+ if radosMonTimeout <= 0 {
304+ radosMonTimeout = api .RBD_DEFAULT_MON_TIMEOUT
305+ }
306+ if radosOsdTimeout <= 0 {
307+ radosOsdTimeout = api .RBD_DEFAULT_OSD_TIMEOUT
308+ }
309+ if clientMountTimeout <= 0 {
310+ clientMountTimeout = api .RBD_DEFAULT_MOUNT_TIMEOUT
311+ }
267312
268313 conf := fmt .Sprintf (`[global]
269314mon host = %s
270- rados mon op timeout = 5
271- rados osd_op timeout = 1200
272- client mount timeout = 120
273- ` , client .monHost )
315+ rados mon op timeout = %d
316+ rados osd_op timeout = %d
317+ client mount timeout = %d
318+ ` , client .monHost , radosMonTimeout , radosOsdTimeout , clientMountTimeout )
274319 if len (client .key ) == 0 {
275320 conf = fmt .Sprintf (`%s
276321auth_cluster_required = none
@@ -282,14 +327,24 @@ auth_client_required = none
282327keyring = %s
283328` , conf , client .keyConf )
284329 }
285- client .cephConf , err = writeFile ("ceph.*.conf" , conf )
286- if err != nil {
287- return nil , errors .Wrapf (err , "write file" )
330+ if len (confPath ) == 0 {
331+ client .cephConf , err = writeFile ("ceph.*.conf" , conf )
332+ if err != nil {
333+ return nil , errors .Wrapf (err , "write file" )
334+ }
335+ } else {
336+ err = fileutils2 .FilePutContents (confPath , conf , false )
337+ if err != nil {
338+ return nil , errors .Wrapf (err , "write conf to %s" , confPath )
339+ }
340+ client .cephConf = confPath
341+ client .persistentConf = true
288342 }
343+
289344 return client , nil
290345}
291346
292- func CephConfString (monHost , key string , radosMonOpTimeout , radosOsdOpTimeout , clientMountTimeout int64 ) string {
347+ func CephConfString (monHost , key string , radosMonOpTimeout , radosOsdOpTimeout , clientMountTimeout int ) string {
293348 conf := []string {}
294349 monHosts := strings .Split (monHost , "," )
295350 for i := range monHosts {
@@ -305,7 +360,7 @@ func CephConfString(monHost, key string, radosMonOpTimeout, radosOsdOpTimeout, c
305360 }
306361 conf = append (conf , "key=" + key )
307362 }
308- for k , timeout := range map [string ]int64 {
363+ for k , timeout := range map [string ]int {
309364 "rados_mon_op_timeout" : radosMonOpTimeout ,
310365 "rados_osd_op_timeout" : radosOsdOpTimeout ,
311366 "client_mount_timeout" : clientMountTimeout ,
0 commit comments