Skip to content

Commit 50ab749

Browse files
committed
feat(parser): add ansible methods
1 parent 1f31aa8 commit 50ab749

File tree

10 files changed

+112
-30
lines changed

10 files changed

+112
-30
lines changed

db/Store.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ type Store interface {
345345
CreateTaskStage(stage TaskStage) (TaskStage, error)
346346
EndTaskStage(taskID int, stageID int, end time.Time, endOutputID int) error
347347
CreateTaskStageResult(taskID int, stageID int, result map[string]any) error
348+
CreateAnsibleTaskHost(host AnsibleTaskHost) error
349+
CreateAnsibleTaskError(error AnsibleTaskError) error
350+
GetAnsibleTaskHosts(projectID int, taskID int) ([]AnsibleTaskHost, error)
351+
GetAnsibleTaskError(projectID int, taskID int) ([]AnsibleTaskError, error)
348352

349353
GetTaskStages(projectID int, taskID int) ([]TaskStageWithResult, error)
350354
GetTaskStageResult(projectID int, taskID int, stageID int) (TaskStageResult, error)
@@ -375,8 +379,6 @@ type Store interface {
375379
UpdateTemplateVaults(projectID int, templateID int, vaults []TemplateVault) error
376380

377381
GetTaskStats(projectID int, templateID *int, unit TaskStatUnit, filter TaskFilter) ([]TaskStat, error)
378-
379-
//GetProjectStats(projectID int) (ProjectStats, error)
380382
}
381383

382384
var AccessKeyProps = ObjectProps{

db/ansible.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package db
2+
3+
import "time"
4+
5+
type AnsibleTaskHost struct {
6+
TaskID int `json:"task_id" db:"task_id"`
7+
ProjectID int `json:"project_id" db:"task_id"`
8+
Host string `json:"host" db:"host"`
9+
Changed int `json:"changed" db:"changed"`
10+
Failed int `json:"failed" db:"failed"`
11+
Ignored int `json:"ignored" db:"ignored"`
12+
Ok int `json:"ok" db:"ok"`
13+
Rescued int `json:"rescued" db:"rescued"`
14+
Skipped int `json:"skipped" db:"skipped"`
15+
Unreachable int `json:"unreachable" db:"unreachable"`
16+
Created time.Time `db:"created" json:"created"`
17+
}
18+
19+
type AnsibleTaskError struct {
20+
TaskID int `json:"task_id" db:"task_id"`
21+
ProjectID int `json:"project_id" db:"task_id"`
22+
Host string `json:"host" db:"host"`
23+
Task string `json:"task" db:"task"`
24+
Error string `json:"error" db:"error"`
25+
Created time.Time `db:"created" json:"created"`
26+
}

db/bolt/ansible.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package bolt
2+
3+
import (
4+
"github.com/pkg/errors"
5+
"github.com/semaphoreui/semaphore/db"
6+
)
7+
8+
func (d *BoltDb) CreateAnsibleTaskHost(host db.AnsibleTaskHost) error {
9+
return errors.New("not implemented")
10+
}
11+
12+
func (d *BoltDb) CreateAnsibleTaskError(error db.AnsibleTaskError) error {
13+
return errors.New("not implemented")
14+
}
15+
16+
func (d *BoltDb) GetAnsibleTaskHosts(projectID int, taskID int) (res []db.AnsibleTaskHost, err error) {
17+
err = errors.New("not implemented")
18+
return
19+
}
20+
21+
func (d *BoltDb) GetAnsibleTaskError(projectID int, taskID int) (res []db.AnsibleTaskError, err error) {
22+
err = errors.New("not implemented")
23+
return
24+
}

db/sql/ansible.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sql
2+
3+
import (
4+
"errors"
5+
"github.com/semaphoreui/semaphore/db"
6+
)
7+
8+
func (d *SqlDb) CreateAnsibleTaskHost(host db.AnsibleTaskHost) error {
9+
return errors.New("not implemented")
10+
}
11+
12+
func (d *SqlDb) CreateAnsibleTaskError(error db.AnsibleTaskError) error {
13+
return errors.New("not implemented")
14+
}
15+
16+
func (d *SqlDb) GetAnsibleTaskHosts(projectID int, taskID int) (res []db.AnsibleTaskHost, err error) {
17+
err = errors.New("not implemented")
18+
return
19+
}
20+
21+
func (d *SqlDb) GetAnsibleTaskError(projectID int, taskID int) (res []db.AnsibleTaskError, err error) {
22+
err = errors.New("not implemented")
23+
return
24+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/gorilla/websocket v1.5.3
1919
github.com/lib/pq v1.10.9
2020
github.com/mdp/qrterminal/v3 v3.2.1
21+
github.com/pkg/errors v0.9.1
2122
github.com/pquerna/otp v1.4.0
2223
github.com/robfig/cron/v3 v3.0.1
2324
github.com/sirupsen/logrus v1.9.3

go.sum

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,13 @@ github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI
161161
go.etcd.io/bbolt v1.4.0 h1:TU77id3TnN/zKr7CO/uk+fBCwF2jGcMuw2B/FMAzYIk=
162162
go.etcd.io/bbolt v1.4.0/go.mod h1:AsD+OCi/qPN1giOX1aiLAha3o1U8rAz65bvN4j0sRuk=
163163
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
164-
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
165-
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
166164
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
167165
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
168166
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
169167
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
170168
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
171169
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
172170
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
173-
golang.org/x/oauth2 v0.29.0 h1:WdYw2tdTK1S8olAzWHdgeqfy+Mtm9XNhv/xJsY65d98=
174-
golang.org/x/oauth2 v0.29.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
175171
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
176172
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
177173
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
@@ -182,18 +178,14 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
182178
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
183179
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
184180
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
185-
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
186-
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
187181
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
188182
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
189183
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
190-
golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
191-
golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
192184
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
193185
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
194186
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
195-
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
196-
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
187+
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
188+
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
197189
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
198190
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
199191
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

services/tasks/TaskPool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (p *TaskPool) MoveToNextStage(
186186
matched = true
187187
} else {
188188
var ok bool
189-
ok, err = parser.Parse(currentStage, newOutput, p.store)
189+
ok, err = parser.Parse(currentStage, newOutput, p.store, projectID)
190190
if err != nil {
191191
log.Error(err.Error())
192192
} else if ok {

services/tasks/stage_parsers/ansible_play.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (p AnsibleRunningStageParser) IsEnd(currentStage *db.TaskStage, output db.T
4848
const ansibleTaskMaker = "TASK ["
4949
const failedTaskMaker = "fatal: ["
5050

51-
func (p AnsibleRunningStageParser) Parse(currentStage *db.TaskStage, output db.TaskOutput, store db.Store) (ok bool, err error) {
51+
func (p AnsibleRunningStageParser) Parse(currentStage *db.TaskStage, output db.TaskOutput, store db.Store, projectID int) (ok bool, err error) {
5252

5353
if currentStage == nil {
5454
return

services/tasks/stage_parsers/ansible_result.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ func (p AnsibleResultStageParser) IsEnd(currentStage *db.TaskStage, output db.Ta
4343
return strings.TrimSpace(output.Output) == ""
4444
}
4545

46-
type AnsibleResultHost struct {
47-
Host string `json:"host"`
48-
Ok int `json:"ok"`
49-
Changed int `json:"changed"`
50-
Unreachable int `json:"unreachable"`
51-
Failed int `json:"failed"`
52-
Skipped int `json:"skipped"`
53-
Rescued int `json:"rescued"`
54-
Ignored int `json:"ignored"`
55-
}
46+
//type AnsibleResultHost struct {
47+
// Host string `json:"host"`
48+
// Ok int `json:"ok"`
49+
// Changed int `json:"changed"`
50+
// Unreachable int `json:"unreachable"`
51+
// Failed int `json:"failed"`
52+
// Skipped int `json:"skipped"`
53+
// Rescued int `json:"rescued"`
54+
// Ignored int `json:"ignored"`
55+
//}
5656

5757
var ansibleResultHostRE = regexp.MustCompile(
5858
`^([^\s]+)\s*:\s*` +
@@ -72,10 +72,10 @@ func toInt(s string) int {
7272
}
7373

7474
type AnsibleResultStageParserState struct {
75-
Hosts []AnsibleResultHost
75+
//Hosts []AnsibleResultHost
7676
}
7777

78-
func (p AnsibleResultStageParser) Parse(currentStage *db.TaskStage, output db.TaskOutput, store db.Store) (ok bool, err error) {
78+
func (p AnsibleResultStageParser) Parse(currentStage *db.TaskStage, output db.TaskOutput, store db.Store, projectID int) (ok bool, err error) {
7979

8080
if currentStage == nil {
8181
return
@@ -105,7 +105,20 @@ func (p AnsibleResultStageParser) Parse(currentStage *db.TaskStage, output db.Ta
105105
return
106106
}
107107

108-
p.state.Hosts = append(p.state.Hosts, AnsibleResultHost{
108+
//p.state.Hosts = append(p.state.Hosts, AnsibleResultHost{
109+
// Host: m[1],
110+
// Ok: toInt(m[2]),
111+
// Changed: toInt(m[3]),
112+
// Unreachable: toInt(m[4]),
113+
// Failed: toInt(m[5]),
114+
// Skipped: toInt(m[6]),
115+
// Rescued: toInt(m[7]),
116+
// Ignored: toInt(m[8]),
117+
//})
118+
119+
err = store.CreateAnsibleTaskHost(db.AnsibleTaskHost{
120+
TaskID: currentStage.TaskID,
121+
ProjectID: projectID,
109122
Host: m[1],
110123
Ok: toInt(m[2]),
111124
Changed: toInt(m[3]),
@@ -121,7 +134,7 @@ func (p AnsibleResultStageParser) Parse(currentStage *db.TaskStage, output db.Ta
121134

122135
func (p AnsibleResultStageParser) Result() (res map[string]any) {
123136
res = make(map[string]any)
124-
res["hosts"] = p.state.Hosts
137+
//res["hosts"] = p.state.Hosts
125138
return
126139
}
127140

services/tasks/stage_parsers/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type StageResultParser interface {
1010
IsEnd(currentStage *db.TaskStage, output db.TaskOutput) bool
1111
NeedParse() bool
1212
State() any
13-
Parse(currentStage *db.TaskStage, output db.TaskOutput, store db.Store) (bool, error)
13+
Parse(currentStage *db.TaskStage, output db.TaskOutput, store db.Store, projectID int) (bool, error)
1414
Result() map[string]any
1515
}
1616

@@ -82,7 +82,7 @@ func (p InitStageParser) IsEnd(currentStage *db.TaskStage, output db.TaskOutput)
8282
return false
8383
}
8484

85-
func (p InitStageParser) Parse(currentStage *db.TaskStage, output db.TaskOutput, store db.Store) (bool, error) {
85+
func (p InitStageParser) Parse(currentStage *db.TaskStage, output db.TaskOutput, store db.Store, projectID int) (bool, error) {
8686
return false, nil
8787
}
8888

0 commit comments

Comments
 (0)