@@ -14,6 +14,7 @@ import (
1414 "io"
1515 "io/ioutil"
1616 "strings"
17+ "sync"
1718 "time"
1819)
1920
@@ -40,18 +41,21 @@ type ImageSrv interface {
4041
4142type imageService struct {
4243 docker docker.DockerService
44+ lock sync.Mutex
4345}
4446
4547var _ ImageSrv = (* imageService )(nil )
4648
4749func newImage (srv * service ) * imageService {
4850 return & imageService {
4951 docker : srv .docker ,
52+ lock : sync.Mutex {},
5053 }
5154}
5255
5356// Build build an image and push it to registry
54- func (i imageService ) Build (ctx context.Context , dockerfile string , tags []string , content io.Reader ) (io.ReadCloser , error ) {
57+ func (i * imageService ) Build (ctx context.Context , dockerfile string , tags []string , content io.Reader ) (io.ReadCloser , error ) {
58+
5559 imageOptions := types.ImageBuildOptions {
5660 Dockerfile : dockerfile ,
5761 Tags : tags ,
@@ -65,6 +69,8 @@ func (i imageService) Build(ctx context.Context, dockerfile string, tags []strin
6569 return nil , errors .New ("content is nil" )
6670 }
6771
72+ // lock
73+ i .lock .Lock ()
6874 resp , err := i .docker .ImageBuild (ctx , content , imageOptions )
6975 if err != nil {
7076 log .Errorf ("build image failed: %v" , err )
@@ -80,6 +86,10 @@ func (i imageService) Build(ctx context.Context, dockerfile string, tags []strin
8086 }
8187 time .Sleep (1 * time .Second )
8288 }
89+
90+ // release lock
91+ i .lock .Unlock ()
92+
8393 log .Infof ("build image %s complete" , tags [0 ])
8494 log .Infof ("ready push" )
8595
@@ -128,7 +138,7 @@ func (i imageService) Build(ctx context.Context, dockerfile string, tags []strin
128138}
129139
130140// Create pull a image
131- func (i imageService ) Create (ctx context.Context , fromImage string ) (io.ReadCloser , error ) {
141+ func (i * imageService ) Create (ctx context.Context , fromImage string ) (io.ReadCloser , error ) {
132142 resp , err := i .docker .ImagePull (ctx , fromImage , types.ImagePullOptions {})
133143 if err != nil {
134144 log .Errorf ("pull image failed: %v" , err )
@@ -140,7 +150,7 @@ func (i imageService) Create(ctx context.Context, fromImage string) (io.ReadClos
140150}
141151
142152// Inspect inspect image information
143- func (i imageService ) Inspect (ctx context.Context , imageID string ) (interface {}, error ) {
153+ func (i * imageService ) Inspect (ctx context.Context , imageID string ) (interface {}, error ) {
144154 if ! strings .HasPrefix (imageID , i .docker .GetServerAddress ()) {
145155 // for chiancode, pull it firstly then inspect
146156 imageID = fmt .Sprintf ("%s/%s/%s" , i .docker .GetServerAddress (), i .docker .GetProjectName (), imageID )
@@ -170,7 +180,7 @@ func (i imageService) Inspect(ctx context.Context, imageID string) (interface{},
170180}
171181
172182// AddTag add a new tag for image
173- func (i imageService ) AddTag (ctx context.Context , imageTag , newTag string ) error {
183+ func (i * imageService ) AddTag (ctx context.Context , imageTag , newTag string ) error {
174184 if err := i .docker .ImageTag (ctx , imageTag , newTag ); err != nil {
175185 log .Errorf ("add tag failed: %v" , err )
176186
@@ -181,7 +191,7 @@ func (i imageService) AddTag(ctx context.Context, imageTag, newTag string) error
181191}
182192
183193// Push push a image
184- func (i imageService ) Push (ctx context.Context , imageTag string ) (io.ReadCloser , error ) {
194+ func (i * imageService ) Push (ctx context.Context , imageTag string ) (io.ReadCloser , error ) {
185195 auth , err := i .docker .RegistryAuth ()
186196 if err != nil {
187197 log .Errorf ("get RegistryAuth failed: %v" , err )
0 commit comments