2021-11-13 21:18:06 +02:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// 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
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable 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.
|
|
|
|
|
2017-03-28 10:53:06 +02:00
|
|
|
package model
|
|
|
|
|
2019-06-06 09:41:42 +02:00
|
|
|
import "fmt"
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
// StepStore persists process information to storage.
|
|
|
|
type StepStore interface {
|
|
|
|
StepLoad(int64) (*Step, error)
|
|
|
|
StepFind(*Pipeline, int) (*Step, error)
|
|
|
|
StepChild(*Pipeline, int, string) (*Step, error)
|
|
|
|
StepList(*Pipeline) ([]*Step, error)
|
|
|
|
StepCreate([]*Step) error
|
|
|
|
StepUpdate(*Step) error
|
|
|
|
StepClear(*Pipeline) error
|
2017-03-28 10:53:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
// Step represents a process in the pipeline.
|
|
|
|
// swagger:model step
|
|
|
|
type Step struct {
|
|
|
|
ID int64 `json:"id" xorm:"pk autoincr 'step_id'"`
|
|
|
|
PipelineID int64 `json:"pipeline_id" xorm:"UNIQUE(s) INDEX 'step_pipeline_id'"`
|
|
|
|
PID int `json:"pid" xorm:"UNIQUE(s) 'step_pid'"`
|
|
|
|
PPID int `json:"ppid" xorm:"step_ppid"`
|
|
|
|
PGID int `json:"pgid" xorm:"step_pgid"`
|
|
|
|
Name string `json:"name" xorm:"step_name"`
|
|
|
|
State StatusValue `json:"state" xorm:"step_state"`
|
|
|
|
Error string `json:"error,omitempty" xorm:"VARCHAR(500) step_error"`
|
|
|
|
ExitCode int `json:"exit_code" xorm:"step_exit_code"`
|
|
|
|
Started int64 `json:"start_time,omitempty" xorm:"step_started"`
|
|
|
|
Stopped int64 `json:"end_time,omitempty" xorm:"step_stopped"`
|
|
|
|
Machine string `json:"machine,omitempty" xorm:"step_machine"`
|
|
|
|
Platform string `json:"platform,omitempty" xorm:"step_platform"`
|
|
|
|
Environ map[string]string `json:"environ,omitempty" xorm:"json 'step_environ'"`
|
|
|
|
Children []*Step `json:"children,omitempty" xorm:"-"`
|
2021-11-13 21:18:06 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
type UpdateStepStore interface {
|
|
|
|
StepUpdate(*Step) error
|
2022-06-16 15:35:58 +02:00
|
|
|
}
|
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
// TableName return database table name for xorm
|
2022-10-28 17:38:53 +02:00
|
|
|
func (Step) TableName() string {
|
|
|
|
return "steps"
|
2017-03-28 10:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Running returns true if the process state is pending or running.
|
2022-10-28 17:38:53 +02:00
|
|
|
func (p *Step) Running() bool {
|
2017-03-28 10:53:06 +02:00
|
|
|
return p.State == StatusPending || p.State == StatusRunning
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failing returns true if the process state is failed, killed or error.
|
2022-10-28 17:38:53 +02:00
|
|
|
func (p *Step) Failing() bool {
|
2017-03-28 10:53:06 +02:00
|
|
|
return p.State == StatusError || p.State == StatusKilled || p.State == StatusFailure
|
|
|
|
}
|
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
// IsParent returns true if the process is a parent process.
|
2022-10-28 17:38:53 +02:00
|
|
|
func (p *Step) IsParent() bool {
|
2021-12-28 18:02:49 +02:00
|
|
|
return p.PPID == 0
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
// IsMultiPipeline checks if step list contain more than one parent step
|
|
|
|
func IsMultiPipeline(steps []*Step) bool {
|
2022-01-31 16:38:39 +02:00
|
|
|
c := 0
|
2022-10-28 17:38:53 +02:00
|
|
|
for _, step := range steps {
|
|
|
|
if step.IsParent() {
|
2022-01-31 16:38:39 +02:00
|
|
|
c++
|
|
|
|
}
|
|
|
|
if c > 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-28 10:53:06 +02:00
|
|
|
// Tree creates a process tree from a flat process list.
|
2022-10-28 17:38:53 +02:00
|
|
|
func Tree(steps []*Step) ([]*Step, error) {
|
|
|
|
var nodes []*Step
|
2021-12-31 18:43:09 +02:00
|
|
|
|
|
|
|
// init parent nodes
|
2022-10-28 17:38:53 +02:00
|
|
|
for i := range steps {
|
|
|
|
if steps[i].IsParent() {
|
|
|
|
nodes = append(nodes, steps[i])
|
2021-12-31 18:43:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign children to parrents
|
2022-10-28 17:38:53 +02:00
|
|
|
for i := range steps {
|
|
|
|
if !steps[i].IsParent() {
|
|
|
|
parent, err := findNode(nodes, steps[i].PPID)
|
2021-12-09 00:36:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-28 17:38:53 +02:00
|
|
|
parent.Children = append(parent.Children, steps[i])
|
2017-03-28 10:53:06 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-31 18:43:09 +02:00
|
|
|
|
2021-12-09 00:36:23 +02:00
|
|
|
return nodes, nil
|
2017-03-28 10:53:06 +02:00
|
|
|
}
|
2019-06-06 09:41:42 +02:00
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
// PipelineStatus determine pipeline status based on corresponding step list
|
|
|
|
func PipelineStatus(steps []*Step) StatusValue {
|
2022-01-31 16:38:39 +02:00
|
|
|
status := StatusSuccess
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
for _, p := range steps {
|
2022-01-31 16:38:39 +02:00
|
|
|
if p.IsParent() && p.Failing() {
|
|
|
|
status = p.State
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
// IsThereRunningStage determine if it contains steps running or pending to run
|
2022-01-31 16:38:39 +02:00
|
|
|
// TODO: return false based on depends_on (https://github.com/woodpecker-ci/woodpecker/pull/730#discussion_r795681697)
|
2022-10-28 17:38:53 +02:00
|
|
|
func IsThereRunningStage(steps []*Step) bool {
|
|
|
|
for _, p := range steps {
|
2022-01-31 16:38:39 +02:00
|
|
|
if p.IsParent() {
|
|
|
|
if p.Running() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
func findNode(nodes []*Step, pid int) (*Step, error) {
|
2019-06-06 09:41:42 +02:00
|
|
|
for _, node := range nodes {
|
|
|
|
if node.PID == pid {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
return nil, fmt.Errorf("Corrupt step structure")
|
2019-06-06 09:41:42 +02:00
|
|
|
}
|