66package main
77
88import (
9+ "archive/zip"
910 "fmt"
1011 "github.com/jessevdk/go-flags"
1112 log "github.com/sirupsen/logrus"
@@ -15,13 +16,16 @@ import (
1516 "math"
1617 "net/http"
1718 "os"
19+ "path"
20+ "path/filepath"
1821 "strings"
1922 "time"
2023)
2124
2225const (
2326 optBootstrap = "/opt/bootstrap"
2427 runtimeBootstrap = "/var/runtime/bootstrap"
28+ taskFolder = "/var/task"
2529)
2630
2731type options struct {
@@ -128,6 +132,70 @@ func GetenvWithDefault(key string, defaultValue string) string {
128132 return envValue
129133}
130134
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+
131199func InitHandler (sandbox Sandbox , functionVersion string , timeout int64 ) (time.Time , time.Time ) {
132200 additionalFunctionEnvironmentVariables := map [string ]string {}
133201
0 commit comments