Skip to content

Commit

Permalink
Merge pull request #244 from sdslabs/direct-deploy-final
Browse files Browse the repository at this point in the history
Deploy local applications through gctl
  • Loading branch information
mhk19 authored Mar 14, 2023
2 parents cb4f1bd + 5c4aa6d commit c88352d
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ site/
coverage.txt
releases/
gasper*.log
vendor
vendor/
11 changes: 10 additions & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,13 @@ entrypoint_ip = ""

[services.jikan]
deploy = false # Deploy Jikan?
port = 3333
port = 3333

############################
# Github Configuration #
############################

[github]
username = "gasper-github-username"
email = "gasper-mail-id"
pat = "personal-access-token"
3 changes: 3 additions & 0 deletions configs/project_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var (
// JWTConfig is the configuration for json web auth token
JWTConfig = GasperConfig.JWT

//GithubConfig is the authentication configuration for the Gasper Github user
GithubConfig = GasperConfig.Github

// ServiceMap is the configuration binding the service name to its
// deployment status and port
ServiceMap = map[string]*GenericService{
Expand Down
7 changes: 7 additions & 0 deletions configs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ type Services struct {
Jikan JikanService `toml:"jikan"`
}

type Github struct {
Username string `toml:"username"`
Email string `toml:"email"`
PAT string `toml:"pat"`
}

// GasperCfg is the configuration for the entire project
type GasperCfg struct {
Debug bool `toml:"debug"`
Expand All @@ -159,4 +165,5 @@ type GasperCfg struct {
Redis Redis `toml:"redis"`
Images Images `toml:"images"`
Services Services `toml:"services"`
Github Github `toml:"github"`
}
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/sdslabs/gasper

require (
github.com/BurntSushi/toml v0.3.1
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/alphadose/gotty v0.0.0-20191208194000-a33c4414c39e
github.com/anmitsu/go-shlex v0.0.0-20200502080107-070676123096 // indirect
Expand All @@ -22,29 +22,35 @@ require (
github.com/go-redis/redis v6.15.7+incompatible
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.4.1
github.com/google/go-github/v41 v41.0.0
github.com/google/uuid v1.1.1
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/jackc/pgx/v4 v4.6.0
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/klauspost/compress v1.10.5 // indirect
github.com/kr/pty v1.1.8
github.com/kr/text v0.2.0 // indirect
github.com/miekg/dns v1.1.29
github.com/onsi/ginkgo v1.10.3 // indirect
github.com/onsi/gomega v1.7.1 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sdslabs/gin-jwt v1.0.0
github.com/sergi/go-diff v1.1.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/urfave/cli v1.22.4 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/xdg/stringprep v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.3.3
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
golang.org/x/net v0.0.0-20210326060303-6b1517762897
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
google.golang.org/genproto v0.0.0-20200507105951-43844f6eee31 // indirect
google.golang.org/grpc v1.29.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.3.0 // indirect
)

go 1.14
104 changes: 45 additions & 59 deletions go.sum

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions lib/factory/application_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package factory
import (
"context"

"github.com/google/go-github/v41/github"
"github.com/sdslabs/gasper/configs"
pb "github.com/sdslabs/gasper/lib/factory/protos/application"
"github.com/sdslabs/gasper/types"
"golang.org/x/oauth2"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -119,3 +123,29 @@ func NewApplicationFactory(bindings pb.ApplicationFactoryServer) *grpc.Server {
pb.RegisterApplicationFactoryServer(srv, bindings)
return srv
}

// CreateGithubRepository returns a git clone URL after creating a new repository
func CreateGithubRepository(repoName string) (*types.RepositoryResponse, error) {
tc := oauth2.NewClient(
context.Background(),
oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: configs.GithubConfig.PAT, //PAT
},
),
)
client := github.NewClient(tc)
repo := &github.Repository{
Name: github.String(repoName),
Private: github.Bool(true),
}
repo, _, err := client.Repositories.Create(context.Background(), "", repo)
response := &types.RepositoryResponse{
CloneURL: *repo.CloneURL,
PAT: configs.GithubConfig.PAT,
Username: configs.GithubConfig.Username,
Repository: repoName,
Email: configs.GithubConfig.Email,
}
return response, err
}
49 changes: 49 additions & 0 deletions services/master/controllers/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package controllers

import (
"bytes"
"encoding/json"
"errors"
_ "io/ioutil"

"github.com/gin-gonic/gin"
"github.com/sdslabs/gasper/lib/factory"
"github.com/sdslabs/gasper/lib/utils"
"github.com/sdslabs/gasper/services/master/middlewares"
"github.com/sdslabs/gasper/types"
)

// Endpoint to create repository in GitHub
func CreateRepository(c *gin.Context) {
raw, err := c.GetRawData()
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}

var data *types.RepositoryRequest = &types.RepositoryRequest{}
claims := middlewares.ExtractClaims(c)
if claims == nil {
utils.SendServerErrorResponse(c, errors.New("failed to extract JWT claims"))
return
}
err = json.Unmarshal(raw, data)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
response, err := factory.CreateGithubRepository(claims.Username + data.Name)
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"success": false,
"error": err.Error(),
})
}
responseBody := new(bytes.Buffer)
json.NewEncoder(responseBody).Encode(response)
c.Data(200, "application/json", responseBody.Bytes())
}
4 changes: 2 additions & 2 deletions services/master/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func DeleteUser(c *gin.Context) {
deleteUser(c, claims.GetEmail())
}

//GctlLogin validates the email id and alllow user to login in gctl
// GctlLogin validates the email id and alllow user to login in gctl
func GctlLogin(c *gin.Context) {
claims := jwt.ExtractClaims(c)
if claims == nil {
Expand Down Expand Up @@ -186,7 +186,7 @@ func GctlLogin(c *gin.Context) {
})
}

//RevokeToken updates the uuid of user so that gctl token gets invalidated
// RevokeToken updates the uuid of user so that gctl token gets invalidated
func RevokeToken(c *gin.Context) {
auth := &types.Login{}
if err := c.ShouldBind(auth); err != nil {
Expand Down
1 change: 1 addition & 0 deletions services/master/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewService() http.Handler {

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

app := router.Group("/apps")
app.Use(m.AuthRequired())
Expand Down
12 changes: 12 additions & 0 deletions types/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ type Resources struct {
CPU float64 `json:"cpu" bson:"cpu" valid:"float~Field 'cpu' inside field 'resources' should be of type float"`
}

type RepositoryRequest struct {
Name string `json:"name" bson:"name" valid:"required~Field 'name' is required but was not provided,alphanum~Field 'name' should only have alphanumeric characters,stringlength(3|40)~Field 'name' should have length between 3 to 40 characters,lowercase~Field 'name' should have only lowercase characters"`
}

type RepositoryResponse struct {
CloneURL string `json:"cloneurl" bson:"cloneurl"`
PAT string `json:"pat" bson:"pat"`
Username string `json:"username" bson:"username"`
Repository string `json:"repository" bson:"repository"`
Email string `json:"email" bson:"email"`
}

// ApplicationConfig is the configuration required for creating an application
type ApplicationConfig struct {
Name string `json:"name" bson:"name" valid:"required~Field 'name' is required but was not provided,alphanum~Field 'name' should only have alphanumeric characters,stringlength(3|40)~Field 'name' should have length between 3 to 40 characters,lowercase~Field 'name' should have only lowercase characters"`
Expand Down

0 comments on commit c88352d

Please sign in to comment.