-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
127 lines (108 loc) · 2.67 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Copyright (C) 2020 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/scud
//
package scud
import (
"crypto/sha256"
"fmt"
"hash"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"time"
"github.com/aws/aws-cdk-go/awscdk/v2"
"github.com/aws/aws-cdk-go/awscdk/v2/awslambda"
"github.com/aws/aws-cdk-go/awscdk/v2/awss3assets"
"github.com/aws/jsii-runtime-go"
)
type Compiler interface {
awscdk.ILocalBundling
SourceCodeModule() string
SourceCodeLambda() string
SourceCodeVersion() string
}
// AssetCodeGo bundles lambda function from source code
func AssetCodeGo(compiler Compiler) awslambda.Code {
hash := hashpkg(compiler)
return awslambda.NewAssetCode(
jsii.String("."),
&awss3assets.AssetOptions{
AssetHashType: awscdk.AssetHashType_CUSTOM,
AssetHash: jsii.String(hash),
Bundling: &awscdk.BundlingOptions{
Image: awscdk.DockerImage_FromRegistry(jsii.String("golang")),
Local: compiler.(awscdk.ILocalBundling),
// Note: it make no sense to build Golang code inside container
},
})
}
func hashpkg(compiler Compiler) string {
vsn := ""
if compiler.SourceCodeVersion() != "" {
vsn = fmt.Sprintf("@%s", compiler.SourceCodeVersion())
}
pkg := fmt.Sprintf("package: %s %s%s", compiler.SourceCodeModule(), compiler.SourceCodeLambda(), vsn)
path := filepath.Join(compiler.SourceCodeModule(), compiler.SourceCodeLambda())
t := time.Now()
hash := sha256.New()
_, err := hash.Write([]byte(pkg))
if err != nil {
panic(err)
}
exp, err := regexp.Compile(`(.*\.go$)|(.*\.(mod|sum)$)`)
if err != nil {
panic(err)
}
err = filepath.Walk(
rootSourceCode(compiler.SourceCodeModule()),
func(path string, info fs.FileInfo, err error) error {
if exp.MatchString(path) {
if err := hashfile(hash, path); err != nil {
return err
}
}
return nil
},
)
if err != nil {
panic(err)
}
v := hash.Sum(nil)
d := time.Since(t)
log.Printf("==> checksum %x | %s%s (%v)\n", v[:4], path, vsn, d)
return fmt.Sprintf("%x", v)
}
func hashfile(hash hash.Hash, file string) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
_, err = hash.Write([]byte(fmt.Sprintf("<file name=%s}>", file)))
if err != nil {
return err
}
_, err = io.Copy(hash, f)
if err != nil {
return err
}
_, err = hash.Write([]byte("</file>"))
if err != nil {
return err
}
return nil
}
func rootSourceCode(sourceCodeModule string) string {
sourceCode := os.Getenv("GITHUB_WORKSPACE")
if sourceCode == "" {
sourceCode = filepath.Join(os.Getenv("GOPATH"), "src", sourceCodeModule)
}
return sourceCode
}