Skip to content
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

Refactor env loading, appDir resolution, and exec shell #302

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
10 changes: 8 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, macos-11.0, ubuntu-latest]
os: [macos-latest, ubuntu-latest]
go-version: [1.17.6]
shell: [/bin/bash, /bin/zsh]

name: ${{ matrix.os }} / go-${{ matrix.go-version }}
name: ${{ matrix.os }} / go-${{ matrix.go-version }} / ${{ matrix.shell }}
steps:
- uses: actions/setup-go@v1
with:
Expand All @@ -30,6 +31,9 @@ jobs:

- uses: actions/checkout@v2

- if: contains(matrix.os, 'ubuntu')
run: sudo apt install zsh

- if: contains(matrix.os, 'macos')
run: |
sw_vers
Expand All @@ -40,6 +44,8 @@ jobs:
- run: go mod download

- run: go test -v -race -coverprofile=coverage.out -covermode=atomic -timeout=300s ./...
env:
SHELL: ${{ matrix.shell }}

devel-release:
runs-on: macos-latest
Expand Down
120 changes: 61 additions & 59 deletions dev/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"gopkg.in/tomb.v2"
)

const DefaultThreads = 5

var ErrUnexpectedExit = errors.New("unexpected exit")

type App struct {
Expand All @@ -49,10 +47,6 @@ type App struct {
pool *AppPool
lastUse time.Time

lock sync.Mutex

booting bool

readyChan chan struct{}
}

Expand Down Expand Up @@ -133,7 +127,7 @@ func (a *App) watch() error {
reason := "detected interval shutdown"

select {
case err = <-c:
case <-c:
reason = "stdout/stderr closed"
err = fmt.Errorf("%s:\n\t%s", ErrUnexpectedExit, a.lastLogLine)
case <-a.t.Dying():
Expand Down Expand Up @@ -237,64 +231,24 @@ func (a *App) Log() string {
return buf.String()
}

const executionShell = `exec bash -c '
cd %s

if test -e ~/.powconfig && [ "$PUMADEV_SOURCE_POWCONFIG" != "0" ]; then
source ~/.powconfig
fi

if test -e .env && [ "$PUMADEV_SOURCE_ENV" != "0" ]; then
source .env
fi

if test -e .powrc && [ "$PUMADEV_SOURCE_POWRC" != "0" ]; then
source .powrc
fi

if test -e .powenv && [ "$PUMADEV_SOURCE_POWENV" != "0" ]; then
source .powenv
fi

if test -e .pumaenv && [ "$PUMADEV_SOURCE_PUMAENV" != "0" ]; then
source .pumaenv
fi

if test -e Gemfile && bundle exec puma -V &>/dev/null; then
exec bundle exec puma -C $CONFIG --tag puma-dev:%s -w $WORKERS -t 0:$THREADS -b unix:%s
fi

exec puma -C $CONFIG --tag puma-dev:%s -w $WORKERS -t 0:$THREADS -b unix:%s'
`

func (pool *AppPool) LaunchApp(name, dir string) (*App, error) {
tmpDir := filepath.Join(dir, "tmp")
err := os.MkdirAll(tmpDir, 0755)
if err != nil {
appDir, dirErr := filepath.EvalSymlinks(dir)
if dirErr != nil {
return nil, dirErr
}

tmpDir := filepath.Join(appDir, "tmp")
if err := os.MkdirAll(tmpDir, 0755); err != nil {
return nil, err
}

socket := filepath.Join(tmpDir, fmt.Sprintf("puma-dev-%d.sock", os.Getpid()))

shell := os.Getenv("SHELL")

if shell == "" {
fmt.Printf("! SHELL env var not set, using /bin/bash by default")
shell = "/bin/bash"
cmd, err := BuildPumaCommand(name, socket, appDir)
if err != nil {
return nil, err
}

cmd := exec.Command(shell, "-l", "-i", "-c",
fmt.Sprintf(executionShell, dir, name, socket, name, socket))

cmd.Dir = dir

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env,
fmt.Sprintf("THREADS=%d", DefaultThreads),
"WORKERS=0",
"CONFIG=-",
)

stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
Expand All @@ -314,15 +268,15 @@ func (pool *AppPool) LaunchApp(name, dir string) (*App, error) {
Command: cmd,
Events: pool.Events,
stdout: stdout,
dir: dir,
dir: appDir,
pool: pool,
readyChan: make(chan struct{}),
lastUse: time.Now(),
}

app.eventAdd("booting_app", "socket", socket)

stat, err := os.Stat(filepath.Join(dir, "public"))
stat, err := os.Stat(filepath.Join(appDir, "public"))
if err == nil {
app.Public = stat.IsDir()
}
Expand Down Expand Up @@ -639,3 +593,51 @@ func (a *AppPool) Purge() {

a.Events.Add("apps_purged")
}

const pumaShellScriptTemplate = `exec %s -c '
cd %s

if test -e Gemfile && bundle exec puma -V &>/dev/null; then
exec bundle exec puma %s
fi

exec puma %s
'` // <-- don't forget this closing quote

func BuildPumaCommand(appName string, socketPath string, appDir string) (*exec.Cmd, error) {
osMapEnv := GetMapEnviron()

mapEnv, err := LoadEnv(osMapEnv, appDir)
if err != nil {
return nil, err
}

pumaArgs := fmt.Sprintf("--tag puma-dev:%s -b unix:%s", appName, socketPath)

if workers, exist := mapEnv["WORKERS"]; exist {
pumaArgs = fmt.Sprintf("-w%s %s", workers, pumaArgs)
}

if threads, exist := mapEnv["THREADS"]; exist {
pumaArgs = fmt.Sprintf("-t0:%s %s", threads, pumaArgs)
}

if config, exist := mapEnv["CONFIG"]; exist {
pumaArgs = fmt.Sprintf("-C%s %s", config, pumaArgs)
}

// use SHELL from OS env or app env
execShell := mapEnv["SHELL"]
if execShell == "" {
fmt.Printf("! SHELL env var not set, using /bin/bash by default")
execShell = "/bin/bash"
}

script := fmt.Sprintf(pumaShellScriptTemplate, execShell, appDir, pumaArgs, pumaArgs)

cmd := exec.Command(execShell, "-l", "-i", "-c", script)
cmd.Dir = appDir
cmd.Env = ToCmdEnv(mapEnv)

return cmd, nil
}
27 changes: 27 additions & 0 deletions dev/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuildPumaCommand_withEnv(t *testing.T) {
t.Setenv("CONFIG", "-")
t.Setenv("WORKERS", "4")
t.Setenv("THREADS", "5")

cmd, _ := BuildPumaCommand("foo", "/tmp/tmp/foo.sock", "/tmp")

assert.Regexp(t, " -w4 ", cmd.Args[4])
assert.Regexp(t, " -t0:5 ", cmd.Args[4])
assert.Regexp(t, " -C- ", cmd.Args[4])
}

func TestBuildPumaCommand_withoutEnv(t *testing.T) {
cmd, _ := BuildPumaCommand("foo", "/tmp/tmp/foo.sock", "/tmp")

assert.NotRegexp(t, " -w", cmd.Args[4])
assert.NotRegexp(t, " -t", cmd.Args[4])
assert.NotRegexp(t, " -C", cmd.Args[4])
}
74 changes: 74 additions & 0 deletions dev/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dev

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/joho/godotenv"
"github.com/puma/puma-dev/homedir"
)

// load paths in order of preference, first to set a variable wins
// `.pumaenv` has highest priority, `~/.powconfig` has lowest.
var AppEnvPaths = []string{".env", ".powrc", ".powenv", ".pumaenv"}
var HomeEnvPaths = []string{"~/.powconfig", "~/.pumaenv"}

func LoadEnv(baseEnv map[string]string, dir string) (envMap map[string]string, err error) {
// build a list of all the supported env files that exist on the filesystem to load
var envPaths []string

for _, path := range AppEnvPaths {
fullPath := filepath.Join(dir, path)
if _, err := os.Stat(fullPath); err == nil {
envPaths = append(envPaths, fullPath)
}
}

for _, path := range HomeEnvPaths {
if fullPath, err := homedir.Expand(path); err == nil {
if _, err := os.Stat(fullPath); err == nil {
envPaths = append(envPaths, fullPath)
}
}
}

envMap = baseEnv

// if we have any optional env paths to source, source them
if len(envPaths) > 0 {
// load the env into a map
appEnv, err := godotenv.Read(envPaths...)
if err != nil {
// if any path fails to load, bail out.
return nil, fmt.Errorf("LoadEnv failed. %s", err.Error())
}

// merge contents from appEnv into envMap
// this way dotenv values will supersede os env values
for k, v := range appEnv {
envMap[k] = v
}
}

return
}

func ToCmdEnv(envMap map[string]string) (env []string) {
for k, v := range envMap {
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
return
}

func GetMapEnviron() (envMap map[string]string) {
envMap = make(map[string]string)

for _, keyval := range os.Environ() {
pair := strings.Split(keyval, "=")
envMap[pair[0]] = pair[1]
}

return
}
62 changes: 62 additions & 0 deletions dev/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package dev

import (
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/puma/puma-dev/homedir"
"github.com/stretchr/testify/assert"
)

func TestLoadEnv_fileEnvPriority(t *testing.T) {
base := make(map[string]string)
appDir, _ := os.MkdirTemp("/tmp", "TestLoadEnv_fileEnvPriority")

ioutil.WriteFile(filepath.Join(appDir, ".pumaenv"), []byte("FILE=.pumaenv"), fs.FileMode(0600))
ioutil.WriteFile(filepath.Join(appDir, ".powenv"), []byte("FILE=.powenv"), fs.FileMode(0600))
ioutil.WriteFile(filepath.Join(appDir, ".powrc"), []byte("FILE=.powrc"), fs.FileMode(0600))
ioutil.WriteFile(filepath.Join(appDir, ".env"), []byte("FILE=.env"), fs.FileMode(0600))

res, _ := LoadEnv(base, appDir)

assert.Equal(t, res["FILE"], ".pumaenv")
}

func TestLoadEnv_homedirFileEnv(t *testing.T) {
if os.Getenv("CI") != "true" {
t.Skip("Skipping LoadEnv homedir tests outside CI")
}

base := make(map[string]string)
appDir, _ := os.MkdirTemp("/tmp", "TestLoadEnv_homedirFileEnv")

powconfig := homedir.MustExpand("~/.powconfig")
pumaenv := homedir.MustExpand("~/.pumaenv")

ioutil.WriteFile(powconfig, []byte("FILE=~/.powconfig"), fs.FileMode(0600))
ioutil.WriteFile(pumaenv, []byte("FILE=~/.pumaenv"), fs.FileMode(0600))

defer func() {
os.Remove(powconfig)
os.Remove(pumaenv)
}()

res, _ := LoadEnv(base, appDir)

assert.Equal(t, res["FILE"], "~/.pumaenv")

}

func TestLoadEnv(t *testing.T) {
t.Setenv("FILE", "environment")

base := GetMapEnviron()
appDir, _ := os.MkdirTemp("/tmp", "TestLoadEnv")

res, _ := LoadEnv(base, appDir)

assert.Equal(t, res["FILE"], "environment")
}
4 changes: 2 additions & 2 deletions dev/ssl_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TrustCert(cert string) error {

func DeleteAllPumaDevCAFromDefaultKeychain() error {
deleteAllBashCommand := `
for sha in $(security find-certificate -a -c "Puma-dev CA" -Z | awk '/SHA-1/ {print $3}'); do
for sha in $(security find-certificate -a -c "Puma-dev CA" -Z | awk '/SHA-1/ {print $3}'); do
security delete-certificate -t -Z $sha || security delete-certificate -Z $sha
done
`
Expand Down Expand Up @@ -65,5 +65,5 @@ func loginKeyChain() (string, error) {
return "", fmt.Errorf("could not find login keychain. security login-keychain had %s, %s", err.Error(), stderr.Bytes())
}

return string(stdout.Bytes()), nil
return stdout.String(), nil
}
Loading