storages.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package compute
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/cmd/climc/shell"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  24. "yunion.io/x/onecloud/pkg/util/cephutils"
  25. )
  26. func init() {
  27. cmd := shell.NewResourceCmd(&modules.Storages).WithContextManager(&modules.Zones)
  28. cmd.List(&compute.StorageListOptions{})
  29. cmd.Update(&compute.StorageUpdateOptions{})
  30. cmd.Create(&compute.StorageCreateOptions{})
  31. cmd.Show(&options.BaseShowOptions{})
  32. cmd.Delete(&options.BaseIdOptions{})
  33. cmd.Perform("enable", &options.BaseIdOptions{})
  34. cmd.Perform("disable", &options.BaseIdOptions{})
  35. cmd.Perform("online", &options.BaseIdOptions{})
  36. cmd.Perform("offline", &options.BaseIdOptions{})
  37. cmd.Perform("cache-image", &compute.StorageCacheImageActionOptions{})
  38. cmd.Perform("uncache-image", &compute.StorageUncacheImageActionOptions{})
  39. cmd.Perform("change-owner", &options.ChangeOwnerOptions{})
  40. cmd.Perform("force-detach-host", &compute.StorageForceDetachHost{})
  41. cmd.Perform("public", &options.BasePublicOptions{})
  42. cmd.Perform("private", &options.BaseIdOptions{})
  43. cmd.Get("hardware-info", &options.BaseIdOptions{})
  44. cmd.Perform("set-hardware-info", &compute.StorageSetHardwareInfoOptions{})
  45. cmd.Perform("set-commit-bound", &compute.StorageSetCommitBoundOptions{})
  46. type StorageCephRunOptions struct {
  47. ID string `help:"ID or name of ceph storage"`
  48. SUBCMD string `help:"ceph subcommand"`
  49. }
  50. R(&StorageCephRunOptions{}, "storage-ceph-run", "Run ceph command against a ceph storage", func(s *mcclient.ClientSession, args *StorageCephRunOptions) error {
  51. result, err := modules.Storages.Get(s, args.ID, nil)
  52. if err != nil {
  53. return errors.Wrap(err, "Get")
  54. }
  55. info := struct {
  56. StorageType string `json:"storage_type"`
  57. StorageConf struct {
  58. Key string `json:"key"`
  59. MonHost string `json:"mon_host"`
  60. Pool string `json:"pool"`
  61. EnableMessengerV2 bool `json:"enable_messenger_v2"`
  62. }
  63. }{}
  64. err = result.Unmarshal(&info)
  65. if err != nil {
  66. return errors.Wrap(err, "Unmarshal")
  67. }
  68. if info.StorageType != "rbd" {
  69. return errors.Errorf("invalid storage_type %s", info.StorageType)
  70. }
  71. cli, err := cephutils.NewClient(info.StorageConf.MonHost, info.StorageConf.Key, info.StorageConf.Pool, info.StorageConf.EnableMessengerV2, 0, 0, 0)
  72. if err != nil {
  73. return errors.Wrap(err, "cephutils.NewClient")
  74. }
  75. defer cli.Close()
  76. if args.SUBCMD == "showconf" {
  77. cli.ShowConf()
  78. return nil
  79. }
  80. opts := strings.Split(args.SUBCMD, " ")
  81. if len(opts) == 0 {
  82. return errors.Errorf("empty command")
  83. }
  84. output, err := cli.Output(opts[0], opts[1:])
  85. if err != nil {
  86. return errors.Wrap(err, "Run")
  87. }
  88. fmt.Println(output.PrettyString())
  89. return nil
  90. })
  91. }