Skip to content

Commit

Permalink
add log streaming to bootstrap function
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Aug 6, 2021
1 parent 0de9051 commit 6c67b97
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 14 deletions.
5 changes: 4 additions & 1 deletion api/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/atoz-technology/mantil-backend/internal/bootstrap"
"github.com/atoz-technology/mantil-backend/internal/stream"
)

type Bootstrap struct{}
Expand All @@ -21,7 +22,9 @@ func (f *Bootstrap) Invoke(ctx context.Context, req *BootstrapRequest) (*Bootstr
}

func (f *Bootstrap) Bootstrap(ctx context.Context, req *BootstrapRequest) (*BootstrapResponse, error) {
if err := bootstrap.Bootstrap("/tmp", req.Destroy); err != nil {
if err := stream.LambdaLogStream(ctx, func() error {
return bootstrap.Bootstrap("/tmp", req.Destroy)
}); err != nil {
log.Println(err)
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (f *Data) isRequestValid(req *DataRequest) bool {

func (f *Data) streamingLogsProject(ctx context.Context, name string) (*mantil.Project, error) {
var p *mantil.Project
err := stream.LambdaLogStream(ctx, func() error {
err := stream.APIGatewayLambdaLogStream(ctx, func() error {
var err error
p, err = mantil.LoadProject(name)
return err
Expand Down
2 changes: 1 addition & 1 deletion api/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (h *Deploy) Deploy(ctx context.Context, req *DeployRequest) (*DeployRespons
if err != nil {
return nil, err
}
err = stream.LambdaLogStream(ctx, d.Deploy)
err = stream.APIGatewayLambdaLogStream(ctx, d.Deploy)
if err != nil {
log.Println(err)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion api/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (f *Destroy) Destroy(ctx context.Context, req *DestroyRequest) (*DestroyRes
if p.Token != req.Token {
return nil, fmt.Errorf("access denied")
}
err = stream.LambdaLogStream(ctx, func() error {
err = stream.APIGatewayLambdaLogStream(ctx, func() error {
return destroy.Destroy(p, "/tmp")
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/initialize/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *Init) isRequestValid(req *InitRequest) bool {

func (f *Init) streamingLogsInitProject(ctx context.Context, name string) (string, error) {
var token string
err := stream.LambdaLogStream(ctx, func() error {
err := stream.APIGatewayLambdaLogStream(ctx, func() error {
var err error
token, err = initialize.InitProject(name)
return err
Expand Down
2 changes: 1 addition & 1 deletion api/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (f *Security) isRequestValid(req *SecurityRequest) bool {

func (f *Security) streamingLogsCredentials(ctx context.Context, p *mantil.Project) (*stsTypes.Credentials, error) {
var creds *stsTypes.Credentials
err := stream.LambdaLogStream(ctx, func() error {
err := stream.APIGatewayLambdaLogStream(ctx, func() error {
var err error
creds, err = security.Credentials(p)
return err
Expand Down
8 changes: 4 additions & 4 deletions internal/assets/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"errors"
"io"
"log"
"os"
"os/exec"
"strings"
)

func Exec(args []string, dir string, successStatuses ...int) error {
var std = log.New(os.Stderr, log.Prefix(), 0)
// var std = log.New(os.Stderr, log.Prefix(), 0)
r := runner{
dir: dir,
verbose: true,
output: func(format string, v ...interface{}) {
std.Printf(format, v...)
// std.Printf(format, v...)
log.Printf(format, v...)
},
}
return r.runCmd(args)
Expand Down
15 changes: 14 additions & 1 deletion internal/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func LogStream(subject string, callback func() error) error {
return nil
}

func LambdaLogStream(ctx context.Context, callback func() error) error {
func APIGatewayLambdaLogStream(ctx context.Context, callback func() error) error {
var inbox string
lctx, ok := mgo.FromContext(ctx)
if !ok {
Expand All @@ -83,6 +83,19 @@ func LambdaLogStream(ctx context.Context, callback func() error) error {
return LogStream(inbox, callback)
}

func LambdaLogStream(ctx context.Context, callback func() error) error {
var inbox string
lctx, ok := mgo.FromContext(ctx)
if !ok {
return fmt.Errorf("error retrieving nats subject")
}
inbox = lctx.Lambda.ClientContext.Custom["logInbox"]
if inbox == "" {
return fmt.Errorf("invalid nats subject")
}
return LogStream(inbox, callback)
}

func natsAuth() nats.Option {
nkeySeed := "SUADEJU2KOHEGHFLBXJS4QF75E2II3PU63I3GCK4OBJLINOC7LDVEOX42A"
nkeyUser := "UDQPHZBVNZCJSM5JXUDICFALEQ7Y5KPPAF7KGHTH77OGG7COQOJEYBZ7"
Expand Down

0 comments on commit 6c67b97

Please sign in to comment.