This repository was archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage.go
More file actions
120 lines (101 loc) · 2.69 KB
/
image.go
File metadata and controls
120 lines (101 loc) · 2.69 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
// Handle docker pull requests. Don't allow duplicate outstanding pull
// requests for the same repo tag, and track start time for each pull
// request, for future logging/display.
import (
"log"
"strings"
"time"
docker "github.com/fsouza/go-dockerclient"
)
// FindRepoTags finds valid repotags in the list of local images. Accepts 3 forms:
// "*" - Match any repotag.
// "foo" - Match any repotag in the repo "foo".
// "foo:bar" - Exact match the repotag "foo:bar" if it exists.
func (deployer *Deployer) FindRepoTags(repo string) ([]string, error) {
images, err := deployer.docker.ListImages(docker.ListImagesOptions{All: false})
if err != nil {
return nil, err
}
prefix := false
if repo != "*" && !strings.Contains(repo, ":") {
repo += ":"
prefix = true
}
repotags := make([]string, 0, 5)
for _, image := range images {
for _, repotag := range image.RepoTags {
if repotag == "<none>:<none>" {
continue
}
if repo == "*" || repo == repotag {
repotags = append(repotags, repotag)
} else if prefix && strings.HasPrefix(repotag, repo) {
repotags = append(repotags, repotag)
}
}
}
return repotags, nil
}
func (deployer *Deployer) ListRepotags() (map[string]string, error) {
images, err := deployer.docker.ListImages(docker.ListImagesOptions{All: false})
if err != nil {
return nil, err
}
repotagMap := make(map[string]string)
for _, image := range images {
for _, repotag := range image.RepoTags {
if repotag == "<none>:<none>" {
continue
}
repotagMap[repotag] = image.ID
}
}
return repotagMap, nil
}
func (deployer *Deployer) repoTimerWorker(period time.Duration) {
tick := time.NewTicker(period)
for {
deployer.repoUpdate <- "*" // Update all.
<-tick.C
}
}
func (deployer *Deployer) repoUpdateWorker() {
for repo := range deployer.repoUpdate {
_ = deployer.ImageUpdateRepo(repo)
deployer.StopStaleContainers()
}
}
func (deployer *Deployer) ImageUpdateRepo(repo string) error {
repotags, err := deployer.FindRepoTags(repo)
if err != nil {
return err
}
return deployer.PullImages(repotags)
}
func (deployer *Deployer) PullImages(repotags []string) error {
var ret error
for _, repotag := range repotags {
if err := deployer.PullImage(repotag); err != nil {
ret = err
}
}
return ret
}
func (deployer *Deployer) PullImage(repotag string) error {
repo, tag := splitTwo(repotag, ":")
if tag == "" {
return ErrNoTag
}
opts := docker.PullImageOptions{
Repository: repo,
Registry: deployer.registry,
Tag: tag,
}
log.Println("Pulling image", repotag)
err := deployer.docker.PullImage(opts, deployer.auth)
if err != nil {
log.Println("Pull image error", repotag, err)
}
return err
}