Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix app rebuild route #275

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
14 changes: 13 additions & 1 deletion lib/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,17 @@ func SetupApplication(app types.Application) types.ResponseError {
return types.NewResErr(500, "pulling contents unsuccessful", err)
}

return RunBuildAndStartCommands(app)

}

func RunBuildAndStartCommands(app types.Application) types.ResponseError {
if app.HasRcFile() {
cmd := []string{"sh", "-c",
fmt.Sprintf(`chmod 755 ./%s &> /proc/1/fd/1 && ./%s &> /proc/1/fd/1`,
configs.GasperConfig.RcFile, configs.GasperConfig.RcFile)}

_, err = docker.ExecDetachedProcess(app.GetContainerID(), cmd)
_, err := docker.ExecDetachedProcess(app.GetContainerID(), cmd)
if err != nil {
// this error cannot be ignored; the chances of error here are very less
// but if an error arises, this means there's some issue with "execing"
Expand All @@ -134,6 +139,13 @@ func SetupApplication(app types.Application) types.ResponseError {
} else {
go buildAndRun(app)
}
return nil
}

func StopAllProcesses(app types.Application) types.ResponseError {
_, err := docker.ExecProcessWthStream(app.GetContainerID(), []string{"kill", "-TERM", "-1"})
if err != nil {
return types.NewResErr(500, "Unable to stop processes running inside the container", err)
}
return nil
}
15 changes: 9 additions & 6 deletions services/appmaker/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ func (s *server) Create(ctx context.Context, body *pb.RequestBody) (*pb.Response
utils.LogError("AppMaker-Controller-1", fmt.Errorf("GenDNS instance %s is of invalid format", nameServer))
}
}

if pipeline[language] == nil {
return nil, fmt.Errorf("language `%s` is not supported", language)
}
utils.LogError("AppMaker-Controller-1", fmt.Errorf("%s", app.GetDockerImage()))

if app.GetDockerImage() != "" {
utils.LogError("AppMaker-Controller-1", fmt.Errorf("docker img"))
docker.CheckAndPullImages(app.GetDockerImage())
containerPort, err := utils.GetFreePort()
if err != nil {
return nil, types.NewResErr(500, "No free port available", err)
}
app.SetContainerPort(containerPort)
app.SetContainerPort(containerPort)

errList := api.CreateBasicApplication(app)
for _, err := range errList {
Expand Down Expand Up @@ -159,17 +159,20 @@ func (s *server) Create(ctx context.Context, body *pb.RequestBody) (*pb.Response
return &pb.ResponseBody{Data: response}, err
}

// Rebuild rebuilds an application
// Rebuild stops all currently running processes ,rebuilds and restarts 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
}

pullChanges := []string{"git", "pull", "origin", app.GetGitRepositoryBranch()}
_, err = docker.ExecProcess(app.ContainerID, pullChanges)
err = api.StopAllProcesses(app)
if err != nil {
return nil, err
}

err = api.RunBuildAndStartCommands(app)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions services/master/controllers/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func BulkUpdateApps(c *gin.Context) {

// UpdateAppByName updates the app getting name from url params
func UpdateAppByName(c *gin.Context) {
app := c.Param("app")
appName := c.Param("app")
filter := types.M{
mongo.NameKey: app,
mongo.NameKey: appName,
mongo.InstanceTypeKey: mongo.AppInstance,
}
var data types.M
Expand Down