docker.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package dao
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "io"
  7. "os"
  8. "strings"
  9. "go-common/library/ecode"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/api/types/filters"
  12. )
  13. // authConfig get Auth Config.
  14. func (d *Dao) authConfig() (authStr string, err error) {
  15. authConfig := types.AuthConfig{
  16. Username: d.c.BiliHub.Username,
  17. Password: d.c.BiliHub.Password,
  18. ServerAddress: d.c.BiliHub.HostName,
  19. }
  20. var encodedJSON []byte
  21. if encodedJSON, err = json.Marshal(authConfig); err != nil {
  22. return
  23. }
  24. authStr = base64.URLEncoding.EncodeToString(encodedJSON)
  25. return
  26. }
  27. // ImagePull Image Pull.
  28. func (d *Dao) ImagePull(imageName string) (err error) {
  29. var (
  30. out io.ReadCloser
  31. authStr string
  32. )
  33. if authStr, err = d.authConfig(); err != nil {
  34. return
  35. }
  36. if out, err = d.dockerClient.ImagePull(context.TODO(), imageName, types.ImagePullOptions{RegistryAuth: authStr}); err != nil {
  37. err = ecode.MerlinImagePullErr
  38. return
  39. }
  40. defer out.Close()
  41. io.Copy(os.Stdout, out)
  42. return
  43. }
  44. // ImagePush Image Push.
  45. func (d *Dao) ImagePush(imageName string) (err error) {
  46. var (
  47. out io.ReadCloser
  48. authStr string
  49. )
  50. if authStr, err = d.authConfig(); err != nil {
  51. return
  52. }
  53. if out, err = d.dockerClient.ImagePush(context.TODO(), imageName, types.ImagePushOptions{RegistryAuth: authStr}); err != nil {
  54. err = ecode.MerlinImagePushErr
  55. return
  56. }
  57. defer out.Close()
  58. io.Copy(os.Stdout, out)
  59. return
  60. }
  61. // ImageTag Image Tag.
  62. func (d *Dao) ImageTag(imageSrcName, imageTagName string) (err error) {
  63. err = d.dockerClient.ImageTag(context.TODO(), imageSrcName, imageTagName)
  64. return
  65. }
  66. // ImageRemove Image Remove.
  67. func (d *Dao) ImageRemove(imageID string) (err error) {
  68. _, err = d.dockerClient.ImageRemove(context.TODO(), imageID, types.ImageRemoveOptions{Force: true, PruneChildren: true})
  69. return
  70. }
  71. // ImageGetID Image Get ID.
  72. func (d *Dao) ImageGetID(imageName string) (imageID string, err error) {
  73. var images []types.ImageSummary
  74. if images, err = d.dockerClient.ImageList(context.TODO(), types.ImageListOptions{}); err != nil {
  75. return
  76. }
  77. for _, image := range images {
  78. for _, tag := range image.RepoTags {
  79. if tag == imageName {
  80. imageID = image.ID
  81. return
  82. }
  83. }
  84. }
  85. return
  86. }
  87. // ImageGetAll Image Get All.
  88. func (d *Dao) ImageGetAll() (imageNames []string, err error) {
  89. var images []types.ImageSummary
  90. if images, err = d.dockerClient.ImageList(context.Background(), types.ImageListOptions{Filters: filters.NewArgs()}); err != nil {
  91. return
  92. }
  93. for _, image := range images {
  94. for _, tag := range image.RepoTags {
  95. if strings.Contains(tag, d.c.BiliHub.HostName) {
  96. imageNames = append(imageNames, tag)
  97. }
  98. }
  99. }
  100. return
  101. }