forked from vmware-archive/atc
/
container_fetch_source.go
87 lines (73 loc) · 2.01 KB
/
container_fetch_source.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
package resource
import (
"os"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/worker"
)
type containerFetchSource struct {
logger lager.Logger
container worker.Container
volume worker.Volume
resourceOptions ResourceOptions
versionedSource VersionedSource
}
func NewContainerFetchSource(
logger lager.Logger,
container worker.Container,
volume worker.Volume,
resourceOptions ResourceOptions,
) FetchSource {
return &containerFetchSource{
logger: logger,
container: container,
volume: volume,
versionedSource: NewGetVersionedSource(volume, resourceOptions.Version(), nil),
resourceOptions: resourceOptions,
}
}
func (s *containerFetchSource) IsInitialized() (bool, error) {
return s.volume.IsInitialized()
}
func (s *containerFetchSource) VersionedSource() VersionedSource {
return s.versionedSource
}
func (s *containerFetchSource) LockName() (string, error) {
return s.resourceOptions.LockName(s.container.WorkerName())
}
// Initialize runs under the lock but we need to make sure volume
// does not exist yet before creating it under the lock
func (s *containerFetchSource) Initialize(signals <-chan os.Signal, ready chan<- struct{}) error {
sLog := s.logger.Session("initialize")
initialized, err := s.volume.IsInitialized()
if err != nil {
sLog.Error("failed-to-check-if-initialized", err)
return err
}
if initialized {
sLog.Debug("already-initialized")
return nil
}
s.versionedSource, err = NewResourceForContainer(s.container).Get(
s.volume,
s.resourceOptions.IOConfig(),
s.resourceOptions.Source(),
s.resourceOptions.Params(),
s.resourceOptions.Version(),
signals,
ready,
)
if err == ErrAborted {
sLog.Error("get-run-resource-aborted", err, lager.Data{"container": s.container.Handle()})
return ErrInterrupted
}
if err != nil {
sLog.Error("failed-to-fetch-resource", err)
return err
}
err = s.volume.Initialize()
if err != nil {
sLog.Error("failed-to-initialize-cache", err)
return err
}
return nil
}