| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package docker
- import (
- "context"
- "encoding/base64"
- "encoding/json"
- "fmt"
- fw "git.buran.team/main/fairwind"
- mobyregistry "github.com/moby/moby/api/types/registry"
- moby "github.com/moby/moby/client"
- )
- type Image struct {
- ctx context.Context
- log *fw.Log
- client *Docker
- registry *Registry
- tag string
- version string
- pull bool
- }
- func NewImage(ctx context.Context, log *fw.Log, client *Docker, registry *Registry, tag string, version string, pull bool) *Image {
- return &Image{
- ctx: ctx,
- log: log,
- client: client,
- registry: registry,
- tag: tag,
- version: version,
- pull: pull,
- }
- }
- func (this *Image) Registry() *Registry {
- return this.registry
- }
- func (this *Image) Name() string {
- return fmt.Sprintf("%s/%s:%s", this.registry.address, this.tag, this.version)
- }
- func (this *Image) Tag() string {
- return this.tag
- }
- func (this *Image) Version() string {
- return this.version
- }
- func (this *Image) Pull() error {
- if !this.pull {
- return nil
- }
- credentials, err := credentialsToString(
- this.registry.login,
- this.registry.password,
- )
- if err != nil {
- return fmt.Errorf("can't pull image: %w", err)
- }
- response, err := this.client.Docker.ImagePull(
- this.ctx,
- this.Name(),
- moby.ImagePullOptions{
- RegistryAuth: credentials,
- },
- )
- if err != nil {
- return fmt.Errorf("can't pull image: %w", err)
- }
- err = response.Wait(this.ctx)
- if err != nil {
- return fmt.Errorf("can't pull image: %w", err)
- }
- return nil
- }
- func credentialsToString(login string, password string) (string, error) {
- encodedJSON, err := json.Marshal(
- &mobyregistry.AuthConfig{
- Username: login,
- Password: password,
- },
- )
- if err != nil {
- return "", fmt.Errorf("can't serialize credentials: %w", err)
- }
- authStr := base64.URLEncoding.EncodeToString(encodedJSON)
- return authStr, nil
- }
|