nerdctl.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 nerdctl
  15. import (
  16. "yunion.io/x/pkg/errors"
  17. "yunion.io/x/onecloud/pkg/util/procutils"
  18. )
  19. type Nerdctl interface {
  20. Commit(ctrId string, opt *CommitOptions) (string, error)
  21. }
  22. type CommitOptions struct {
  23. Repository string
  24. }
  25. type nerdctl struct {
  26. address string
  27. namespace string
  28. }
  29. func NewNerdctl(address, namespace string) Nerdctl {
  30. return &nerdctl{
  31. address: address,
  32. namespace: namespace,
  33. }
  34. }
  35. func (n nerdctl) newCmd(args ...string) *procutils.Command {
  36. newArgs := []string{"--address", n.address}
  37. if n.namespace != "" {
  38. newArgs = append(newArgs, "--namespace", n.namespace)
  39. }
  40. newArgs = append(newArgs, args...)
  41. return procutils.NewCommand("nerdctl", newArgs...)
  42. }
  43. func (n nerdctl) Commit(ctrId string, opt *CommitOptions) (string, error) {
  44. if opt.Repository == "" {
  45. return "", errors.Wrap(errors.ErrEmpty, "repository")
  46. }
  47. cmd := n.newCmd("commit", ctrId, opt.Repository)
  48. out, err := cmd.Output()
  49. if err != nil {
  50. return "", errors.Wrapf(err, "commit %s %s: %s", ctrId, opt.Repository, out)
  51. }
  52. return opt.Repository, nil
  53. }