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

Build Apps using custom docker images #264

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/content/api/specs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
"type": "string",
"description": "Password required for SSH access to the application's docker container"
},
"docker_image": {
"type": "",
"description": "Docker image used in building the application's container"
},
"git": {
"$ref": "#/components/schemas/Git"
},
Expand Down
4 changes: 4 additions & 0 deletions docs/content/api/specs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ components:
password:
type: string
description: Password required for SSH access to the application's docker container
docker_image:
type: string
description: Docker image used in building the application's container
example: sdsws/node:2.0
git:
$ref: '#/components/schemas/Git'
context:
Expand Down
17 changes: 0 additions & 17 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@ import (
"google.golang.org/grpc"
)

func checkAndPullImages(imageList ...string) {
availableImages, err := docker.ListImages()
if err != nil {
utils.LogError("Main-Helper-1", err)
os.Exit(1)
}
for _, image := range imageList {
imageWithoutRepoName := strings.Replace(image, "docker.io/", "", -1)
if utils.Contains(availableImages, image) || utils.Contains(availableImages, imageWithoutRepoName) {
continue
}
utils.LogInfo("Main-Helper-2", "Image %s not present locally, pulling from DockerHUB", image)
if err = docker.DirectPull(image); err != nil {
utils.LogError("Main-Helper-3", err)
}
}
}

func startGrpcServer(server *grpc.Server, port int) error {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
Expand Down
6 changes: 3 additions & 3 deletions lib/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func setupContainer(app types.Application, storedir string, setup chan types.Res
Name: app.GetName(),
Image: app.GetDockerImage(),
ApplicationPort: app.GetApplicationPort(),
ContainerPort: app.GetContainerPort(),
ContainerPort: app.GetContainerPort(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove extra space

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ye formatter ne daal diya hoga,
I will remove it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iska commit daalde baaki LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

WorkDir: workdir,
StoreDir: storedir,
Env: app.GetEnvVars(),
Expand Down Expand Up @@ -67,7 +67,7 @@ func setupContainer(app types.Application, storedir string, setup chan types.Res
}

// createBasicApplication spawns a new container with the application of a particular service
func createBasicApplication(app types.Application) []types.ResponseError {
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)
Expand All @@ -87,7 +87,7 @@ func SetupApplication(app types.Application) types.ResponseError {

app.SetContainerPort(containerPort)

errList := createBasicApplication(app)
errList := CreateBasicApplication(app)

for _, err := range errList {
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions lib/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,32 @@ import (
"io"
"os"
"os/exec"
"strings"

"github.com/docker/docker/api/types"
"github.com/sdslabs/gasper/lib/utils"
"golang.org/x/net/context"
)

// Check for available images and pull if not present
func CheckAndPullImages(imageList ...string) {
itsdarshankumar marked this conversation as resolved.
Show resolved Hide resolved
availableImages, err := ListImages()
if err != nil {
utils.LogError("Main-Helper-1", err)
os.Exit(1)
}
for _, image := range imageList {
imageWithoutRepoName := strings.Replace(image, "docker.io/", "", -1)
if utils.Contains(availableImages, image) || utils.Contains(availableImages, imageWithoutRepoName) {
continue
}
utils.LogInfo("Main-Helper-2", "Image %s not present locally, pulling from DockerHUB", image)
if err = DirectPull(image); err != nil {
utils.LogError("Main-Helper-3", err)
}
}
}

// ListImages function returns a list of docker images present in the system
func ListImages() ([]string, error) {
ctx := context.Background()
Expand Down
15 changes: 8 additions & 7 deletions service_launchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"runtime"

"github.com/sdslabs/gasper/configs"
"github.com/sdslabs/gasper/lib/docker"
"github.com/sdslabs/gasper/lib/utils"
"github.com/sdslabs/gasper/services/appmaker"
"github.com/sdslabs/gasper/services/dbmaker"
Expand Down Expand Up @@ -59,19 +60,19 @@ var launcherBindings = map[string]*serviceLauncher{

func startDbMakerService() error {
if configs.ServiceConfig.DbMaker.MySQL.PlugIn {
checkAndPullImages(configs.ImageConfig.Mysql)
docker.CheckAndPullImages(configs.ImageConfig.Mysql)
setupDatabaseContainer(types.MySQL)
}
if configs.ServiceConfig.DbMaker.MongoDB.PlugIn {
checkAndPullImages(configs.ImageConfig.Mongodb)
docker.CheckAndPullImages(configs.ImageConfig.Mongodb)
setupDatabaseContainer(types.MongoDB)
}
if configs.ServiceConfig.DbMaker.PostgreSQL.PlugIn {
checkAndPullImages(configs.ImageConfig.Postgresql)
docker.CheckAndPullImages(configs.ImageConfig.Postgresql)
setupDatabaseContainer(types.PostgreSQL)
}
if configs.ServiceConfig.DbMaker.Redis.PlugIn {
checkAndPullImages(configs.ImageConfig.Redis)
docker.CheckAndPullImages(configs.ImageConfig.Redis)
}
return startGrpcServer(dbmaker.NewService(), configs.ServiceConfig.DbMaker.Port)
}
Expand All @@ -87,17 +88,17 @@ func startAppMakerService() error {
configs.ImageConfig.Ruby,
configs.ImageConfig.Rust,
}
checkAndPullImages(images...)
docker.CheckAndPullImages(images...)
return startGrpcServer(appmaker.NewService(), configs.ServiceConfig.AppMaker.Port)
}

func startMasterService() error {
if configs.ServiceConfig.Master.MongoDB.PlugIn {
checkAndPullImages(configs.ImageConfig.Mongodb)
docker.CheckAndPullImages(configs.ImageConfig.Mongodb)
setupDatabaseContainer(types.MongoDBGasper)
}
if configs.ServiceConfig.Master.Redis.PlugIn {
checkAndPullImages(configs.ImageConfig.Redis)
docker.CheckAndPullImages(configs.ImageConfig.Redis)
setupDatabaseContainer(types.RedisGasper)
}
return buildHTTPServer(master.NewService(), configs.ServiceConfig.Master.Port).ListenAndServe()
Expand Down
34 changes: 27 additions & 7 deletions services/appmaker/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/sdslabs/gasper/configs"
"github.com/sdslabs/gasper/lib/api"
"github.com/sdslabs/gasper/lib/cloudflare"
"github.com/sdslabs/gasper/lib/docker"
"github.com/sdslabs/gasper/lib/factory"
Expand Down Expand Up @@ -68,18 +69,37 @@ 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)
}
resErr := pipeline[language].create(app)
if resErr != nil {
if resErr.Message() != "repository already exists" && resErr.Message() != "container already exists" {
go diskCleanup(app.GetName())
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)
}
return nil, fmt.Errorf(resErr.Error())
}
app.SetContainerPort(containerPort)

errList := api.CreateBasicApplication(app)
for _, err := range errList {
if err != nil {
return nil, err
}
}
} else {
utils.LogError("AppMaker-Controller-1", fmt.Errorf("git img"))
resErr := pipeline[language].create(app)
if resErr != nil {
if resErr.Message() != "repository already exists" && resErr.Message() != "container already exists" {
go diskCleanup(app.GetName())
}
return nil, fmt.Errorf(resErr.Error())
}
}
sshEntrypointIP := configs.ServiceConfig.GenSSH.EntrypointIP
if len(sshEntrypointIP) == 0 {
sshEntrypointIP = utils.HostIP
Expand Down