@@ -7,15 +7,16 @@ SPDX-License-Identifier: Apache-2.0
77package util
88
99import (
10- "bytes "
10+ "context "
1111 "fmt"
1212 "io"
1313 "runtime"
1414 "strings"
1515
16- docker "github.com/fsouza/go-dockerclient"
1716 "github.com/hyperledger/fabric-lib-go/common/flogging"
1817 "github.com/hyperledger/fabric/common/metadata"
18+ dcontainer "github.com/moby/moby/api/types/container"
19+ dcli "github.com/moby/moby/client"
1920 "github.com/spf13/viper"
2021)
2122
@@ -57,7 +58,7 @@ func (dbo DockerBuildOptions) String() string {
5758// after successful execution of Cmd.
5859//
5960// -------------------------------------------------------------------------------------------
60- func DockerBuild (opts DockerBuildOptions , client * docker. Client ) error {
61+ func DockerBuild (opts DockerBuildOptions , client dcli. APIClient ) error {
6162 if opts .Image == "" {
6263 opts .Image = GetDockerImageFromConfig ("chaincode.builder" )
6364 if opts .Image == "" {
@@ -67,105 +68,102 @@ func DockerBuild(opts DockerBuildOptions, client *docker.Client) error {
6768
6869 logger .Debugf ("Attempting build with options: %s" , opts )
6970
70- //-----------------------------------------------------------------------------------
71+ // -----------------------------------------------------------------------------------
7172 // Ensure the image exists locally, or pull it from a registry if it doesn't
72- //-----------------------------------------------------------------------------------
73- _ , err := client .InspectImage ( opts .Image )
73+ // -----------------------------------------------------------------------------------
74+ _ , err := client .ImageInspect ( context . Background (), opts .Image )
7475 if err != nil {
7576 logger .Debugf ("Image %s does not exist locally, attempt pull" , opts .Image )
7677
77- err = client .PullImage (docker. PullImageOptions { Repository : opts .Image }, docker. AuthConfiguration {})
78+ _ , err = client .ImagePull ( context . Background (), opts .Image , dcli. ImagePullOptions {})
7879 if err != nil {
7980 return fmt .Errorf ("Failed to pull %s: %s" , opts .Image , err )
8081 }
8182 }
8283
83- //-----------------------------------------------------------------------------------
84+ // -----------------------------------------------------------------------------------
8485 // Create an ephemeral container, armed with our Image/Cmd
85- //-----------------------------------------------------------------------------------
86- container , err := client .CreateContainer (docker.CreateContainerOptions {
87- Config : & docker.Config {
88- Image : opts .Image ,
89- Cmd : []string {"/bin/sh" , "-c" , opts .Cmd },
90- Env : opts .Env ,
86+ // -----------------------------------------------------------------------------------
87+ container , err := client .ContainerCreate (context .Background (), dcli.ContainerCreateOptions {
88+ Config : & dcontainer.Config {
9189 AttachStdout : true ,
9290 AttachStderr : true ,
91+ Env : opts .Env ,
92+ Cmd : []string {"/bin/sh" , "-c" , opts .Cmd },
93+ Image : opts .Image ,
9394 },
9495 })
9596 if err != nil {
9697 return fmt .Errorf ("Error creating container: %s" , err )
9798 }
98- defer client .RemoveContainer (docker. RemoveContainerOptions { ID : container .ID })
99+ defer client .ContainerRemove ( context . Background (), container .ID , dcli. ContainerRemoveOptions { })
99100
100- //-----------------------------------------------------------------------------------
101+ // -----------------------------------------------------------------------------------
101102 // Upload our input stream
102- //-----------------------------------------------------------------------------------
103- err = client .UploadToContainer (container .ID , docker.UploadToContainerOptions {
104- Path : "/chaincode/input" ,
105- InputStream : opts .InputStream ,
103+ // -----------------------------------------------------------------------------------
104+ _ , err = client .CopyToContainer (context .Background (), container .ID , dcli.CopyToContainerOptions {
105+ DestinationPath : "/chaincode/input" ,
106+ Content : opts .InputStream ,
107+ AllowOverwriteDirWithFile : true ,
106108 })
107109 if err != nil {
108110 return fmt .Errorf ("Error uploading input to container: %s" , err )
109111 }
110112
111- //-----------------------------------------------------------------------------------
113+ // -----------------------------------------------------------------------------------
112114 // Attach stdout buffer to capture possible compilation errors
113- //-----------------------------------------------------------------------------------
114- stdout := bytes .NewBuffer (nil )
115- cw , err := client .AttachToContainerNonBlocking (docker.AttachToContainerOptions {
116- Container : container .ID ,
117- OutputStream : stdout ,
118- ErrorStream : stdout ,
119- Logs : true ,
120- Stdout : true ,
121- Stderr : true ,
122- Stream : true ,
115+ // -----------------------------------------------------------------------------------
116+ cw , err := client .ContainerAttach (context .Background (), container .ID , dcli.ContainerAttachOptions {
117+ Stream : true ,
118+ Stdout : true ,
119+ Stderr : true ,
120+ Logs : true ,
123121 })
124122 if err != nil {
125123 return fmt .Errorf ("Error attaching to container: %s" , err )
126124 }
127125
128- //-----------------------------------------------------------------------------------
126+ // -----------------------------------------------------------------------------------
129127 // Launch the actual build, realizing the Env/Cmd specified at container creation
130- //-----------------------------------------------------------------------------------
131- err = client .StartContainer ( container .ID , nil )
128+ // -----------------------------------------------------------------------------------
129+ _ , err = client .ContainerStart ( context . Background (), container .ID , dcli. ContainerStartOptions {} )
132130 if err != nil {
131+ buff , _ := io .ReadAll (cw .Reader )
133132 cw .Close ()
134- return fmt .Errorf ("Error executing build: %s \" %s\" " , err , stdout . String ( ))
133+ return fmt .Errorf ("Error executing build: %s \" %s\" " , err , string ( buff ))
135134 }
136135
137- //-----------------------------------------------------------------------------------
136+ // -----------------------------------------------------------------------------------
138137 // Wait for the build to complete and gather the return value
139- //-----------------------------------------------------------------------------------
140- retval , err := client .WaitContainer (container .ID )
141- if err != nil {
138+ // -----------------------------------------------------------------------------------
139+ resWait := client .ContainerWait (context .Background (), container .ID , dcli.ContainerWaitOptions {})
140+ var res dcontainer.WaitResponse
141+ select {
142+ case res = <- resWait .Result :
143+ case err = <- resWait .Error :
142144 cw .Close ()
143145 return fmt .Errorf ("Error waiting for container to complete: %s" , err )
144146 }
145147
146148 // Wait for stream copying to complete before accessing stdout.
147- cw .Close ()
148- if err := cw .Wait (); err != nil {
149- logger .Errorf ("attach wait failed: %s" , err )
150- }
151-
152- if retval > 0 {
149+ defer cw .Close ()
150+ buff , _ := io .ReadAll (cw .Reader )
151+ if res .StatusCode > 0 {
153152 logger .Errorf ("Docker build failed using options: %s" , opts )
154- return fmt .Errorf ("Error returned from build: %d \" %s\" " , retval , stdout . String ( ))
153+ return fmt .Errorf ("Error returned from build: %d \" %s\" " , res . StatusCode , string ( buff ))
155154 }
156155
157- logger .Debugf ("Build output is %s" , stdout . String ( ))
156+ logger .Debugf ("Build output is %s" , string ( buff ))
158157
159- //-----------------------------------------------------------------------------------
158+ // -----------------------------------------------------------------------------------
160159 // Finally, download the result
161- //-----------------------------------------------------------------------------------
162- err = client .DownloadFromContainer (container .ID , docker.DownloadFromContainerOptions {
163- Path : "/chaincode/output/." ,
164- OutputStream : opts .OutputStream ,
165- })
160+ // -----------------------------------------------------------------------------------
161+ resCont , err := client .CopyFromContainer (context .Background (), container .ID , dcli.CopyFromContainerOptions {SourcePath : "/chaincode/output/." })
166162 if err != nil {
167163 return fmt .Errorf ("Error downloading output: %s" , err )
168164 }
165+ defer resCont .Content .Close ()
166+ io .Copy (opts .OutputStream , resCont .Content )
169167
170168 return nil
171169}
0 commit comments