forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerartifact.go
101 lines (81 loc) · 2.19 KB
/
dockerartifact.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package baur
import (
"fmt"
"github.com/pkg/errors"
"github.com/simplesurance/baur/digest"
"github.com/simplesurance/baur/fs"
"github.com/simplesurance/baur/upload/scheduler"
)
// DockerArtifact is a docker container artifact
type DockerArtifact struct {
ImageIDFile string
Tag string
Repository string
}
// Exists returns true if the ImageIDFile exists
func (d *DockerArtifact) Exists() bool {
return fs.FileExists(d.ImageIDFile)
}
// ImageID reads the image from ImageIDFile
func (d *DockerArtifact) ImageID() (string, error) {
id, err := fs.FileReadLine(d.ImageIDFile)
if err != nil {
return "", err
}
if len(id) == 0 {
return "", errors.New("file is empty")
}
return id, nil
}
// UploadJob returns a upload.DockerJob for the artifact
func (d *DockerArtifact) UploadJob() (scheduler.Job, error) {
id, err := d.ImageID()
if err != nil {
return nil, err
}
return &scheduler.DockerJob{
ImageID: id,
Repository: d.Repository,
Tag: d.Tag,
}, nil
}
// String returns the absolute path to the ImageID file
func (d *DockerArtifact) String() string {
return d.LocalPath()
}
// LocalPath returns the local path to the artifact
func (d *DockerArtifact) LocalPath() string {
return d.ImageIDFile
}
// Name returns the docker repository name
func (d *DockerArtifact) Name() string {
return d.Repository
}
// UploadDestination returns the upload destination
func (d *DockerArtifact) UploadDestination() string {
return fmt.Sprintf("%s:%s", d.Repository, d.Tag)
}
// Digest returns the image ID as Digest object
func (d *DockerArtifact) Digest() (*digest.Digest, error) {
id, err := d.ImageID()
if err != nil {
return nil, errors.Wrap(err, "reading imageID from file failed")
}
digest, err := digest.FromString(id)
if err != nil {
return nil, errors.Wrap(err, "converting imageID to digest failed")
}
return digest, nil
}
// Size returns the size of the docker image in bytes
func (d *DockerArtifact) Size(b *BuildOutputBackends) (int64, error) {
id, err := d.ImageID()
if err != nil {
return -1, errors.Wrap(err, "reading imageID from file failed")
}
return b.DockerClt.Size(id)
}
// Type returns "docker"
func (d *DockerArtifact) Type() string {
return "docker"
}