Skip to content

Commit

Permalink
Merge branch 'main' into jermignore
Browse files Browse the repository at this point in the history
  • Loading branch information
spatocode authored Sep 16, 2023
2 parents aed3a3b + 895c701 commit 32786d3
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 88 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Static Analysis
on: [pull_request]
permissions:
contents: read

jobs:
static_analysis:
runs-on: ubuntu-latest
strategy:
fail-fast: false

steps:
- uses: actions/checkout@v2
with:
persist-credentials: false

- uses: WillAbides/[email protected]
with:
go-version: '1.21.x'

- name: Install dependencies
run: |
sudo apt-get update && sudo apt-get install gcc libgl1-mesa-dev libegl1-mesa-dev libgles2-mesa-dev libx11-dev xorg-dev
- name: Install analysis tools
run: |
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
go install honnef.co/go/tools/cmd/[email protected]
- name: Vet
run: go vet -tags ci ./...

- name: Goimports
run: test -z "$(goimports -e -d . | tee /dev/stderr)"

- name: Gocyclo
run: gocyclo -over 30 .

- name: Staticcheck
run: staticcheck ./...

- name: Coverage
run: |
set -e
go test -covermode=atomic -coverprofile=coverage.out -vet=off ./...
coverage=`go tool cover -func coverage.out | grep total | tr -s '\t' | cut -f 3 | grep -o '[^%]*'`
if (( $(echo "$coverage < 63" | bc) )); then echo "Test coverage lowered"; exit 1; fi
- name: PR coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Tests
on: [pull_request]
permissions:
contents: read

jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false

steps:
- uses: actions/checkout@v2
with:
persist-credentials: false

- uses: WillAbides/[email protected]
with:
go-version: '1.21.x'

- name: Install dependencies
run: |
sudo apt-get update && sudo apt-get install gcc libgl1-mesa-dev libegl1-mesa-dev libgles2-mesa-dev libx11-dev xorg-dev
- name: Tests
run: go test -v -vet=off ./...
2 changes: 1 addition & 1 deletion cloud/aws/apigateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (a *ApiGateway) setup(functionArn *string) error {
return err
}

log.PrintInfo(fmt.Sprintf("Project %s is now live at %s", a.config.Name, apiUrl))
fmt.Printf("%s %s", log.Magenta("url:"), log.Green(apiUrl))

return nil
}
Expand Down
18 changes: 14 additions & 4 deletions cloud/aws/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ func NewLambda(cfg *config.Config) (*Lambda, error) {
l.access = NewIAM(cfg, *awsConfig)
l.apigateway = NewApiGateway(cfg, *awsConfig)

err = l.config.ToJson(jerm.DefaultConfigFile)
if err != nil {
return nil, err
}
go func() {
err := l.config.ToJson(jerm.DefaultConfigFile)
if err != nil {
log.PrintWarn(err)
}
}()

err = l.access.checkPermissions()
if err != nil {
Expand All @@ -88,11 +90,19 @@ func NewLambda(cfg *config.Config) (*Lambda, error) {
// Build builds the deployment package for lambda
func (l *Lambda) Build() (string, error) {
log.Debug("building Jerm project for Lambda...")

p := config.NewPythonConfig()
if l.config.Entry == "" {
l.config.Entry = p.Entry()
}

go func() {
err := l.config.ToJson(jerm.DefaultConfigFile)
if err != nil {
log.PrintWarn(err)
}
}()

handler, err := p.Build(l.config)
dir := filepath.Dir(handler)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cloud/aws/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *CloudWatch) printLogs(logs []cwTypes.FilteredLogEvent) {
strings.Contains(*message, "END RequestId") {
continue
}
log.PrintInfo(fmt.Sprintf("[%s] %s\n", time, strings.TrimSpace(*message)))
log.PrintfInfo("[%s] %s\n", time, strings.TrimSpace(*message))
}
}

Expand Down
161 changes: 99 additions & 62 deletions config/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"runtime"
"strings"

"golang.org/x/sync/errgroup"

"github.com/otiai10/copy"
"github.com/spatocode/jerm/internal/log"
"github.com/spatocode/jerm/internal/utils"
Expand Down Expand Up @@ -86,8 +88,8 @@ func (p *Python) Build(config *Config) (string, error) {

venv, err := p.getVirtualEnvironment()
if err != nil {
err = p.installRequirements(tempDir)
return handlerPath, err
//TODO: installs requirements listed in requirements.txt file
return "", fmt.Errorf("cannot find a virtual env. Please ensure you're running in a virtual env")
}

version := strings.Split(DetectRuntime().Version, ".")
Expand All @@ -96,16 +98,37 @@ func (p *Python) Build(config *Config) (string, error) {
sitePackages = path.Join(venv, "Lib", "site-packages")
}

p.installNecessaryDependencies(tempDir, sitePackages)
p.copyNecessaryFilesToTempDir(config.Dir, tempDir)
p.copyNecessaryFilesToTempDir(sitePackages, tempDir)
log.Debug(fmt.Sprintf("build Python deployment package at %s", tempDir))
return handlerPath, nil
dependencies := map[string]string{
"lambda-wsgi-adapter": "0.1.1",
}
if !utils.FileExists(filepath.Join(sitePackages, "werkzeug")) {
dependencies["werkzeug"] = "0.16.1"
}

err = p.installNecessaryDependencies(tempDir, sitePackages, dependencies)
if err != nil {
return "", err
}

err = p.copyNecessaryFilesToTempDir(config.Dir, tempDir)
if err != nil {
return "", err
}

err = p.copyNecessaryFilesToTempDir(sitePackages, tempDir)
if err != nil {
return "", err
}

log.Debug(fmt.Sprintf("built Python deployment package at %s", tempDir))

return handlerPath, err
}

func (p *Python) copyNecessaryFilesToTempDir(src, dest string) error {
ignoredFiles := defaultIgnoredGlobs
log.Debug("copying necessary Python files...")

ignoredFiles := defaultIgnoredGlobs
files, err := ReadIgnoredFiles()
if err == nil {
ignoredFiles = append(ignoredFiles, files...)
Expand Down Expand Up @@ -134,49 +157,54 @@ func (p *Python) copyNecessaryFilesToTempDir(src, dest string) error {
}

// installRequirements installs requirements listed in requirements.txt file
func (p *Python) installRequirements(dir string) error {
return nil
}
// func (p *Python) installRequirements(dir string) error {
// return nil
// }

// installNecessaryDependencies installs dependencies needed to run serverless Python
func (p *Python) installNecessaryDependencies(dir, sitePackages string) error {
func (p *Python) installNecessaryDependencies(dir, sitePackages string, dependencies map[string]string) error {
log.Debug("installing necessary Python dependencies...")

dependencies := map[string]string{
"lambda-wsgi-adapter": "0.1.1",
}
if !utils.FileExists(filepath.Join(sitePackages, "werkzeug")) {
dependencies["werkzeug"] = "0.16.1"
}
var eg errgroup.Group

for project, version := range dependencies {
url := fmt.Sprintf("https://pypi.org/pypi/%s/json", project)
res, err := utils.Request(url)
if err != nil {
return err
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
return err
}
data := make(map[string]interface{})
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
func(dep, ver string) {
eg.Go(func() error {
url := fmt.Sprintf("https://pypi.org/pypi/%s/json", dep)
res, err := utils.Request(url)
if err != nil {
return err
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
return err
}
data := make(map[string]interface{})
err = json.Unmarshal(b, &data)
if err != nil {
return err
}

r := data["releases"]
releases, _ := r.(map[string]interface{})
for _, v := range releases[version].([]interface{}) {
url := v.(map[string]interface{})["url"].(string)
filename := v.(map[string]interface{})["filename"].(string)
if filepath.Ext(filename) == ".whl" {
p.downloadDependencies(url, filename, dir)
}
}
r := data["releases"]
releases, _ := r.(map[string]interface{})
for _, v := range releases[ver].([]interface{}) {
url := v.(map[string]interface{})["url"].(string)
filename := v.(map[string]interface{})["filename"].(string)
if filepath.Ext(filename) == ".whl" {
err := p.downloadDependencies(url, filename, dir)
if err != nil {
return err
}
}
}
return nil
})
}(project, version)
}
return nil

err := eg.Wait()

return err
}

// downloadDependencies downloads dependencies from pypi
Expand Down Expand Up @@ -215,36 +243,45 @@ func (p *Python) downloadDependencies(url, filename, dir string) error {

func (p *Python) extractWheel(wheelPath, outputDir string) error {
log.Debug("extracting python wheel...")
var eg errgroup.Group

reader, err := zip.OpenReader(wheelPath)
if err != nil {
return err
}
defer reader.Close()

for _, file := range reader.File {
os.MkdirAll(filepath.Join(outputDir, filepath.Dir(file.Name)), 0755)
if err != nil {
return err
}
func(file *zip.File) {
eg.Go(func() error {
os.MkdirAll(filepath.Join(outputDir, filepath.Dir(file.Name)), 0755)
if err != nil {
return err
}

extractedFile, err := os.Create(filepath.Join(outputDir, file.Name))
if err != nil {
return err
}
defer extractedFile.Close()
extractedFile, err := os.Create(filepath.Join(outputDir, file.Name))
if err != nil {
return err
}
defer extractedFile.Close()

zippedFile, err := file.Open()
if err != nil {
return err
}
defer zippedFile.Close()
zippedFile, err := file.Open()
if err != nil {
return err
}
defer zippedFile.Close()

if _, err = io.Copy(extractedFile, zippedFile); err != nil {
return err
}
if _, err = io.Copy(extractedFile, zippedFile); err != nil {
return err
}
return nil
})
}(file)
}

return nil
err = eg.Wait()

return err
}

func (p *Python) IsDjango() bool {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/otiai10/copy v1.12.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.3.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
15 changes: 12 additions & 3 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ import (
)

var (
PrintError = color.New(color.FgRed).PrintlnFunc()
PrintWarn = color.New(color.FgYellow).PrintlnFunc()
PrintInfo = color.New(color.FgCyan).PrintlnFunc()
PrintError = color.New(color.FgRed).PrintlnFunc()
PrintfError = color.New(color.FgRed).PrintfFunc()
PrintWarn = color.New(color.FgYellow).PrintlnFunc()
PrintfWarn = color.New(color.FgYellow).PrintfFunc()
PrintInfo = color.New(color.FgCyan).PrintlnFunc()
PrintfInfo = color.New(color.FgCyan).PrintfFunc()
Yellow = color.New(color.FgYellow).SprintFunc()
Red = color.New(color.FgRed).SprintFunc()
Blue = color.New(color.FgBlue).SprintFunc()
Green = color.New(color.FgGreen).SprintFunc()
White = color.New(color.FgWhite).SprintFunc()
Magenta = color.New(color.FgMagenta).SprintFunc()
)

func Info(msg string, v ...interface{}) {
Expand Down
Loading

0 comments on commit 32786d3

Please sign in to comment.