image.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 image
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/util/procutils"
  19. )
  20. type ImageTool interface {
  21. Pull(image string, opt *PullOptions) (string, error)
  22. Push(image string, opt *PushOptions) error
  23. }
  24. type imageTool struct {
  25. address string
  26. namespace string
  27. }
  28. func NewImageTool(address, namespace string) ImageTool {
  29. return &imageTool{
  30. address: address,
  31. namespace: namespace,
  32. }
  33. }
  34. func (i imageTool) newCtrCmd(args ...string) *procutils.Command {
  35. reqArgs := []string{"--address", i.address}
  36. if i.namespace != "" {
  37. reqArgs = append(reqArgs, "--namespace", i.namespace)
  38. }
  39. args = append(reqArgs, args...)
  40. return procutils.NewRemoteCommandAsFarAsPossible("ctr", args...)
  41. }
  42. type RepoCommonOptions struct {
  43. SkipVerify bool
  44. PlainHttp bool
  45. Username string
  46. Password string
  47. }
  48. type PullOptions struct {
  49. RepoCommonOptions
  50. }
  51. func (i imageTool) newRepoCommonArgs(opt RepoCommonOptions) []string {
  52. args := []string{}
  53. if opt.PlainHttp {
  54. args = append(args, "--plain-http")
  55. }
  56. if opt.SkipVerify {
  57. args = append(args, "--skip-verify")
  58. }
  59. if opt.Username != "" && opt.Password != "" {
  60. args = append(args, "--user", fmt.Sprintf("%s:%s", opt.Username, opt.Password))
  61. }
  62. return args
  63. }
  64. func (i imageTool) Pull(image string, opt *PullOptions) (string, error) {
  65. args := []string{}
  66. args = append(args, []string{"images", "pull"}...)
  67. args = append(args, i.newRepoCommonArgs(opt.RepoCommonOptions)...)
  68. args = append(args, []string{image}...)
  69. cmd := i.newCtrCmd(args...)
  70. out, err := cmd.Output()
  71. if err != nil {
  72. return "", errors.Wrapf(err, "pull imageTool: %s", out)
  73. }
  74. return image, nil
  75. }
  76. type PushOptions struct {
  77. RepoCommonOptions
  78. }
  79. func (i imageTool) Push(image string, opt *PushOptions) error {
  80. args := []string{}
  81. args = append(args, []string{"images", "push"}...)
  82. args = append(args, i.newRepoCommonArgs(opt.RepoCommonOptions)...)
  83. args = append(args, []string{image}...)
  84. cmd := i.newCtrCmd(args...)
  85. out, err := cmd.Output()
  86. if err != nil {
  87. return errors.Wrapf(err, "push %s: %s", image, out)
  88. }
  89. return nil
  90. }