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

feat: add docker compose yaml #9

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM ubuntu:22.04

RUN apt-get update && \
apt install mysql-server -y && \
apt-get install ca-certificates curl gnupg -y && \
install -m 0755 -d /etc/apt/keyrings && \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \
chmod a+r /etc/apt/keyrings/docker.gpg && \
echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null && \
apt-get update && \
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
10 changes: 10 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ package main
import (
"k8s.io/klog"

"opennaslab.io/bifrost/cmd/options"
"opennaslab.io/bifrost/pkg/database"
"opennaslab.io/bifrost/pkg/server"
)

func main() {
opt := options.NewBifrostDBOptions()
if err := opt.Validate(); err != nil {
klog.Errorf("validate bifrost db options failed:%v", err)
return
}
if err := database.DatabaseConnectionInit(opt); err != nil {
klog.Errorf("init bifrost db failed:%v", err)
}
router := server.NewServerRouter()
if err := router.Run(":8080"); err != nil {
klog.Errorf("run server failed:%v", err)
Expand Down
59 changes: 59 additions & 0 deletions cmd/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2023 The opennaslab Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicabl e law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package options

import (
"fmt"
"os"
)

type BifrostDBOptions struct {
DBUser string
DBPassword string
DBHost string
DBPort string
DBSchema string
}

func NewBifrostDBOptions() *BifrostDBOptions {
return &BifrostDBOptions{
DBUser: os.Getenv("DB_USER"),
DBPassword: os.Getenv("DB_PASSWORD"),
DBHost: os.Getenv("DB_HOST"),
DBPort: os.Getenv("DB_PORT"),
DBSchema: os.Getenv("DB_SCHEMA"),
}
}

func (o *BifrostDBOptions) Validate() error {
if o.DBUser == "" {
return fmt.Errorf("DB_USER is required")
}
if o.DBPassword == "" {
return fmt.Errorf("DB_PASSWORD is required")
}
if o.DBHost == "" {
return fmt.Errorf("DB_HOST is required")
}
if o.DBPort == "" {
return fmt.Errorf("DB_PORT is required")
}
if o.DBSchema == "" {
return fmt.Errorf("DB_SCHEMA is required")
}
return nil
}
17 changes: 17 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
bifrost:
build: ./
volumes:
- ${HOME}/.bifrost:/root/.bifrost
ports:
- "8080:8080"
environment:
- DB_USER=admin
- DB_PASSWORD=admin
- DB_HOST=127.0.0.1
- DB_PORT=5432
- DB_SCHEMA=bifrost
image: opennaslab/bifrost:latest
privileged: true
restart: always
network_mode: "host"
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down Expand Up @@ -66,6 +67,7 @@ require (
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gorm.io/driver/mysql v1.5.1 // indirect
gorm.io/gorm v1.25.4 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
Expand Down Expand Up @@ -269,6 +271,9 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw=
gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o=
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"time"

"gorm.io/gorm"

"opennaslab.io/bifrost/pkg/api"
"opennaslab.io/bifrost/pkg/container"
"opennaslab.io/bifrost/pkg/customapi"
Expand Down Expand Up @@ -53,8 +54,8 @@
func (w *WorkflowQueue) Run() {
for {
w.mutex.Lock()
defer w.mutex.Unlock()

Check failure on line 57 in pkg/controller/workflow.go

View workflow job for this annotation

GitHub Actions / lint

SA5003: defers in this infinite loop will never run (staticcheck)
for name, _ := range w.Workflow {

Check failure on line 58 in pkg/controller/workflow.go

View workflow job for this annotation

GitHub Actions / lint

S1005: unnecessary assignment to the blank identifier (gosimple)
requeue, err := w.Reconcile(name)
if err == nil && !requeue {
delete(w.Workflow, name)
Expand Down
43 changes: 43 additions & 0 deletions pkg/database/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2023 The opennaslab Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicabl e law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package database

import (
"fmt"

"gorm.io/driver/mysql"
"gorm.io/gorm"

"opennaslab.io/bifrost/cmd/options"
)

var dbConnection *gorm.DB

func DatabaseConnectionInit(opts *options.BifrostDBOptions) error {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
opts.DBUser, opts.DBPassword, opts.DBHost, opts.DBPort, opts.DBSchema)

var err error
dbConnection, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
return err
}
if err := InitWorkflow(dbConnection); err != nil {
return err
}
return nil
}
3 changes: 2 additions & 1 deletion pkg/database/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import (
"errors"

"gorm.io/gorm"

"opennaslab.io/bifrost/pkg/api"
)

type Workflow struct {
Id string `gorm:"column:id;type:integer;autoIncrement;primary_key"`
Name string `gorm:"column:name;type:text"`
Manifest string `gorm:"column:deploy_yaml;type:text"`
Manifest string `gorm:"column:manifest;type:text"`
}

func (w Workflow) TableName() string {
Expand Down
52 changes: 52 additions & 0 deletions pkg/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

"opennaslab.io/bifrost/pkg/api"
"opennaslab.io/bifrost/pkg/customapi"
"opennaslab.io/bifrost/pkg/database"
)

func ListLocalStepsHandler(ctx *gin.Context) {
Expand All @@ -32,7 +33,7 @@
respData, err := json.Marshal(steps)
if err != nil {
klog.Errorf("Marshal local steps failed:%v", err)
ctx.AbortWithError(http.StatusInternalServerError, err)

Check failure on line 36 in pkg/server/handler.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `ctx.AbortWithError` is not checked (errcheck)
return
}
ctx.Data(http.StatusOK, "application/json", respData)
Expand All @@ -43,7 +44,7 @@
respData, err := json.Marshal(steps)
if err != nil {
klog.Errorf("Marshal remote steps failed:%v", err)
ctx.AbortWithError(http.StatusInternalServerError, err)

Check failure on line 47 in pkg/server/handler.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `ctx.AbortWithError` is not checked (errcheck)
return
}
ctx.Data(http.StatusOK, "application/json", respData)
Expand All @@ -58,7 +59,7 @@
respData, err := json.Marshal(step)
if err != nil {
klog.Errorf("Marshal local steps failed:%v", err)
ctx.AbortWithError(http.StatusInternalServerError, err)

Check failure on line 62 in pkg/server/handler.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `ctx.AbortWithError` is not checked (errcheck)
return
}
ctx.Data(http.StatusOK, "application/json", respData)
Expand All @@ -81,7 +82,7 @@

func CreateOrUpdateWorkflowHandler(ctx *gin.Context) {
workflow := &api.ConfigurationWorkflow{}
ctx.BindJSON(workflow)

Check failure on line 85 in pkg/server/handler.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `ctx.BindJSON` is not checked (errcheck)
err := validateSteps(api.LocalStepType, workflow.LocalConfigurationSteps)
if err != nil {
klog.Errorf("Validate local steps failed:%v", err)
Expand Down Expand Up @@ -118,3 +119,54 @@

return nil
}

func ListWorkflowsHandler(ctx *gin.Context) {
db := database.GetWorkflowMode()
workflows, err := db.ListWorkflows()
if err != nil {
klog.Errorf("List workflows failed:%v", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
respData, err := json.Marshal(workflows)
if err != nil {
klog.Errorf("Marshal workflows failed:%v", err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.Data(http.StatusOK, "application/json", respData)
}

func GetWorkflowHandler(ctx *gin.Context) {
db := database.GetWorkflowMode()
workflow, err := db.GetWorkflow(ctx.Param("name"))
if err != nil {
klog.Errorf("Get workflow %s failed:%v", ctx.Param("name"), err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
respData, err := json.Marshal(workflow)
if err != nil {
klog.Errorf("Marshal workflow %s failed:%v", ctx.Param("name"), err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.Data(http.StatusOK, "application/json", respData)
}

func DeleteWorkflowHandler(ctx *gin.Context) {
db := database.GetWorkflowMode()
wf, err := db.GetWorkflow(ctx.Param("name"))
if err != nil {
klog.Errorf("Delete workflow %s failed:%v", ctx.Param("name"), err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
wf.Status.State = api.ConfigurationWorkflowStateDeleting
if err := db.UpdateWorkflow(wf); err != nil {
klog.Errorf("Update workflow %s failed:%v", ctx.Param("name"), err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.Data(http.StatusOK, "application/json", []byte("OK"))
}
7 changes: 4 additions & 3 deletions pkg/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ func initStepRouter(router *gin.Engine, corsHandler gin.HandlerFunc) {

func initWorkflowRouter(router *gin.Engine, corsHandler gin.HandlerFunc) {
workflowGroup := router.Group("/api/v1/workflows")
workflowGroup.GET("", nil)
// List all workflow
workflowGroup.GET("", ListWorkflowsHandler)
// Get specific workflow
workflowGroup.GET("/:name", nil)
workflowGroup.GET("/:name", GetWorkflowHandler)
// Create/Update workflow
workflowGroup.POST("/:name", CreateOrUpdateWorkflowHandler)
// Delete workflow
workflowGroup.DELETE("/:name", nil)
workflowGroup.DELETE("/:name", DeleteWorkflowHandler)
}
Loading