4
4
package builder
5
5
6
6
import (
7
+ "encoding/json"
7
8
"fmt"
9
+ "io"
8
10
"io/ioutil"
11
+ "log"
12
+ "net/http"
9
13
"os"
10
14
"path"
11
15
"path/filepath"
@@ -23,7 +27,7 @@ const AdditionalPackageBuildArg = "ADDITIONAL_PACKAGE"
23
27
24
28
// BuildImage construct Docker image from function parameters
25
29
// TODO: refactor signature to a struct to simplify the length of the method header
26
- func BuildImage (image string , handler string , functionName string , language string , nocache bool , squash bool , shrinkwrap bool , buildArgMap map [string ]string , buildOptions []string , tagMode schema.BuildFormat , buildLabelMap map [string ]string , quietBuild bool , copyExtraPaths []string ) error {
30
+ func BuildImage (image string , handler string , functionName string , language string , nocache bool , squash bool , shrinkwrap bool , buildArgMap map [string ]string , buildOptions []string , tagMode schema.BuildFormat , buildLabelMap map [string ]string , quietBuild bool , copyExtraPaths []string , remoteBuilder , payloadSecretPath string ) error {
27
31
28
32
if stack .IsValidTemplate (language ) {
29
33
pathToTemplateYAML := fmt .Sprintf ("./template/%s/template.yml" , language )
@@ -63,50 +67,91 @@ func BuildImage(image string, handler string, functionName string, language stri
63
67
return nil
64
68
}
65
69
66
- buildOptPackages , err := getBuildOptionPackages (buildOptions , language , langTemplate .BuildOptions )
67
- if err != nil {
68
- return err
70
+ if remoteBuilder != "" {
71
+ tempDir , err := os .MkdirTemp (os .TempDir (), "builder-*" )
72
+ if err != nil {
73
+ return fmt .Errorf ("failed to create temporary directory for %s, error: %w" , functionName , err )
74
+ }
75
+ defer os .RemoveAll (tempDir )
69
76
70
- }
77
+ tarPath := path . Join ( tempDir , "req.tar" )
71
78
72
- dockerBuildVal := dockerBuild {
73
- Image : imageName ,
74
- NoCache : nocache ,
75
- Squash : squash ,
76
- HTTPProxy : os .Getenv ("http_proxy" ),
77
- HTTPSProxy : os .Getenv ("https_proxy" ),
78
- BuildArgMap : buildArgMap ,
79
- BuildOptPackages : buildOptPackages ,
80
- BuildLabelMap : buildLabelMap ,
81
- }
79
+ if err := makeTar (builderConfig {Image : imageName , BuildArgs : buildArgMap }, path .Join ("build" , functionName ), tarPath ); err != nil {
80
+ return fmt .Errorf ("failed to create tar file for %s, error: %w" , functionName , err )
81
+ }
82
82
83
- command , args := getDockerBuildCommand (dockerBuildVal )
83
+ res , err := callBuilder (tarPath , tempPath , remoteBuilder , functionName , payloadSecretPath )
84
+ if err != nil {
85
+ return err
86
+ }
87
+ defer res .Body .Close ()
84
88
85
- envs := os .Environ ()
86
- if mountSSH {
87
- envs = append (envs , "DOCKER_BUILDKIT=1" )
88
- }
89
+ data , _ := io .ReadAll (res .Body )
89
90
90
- task := v1execute.ExecTask {
91
- Cwd : tempPath ,
92
- Command : command ,
93
- Args : args ,
94
- StreamStdio : ! quietBuild ,
95
- Env : envs ,
96
- }
91
+ result := builderResult {}
92
+ if err := json .Unmarshal (data , & result ); err != nil {
93
+ return err
94
+ }
97
95
98
- res , err := task .Execute ()
96
+ if ! quietBuild {
97
+ for _ , logMsg := range result .Log {
98
+ fmt .Printf ("%s\n " , logMsg )
99
+ }
100
+ }
99
101
100
- if err != nil {
101
- return err
102
- }
102
+ if res .StatusCode != http .StatusOK && res .StatusCode != http .StatusAccepted {
103
+ fmt .Println (res .StatusCode )
104
+ return fmt .Errorf ("%s failure while building or pushing image %s: %s" , functionName , imageName , result .Status )
105
+ }
103
106
104
- if res .ExitCode != 0 {
105
- return fmt .Errorf ("[%s] received non-zero exit code from build, error: %s" , functionName , res .Stderr )
106
- }
107
+ log .Printf ("%s success building and pushing image: %s" , functionName , result .Image )
107
108
108
- fmt . Printf ( "Image: %s built. \n " , imageName )
109
+ } else {
109
110
111
+ buildOptPackages , err := getBuildOptionPackages (buildOptions , language , langTemplate .BuildOptions )
112
+ if err != nil {
113
+ return err
114
+
115
+ }
116
+
117
+ dockerBuildVal := dockerBuild {
118
+ Image : imageName ,
119
+ NoCache : nocache ,
120
+ Squash : squash ,
121
+ HTTPProxy : os .Getenv ("http_proxy" ),
122
+ HTTPSProxy : os .Getenv ("https_proxy" ),
123
+ BuildArgMap : buildArgMap ,
124
+ BuildOptPackages : buildOptPackages ,
125
+ BuildLabelMap : buildLabelMap ,
126
+ }
127
+
128
+ command , args := getDockerBuildCommand (dockerBuildVal )
129
+
130
+ envs := os .Environ ()
131
+ if mountSSH {
132
+ envs = append (envs , "DOCKER_BUILDKIT=1" )
133
+ }
134
+
135
+ task := v1execute.ExecTask {
136
+ Cwd : tempPath ,
137
+ Command : command ,
138
+ Args : args ,
139
+ StreamStdio : ! quietBuild ,
140
+ Env : envs ,
141
+ }
142
+
143
+ res , err := task .Execute ()
144
+
145
+ if err != nil {
146
+ return err
147
+ }
148
+
149
+ if res .ExitCode != 0 {
150
+ return fmt .Errorf ("[%s] received non-zero exit code from build, error: %s" , functionName , res .Stderr )
151
+ }
152
+
153
+ fmt .Printf ("Image: %s built.\n " , imageName )
154
+ }
110
155
} else {
111
156
return fmt .Errorf ("language template: %s not supported, build a custom Dockerfile" , language )
112
157
}
0 commit comments