Skip to content

Commit 2a68595

Browse files
authored
Add download / unzip of code archive (#4)
1 parent 77fb282 commit 2a68595

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

cmd/localstack/awsutil.go

+68
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package main
77

88
import (
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

2225
const (
2326
optBootstrap = "/opt/bootstrap"
2427
runtimeBootstrap = "/var/runtime/bootstrap"
28+
taskFolder = "/var/task"
2529
)
2630

2731
type 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+
131199
func InitHandler(sandbox Sandbox, functionVersion string, timeout int64) (time.Time, time.Time) {
132200
additionalFunctionEnvironmentVariables := map[string]string{}
133201

cmd/localstack/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type LsOpts struct {
1616
RuntimeEndpoint string
1717
RuntimeId string
1818
InitTracingPort string
19+
CodeDownloadUrl string
1920
}
2021

2122
func GetEnvOrDie(env string) string {
@@ -33,6 +34,8 @@ func InitLsOpts() *LsOpts {
3334
// optional with default
3435
InteropPort: GetenvWithDefault("LOCALSTACK_INTEROP_PORT", "9563"),
3536
InitTracingPort: GetenvWithDefault("LOCALSTACK_RUNTIME_TRACING_PORT", "9564"),
37+
// optional or empty
38+
CodeDownloadUrl: os.Getenv("LOCALSTACK_CODE_ARCHIVE_DOWNLOAD_URL"),
3639
}
3740
}
3841

@@ -47,7 +50,8 @@ func main() {
4750
//log.SetLevel(log.TraceLevel)
4851
log.SetLevel(log.DebugLevel)
4952
log.SetReportCaller(true)
50-
53+
// download code archive if env variable is set
54+
DownloadCodeArchive(lsOpts.CodeDownloadUrl)
5155
// parse CLI args
5256
opts, args := getCLIArgs()
5357
bootstrap, handler := getBootstrap(args, opts)

0 commit comments

Comments
 (0)