repos.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 k8s
  15. import (
  16. "io"
  17. "os"
  18. "path/filepath"
  19. "github.com/cheggaaa/pb/v3"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
  24. o "yunion.io/x/onecloud/pkg/mcclient/options/k8s"
  25. )
  26. func initRepo() {
  27. cmd := NewK8sResourceCmd(k8s.Repos)
  28. cmd.List(new(o.RepoListOptions))
  29. cmd.Show(new(o.RepoGetOptions))
  30. cmd.Create(new(o.RepoCreateOptions))
  31. cmd.Update(new(o.RepoUpdateOptions))
  32. cmd.Delete(new(o.RepoGetOptions))
  33. cmd.Perform("sync", new(o.RepoGetOptions))
  34. cmd.Perform("public", new(o.RepoPublicOptions))
  35. cmd.Perform("private", new(o.RepoGetOptions))
  36. type UploadOptions struct {
  37. REPO string `help:"The name or id of helm repository`
  38. FILE string `help:"Helm packaged file"`
  39. }
  40. R(new(UploadOptions), "k8s-repo-upload-chart", "Upload chart to the repository", func(s *mcclient.ClientSession, args *UploadOptions) error {
  41. f, err := os.Open(args.FILE)
  42. if err != nil {
  43. return err
  44. }
  45. defer f.Close()
  46. finfo, err := f.Stat()
  47. if err != nil {
  48. return err
  49. }
  50. size := finfo.Size()
  51. bar := pb.Full.Start64(size)
  52. barReader := bar.NewProxyReader(f)
  53. param := jsonutils.Marshal(args)
  54. param.(*jsonutils.JSONDict).Add(jsonutils.NewString(filepath.Base(args.FILE)), "chart_name")
  55. ret, err := k8s.Repos.UploadChart(s, args.REPO, param, barReader, size)
  56. if err != nil {
  57. return err
  58. }
  59. printObject(ret)
  60. return nil
  61. })
  62. type DownloadOptions struct {
  63. REPO string `help:"The name or id of helm repository`
  64. CHART string `help:"The name of chart"`
  65. Output string `help:"Saved file path"`
  66. Version string `help:"The version of chart"`
  67. }
  68. R(new(DownloadOptions), "k8s-repo-download-chart", "Download helm chart to a file", func(s *mcclient.ClientSession, args *DownloadOptions) error {
  69. chartFileName, src, size, err := k8s.Repos.DownloadChart(s, args.REPO, args.CHART, args.Version)
  70. if err != nil {
  71. return errors.Wrap(err, "download chart")
  72. }
  73. output := args.Output
  74. if output == "" && chartFileName != "" {
  75. output = chartFileName
  76. }
  77. if output == "" {
  78. return errors.Errorf("--output filepath must provide")
  79. }
  80. f, err := os.Create(output)
  81. if err != nil {
  82. return errors.Wrapf(err, "create saved file: %q", args.Output)
  83. }
  84. defer f.Close()
  85. var sink io.Writer = f
  86. bar := pb.Full.Start64(size)
  87. barReader := bar.NewProxyReader(src)
  88. if _, err := io.Copy(sink, barReader); err != nil {
  89. return errors.Wrap(err, "save chart")
  90. }
  91. return nil
  92. })
  93. }