Skip to content

Commit db1cead

Browse files
Generalize code archive downloads (#11)
Co-authored-by: Daniel Fangl <[email protected]>
1 parent f03a1f0 commit db1cead

File tree

3 files changed

+108
-70
lines changed

3 files changed

+108
-70
lines changed

cmd/localstack/awsutil.go

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

88
import (
9-
"archive/zip"
109
"context"
1110
"fmt"
1211
"github.com/jessevdk/go-flags"
@@ -18,7 +17,6 @@ import (
1817
"math"
1918
"net/http"
2019
"os"
21-
"path"
2220
"path/filepath"
2321
"strings"
2422
"time"
@@ -27,7 +25,6 @@ import (
2725
const (
2826
optBootstrap = "/opt/bootstrap"
2927
runtimeBootstrap = "/var/runtime/bootstrap"
30-
taskFolder = "/var/task"
3128
)
3229

3330
type options struct {
@@ -134,70 +131,6 @@ func GetenvWithDefault(key string, defaultValue string) string {
134131
return envValue
135132
}
136133

137-
func DownloadCodeArchive(url string) {
138-
// download and unzip code archive, if url is given
139-
if url == "" {
140-
return
141-
}
142-
log.Infoln("Downloading code archive")
143-
// create tmp directory
144-
tmpDir := os.TempDir()
145-
// download code archive into tmp directory
146-
res, err := http.Get(url)
147-
if err != nil {
148-
log.Fatal(err)
149-
}
150-
defer res.Body.Close()
151-
tmp_file_path := path.Join(tmpDir, "code-archive.zip")
152-
tmp_file, err := os.OpenFile(tmp_file_path, os.O_WRONLY|os.O_CREATE, os.ModePerm)
153-
if err != nil {
154-
log.Fatal(err)
155-
}
156-
_, err = io.Copy(tmp_file, res.Body)
157-
if err != nil {
158-
log.Fatal(err)
159-
}
160-
err = tmp_file.Close()
161-
if err != nil {
162-
log.Fatal(err)
163-
}
164-
// unzip into /var/task
165-
log.Infoln("Unzipping code archive")
166-
r, err := zip.OpenReader(tmp_file_path)
167-
if err != nil {
168-
log.Fatal(err)
169-
}
170-
defer r.Close()
171-
for _, f := range r.File {
172-
rc, err := f.Open()
173-
if err != nil {
174-
log.Fatal(err)
175-
}
176-
target_file_name := path.Join(taskFolder, f.Name)
177-
if f.FileInfo().IsDir() {
178-
err = os.MkdirAll(target_file_name, os.ModePerm)
179-
if err != nil {
180-
log.Fatal(err)
181-
}
182-
continue
183-
}
184-
if err := os.MkdirAll(filepath.Dir(target_file_name), os.ModePerm); err != nil {
185-
panic(err)
186-
}
187-
target_file, err := os.OpenFile(target_file_name, os.O_WRONLY|os.O_CREATE, os.ModePerm)
188-
if err != nil {
189-
log.Fatal(err)
190-
}
191-
_, err = io.Copy(target_file, rc)
192-
if err != nil {
193-
log.Fatal(err)
194-
}
195-
target_file.Close()
196-
rc.Close()
197-
}
198-
199-
}
200-
201134
func resetListener(changeChannel <-chan bool, server *CustomInteropServer) {
202135
for {
203136
_, more := <-changeChannel

cmd/localstack/codearchive.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"archive/zip"
5+
"encoding/json"
6+
log "github.com/sirupsen/logrus"
7+
"io"
8+
"net/http"
9+
"os"
10+
"path"
11+
"path/filepath"
12+
)
13+
14+
type ArchiveDownload struct {
15+
Url string `json:"url"`
16+
TargetPath string `json:"target_path"`
17+
}
18+
19+
func DownloadCodeArchives(archives string) error {
20+
if archives == "" {
21+
log.Debugln("No code archives set. Skipping download.")
22+
return nil
23+
}
24+
var parsedArchives []ArchiveDownload
25+
err := json.Unmarshal([]byte(archives), &parsedArchives)
26+
if err != nil {
27+
return err
28+
}
29+
30+
for _, downloadArchive := range parsedArchives {
31+
if err := DownloadCodeArchive(downloadArchive.Url, downloadArchive.TargetPath); err != nil {
32+
return err
33+
}
34+
}
35+
return nil
36+
}
37+
38+
func DownloadCodeArchive(url string, targetPath string) error {
39+
// download and unzip code archive
40+
log.Infoln("Downloading code archive")
41+
// create tmp directory
42+
// empty string will make use of the default tmp directory
43+
tmpDir, err := os.MkdirTemp("", "localstack-code-archive")
44+
if err != nil {
45+
return err
46+
}
47+
// download code archive into tmp directory
48+
res, err := http.Get(url)
49+
if err != nil {
50+
return err
51+
}
52+
defer res.Body.Close()
53+
tmp_file_path := path.Join(tmpDir, "code-archive.zip")
54+
tmp_file, err := os.OpenFile(tmp_file_path, os.O_WRONLY|os.O_CREATE, os.ModePerm)
55+
if err != nil {
56+
return err
57+
}
58+
_, err = io.Copy(tmp_file, res.Body)
59+
if err != nil {
60+
return err
61+
}
62+
err = tmp_file.Close()
63+
if err != nil {
64+
return err
65+
}
66+
// unzip into targetPath
67+
log.Infoln("Unzipping code archive")
68+
r, err := zip.OpenReader(tmp_file_path)
69+
if err != nil {
70+
return err
71+
}
72+
defer r.Close()
73+
for _, f := range r.File {
74+
rc, err := f.Open()
75+
if err != nil {
76+
return err
77+
}
78+
// TODO: check if exists, otherwise build path
79+
target_file_name := path.Join(targetPath, f.Name)
80+
if f.FileInfo().IsDir() {
81+
err = os.MkdirAll(target_file_name, os.ModePerm)
82+
if err != nil {
83+
return err
84+
}
85+
continue
86+
}
87+
if err := os.MkdirAll(filepath.Dir(target_file_name), os.ModePerm); err != nil {
88+
panic(err)
89+
}
90+
target_file, err := os.OpenFile(target_file_name, os.O_WRONLY|os.O_CREATE, os.ModePerm)
91+
if err != nil {
92+
return err
93+
}
94+
_, err = io.Copy(target_file, rc)
95+
if err != nil {
96+
return err
97+
}
98+
target_file.Close()
99+
rc.Close()
100+
}
101+
return nil
102+
}

cmd/localstack/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type LsOpts struct {
1919
RuntimeEndpoint string
2020
RuntimeId string
2121
InitTracingPort string
22-
CodeDownloadUrl string
22+
CodeArchives string
2323
HotReloadingPaths []string
2424
EnableDnsServer string
2525
LocalstackIP string
@@ -41,7 +41,7 @@ func InitLsOpts() *LsOpts {
4141
InteropPort: GetenvWithDefault("LOCALSTACK_INTEROP_PORT", "9563"),
4242
InitTracingPort: GetenvWithDefault("LOCALSTACK_RUNTIME_TRACING_PORT", "9564"),
4343
// optional or empty
44-
CodeDownloadUrl: os.Getenv("LOCALSTACK_CODE_ARCHIVE_DOWNLOAD_URL"),
44+
CodeArchives: os.Getenv("LOCALSTACK_CODE_ARCHIVES"),
4545
HotReloadingPaths: strings.Split(GetenvWithDefault("LOCALSTACK_HOT_RELOADING_PATHS", ""), ","),
4646
EnableDnsServer: os.Getenv("LOCALSTACK_ENABLE_DNS_SERVER"),
4747
LocalstackIP: os.Getenv("LOCALSTACK_HOSTNAME"),
@@ -59,8 +59,11 @@ func main() {
5959
//log.SetLevel(log.TraceLevel)
6060
log.SetLevel(log.DebugLevel)
6161
log.SetReportCaller(true)
62+
6263
// download code archive if env variable is set
63-
DownloadCodeArchive(lsOpts.CodeDownloadUrl)
64+
if err := DownloadCodeArchives(lsOpts.CodeArchives); err != nil {
65+
log.Fatal("Failed to download code archives")
66+
}
6467
// enable dns server
6568
dnsServerContext, stopDnsServer := context.WithCancel(context.Background())
6669
go RunDNSRewriter(lsOpts, dnsServerContext)

0 commit comments

Comments
 (0)