Skip to content

Commit

Permalink
(cgi-ctl) add windows and darwin support
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Jun 17, 2020
1 parent 554794a commit 69df6d8
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 97 deletions.
9 changes: 4 additions & 5 deletions api/client/lambda_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
client "github.com/reddec/jsonrpc2/client"
api "github.com/reddec/trusted-cgi/api"
application "github.com/reddec/trusted-cgi/application"
stats "github.com/reddec/trusted-cgi/stats"
types "github.com/reddec/trusted-cgi/types"
"sync/atomic"
Expand Down Expand Up @@ -56,13 +55,13 @@ func (impl *LambdaAPIClient) Files(ctx context.Context, token *api.Token, uid st
}

// Info about application
func (impl *LambdaAPIClient) Info(ctx context.Context, token *api.Token, uid string) (reply *application.App, err error) {
func (impl *LambdaAPIClient) Info(ctx context.Context, token *api.Token, uid string) (reply *types.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Info", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid)
return
}

// Update application manifest
func (impl *LambdaAPIClient) Update(ctx context.Context, token *api.Token, uid string, manifest types.Manifest) (reply *application.App, err error) {
func (impl *LambdaAPIClient) Update(ctx context.Context, token *api.Token, uid string, manifest types.Manifest) (reply *types.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Update", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, manifest)
return
}
Expand Down Expand Up @@ -104,13 +103,13 @@ func (impl *LambdaAPIClient) Invoke(ctx context.Context, token *api.Token, uid s
}

// Make link/alias for app
func (impl *LambdaAPIClient) Link(ctx context.Context, token *api.Token, uid string, alias string) (reply *application.App, err error) {
func (impl *LambdaAPIClient) Link(ctx context.Context, token *api.Token, uid string, alias string) (reply *types.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Link", atomic.AddUint64(&impl.sequence, 1), &reply, token, uid, alias)
return
}

// Remove link
func (impl *LambdaAPIClient) Unlink(ctx context.Context, token *api.Token, alias string) (reply *application.App, err error) {
func (impl *LambdaAPIClient) Unlink(ctx context.Context, token *api.Token, alias string) (reply *types.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Unlink", atomic.AddUint64(&impl.sequence, 1), &reply, token, alias)
return
}
10 changes: 5 additions & 5 deletions api/client/project_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
client "github.com/reddec/jsonrpc2/client"
api "github.com/reddec/trusted-cgi/api"
application "github.com/reddec/trusted-cgi/application"
stats "github.com/reddec/trusted-cgi/stats"
types "github.com/reddec/trusted-cgi/types"
"sync/atomic"
)

Expand Down Expand Up @@ -43,7 +43,7 @@ func (impl *ProjectAPIClient) AllTemplates(ctx context.Context, token *api.Token
}

// List available apps (lambdas) in a project
func (impl *ProjectAPIClient) List(ctx context.Context, token *api.Token) (reply []*application.App, err error) {
func (impl *ProjectAPIClient) List(ctx context.Context, token *api.Token) (reply []*types.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.List", atomic.AddUint64(&impl.sequence, 1), &reply, token)
return
}
Expand All @@ -61,19 +61,19 @@ func (impl *ProjectAPIClient) Stats(ctx context.Context, token *api.Token, limit
}

// Create new app (lambda)
func (impl *ProjectAPIClient) Create(ctx context.Context, token *api.Token) (reply *application.App, err error) {
func (impl *ProjectAPIClient) Create(ctx context.Context, token *api.Token) (reply *types.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.Create", atomic.AddUint64(&impl.sequence, 1), &reply, token)
return
}

// Create new app/lambda/function using pre-defined template
func (impl *ProjectAPIClient) CreateFromTemplate(ctx context.Context, token *api.Token, templateName string) (reply *application.App, err error) {
func (impl *ProjectAPIClient) CreateFromTemplate(ctx context.Context, token *api.Token, templateName string) (reply *types.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.CreateFromTemplate", atomic.AddUint64(&impl.sequence, 1), &reply, token, templateName)
return
}

// Create new app/lambda/function using remote Git repo
func (impl *ProjectAPIClient) CreateFromGit(ctx context.Context, token *api.Token, repo string) (reply *application.App, err error) {
func (impl *ProjectAPIClient) CreateFromGit(ctx context.Context, token *api.Token, repo string) (reply *types.App, err error) {
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.CreateFromGit", atomic.AddUint64(&impl.sequence, 1), &reply, token, repo)
return
}
17 changes: 8 additions & 9 deletions api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"context"
"encoding/json"
"github.com/reddec/trusted-cgi/application"
"github.com/reddec/trusted-cgi/stats"
"github.com/reddec/trusted-cgi/types"
)
Expand Down Expand Up @@ -63,9 +62,9 @@ type LambdaAPI interface {
// Files in func dir
Files(ctx context.Context, token *Token, uid string, dir string) ([]*File, error)
// Info about application
Info(ctx context.Context, token *Token, uid string) (*application.App, error)
Info(ctx context.Context, token *Token, uid string) (*types.App, error)
// Update application manifest
Update(ctx context.Context, token *Token, uid string, manifest types.Manifest) (*application.App, error)
Update(ctx context.Context, token *Token, uid string, manifest types.Manifest) (*types.App, error)
// Create file or directory inside app
CreateFile(ctx context.Context, token *Token, uid string, path string, dir bool) (bool, error)
// Remove file or directory
Expand All @@ -79,9 +78,9 @@ type LambdaAPI interface {
// Invoke action in the app (if make installed)
Invoke(ctx context.Context, token *Token, uid string, action string) (string, error)
// Make link/alias for app
Link(ctx context.Context, token *Token, uid string, alias string) (*application.App, error)
Link(ctx context.Context, token *Token, uid string, alias string) (*types.App, error)
// Remove link
Unlink(ctx context.Context, token *Token, alias string) (*application.App, error)
Unlink(ctx context.Context, token *Token, alias string) (*types.App, error)
}

// API for global project
Expand All @@ -95,17 +94,17 @@ type ProjectAPI interface {
// Get all templates without filtering
AllTemplates(ctx context.Context, token *Token) ([]*TemplateStatus, error)
// List available apps (lambdas) in a project
List(ctx context.Context, token *Token) ([]*application.App, error)
List(ctx context.Context, token *Token) ([]*types.App, error)
// Templates with filter by availability including embedded
Templates(ctx context.Context, token *Token) ([]*Template, error)
// Global last records
Stats(ctx context.Context, token *Token, limit int) ([]stats.Record, error)
// Create new app (lambda)
Create(ctx context.Context, token *Token) (*application.App, error)
Create(ctx context.Context, token *Token) (*types.App, error)
// Create new app/lambda/function using pre-defined template
CreateFromTemplate(ctx context.Context, token *Token, templateName string) (*application.App, error)
CreateFromTemplate(ctx context.Context, token *Token, templateName string) (*types.App, error)
// Create new app/lambda/function using remote Git repo
CreateFromGit(ctx context.Context, token *Token, repo string) (*application.App, error)
CreateFromGit(ctx context.Context, token *Token, repo string) (*types.App, error)
}

// User/admin profile API
Expand Down
24 changes: 16 additions & 8 deletions api/services/lambda_srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ func (srv *lambdaSrv) Files(ctx context.Context, token *api.Token, uid string, d
return ans, nil
}

func (srv *lambdaSrv) Info(ctx context.Context, token *api.Token, uid string) (*application.App, error) {
func (srv *lambdaSrv) Info(ctx context.Context, token *api.Token, uid string) (*types.App, error) {
app := srv.project.FindApp(uid)
if app == nil {
return nil, fmt.Errorf("unknown app")
}
return app, nil
return &app.App, nil
}

func (srv *lambdaSrv) Update(ctx context.Context, token *api.Token, uid string, manifest types.Manifest) (*application.App, error) {
func (srv *lambdaSrv) Update(ctx context.Context, token *api.Token, uid string, manifest types.Manifest) (*types.App, error) {
app := srv.project.FindApp(uid)
if app == nil {
return nil, fmt.Errorf("unknown app")
Expand All @@ -102,7 +102,7 @@ func (srv *lambdaSrv) Update(ctx context.Context, token *api.Token, uid string,
return nil, err
}
app.Manifest = manifest
return app, app.Manifest.SaveAs(app.ManifestFile())
return &app.App, app.Manifest.SaveAs(app.ManifestFile())
}

func (srv *lambdaSrv) CreateFile(ctx context.Context, token *api.Token, uid string, path string, dir bool) (bool, error) {
Expand Down Expand Up @@ -164,10 +164,18 @@ func (srv *lambdaSrv) Invoke(ctx context.Context, token *api.Token, uid string,
return app.InvokeAction(ctx, action, 0, srv.project.GlobalEnvironment())
}

func (srv *lambdaSrv) Link(ctx context.Context, token *api.Token, uid string, alias string) (*application.App, error) {
return srv.project.Link(uid, alias)
func (srv *lambdaSrv) Link(ctx context.Context, token *api.Token, uid string, alias string) (*types.App, error) {
app, err := srv.project.Link(uid, alias)
if err != nil {
return nil, err
}
return &app.App, nil
}

func (srv *lambdaSrv) Unlink(ctx context.Context, token *api.Token, alias string) (*application.App, error) {
return srv.project.Unlink(alias)
func (srv *lambdaSrv) Unlink(ctx context.Context, token *api.Token, alias string) (*types.App, error) {
app, err := srv.project.Unlink(alias)
if err != nil {
return nil, err
}
return &app.App, nil
}
34 changes: 26 additions & 8 deletions api/services/project_srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/reddec/trusted-cgi/application"
"github.com/reddec/trusted-cgi/stats"
"github.com/reddec/trusted-cgi/templates"
"github.com/reddec/trusted-cgi/types"
)

func NewProjectSrv(project *application.Project, tracker stats.Reader, templatesDir string) *projectSrv {
Expand All @@ -23,15 +24,23 @@ type projectSrv struct {
templatesDir string
}

func (srv *projectSrv) Create(ctx context.Context, token *api.Token) (*application.App, error) {
return srv.project.Create(ctx)
func (srv *projectSrv) Create(ctx context.Context, token *api.Token) (*types.App, error) {
app, err := srv.project.Create(ctx)
if err != nil {
return nil, err
}
return &app.App, nil
}

func (srv *projectSrv) CreateFromGit(ctx context.Context, token *api.Token, repo string) (*application.App, error) {
return srv.project.CreateFromGit(ctx, repo)
func (srv *projectSrv) CreateFromGit(ctx context.Context, token *api.Token, repo string) (*types.App, error) {
app, err := srv.project.CreateFromGit(ctx, repo)
if err != nil {
return nil, err
}
return &app.App, nil
}

func (srv *projectSrv) CreateFromTemplate(ctx context.Context, token *api.Token, templateName string) (*application.App, error) {
func (srv *projectSrv) CreateFromTemplate(ctx context.Context, token *api.Token, templateName string) (*types.App, error) {
possible, err := templates.List(srv.templatesDir)
if err != nil {
return nil, err
Expand All @@ -43,7 +52,11 @@ func (srv *projectSrv) CreateFromTemplate(ctx context.Context, token *api.Token,
if !tpl.IsAvailable(ctx) {
return nil, fmt.Errorf("template %s is not supported", templateName)
}
return srv.project.CreateFromTemplate(ctx, tpl)
app, err := srv.project.CreateFromTemplate(ctx, tpl)
if err != nil {
return nil, err
}
return &app.App, nil
}

func (srv *projectSrv) Config(ctx context.Context, token *api.Token) (*api.Settings, error) {
Expand Down Expand Up @@ -87,8 +100,13 @@ func (srv *projectSrv) AllTemplates(ctx context.Context, token *api.Token) ([]*a
return ans, nil
}

func (srv *projectSrv) List(ctx context.Context, token *api.Token) ([]*application.App, error) {
return srv.project.List(), nil
func (srv *projectSrv) List(ctx context.Context, token *api.Token) ([]*types.App, error) {
list := srv.project.List()
var ans = make([]*types.App, len(list))
for i, v := range list {
ans[i] = &v.App
}
return ans, nil
}

func (srv *projectSrv) Templates(ctx context.Context, token *api.Token) ([]*api.Template, error) {
Expand Down
4 changes: 2 additions & 2 deletions application/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"github.com/reddec/trusted-cgi/internal"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -59,10 +60,9 @@ func (app *App) InvokeAction(ctx context.Context, name string, timeLimit time.Du
cmd.Stdout = &out
cmd.Stderr = &out
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGINT,
Setpgid: true,
Credential: app.creds,
}
internal.SetFlags(cmd)
cmd.Env = environments

err := cmd.Run()
Expand Down
24 changes: 11 additions & 13 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/reddec/trusted-cgi/internal"
"github.com/reddec/trusted-cgi/types"
"io/ioutil"
"os"
Expand All @@ -13,13 +14,11 @@ import (
"syscall"
)

const (
ManifestFile = "manifest.json"
)

func OpenApp(location string, creds *syscall.Credential) (*App, error) {
var app = &App{
UID: filepath.Base(location),
App: types.App{
UID: filepath.Base(location),
},
creds: creds,
location: location,
}
Expand All @@ -31,8 +30,10 @@ func OpenApp(location string, creds *syscall.Credential) (*App, error) {

func CreateApp(location string, creds *syscall.Credential, manifest types.Manifest) (*App, error) {
var app = &App{
UID: filepath.Base(location),
Manifest: manifest,
App: types.App{
UID: filepath.Base(location),
Manifest: manifest,
},
creds: creds,
location: location,
}
Expand All @@ -50,10 +51,9 @@ func CreateApp(location string, creds *syscall.Credential, manifest types.Manife
func CreateAppGit(ctx context.Context, location, repo, privateKey string, creds *syscall.Credential) (*App, error) {
cmd := exec.CommandContext(ctx, "git", "clone", "--depth", "1", repo, location)
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGINT,
Setpgid: true,
Credential: creds,
}
internal.SetFlags(cmd)
var buffer bytes.Buffer
cmd.Stderr = &buffer
cmd.Stdout = os.Stdout
Expand All @@ -67,15 +67,13 @@ func CreateAppGit(ctx context.Context, location, repo, privateKey string, creds
}

type App struct {
UID string `json:"uid"`
Manifest types.Manifest `json:"manifest"`
IsGit bool `json:"git"`
types.App
creds *syscall.Credential `json:"-"`
location string `json:"-"`
}

func (app *App) ManifestFile() string {
return filepath.Join(app.location, ManifestFile)
return filepath.Join(app.location, internal.ManifestFile)
}

func (app *App) ApplyOwner() error {
Expand Down
5 changes: 2 additions & 3 deletions application/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/reddec/trusted-cgi/internal"
"github.com/reddec/trusted-cgi/stats"
"io"
"io/ioutil"
Expand Down Expand Up @@ -144,11 +145,9 @@ func (app *App) Run(ctx context.Context,
cmd.Stdout = &result
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGINT,
Setpgid: true,
Credential: app.creds,
}

internal.SetFlags(cmd)
var environments = os.Environ()
for header, mapped := range env {
environments = append(environments, header+"="+mapped)
Expand Down
Loading

0 comments on commit 69df6d8

Please sign in to comment.