Skip to content

Commit 537abf5

Browse files
authored
Merge pull request #490 from netlify/golang-al2
feat: upload go binaries as al2-compatible zip
2 parents a22226f + c15e6db commit 537abf5

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed
9.03 MB
Binary file not shown.

go/porcelain/deploy.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"io/ioutil"
1616
"os"
1717
"path/filepath"
18+
"runtime"
1819
"strconv"
1920
"strings"
2021
"sync"
@@ -31,8 +32,9 @@ import (
3132
)
3233

3334
const (
34-
jsRuntime = "js"
35-
goRuntime = "go"
35+
jsRuntime = "js"
36+
goRuntime = "go"
37+
amazonLinux2 = "provided.al2"
3638

3739
preProcessingTimeout = time.Minute * 5
3840

@@ -933,7 +935,7 @@ func jsFile(i os.FileInfo) bool {
933935
func goFile(filePath string, i os.FileInfo, observer DeployObserver) bool {
934936
warner, hasWarner := observer.(DeployWarner)
935937

936-
if m := i.Mode(); m&0111 == 0 { // check if it's an executable file
938+
if m := i.Mode(); m&0111 == 0 && runtime.GOOS != "windows" { // check if it's an executable file. skip on windows, since it doesn't have that mode
937939
if hasWarner {
938940
warner.OnWalkWarning(filePath, "Go binary does not have executable permissions")
939941
}
@@ -980,6 +982,13 @@ func createHeader(archive *zip.Writer, i os.FileInfo, runtime string) (io.Writer
980982
Name: i.Name(),
981983
Method: zip.Deflate,
982984
})
985+
} else if runtime == amazonLinux2 {
986+
return archive.CreateHeader(&zip.FileHeader{
987+
CreatorVersion: 3 << 8, // indicates Unix
988+
ExternalAttrs: 0777 << 16, // -rwxrwxrwx file permissions
989+
Name: "bootstrap",
990+
Method: zip.Deflate,
991+
})
983992
}
984993
return archive.Create(i.Name())
985994
}

go/porcelain/deploy_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,24 +612,27 @@ func TestBundle(t *testing.T) {
612612
functions, schedules, functionsConfig, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})
613613

614614
assert.Nil(t, err)
615-
assert.Equal(t, 3, len(functions.Files))
615+
assert.Equal(t, 4, len(functions.Files))
616616
assert.Empty(t, schedules)
617617
assert.Nil(t, functionsConfig)
618618

619619
jsFunction := functions.Files["hello-js-function-test"]
620620
pyFunction := functions.Files["hello-py-function-test"]
621621
rsFunction := functions.Files["hello-rs-function-test"]
622+
goFunction := functions.Files["hello-go-binary-function"]
622623

623624
assert.Equal(t, "js", jsFunction.Runtime)
624625
assert.Equal(t, "py", pyFunction.Runtime)
625626
assert.Equal(t, "rs", rsFunction.Runtime)
627+
assert.Equal(t, "go", goFunction.Runtime)
626628
}
627629

628630
func TestBundleWithManifest(t *testing.T) {
629631
cwd, _ := os.Getwd()
630632
basePath := path.Join(filepath.Dir(cwd), "internal", "data")
631633
jsFunctionPath := strings.Replace(filepath.Join(basePath, "hello-js-function-test.zip"), "\\", "/", -1)
632634
pyFunctionPath := strings.Replace(filepath.Join(basePath, "hello-py-function-test.zip"), "\\", "/", -1)
635+
goFunctionPath := strings.Replace(filepath.Join(basePath, "hello-go-binary-function"), "\\", "/", -1)
633636
manifestPath := path.Join(basePath, "manifest.json")
634637
manifestFile := fmt.Sprintf(`{
635638
"functions": [
@@ -660,10 +663,16 @@ func TestBundleWithManifest(t *testing.T) {
660663
"mainFile": "/some/path/hello-py-function-test",
661664
"name": "hello-py-function-test",
662665
"invocationMode": "stream"
666+
},
667+
{
668+
"path": "%s",
669+
"runtime": "go",
670+
"runtimeVersion": "provided.al2",
671+
"name": "hello-go-binary-function"
663672
}
664673
],
665674
"version": 1
666-
}`, jsFunctionPath, pyFunctionPath)
675+
}`, jsFunctionPath, pyFunctionPath, goFunctionPath)
667676

668677
err := ioutil.WriteFile(manifestPath, []byte(manifestFile), 0644)
669678
defer os.Remove(manifestPath)
@@ -676,11 +685,13 @@ func TestBundleWithManifest(t *testing.T) {
676685
assert.Equal(t, "hello-js-function-test", schedules[0].Name)
677686
assert.Equal(t, "* * * * *", schedules[0].Cron)
678687

679-
assert.Equal(t, 2, len(functions.Files))
688+
assert.Equal(t, 3, len(functions.Files))
680689
assert.Equal(t, "a-runtime", functions.Files["hello-js-function-test"].Runtime)
681690
assert.Empty(t, functions.Files["hello-js-function-test"].FunctionMetadata.InvocationMode)
682691
assert.Equal(t, "some-other-runtime", functions.Files["hello-py-function-test"].Runtime)
683692
assert.Equal(t, "stream", functions.Files["hello-py-function-test"].FunctionMetadata.InvocationMode)
693+
assert.Equal(t, "provided.al2", functions.Files["hello-go-binary-function"].Runtime)
694+
assert.Empty(t, functions.Files["hello-go-binary-function"].FunctionMetadata.InvocationMode)
684695

685696
helloJSConfig := functionsConfig["hello-js-function-test"]
686697

0 commit comments

Comments
 (0)