6
6
package main
7
7
8
8
import (
9
+ "archive/zip"
9
10
"fmt"
10
11
"github.com/jessevdk/go-flags"
11
12
log "github.com/sirupsen/logrus"
@@ -15,13 +16,16 @@ import (
15
16
"math"
16
17
"net/http"
17
18
"os"
19
+ "path"
20
+ "path/filepath"
18
21
"strings"
19
22
"time"
20
23
)
21
24
22
25
const (
23
26
optBootstrap = "/opt/bootstrap"
24
27
runtimeBootstrap = "/var/runtime/bootstrap"
28
+ taskFolder = "/var/task"
25
29
)
26
30
27
31
type options struct {
@@ -128,6 +132,70 @@ func GetenvWithDefault(key string, defaultValue string) string {
128
132
return envValue
129
133
}
130
134
135
+ func DownloadCodeArchive (url string ) {
136
+ // download and unzip code archive, if url is given
137
+ if url == "" {
138
+ return
139
+ }
140
+ log .Infoln ("Downloading code archive" )
141
+ // create tmp directory
142
+ tmpDir := os .TempDir ()
143
+ // download code archive into tmp directory
144
+ res , err := http .Get (url )
145
+ if err != nil {
146
+ log .Fatal (err )
147
+ }
148
+ defer res .Body .Close ()
149
+ tmp_file_path := path .Join (tmpDir , "code-archive.zip" )
150
+ tmp_file , err := os .OpenFile (tmp_file_path , os .O_WRONLY | os .O_CREATE , os .ModePerm )
151
+ if err != nil {
152
+ log .Fatal (err )
153
+ }
154
+ _ , err = io .Copy (tmp_file , res .Body )
155
+ if err != nil {
156
+ log .Fatal (err )
157
+ }
158
+ err = tmp_file .Close ()
159
+ if err != nil {
160
+ log .Fatal (err )
161
+ }
162
+ // unzip into /var/task
163
+ log .Infoln ("Unzipping code archive" )
164
+ r , err := zip .OpenReader (tmp_file_path )
165
+ if err != nil {
166
+ log .Fatal (err )
167
+ }
168
+ defer r .Close ()
169
+ for _ , f := range r .File {
170
+ rc , err := f .Open ()
171
+ if err != nil {
172
+ log .Fatal (err )
173
+ }
174
+ target_file_name := path .Join (taskFolder , f .Name )
175
+ if f .FileInfo ().IsDir () {
176
+ err = os .MkdirAll (target_file_name , os .ModePerm )
177
+ if err != nil {
178
+ log .Fatal (err )
179
+ }
180
+ continue
181
+ }
182
+ if err := os .MkdirAll (filepath .Dir (target_file_name ), os .ModePerm ); err != nil {
183
+ panic (err )
184
+ }
185
+ target_file , err := os .OpenFile (target_file_name , os .O_WRONLY | os .O_CREATE , os .ModePerm )
186
+ if err != nil {
187
+ log .Fatal (err )
188
+ }
189
+ _ , err = io .Copy (target_file , rc )
190
+ if err != nil {
191
+ log .Fatal (err )
192
+ }
193
+ target_file .Close ()
194
+ rc .Close ()
195
+ }
196
+
197
+ }
198
+
131
199
func InitHandler (sandbox Sandbox , functionVersion string , timeout int64 ) (time.Time , time.Time ) {
132
200
additionalFunctionEnvironmentVariables := map [string ]string {}
133
201
0 commit comments