Skip to content

Add download / unzip of code archive #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions cmd/localstack/awsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package main

import (
"archive/zip"
"fmt"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
Expand All @@ -15,13 +16,16 @@ import (
"math"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
)

const (
optBootstrap = "/opt/bootstrap"
runtimeBootstrap = "/var/runtime/bootstrap"
taskFolder = "/var/task"
)

type options struct {
Expand Down Expand Up @@ -128,6 +132,70 @@ func GetenvWithDefault(key string, defaultValue string) string {
return envValue
}

func DownloadCodeArchive(url string) {
// download and unzip code archive, if url is given
if url == "" {
return
}
log.Infoln("Downloading code archive")
// create tmp directory
tmpDir := os.TempDir()
// download code archive into tmp directory
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
tmp_file_path := path.Join(tmpDir, "code-archive.zip")
tmp_file, err := os.OpenFile(tmp_file_path, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(tmp_file, res.Body)
if err != nil {
log.Fatal(err)
}
err = tmp_file.Close()
if err != nil {
log.Fatal(err)
}
// unzip into /var/task
log.Infoln("Unzipping code archive")
r, err := zip.OpenReader(tmp_file_path)
if err != nil {
log.Fatal(err)
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
target_file_name := path.Join(taskFolder, f.Name)
if f.FileInfo().IsDir() {
err = os.MkdirAll(target_file_name, os.ModePerm)
if err != nil {
log.Fatal(err)
}
continue
}
if err := os.MkdirAll(filepath.Dir(target_file_name), os.ModePerm); err != nil {
panic(err)
}
target_file, err := os.OpenFile(target_file_name, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(target_file, rc)
if err != nil {
log.Fatal(err)
}
target_file.Close()
rc.Close()
}

}

func InitHandler(sandbox Sandbox, functionVersion string, timeout int64) (time.Time, time.Time) {
additionalFunctionEnvironmentVariables := map[string]string{}

Expand Down
6 changes: 5 additions & 1 deletion cmd/localstack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type LsOpts struct {
RuntimeEndpoint string
RuntimeId string
InitTracingPort string
CodeDownloadUrl string
}

func GetEnvOrDie(env string) string {
Expand All @@ -33,6 +34,8 @@ func InitLsOpts() *LsOpts {
// optional with default
InteropPort: GetenvWithDefault("LOCALSTACK_INTEROP_PORT", "9563"),
InitTracingPort: GetenvWithDefault("LOCALSTACK_RUNTIME_TRACING_PORT", "9564"),
// optional or empty
CodeDownloadUrl: os.Getenv("LOCALSTACK_CODE_ARCHIVE_DOWNLOAD_URL"),
}
}

Expand All @@ -47,7 +50,8 @@ func main() {
//log.SetLevel(log.TraceLevel)
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)

// download code archive if env variable is set
DownloadCodeArchive(lsOpts.CodeDownloadUrl)
// parse CLI args
opts, args := getCLIArgs()
bootstrap, handler := getBootstrap(args, opts)
Expand Down