Skip to content

Commit 69df6d8

Browse files
committed
(cgi-ctl) add windows and darwin support
1 parent 554794a commit 69df6d8

24 files changed

+169
-97
lines changed

api/client/lambda_api_client.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
client "github.com/reddec/jsonrpc2/client"
66
api "github.com/reddec/trusted-cgi/api"
7-
application "github.com/reddec/trusted-cgi/application"
87
stats "github.com/reddec/trusted-cgi/stats"
98
types "github.com/reddec/trusted-cgi/types"
109
"sync/atomic"
@@ -56,13 +55,13 @@ func (impl *LambdaAPIClient) Files(ctx context.Context, token *api.Token, uid st
5655
}
5756

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

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

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

112111
// Remove link
113-
func (impl *LambdaAPIClient) Unlink(ctx context.Context, token *api.Token, alias string) (reply *application.App, err error) {
112+
func (impl *LambdaAPIClient) Unlink(ctx context.Context, token *api.Token, alias string) (reply *types.App, err error) {
114113
err = client.CallHTTP(ctx, impl.BaseURL, "LambdaAPI.Unlink", atomic.AddUint64(&impl.sequence, 1), &reply, token, alias)
115114
return
116115
}

api/client/project_api_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
client "github.com/reddec/jsonrpc2/client"
66
api "github.com/reddec/trusted-cgi/api"
7-
application "github.com/reddec/trusted-cgi/application"
87
stats "github.com/reddec/trusted-cgi/stats"
8+
types "github.com/reddec/trusted-cgi/types"
99
"sync/atomic"
1010
)
1111

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

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

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

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

7575
// Create new app/lambda/function using remote Git repo
76-
func (impl *ProjectAPIClient) CreateFromGit(ctx context.Context, token *api.Token, repo string) (reply *application.App, err error) {
76+
func (impl *ProjectAPIClient) CreateFromGit(ctx context.Context, token *api.Token, repo string) (reply *types.App, err error) {
7777
err = client.CallHTTP(ctx, impl.BaseURL, "ProjectAPI.CreateFromGit", atomic.AddUint64(&impl.sequence, 1), &reply, token, repo)
7878
return
7979
}

api/interface.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package api
33
import (
44
"context"
55
"encoding/json"
6-
"github.com/reddec/trusted-cgi/application"
76
"github.com/reddec/trusted-cgi/stats"
87
"github.com/reddec/trusted-cgi/types"
98
)
@@ -63,9 +62,9 @@ type LambdaAPI interface {
6362
// Files in func dir
6463
Files(ctx context.Context, token *Token, uid string, dir string) ([]*File, error)
6564
// Info about application
66-
Info(ctx context.Context, token *Token, uid string) (*application.App, error)
65+
Info(ctx context.Context, token *Token, uid string) (*types.App, error)
6766
// Update application manifest
68-
Update(ctx context.Context, token *Token, uid string, manifest types.Manifest) (*application.App, error)
67+
Update(ctx context.Context, token *Token, uid string, manifest types.Manifest) (*types.App, error)
6968
// Create file or directory inside app
7069
CreateFile(ctx context.Context, token *Token, uid string, path string, dir bool) (bool, error)
7170
// Remove file or directory
@@ -79,9 +78,9 @@ type LambdaAPI interface {
7978
// Invoke action in the app (if make installed)
8079
Invoke(ctx context.Context, token *Token, uid string, action string) (string, error)
8180
// Make link/alias for app
82-
Link(ctx context.Context, token *Token, uid string, alias string) (*application.App, error)
81+
Link(ctx context.Context, token *Token, uid string, alias string) (*types.App, error)
8382
// Remove link
84-
Unlink(ctx context.Context, token *Token, alias string) (*application.App, error)
83+
Unlink(ctx context.Context, token *Token, alias string) (*types.App, error)
8584
}
8685

8786
// API for global project
@@ -95,17 +94,17 @@ type ProjectAPI interface {
9594
// Get all templates without filtering
9695
AllTemplates(ctx context.Context, token *Token) ([]*TemplateStatus, error)
9796
// List available apps (lambdas) in a project
98-
List(ctx context.Context, token *Token) ([]*application.App, error)
97+
List(ctx context.Context, token *Token) ([]*types.App, error)
9998
// Templates with filter by availability including embedded
10099
Templates(ctx context.Context, token *Token) ([]*Template, error)
101100
// Global last records
102101
Stats(ctx context.Context, token *Token, limit int) ([]stats.Record, error)
103102
// Create new app (lambda)
104-
Create(ctx context.Context, token *Token) (*application.App, error)
103+
Create(ctx context.Context, token *Token) (*types.App, error)
105104
// Create new app/lambda/function using pre-defined template
106-
CreateFromTemplate(ctx context.Context, token *Token, templateName string) (*application.App, error)
105+
CreateFromTemplate(ctx context.Context, token *Token, templateName string) (*types.App, error)
107106
// Create new app/lambda/function using remote Git repo
108-
CreateFromGit(ctx context.Context, token *Token, repo string) (*application.App, error)
107+
CreateFromGit(ctx context.Context, token *Token, repo string) (*types.App, error)
109108
}
110109

111110
// User/admin profile API

api/services/lambda_srv.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ func (srv *lambdaSrv) Files(ctx context.Context, token *api.Token, uid string, d
8585
return ans, nil
8686
}
8787

88-
func (srv *lambdaSrv) Info(ctx context.Context, token *api.Token, uid string) (*application.App, error) {
88+
func (srv *lambdaSrv) Info(ctx context.Context, token *api.Token, uid string) (*types.App, error) {
8989
app := srv.project.FindApp(uid)
9090
if app == nil {
9191
return nil, fmt.Errorf("unknown app")
9292
}
93-
return app, nil
93+
return &app.App, nil
9494
}
9595

96-
func (srv *lambdaSrv) Update(ctx context.Context, token *api.Token, uid string, manifest types.Manifest) (*application.App, error) {
96+
func (srv *lambdaSrv) Update(ctx context.Context, token *api.Token, uid string, manifest types.Manifest) (*types.App, error) {
9797
app := srv.project.FindApp(uid)
9898
if app == nil {
9999
return nil, fmt.Errorf("unknown app")
@@ -102,7 +102,7 @@ func (srv *lambdaSrv) Update(ctx context.Context, token *api.Token, uid string,
102102
return nil, err
103103
}
104104
app.Manifest = manifest
105-
return app, app.Manifest.SaveAs(app.ManifestFile())
105+
return &app.App, app.Manifest.SaveAs(app.ManifestFile())
106106
}
107107

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

167-
func (srv *lambdaSrv) Link(ctx context.Context, token *api.Token, uid string, alias string) (*application.App, error) {
168-
return srv.project.Link(uid, alias)
167+
func (srv *lambdaSrv) Link(ctx context.Context, token *api.Token, uid string, alias string) (*types.App, error) {
168+
app, err := srv.project.Link(uid, alias)
169+
if err != nil {
170+
return nil, err
171+
}
172+
return &app.App, nil
169173
}
170174

171-
func (srv *lambdaSrv) Unlink(ctx context.Context, token *api.Token, alias string) (*application.App, error) {
172-
return srv.project.Unlink(alias)
175+
func (srv *lambdaSrv) Unlink(ctx context.Context, token *api.Token, alias string) (*types.App, error) {
176+
app, err := srv.project.Unlink(alias)
177+
if err != nil {
178+
return nil, err
179+
}
180+
return &app.App, nil
173181
}

api/services/project_srv.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/reddec/trusted-cgi/application"
88
"github.com/reddec/trusted-cgi/stats"
99
"github.com/reddec/trusted-cgi/templates"
10+
"github.com/reddec/trusted-cgi/types"
1011
)
1112

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

26-
func (srv *projectSrv) Create(ctx context.Context, token *api.Token) (*application.App, error) {
27-
return srv.project.Create(ctx)
27+
func (srv *projectSrv) Create(ctx context.Context, token *api.Token) (*types.App, error) {
28+
app, err := srv.project.Create(ctx)
29+
if err != nil {
30+
return nil, err
31+
}
32+
return &app.App, nil
2833
}
2934

30-
func (srv *projectSrv) CreateFromGit(ctx context.Context, token *api.Token, repo string) (*application.App, error) {
31-
return srv.project.CreateFromGit(ctx, repo)
35+
func (srv *projectSrv) CreateFromGit(ctx context.Context, token *api.Token, repo string) (*types.App, error) {
36+
app, err := srv.project.CreateFromGit(ctx, repo)
37+
if err != nil {
38+
return nil, err
39+
}
40+
return &app.App, nil
3241
}
3342

34-
func (srv *projectSrv) CreateFromTemplate(ctx context.Context, token *api.Token, templateName string) (*application.App, error) {
43+
func (srv *projectSrv) CreateFromTemplate(ctx context.Context, token *api.Token, templateName string) (*types.App, error) {
3544
possible, err := templates.List(srv.templatesDir)
3645
if err != nil {
3746
return nil, err
@@ -43,7 +52,11 @@ func (srv *projectSrv) CreateFromTemplate(ctx context.Context, token *api.Token,
4352
if !tpl.IsAvailable(ctx) {
4453
return nil, fmt.Errorf("template %s is not supported", templateName)
4554
}
46-
return srv.project.CreateFromTemplate(ctx, tpl)
55+
app, err := srv.project.CreateFromTemplate(ctx, tpl)
56+
if err != nil {
57+
return nil, err
58+
}
59+
return &app.App, nil
4760
}
4861

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

90-
func (srv *projectSrv) List(ctx context.Context, token *api.Token) ([]*application.App, error) {
91-
return srv.project.List(), nil
103+
func (srv *projectSrv) List(ctx context.Context, token *api.Token) ([]*types.App, error) {
104+
list := srv.project.List()
105+
var ans = make([]*types.App, len(list))
106+
for i, v := range list {
107+
ans[i] = &v.App
108+
}
109+
return ans, nil
92110
}
93111

94112
func (srv *projectSrv) Templates(ctx context.Context, token *api.Token) ([]*api.Template, error) {

application/actions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7+
"github.com/reddec/trusted-cgi/internal"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -59,10 +60,9 @@ func (app *App) InvokeAction(ctx context.Context, name string, timeLimit time.Du
5960
cmd.Stdout = &out
6061
cmd.Stderr = &out
6162
cmd.SysProcAttr = &syscall.SysProcAttr{
62-
Pdeathsig: syscall.SIGINT,
63-
Setpgid: true,
6463
Credential: app.creds,
6564
}
65+
internal.SetFlags(cmd)
6666
cmd.Env = environments
6767

6868
err := cmd.Run()

application/application.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"github.com/reddec/trusted-cgi/internal"
78
"github.com/reddec/trusted-cgi/types"
89
"io/ioutil"
910
"os"
@@ -13,13 +14,11 @@ import (
1314
"syscall"
1415
)
1516

16-
const (
17-
ManifestFile = "manifest.json"
18-
)
19-
2017
func OpenApp(location string, creds *syscall.Credential) (*App, error) {
2118
var app = &App{
22-
UID: filepath.Base(location),
19+
App: types.App{
20+
UID: filepath.Base(location),
21+
},
2322
creds: creds,
2423
location: location,
2524
}
@@ -31,8 +30,10 @@ func OpenApp(location string, creds *syscall.Credential) (*App, error) {
3130

3231
func CreateApp(location string, creds *syscall.Credential, manifest types.Manifest) (*App, error) {
3332
var app = &App{
34-
UID: filepath.Base(location),
35-
Manifest: manifest,
33+
App: types.App{
34+
UID: filepath.Base(location),
35+
Manifest: manifest,
36+
},
3637
creds: creds,
3738
location: location,
3839
}
@@ -50,10 +51,9 @@ func CreateApp(location string, creds *syscall.Credential, manifest types.Manife
5051
func CreateAppGit(ctx context.Context, location, repo, privateKey string, creds *syscall.Credential) (*App, error) {
5152
cmd := exec.CommandContext(ctx, "git", "clone", "--depth", "1", repo, location)
5253
cmd.SysProcAttr = &syscall.SysProcAttr{
53-
Pdeathsig: syscall.SIGINT,
54-
Setpgid: true,
5554
Credential: creds,
5655
}
56+
internal.SetFlags(cmd)
5757
var buffer bytes.Buffer
5858
cmd.Stderr = &buffer
5959
cmd.Stdout = os.Stdout
@@ -67,15 +67,13 @@ func CreateAppGit(ctx context.Context, location, repo, privateKey string, creds
6767
}
6868

6969
type App struct {
70-
UID string `json:"uid"`
71-
Manifest types.Manifest `json:"manifest"`
72-
IsGit bool `json:"git"`
70+
types.App
7371
creds *syscall.Credential `json:"-"`
7472
location string `json:"-"`
7573
}
7674

7775
func (app *App) ManifestFile() string {
78-
return filepath.Join(app.location, ManifestFile)
76+
return filepath.Join(app.location, internal.ManifestFile)
7977
}
8078

8179
func (app *App) ApplyOwner() error {

application/handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"github.com/reddec/trusted-cgi/internal"
78
"github.com/reddec/trusted-cgi/stats"
89
"io"
910
"io/ioutil"
@@ -144,11 +145,9 @@ func (app *App) Run(ctx context.Context,
144145
cmd.Stdout = &result
145146
cmd.Stderr = os.Stderr
146147
cmd.SysProcAttr = &syscall.SysProcAttr{
147-
Pdeathsig: syscall.SIGINT,
148-
Setpgid: true,
149148
Credential: app.creds,
150149
}
151-
150+
internal.SetFlags(cmd)
152151
var environments = os.Environ()
153152
for header, mapped := range env {
154153
environments = append(environments, header+"="+mapped)

0 commit comments

Comments
 (0)