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

change state in the meantime #15

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
84 changes: 45 additions & 39 deletions deployments/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,56 @@ spec:
selector:
matchLabels:
app: tumbo-server
replicas: 2
replicas: 1
template:
metadata:
annotations:
version: develop
version: "develop"
labels:
app: tumbo-server
spec:
containers:
- name: tumbo-server
image: philipsahli/go-tumbo:latest
ports:
- containerPort: 8000
volumeMounts:
- name: tumbo-config-volume
mountPath: /etc/tumbo
env:
- name: DB_HOST
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_HOST
- name: DB_PORT
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_PORT
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_USERNAME
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_PASSWORD
- name: DB_NAME
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_NAME
- name: tumbo-server
image: philipsahli/go-tumbo:latest
ports:
- containerPort: 8000
volumeMounts:
- name: tumbo-config-volume
mountPath: /etc/tumbo
env:
- name: DB_HOST
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_HOST
- name: DB_PORT
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_PORT
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_USERNAME
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_PASSWORD
- name: DB_NAME
valueFrom:
secretKeyRef:
name: tumbo-db
key: DB_NAME
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
volumes:
- name: tumbo-config-volume
configMap:
name: tumbo-config

- name: tumbo-config-volume
configMap:
name: tumbo-config
184 changes: 2 additions & 182 deletions examples/example-plugin-go-grpc-fail/go.sum

Large diffs are not rendered by default.

Empty file.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsb
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.1 h1:QzqyMA1tlu6CgqCDUtU9V+ZKhLFT2dkJuANu5QaxI3I=
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
google.golang.org/genproto v0.0.0-20170818010345-ee236bd376b0/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
Expand Down
1 change: 0 additions & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func (a *App) Run() *echo.Echo {
// Run the operator built-in

if flag.Lookup("test.v") == nil {

operator := operator.Operator{}
logger := logrus.New()
log := logger.WithField("process", "operator")
Expand Down
17 changes: 15 additions & 2 deletions pkg/app/router_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func TestServerNew(t *testing.T) {
},
},
},
/*
{
expectedStarting: true,
expectedHTTPStatus: http.StatusOK,
project: models.Project{
Name: "git-project",
Description: "huhu",
GitRepository: &models.GitRepository{
Url: "https://github.com/sahlinet/go-tumbo3-examples.git/example-plugin-go-grpc",
},
},
},
*/
/* {
expectedStarting: false,
expectedHTTPStatus: http.StatusOK,
Expand Down Expand Up @@ -95,15 +108,15 @@ func TestServerNew(t *testing.T) {
if tt.expectedStarting {
err = client.StartProject(url, token, &tt.project)
assert.Nil(t, err)
assert.Equal(t, "Running", tt.project.State)
assert.Equal(t, "Running", tt.project.ProjectState.State)
assert.NotEmpty(t, tt.project.GitRepository.Version)

// Access

// Stop
client.StopProject(url, token, &tt.project)

assert.Equal(t, "Stopped", tt.project.State)
assert.Equal(t, "Stopped", tt.project.ProjectState.State)
}

// Delete
Expand Down
23 changes: 10 additions & 13 deletions pkg/app/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ func localTestProjects() []*models.Project {
projects := make([]*models.Project, 0)

projects = append(projects, &models.Project{
/* Model: models.Model{
ID: 0,
} ,*/
Name: "the-project",
Description: "a project to test",
State: "not started",
Name: "the-project",
Description: "a project to test",
ProjectState: &models.ProjectState{State: "not started"},
GitRepository: &models.GitRepository{
Url: examplePath,
},
Expand All @@ -45,9 +42,9 @@ func localTestProjects() []*models.Project {
examplePath = path.Join(d, "example-plugin-go-grpc-fail")

projects = append(projects, &models.Project{
Name: "failing application",
Description: "an application that fails",
State: "not started",
Name: "failing application",
Description: "an application that fails",
ProjectState: &models.ProjectState{State: "not started"},
GitRepository: &models.GitRepository{
Url: examplePath,
},
Expand All @@ -59,11 +56,11 @@ func gitTestProjects() []*models.Project {
projects := make([]*models.Project, 0)

projects = append(projects, &models.Project{
Name: "the-git-project",
Description: "a project to test",
State: "not started",
Name: "the-git-project",
Description: "a project to test",
ProjectState: &models.ProjectState{State: "not started"},
GitRepository: &models.GitRepository{
Url: "https://github.com/sahlinet/go-tumbo3.git//examples/example-plugin-go-grpc",
Url: "https://github.com/sahlinet/go-tumbo3-examples.git//example-plugin-go-grpc",
},
})
return projects
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func WaitForState(baseUrl, token string, project *models.Project, state string)
case err != nil:
// request error - return it
return err
case project.State != state:
return fmt.Errorf("retry, state is %s, not %s", project.State, state)
case project.ProjectState.State != state:
return fmt.Errorf("retry, state is %s, not %s", project.ProjectState.State, state)
//case project.State == state:
// retryable StatusCode - return it
//return retry.Stop(fmt.Errorf("expected state is " + state))
Expand Down
14 changes: 7 additions & 7 deletions pkg/controllers/rice-box.go

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions pkg/controllers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ func ProjectServiceState(projectID int, state string, tx *gorm.DB) (*models.Proj

project.GitRepository.Version = runnable.Source.Version
log.Info("Running version ", project.GitRepository.Version)
project.State = "Running"
project.ErrorMsg = ""
project.ProjectState = &models.ProjectState{
State: "Running",
}
project.Update(tx)

}
Expand All @@ -139,7 +140,7 @@ func ProjectServiceState(projectID int, state string, tx *gorm.DB) (*models.Proj
return project, err
}

project.State = "Stopped"
project.ProjectState.State = "Stopped"
project.Update(nil)
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ func Setup(repository *Repository) *gorm.DB {
if err != nil {
log.Fatal(err)
}

err = db.Table("git_repositories").AutoMigrate(&GitRepository{})
if err != nil {
log.Fatal(err)
}

err = db.Table("project_state").AutoMigrate(&ProjectState{})
if err != nil {
log.Fatal(err)
}
err = db.Table("auths").AutoMigrate(&Auth{})
if err != nil {
log.Fatal(err)
Expand Down
58 changes: 48 additions & 10 deletions pkg/models/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

type Project struct {
Expand All @@ -15,15 +16,27 @@ type Project struct {
Description string `json:"description"`
CreatedBy string `json:"created_by"`
ModifiedBy string `json:"modified_by"`
State string `json:"state"`
BackOffDatetime time.Time `json:"backoff_datetime"`
ErrorMsg string `json:"errormsg"`
BuildRetries uint `json:"retry"`

ProjectState *ProjectState `json:"state"`
GitRepository *GitRepository `json:"gitrepository"`
Runner *Runner `json:"runner"`
}

type ProjectState struct {
Model

State string `json:"state"`
ErrorMsg string `json:"errormsg"`

ProjectID uint
}

func (ProjectState) TableName() string {
return "project_state"
}

type GitRepository struct {
Model

Expand Down Expand Up @@ -76,7 +89,7 @@ func GetProjectTotal() (int64, error) {
func GetProjects() ([]*Project, error) {
var projects []*Project
//err := db.Offset(pageNum).Limit(pageSize).Find(&projects).Error
err := db.Preload("GitRepository").Find(&projects).Error
err := db.Preload("GitRepository").Preload("ProjectState").Order("name").Find(&projects).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
Expand All @@ -87,7 +100,7 @@ func GetProjects() ([]*Project, error) {
// GetProject Get a single project based on ID
func GetProject(id uint) (*Project, error) {
var project Project
err := db.Preload("GitRepository").Where("id = ?", id).First(&project).Error
err := db.Preload("GitRepository").Preload("ProjectState").Where("id = ?", id).First(&project).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
Expand All @@ -101,8 +114,8 @@ func GetProjectForShareOrUpdate(id uint, strength string) (*Project, *gorm.DB, e
tx := db.Begin()
//tx.Set("gorm:query_option", "NOWAIT")
//err := tx.Debug().Clauses(clause.Locking{Strength: strength}, NoWait{}).Preload("GitRepository").Where("id = ?", id).First(&project).Error
//err := tx.Debug().Clauses(clause.Locking{Strength: strength, Options: "NOWAIT"}).Preload("GitRepository").Where("id = ?", id).First(&project).Error
err := tx.Debug().Preload("GitRepository").Where("id = ?", id).First(&project).Error
err := tx.Debug().Clauses(clause.Locking{Strength: strength, Options: "NOWAIT"}).Preload("GitRepository").Preload("ProjectState").Where("id = ?", id).First(&project).Error
//err := tx.Debug().Preload("GitRepository").Preload("ProjectState").Where("id = ?", id).First(&project).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, nil, err
}
Expand All @@ -117,7 +130,7 @@ func GetProjectForShareOrUpdate(id uint, strength string) (*Project, *gorm.DB, e

func GetProjectByName(name string) (*Project, error) {
var project Project
err := db.Preload("GitRepository").Where("name = ?", name).First(&project).Error
err := db.Preload("GitRepository").Preload("ProjectState").Where("name = ?", name).First(&project).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
Expand All @@ -140,7 +153,9 @@ func AddProject(data map[string]interface{}) error {
Name: data["name"].(string),
Description: data["description"].(string),
//CreatedBy: data["created_by"].(string),
State: data["state"].(string),
ProjectState: &ProjectState{
State: data["state"].(string),
},
}
if err := db.Debug().Create(&project).Error; err != nil {
return err
Expand All @@ -161,6 +176,11 @@ func (project *Project) Update(tx *gorm.DB) error {
log.Fatal(err)
}

err = tx.Model(&project).Association("ProjectState").Error
if err != nil {
log.Fatal(err)
}

tx.Save(project)
if err := tx.Session(&gorm.Session{FullSaveAssociations: true}).Debug().Updates(project).Error; err != nil {
return err
Expand All @@ -170,8 +190,19 @@ func (project *Project) Update(tx *gorm.DB) error {
}

func (project *Project) UpdateStateInDB(state string, tx *gorm.DB) error {
project.State = state
project.ErrorMsg = ""
if tx == nil {
tx = db
}
project.ProjectState.State = state
project.ProjectState.ErrorMsg = ""
return project.Update(tx)
}

func (project *Project) UpdateFullStateInDB(state *ProjectState, tx *gorm.DB) error {
if tx == nil {
tx = db
}
project.ProjectState = state
return project.Update(tx)

}
Expand All @@ -187,6 +218,11 @@ func (project *Project) CreateOrUpdate() (*Project, error) {
log.Fatal(err)
}

err = db.Model(&project).Association("ProjectState").Error
if err != nil {
log.Fatal(err)
}

p, err := GetProjectByName(project.Name)
log.Info(p, err)
if p.Name != "" {
Expand All @@ -198,6 +234,8 @@ func (project *Project) CreateOrUpdate() (*Project, error) {
return project, nil
}

project.ProjectState = &ProjectState{}

err = db.Create(&project).Error
if err != nil {
return project, err
Expand Down
Loading