22package container
33
44import (
5- "archive/tar"
6- "bytes"
75 "context"
86 "crypto/sha256"
97 "fmt"
10- "io"
118 "os"
12- "os/exec"
139 "path/filepath"
1410 "strings"
15- "time"
1611
17- "github.com/docker/docker/api/types/container"
18- "github.com/docker/docker/client"
1912 "github.com/sirupsen/logrus"
2013
2114 "github.com/celestiaorg/knuu/pkg/builder"
@@ -26,24 +19,18 @@ type BuilderFactory struct {
2619 imageNameFrom string
2720 imageNameTo string
2821 imageBuilder builder.Builder
29- cli * client.Client
3022 dockerFileInstructions []string
3123 buildContext string
3224}
3325
3426// NewBuilderFactory creates a new instance of BuilderFactory.
3527func NewBuilderFactory (imageName , buildContext string , imageBuilder builder.Builder ) (* BuilderFactory , error ) {
36- cli , err := client .NewClientWithOpts (client .FromEnv , client .WithAPIVersionNegotiation ())
37- if err != nil {
38- return nil , ErrCreatingDockerClient .Wrap (err )
39- }
40- err = os .MkdirAll (buildContext , 0755 )
41- if err != nil {
28+ if err := os .MkdirAll (buildContext , 0755 ); err != nil {
4229 return nil , ErrFailedToCreateContextDir .Wrap (err )
4330 }
31+
4432 return & BuilderFactory {
4533 imageNameFrom : imageName ,
46- cli : cli ,
4734 dockerFileInstructions : []string {"FROM " + imageName },
4835 buildContext : buildContext ,
4936 imageBuilder : imageBuilder ,
@@ -55,104 +42,24 @@ func (f *BuilderFactory) ImageNameFrom() string {
5542 return f .imageNameFrom
5643}
5744
58- // ExecuteCmdInBuilder runs the provided command in the context of the given builder.
59- // It returns the command's output or any error encountered.
60- func (f * BuilderFactory ) ExecuteCmdInBuilder (command []string ) (string , error ) {
45+ // AddCmdToBuilder adds the provided command to be run in the context of the builder.
46+ func (f * BuilderFactory ) AddCmdToBuilder (command []string ) {
6147 f .dockerFileInstructions = append (f .dockerFileInstructions , "RUN " + strings .Join (command , " " ))
62- // FIXME: does not return expected output
63- return "" , nil
6448}
6549
6650// AddToBuilder adds a file from the source path to the destination path in the image, with the specified ownership.
67- func (f * BuilderFactory ) AddToBuilder (srcPath , destPath , chown string ) error {
51+ func (f * BuilderFactory ) AddToBuilder (srcPath , destPath , chown string ) {
6852 f .dockerFileInstructions = append (f .dockerFileInstructions , "ADD --chown=" + chown + " " + srcPath + " " + destPath )
69- return nil
70- }
71-
72- // ReadFileFromBuilder reads a file from the given builder's mount point.
73- // It returns the file's content or any error encountered.
74- func (f * BuilderFactory ) ReadFileFromBuilder (filePath string ) ([]byte , error ) {
75- if f .imageNameTo == "" {
76- return nil , ErrNoImageNameProvided
77- }
78- containerConfig := & container.Config {
79- Image : f .imageNameTo ,
80- Cmd : []string {"tail" , "-f" , "/dev/null" }, // This keeps the container running
81- }
82- resp , err := f .cli .ContainerCreate (
83- context .Background (),
84- containerConfig ,
85- nil ,
86- nil ,
87- nil ,
88- "" ,
89- )
90- if err != nil {
91- return nil , ErrFailedToCreateContainer .Wrap (err )
92- }
93-
94- defer func () {
95- // Stop the container
96- timeout := int (time .Duration (10 ) * time .Second )
97- stopOptions := container.StopOptions {
98- Timeout : & timeout ,
99- }
100-
101- if err := f .cli .ContainerStop (context .Background (), resp .ID , stopOptions ); err != nil {
102- logrus .Warn (ErrFailedToStopContainer .Wrap (err ))
103- }
104-
105- // Remove the container
106- if err := f .cli .ContainerRemove (context .Background (), resp .ID , container.RemoveOptions {}); err != nil {
107- logrus .Warn (ErrFailedToRemoveContainer .Wrap (err ))
108- }
109- }()
110-
111- if err := f .cli .ContainerStart (context .Background (), resp .ID , container.StartOptions {}); err != nil {
112- return nil , ErrFailedToStartContainer .Wrap (err )
113- }
114-
115- // Now you can copy the file
116- reader , _ , err := f .cli .CopyFromContainer (context .Background (), resp .ID , filePath )
117- if err != nil {
118- return nil , ErrFailedToCopyFileFromContainer .Wrap (err )
119- }
120- defer reader .Close ()
121-
122- tarReader := tar .NewReader (reader )
123-
124- for {
125- header , err := tarReader .Next ()
126-
127- if err == io .EOF {
128- break // End of archive
129- }
130- if err != nil {
131- return nil , ErrFailedToReadFromTar .Wrap (err )
132- }
133-
134- if header .Typeflag == tar .TypeReg { // if it's a file then extract it
135- data , err := io .ReadAll (tarReader )
136- if err != nil {
137- return nil , ErrFailedToReadFileFromTar .Wrap (err )
138- }
139- return data , nil
140- }
141- }
142-
143- return nil , ErrFileNotFoundInTar
14453}
14554
14655// SetEnvVar sets the value of an environment variable in the builder.
147- func (f * BuilderFactory ) SetEnvVar (name , value string ) error {
56+ func (f * BuilderFactory ) SetEnvVar (name , value string ) {
14857 f .dockerFileInstructions = append (f .dockerFileInstructions , "ENV " + name + "=" + value )
149- return nil
15058}
15159
15260// SetUser sets the user in the builder.
153- func (f * BuilderFactory ) SetUser (user string ) error {
61+ func (f * BuilderFactory ) SetUser (user string ) {
15462 f .dockerFileInstructions = append (f .dockerFileInstructions , "USER " + user )
155- return nil
15663}
15764
15865// Changed returns true if the builder has been modified, false otherwise.
@@ -178,6 +85,7 @@ func (f *BuilderFactory) PushBuilderImage(ctx context.Context, imageName string)
17885 return ErrFailedToCreateContextDir .Wrap (err )
17986 }
18087 }
88+
18189 dockerFile := strings .Join (f .dockerFileInstructions , "\n " )
18290 err := os .WriteFile (dockerFilePath , []byte (dockerFile ), 0644 )
18391 if err != nil {
@@ -240,17 +148,6 @@ func (f *BuilderFactory) BuildImageFromGitRepo(ctx context.Context, gitCtx build
240148 return err
241149}
242150
243- func runCommand (cmd * exec.Cmd ) error { // nolint: unused
244- var stdout , stderr bytes.Buffer
245- cmd .Stdout = & stdout
246- cmd .Stderr = & stderr
247- err := cmd .Run ()
248- if err != nil {
249- return fmt .Errorf ("command failed: %w\n stdout: %s\n stderr: %s" , err , stdout .String (), stderr .String ())
250- }
251- return nil
252- }
253-
254151// GenerateImageHash creates a hash value based on the contents of the Dockerfile instructions and all files in the build context.
255152func (f * BuilderFactory ) GenerateImageHash () (string , error ) {
256153 hasher := sha256 .New ()
0 commit comments