Skip to content

Commit c88352d

Browse files
authored
Merge pull request #244 from sdslabs/direct-deploy-final
Deploy local applications through gctl
2 parents cb4f1bd + 5c4aa6d commit c88352d

File tree

11 files changed

+171
-68
lines changed

11 files changed

+171
-68
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ site/
1717
coverage.txt
1818
releases/
1919
gasper*.log
20-
vendor
20+
vendor/

config.sample.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,13 @@ entrypoint_ip = ""
279279

280280
[services.jikan]
281281
deploy = false # Deploy Jikan?
282-
port = 3333
282+
port = 3333
283+
284+
############################
285+
# Github Configuration #
286+
############################
287+
288+
[github]
289+
username = "gasper-github-username"
290+
email = "gasper-mail-id"
291+
pat = "personal-access-token"

configs/project_configs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ var (
3535
// JWTConfig is the configuration for json web auth token
3636
JWTConfig = GasperConfig.JWT
3737

38+
//GithubConfig is the authentication configuration for the Gasper Github user
39+
GithubConfig = GasperConfig.Github
40+
3841
// ServiceMap is the configuration binding the service name to its
3942
// deployment status and port
4043
ServiceMap = map[string]*GenericService{

configs/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ type Services struct {
143143
Jikan JikanService `toml:"jikan"`
144144
}
145145

146+
type Github struct {
147+
Username string `toml:"username"`
148+
Email string `toml:"email"`
149+
PAT string `toml:"pat"`
150+
}
151+
146152
// GasperCfg is the configuration for the entire project
147153
type GasperCfg struct {
148154
Debug bool `toml:"debug"`
@@ -159,4 +165,5 @@ type GasperCfg struct {
159165
Redis Redis `toml:"redis"`
160166
Images Images `toml:"images"`
161167
Services Services `toml:"services"`
168+
Github Github `toml:"github"`
162169
}

go.mod

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/sdslabs/gasper
22

33
require (
44
github.com/BurntSushi/toml v0.3.1
5-
github.com/Microsoft/go-winio v0.4.14 // indirect
5+
github.com/Microsoft/go-winio v0.4.16 // indirect
66
github.com/NYTimes/gziphandler v1.1.1 // indirect
77
github.com/alphadose/gotty v0.0.0-20191208194000-a33c4414c39e
88
github.com/anmitsu/go-shlex v0.0.0-20200502080107-070676123096 // indirect
@@ -22,29 +22,35 @@ require (
2222
github.com/go-redis/redis v6.15.7+incompatible
2323
github.com/go-sql-driver/mysql v1.5.0
2424
github.com/golang/protobuf v1.4.1
25+
github.com/google/go-github/v41 v41.0.0
2526
github.com/google/uuid v1.1.1
2627
github.com/gorilla/websocket v1.4.2 // indirect
2728
github.com/hashicorp/go-multierror v1.1.0 // indirect
2829
github.com/jackc/pgx/v4 v4.6.0
30+
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
2931
github.com/klauspost/compress v1.10.5 // indirect
3032
github.com/kr/pty v1.1.8
33+
github.com/kr/text v0.2.0 // indirect
3134
github.com/miekg/dns v1.1.29
3235
github.com/onsi/ginkgo v1.10.3 // indirect
3336
github.com/onsi/gomega v1.7.1 // indirect
3437
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
35-
github.com/pkg/errors v0.9.1 // indirect
3638
github.com/sdslabs/gin-jwt v1.0.0
3739
github.com/sergi/go-diff v1.1.0 // indirect
40+
github.com/stretchr/testify v1.7.0 // indirect
3841
github.com/urfave/cli v1.22.4 // indirect
42+
github.com/xanzy/ssh-agent v0.3.0 // indirect
3943
github.com/xdg/stringprep v1.0.0 // indirect
4044
go.mongodb.org/mongo-driver v1.3.3
41-
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79
42-
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f
45+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
46+
golang.org/x/net v0.0.0-20210326060303-6b1517762897
47+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
4348
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
44-
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
4549
google.golang.org/genproto v0.0.0-20200507105951-43844f6eee31 // indirect
4650
google.golang.org/grpc v1.29.1
51+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
4752
gopkg.in/src-d/go-git.v4 v4.13.1
53+
gopkg.in/yaml.v2 v2.3.0 // indirect
4854
)
4955

5056
go 1.14

go.sum

Lines changed: 45 additions & 59 deletions
Large diffs are not rendered by default.

lib/factory/application_manager.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package factory
33
import (
44
"context"
55

6+
"github.com/google/go-github/v41/github"
7+
"github.com/sdslabs/gasper/configs"
68
pb "github.com/sdslabs/gasper/lib/factory/protos/application"
9+
"github.com/sdslabs/gasper/types"
10+
"golang.org/x/oauth2"
711
"google.golang.org/grpc"
812
)
913

@@ -119,3 +123,29 @@ func NewApplicationFactory(bindings pb.ApplicationFactoryServer) *grpc.Server {
119123
pb.RegisterApplicationFactoryServer(srv, bindings)
120124
return srv
121125
}
126+
127+
// CreateGithubRepository returns a git clone URL after creating a new repository
128+
func CreateGithubRepository(repoName string) (*types.RepositoryResponse, error) {
129+
tc := oauth2.NewClient(
130+
context.Background(),
131+
oauth2.StaticTokenSource(
132+
&oauth2.Token{
133+
AccessToken: configs.GithubConfig.PAT, //PAT
134+
},
135+
),
136+
)
137+
client := github.NewClient(tc)
138+
repo := &github.Repository{
139+
Name: github.String(repoName),
140+
Private: github.Bool(true),
141+
}
142+
repo, _, err := client.Repositories.Create(context.Background(), "", repo)
143+
response := &types.RepositoryResponse{
144+
CloneURL: *repo.CloneURL,
145+
PAT: configs.GithubConfig.PAT,
146+
Username: configs.GithubConfig.Username,
147+
Repository: repoName,
148+
Email: configs.GithubConfig.Email,
149+
}
150+
return response, err
151+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package controllers
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
_ "io/ioutil"
8+
9+
"github.com/gin-gonic/gin"
10+
"github.com/sdslabs/gasper/lib/factory"
11+
"github.com/sdslabs/gasper/lib/utils"
12+
"github.com/sdslabs/gasper/services/master/middlewares"
13+
"github.com/sdslabs/gasper/types"
14+
)
15+
16+
// Endpoint to create repository in GitHub
17+
func CreateRepository(c *gin.Context) {
18+
raw, err := c.GetRawData()
19+
if err != nil {
20+
c.AbortWithStatusJSON(400, gin.H{
21+
"success": false,
22+
"error": err.Error(),
23+
})
24+
}
25+
26+
var data *types.RepositoryRequest = &types.RepositoryRequest{}
27+
claims := middlewares.ExtractClaims(c)
28+
if claims == nil {
29+
utils.SendServerErrorResponse(c, errors.New("failed to extract JWT claims"))
30+
return
31+
}
32+
err = json.Unmarshal(raw, data)
33+
if err != nil {
34+
c.AbortWithStatusJSON(400, gin.H{
35+
"success": false,
36+
"error": err.Error(),
37+
})
38+
}
39+
response, err := factory.CreateGithubRepository(claims.Username + data.Name)
40+
if err != nil {
41+
c.AbortWithStatusJSON(400, gin.H{
42+
"success": false,
43+
"error": err.Error(),
44+
})
45+
}
46+
responseBody := new(bytes.Buffer)
47+
json.NewEncoder(responseBody).Encode(response)
48+
c.Data(200, "application/json", responseBody.Bytes())
49+
}

services/master/controllers/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func DeleteUser(c *gin.Context) {
149149
deleteUser(c, claims.GetEmail())
150150
}
151151

152-
//GctlLogin validates the email id and alllow user to login in gctl
152+
// GctlLogin validates the email id and alllow user to login in gctl
153153
func GctlLogin(c *gin.Context) {
154154
claims := jwt.ExtractClaims(c)
155155
if claims == nil {
@@ -186,7 +186,7 @@ func GctlLogin(c *gin.Context) {
186186
})
187187
}
188188

189-
//RevokeToken updates the uuid of user so that gctl token gets invalidated
189+
// RevokeToken updates the uuid of user so that gctl token gets invalidated
190190
func RevokeToken(c *gin.Context) {
191191
auth := &types.Login{}
192192
if err := c.ShouldBind(auth); err != nil {

services/master/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func NewService() http.Handler {
5757

5858
router.GET("/instances", m.AuthRequired(), c.FetchAllInstancesByUser)
5959
router.POST("/gctllogin", m.JWTGctl.MiddlewareFunc(), c.GctlLogin)
60+
router.POST("/github", m.AuthRequired(), c.CreateRepository)
6061

6162
app := router.Group("/apps")
6263
app.Use(m.AuthRequired())

0 commit comments

Comments
 (0)