Skip to content

Commit

Permalink
🐛 Fix builder image not exist (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Jan 4, 2024
1 parent 9e24013 commit 2bf56bf
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 72 deletions.
49 changes: 37 additions & 12 deletions pkg/daemon/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/go-sigma/sigma/pkg/builder"
"github.com/go-sigma/sigma/pkg/dal/dao"
"github.com/go-sigma/sigma/pkg/dal/query"
"github.com/go-sigma/sigma/pkg/modules/workq"
"github.com/go-sigma/sigma/pkg/modules/workq/definition"
"github.com/go-sigma/sigma/pkg/types"
Expand All @@ -36,9 +37,9 @@ import (
func init() {
workq.TopicHandlers[enums.DaemonBuilder.String()] = definition.Consumer{
Handler: builderRunner,
MaxRetry: 6,
MaxRetry: 1,
Concurrency: 10,
Timeout: time.Minute * 10,
Timeout: time.Minute * 60,
}
}

Expand Down Expand Up @@ -85,6 +86,35 @@ func (b runner) runner(ctx context.Context, payload types.DaemonBuilderPayload)
return fmt.Errorf("Get runner failed: %v", err)
}

defer func() {
var updates map[string]any
if !(payload.Action == enums.DaemonBuilderActionStart || payload.Action == enums.DaemonBuilderActionRestart || payload.Action == enums.DaemonBuilderActionStop) {
updates = map[string]any{
query.BuilderRunner.Status.ColumnName().String(): enums.BuildStatusFailed,
query.BuilderRunner.StatusMessage.ColumnName().String(): fmt.Sprintf("Daemon builder action(%s) is not support", payload.Action),
query.BuilderRunner.EndedAt.ColumnName().String(): time.Now().UnixMilli(),
}
}
if err != nil {
updates = map[string]any{
query.BuilderRunner.Status.ColumnName().String(): enums.BuildStatusFailed,
query.BuilderRunner.StatusMessage.ColumnName().String(): err.Error(),
query.BuilderRunner.EndedAt.ColumnName().String(): time.Now().UnixMilli(),
}
}
if len(updates) > 0 {
err = builderService.UpdateRunner(ctx, payload.BuilderID, payload.RunnerID, updates)
if err != nil {
log.Error().Err(err).Msg("Update runner after got error")
}
}
}()

if builder.Driver == nil {
err = fmt.Errorf("Builder driver is not initialized")
return fmt.Errorf("Builder driver is not initialized, or check config.daemon.builder.enabled is true or not")
}

platforms := []enums.OciPlatform{}
for _, p := range strings.Split(builderObj.BuildkitPlatforms, ",") {
platforms = append(platforms, enums.OciPlatform(p))
Expand Down Expand Up @@ -144,17 +174,12 @@ func (b runner) runner(ctx context.Context, payload types.DaemonBuilderPayload)
}
buildConfig.Builder.ScmProvider = (*enums.ScmProvider)(&builderObj.CodeRepository.User3rdParty.Provider) // TODO: change type
}
if payload.Action == enums.DaemonBuilderActionStart { // nolint: gocritic
err = builder.Driver.Start(ctx, buildConfig)
} else if payload.Action == enums.DaemonBuilderActionRestart {
if payload.Action == enums.DaemonBuilderActionStart || payload.Action == enums.DaemonBuilderActionRestart {
err = builder.Driver.Start(ctx, buildConfig)
} else {
log.Error().Err(err).Str("action", payload.Action.String()).Msg("Daemon builder action not found")
return fmt.Errorf("Daemon builder action not found")
}
if err != nil {
log.Error().Err(err).Msg("Start or restart builder failed")
return fmt.Errorf("Start or restart builder failed: %v", err)
if err != nil {
log.Error().Err(err).Msg("Start or restart builder failed")
return fmt.Errorf("Start or restart builder failed: %v", err)
}
}
return nil
}
1 change: 1 addition & 0 deletions pkg/dal/migrations/mysql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ CREATE TABLE IF NOT EXISTS `builder_runners` (
`builder_id` bigint NOT NULL,
`log` LONGBLOB,
`status` ENUM ('Success', 'Failed', 'Pending', 'Scheduling', 'Building', 'Stopping', 'Stopped') NOT NULL DEFAULT 'Pending',
`status_message` varchar(255),
-- common settings
`tag` varchar(128),
`raw_tag` varchar(255) NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions pkg/dal/migrations/postgresql/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ CREATE TABLE IF NOT EXISTS "builder_runners" (
"builder_id" bigint NOT NULL,
"log" bytea,
"status" builder_runner_status NOT NULL DEFAULT 'Pending',
"status_message" varchar(255),
-- common settings
"tag" varchar(128),
"raw_tag" varchar(255) NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions pkg/dal/migrations/sqlite3/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ CREATE TABLE IF NOT EXISTS `builder_runners` (
`builder_id` integer NOT NULL,
`log` BLOB,
`status` text CHECK (`status` IN ('Success', 'Failed', 'Pending', 'Scheduling', 'Building', 'Stopping', 'Stopped')) NOT NULL DEFAULT 'Pending',
`status_message` varchar(255),
-- common settings
`tag` varchar(128), -- image tag
`raw_tag` varchar(255) NOT NULL, -- image tag
Expand Down
7 changes: 4 additions & 3 deletions pkg/dal/models/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ type BuilderRunner struct {
DeletedAt soft_delete.DeletedAt `gorm:"softDelete:milli"`
ID int64 `gorm:"primaryKey"`

BuilderID int64
Log []byte
Status enums.BuildStatus `gorm:"default:Pending"`
BuilderID int64
Log []byte
Status enums.BuildStatus `gorm:"default:Pending"`
StatusMessage *string

Tag *string
RawTag string
Expand Down
38 changes: 21 additions & 17 deletions pkg/dal/query/builder_runners.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions pkg/handlers/apidocs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4353,6 +4353,28 @@ const docTemplate = `{
}
}
},
"/systems/config": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"System"
],
"summary": "Get config",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/types.GetSystemConfigResponse"
}
}
}
}
},
"/systems/endpoint": {
"get": {
"consumes": [
Expand Down Expand Up @@ -6704,6 +6726,23 @@ const docTemplate = `{
}
}
},
"types.GetSystemConfigDaemon": {
"type": "object",
"properties": {
"builder": {
"type": "boolean",
"example": false
}
}
},
"types.GetSystemConfigResponse": {
"type": "object",
"properties": {
"daemon": {
"$ref": "#/definitions/types.GetSystemConfigDaemon"
}
}
},
"types.GetSystemEndpointResponse": {
"type": "object",
"properties": {
Expand Down
39 changes: 39 additions & 0 deletions pkg/handlers/apidocs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4345,6 +4345,28 @@
}
}
},
"/systems/config": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"System"
],
"summary": "Get config",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/types.GetSystemConfigResponse"
}
}
}
}
},
"/systems/endpoint": {
"get": {
"consumes": [
Expand Down Expand Up @@ -6696,6 +6718,23 @@
}
}
},
"types.GetSystemConfigDaemon": {
"type": "object",
"properties": {
"builder": {
"type": "boolean",
"example": false
}
}
},
"types.GetSystemConfigResponse": {
"type": "object",
"properties": {
"daemon": {
"$ref": "#/definitions/types.GetSystemConfigDaemon"
}
}
},
"types.GetSystemEndpointResponse": {
"type": "object",
"properties": {
Expand Down
25 changes: 25 additions & 0 deletions pkg/handlers/apidocs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,17 @@ definitions:
example: "2006-01-02 15:04:05"
type: string
type: object
types.GetSystemConfigDaemon:
properties:
builder:
example: false
type: boolean
type: object
types.GetSystemConfigResponse:
properties:
daemon:
$ref: '#/definitions/types.GetSystemConfigDaemon'
type: object
types.GetSystemEndpointResponse:
properties:
endpoint:
Expand Down Expand Up @@ -4542,6 +4553,20 @@ paths:
summary: Redirect oauth2 provider callback
tags:
- OAuth2
/systems/config:
get:
consumes:
- application/json
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/types.GetSystemConfigResponse'
summary: Get config
tags:
- System
/systems/endpoint:
get:
consumes:
Expand Down
11 changes: 6 additions & 5 deletions pkg/handlers/builders/builders_runner_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ func (h *handler) GetRunner(c echo.Context) error {

Log: runnerObj.Log,

Status: runnerObj.Status,
Tag: runnerObj.Tag,
RawTag: runnerObj.RawTag,
Description: runnerObj.Description,
ScmBranch: runnerObj.ScmBranch,
Status: runnerObj.Status,
StatusMessage: runnerObj.StatusMessage,
Tag: runnerObj.Tag,
RawTag: runnerObj.RawTag,
Description: runnerObj.Description,
ScmBranch: runnerObj.ScmBranch,

StartedAt: runnerObj.StartedAt,
EndedAt: runnerObj.EndedAt,
Expand Down
11 changes: 6 additions & 5 deletions pkg/handlers/builders/builders_runners_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ func (h *handler) ListRunners(c echo.Context) error {

Log: runnerObj.Log,

Status: runnerObj.Status,
Tag: runnerObj.Tag,
RawTag: runnerObj.RawTag,
Description: runnerObj.Description,
ScmBranch: runnerObj.ScmBranch,
Status: runnerObj.Status,
StatusMessage: runnerObj.StatusMessage,
Tag: runnerObj.Tag,
RawTag: runnerObj.RawTag,
Description: runnerObj.Description,
ScmBranch: runnerObj.ScmBranch,

StartedAt: runnerObj.StartedAt,
EndedAt: runnerObj.EndedAt,
Expand Down
Loading

0 comments on commit 2bf56bf

Please sign in to comment.