Skip to content

Commit

Permalink
Merge pull request #234 from sdslabs/github-api-integration
Browse files Browse the repository at this point in the history
Integrate GitHub API
  • Loading branch information
Scar26 authored Mar 16, 2022
2 parents b48e9c6 + 1e5edcc commit 84374b4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 70 deletions.
16 changes: 8 additions & 8 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public_ip = "" # IPv4 address for Cloudflare's DNS records to point to.
###################################

[images]
static = "docker.io/sdsws/static:2.0"
php = "docker.io/sdsws/php:3.0"
nodejs = "docker.io/sdsws/node:2.1"
python2 = "docker.io/sdsws/python2:1.1"
python3 = "docker.io/sdsws/python3:1.1"
golang = "docker.io/sdsws/golang:1.1"
ruby = "docker.io/sdsws/ruby:1.0"
rust = "docker.io/sdsws/rust:1.0"
static = "docker.io/sdsws/static:latest"
php = "docker.io/sdsws/php:latest"
nodejs = "docker.io/sdsws/node:latest"
python2 = "docker.io/sdsws/python2:latest"
python3 = "docker.io/sdsws/python3:latest"
golang = "docker.io/sdsws/golang:latest"
ruby = "docker.io/sdsws/ruby:latest"
rust = "docker.io/sdsws/rust:latest"
mysql = "docker.io/wangxian/alpine-mysql:latest"
mongodb = "docker.io/sdsws/alpine-mongo:latest"
postgresql = "docker.io/postgres:12.2-alpine"
Expand Down
58 changes: 17 additions & 41 deletions lib/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,10 @@ import (

"github.com/sdslabs/gasper/configs"
"github.com/sdslabs/gasper/lib/docker"
"github.com/sdslabs/gasper/lib/git"
"github.com/sdslabs/gasper/lib/utils"
"github.com/sdslabs/gasper/types"
gogit "gopkg.in/src-d/go-git.v4"
)

func cloneRepo(app types.Application, storedir string, clone chan types.ResponseError) {
err := os.MkdirAll(storedir, 0755)
if err != nil {
clone <- types.NewResErr(500, "storage directory not created", err)
return
}

if app.HasGitAccessToken() {
err = git.CloneWithToken(
app.GetGitRepositoryURL(),
app.GetGitRepositoryBranch(),
storedir,
app.GetGitAccessToken(),
)
} else {
err = git.Clone(
app.GetGitRepositoryURL(),
app.GetGitRepositoryBranch(),
storedir,
)
}

if err != nil {
if err == gogit.ErrRepositoryAlreadyExists {
clone <- types.NewResErr(500, "repository already exists", err)
} else {
clone <- types.NewResErr(500, "repository not cloned", err)
}
return
}
clone <- nil
}

func setupContainer(app types.Application, storedir string, setup chan types.ResponseError) {
confFileName := fmt.Sprintf("%s.gasper.conf", app.GetName())
workdir := fmt.Sprintf("%s/%s", configs.GasperConfig.ProjectRoot, app.GetName())
Expand Down Expand Up @@ -106,15 +71,11 @@ func createBasicApplication(app types.Application) []types.ResponseError {
storepath, _ := os.Getwd()
storedir := filepath.Join(storepath, fmt.Sprintf("storage/%s", app.GetName()))
setup := make(chan types.ResponseError)
clone := make(chan types.ResponseError)

// Step 1: clone the repo in the storage
go cloneRepo(app, storedir, clone)

// Step 2: setup the container
// Step 1: setup the container
go setupContainer(app, storedir, setup)

return []types.ResponseError{<-setup, <-clone}
return []types.ResponseError{<-setup}
}

// SetupApplication sets up a basic container for the application with all the prerequisites
Expand All @@ -134,6 +95,21 @@ func SetupApplication(app types.Application) types.ResponseError {
}
}

_, err = docker.ExecProcess(app.GetContainerID(), []string{"git", "init"})
if err != nil {
return types.NewResErr(500, "git init unsuccessful", err)
}

_, err = docker.ExecProcess(app.GetContainerID(), []string{"git", "remote", "add", "origin", app.GetGitRepositoryURL()})
if err != nil {
return types.NewResErr(500, "setting remote unsuccessful", err)
}

_, err = docker.ExecProcess(app.GetContainerID(), []string{"git", "pull", "origin", app.GetGitRepositoryBranch()})
if err != nil {
return types.NewResErr(500, "pulling contents unsuccessful", err)
}

if app.HasRcFile() {
cmd := []string{"sh", "-c",
fmt.Sprintf(`chmod 755 ./%s &> /proc/1/fd/1 && ./%s &> /proc/1/fd/1`,
Expand Down
27 changes: 6 additions & 21 deletions services/appmaker/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func (s *server) Create(ctx context.Context, body *pb.RequestBody) (*pb.Response
maxCount := configs.ServiceConfig.AppMaker.AppLimit
rateCount := configs.ServiceConfig.RateLimit
timeInterval := configs.ServiceConfig.RateInterval
if user.IsAdmin() == false && maxCount >= 0 {
if !user.IsAdmin() && maxCount >= 0 {
rateLimitCount := mongo.CountInstanceInTimeFrame(body.GetOwner(), mongo.AppInstance, timeInterval)
totalCount := mongo.CountInstancesByUser(body.GetOwner(), mongo.AppInstance)
if totalCount < maxCount {
if rateLimitCount >= rateCount && rateCount >= 0 {
return nil, fmt.Errorf("Cannot deploy more than %d app instances in %d hours", rateCount, timeInterval)
return nil, fmt.Errorf("cannot deploy more than %d app instances in %d hours", rateCount, timeInterval)
}
} else {
return nil, fmt.Errorf("Cannot deploy more than %d app instances", maxCount)
return nil, fmt.Errorf("cannot deploy more than %d app instances", maxCount)
}
}

Expand All @@ -70,7 +70,7 @@ func (s *server) Create(ctx context.Context, body *pb.RequestBody) (*pb.Response
}

if pipeline[language] == nil {
return nil, fmt.Errorf("Language `%s` is not supported", language)
return nil, fmt.Errorf("language `%s` is not supported", language)
}
resErr := pipeline[language].create(app)
if resErr != nil {
Expand Down Expand Up @@ -142,33 +142,18 @@ func (s *server) Create(ctx context.Context, body *pb.RequestBody) (*pb.Response
// Rebuild rebuilds an application
func (s *server) Rebuild(ctx context.Context, body *pb.NameHolder) (*pb.ResponseBody, error) {
appName := body.GetName()

app, err := mongo.FetchSingleApp(appName)
if err != nil {
return nil, err
}

diskCleanup(appName)

if pipeline[app.Language] == nil {
return nil, fmt.Errorf("Non-supported language `%s` specified for `%s`", app.Language, appName)
}
pullChanges := []string{"git", "pull", "origin", app.GetGitRepositoryBranch()}
_, err = docker.ExecProcess(app.ContainerID, pullChanges)

resErr := pipeline[app.Language].create(app)
if resErr != nil {
if resErr.Message() != "repository already exists" && resErr.Message() != "container not created" {
go diskCleanup(app.GetName())
}
return nil, fmt.Errorf(resErr.Error())
}

err = mongo.UpdateInstance(types.M{mongo.NameKey: appName}, app)
if err != nil {
return nil, err
}

app.SetSuccess(true)

response, err := json.Marshal(app)
return &pb.ResponseBody{Data: response}, err
}
Expand Down

0 comments on commit 84374b4

Please sign in to comment.