1
0
out-of-tree/api/api.go

190 lines
3.1 KiB
Go
Raw Normal View History

2024-02-20 13:25:31 +00:00
package api
import (
"bytes"
"encoding/gob"
2024-02-20 13:25:31 +00:00
"errors"
"fmt"
"net"
"reflect"
2024-02-27 02:00:07 +00:00
"time"
2024-02-20 13:25:31 +00:00
"code.dumpstack.io/tools/out-of-tree/artifact"
"code.dumpstack.io/tools/out-of-tree/distro"
"github.com/google/uuid"
)
var ErrInvalid = errors.New("")
type Status string
const (
StatusNew Status = "new"
StatusWaiting Status = "waiting"
StatusRunning Status = "running"
StatusSuccess Status = "success"
StatusFailure Status = "failure"
)
type Command string
const (
RawMode Command = "rawmode"
AddJob Command = "add_job"
ListJobs Command = "list_jobs"
JobLogs Command = "job_logs"
JobStatus Command = "job_status"
AddRepo Command = "add_repo"
ListRepos Command = "list_repos"
Kernels Command = "kernels"
)
type Job struct {
2024-02-26 08:55:27 +00:00
ID int64
UpdatedAt time.Time
2024-02-26 08:55:27 +00:00
// Job UUID
2024-02-20 13:25:31 +00:00
UUID string
2024-02-26 08:55:27 +00:00
// Group UUID
Group string
2024-02-20 13:25:31 +00:00
RepoName string
Commit string
Artifact artifact.Artifact
Target distro.KernelInfo
2024-02-27 02:00:07 +00:00
Created time.Time
Started time.Time
Finished time.Time
2024-02-20 13:25:31 +00:00
Status Status
}
func (job *Job) GenUUID() {
job.UUID = uuid.New().String()
}
2024-02-26 08:55:27 +00:00
// ListJobsParams is the parameters for ListJobs command
type ListJobsParams struct {
// Group UUID
Group string
// Repo name
Repo string
// Commit hash
Commit string
// Status of the job
Status Status
2024-02-27 02:00:07 +00:00
UpdatedAfter int64
2024-02-26 08:55:27 +00:00
}
2024-02-20 13:25:31 +00:00
type Repo struct {
ID int64
Name string
Path string
}
type JobLog struct {
Name string
Text string
}
type Req struct {
Command Command
Type string
Data []byte
}
func (r *Req) SetData(data any) (err error) {
2024-02-20 13:25:31 +00:00
r.Type = fmt.Sprintf("%v", reflect.TypeOf(data))
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(data)
r.Data = buf.Bytes()
return
2024-02-20 13:25:31 +00:00
}
func (r *Req) GetData(data any) (err error) {
if len(r.Data) == 0 {
return
}
t := fmt.Sprintf("%v", reflect.TypeOf(data))
if r.Type != t {
err = fmt.Errorf("type mismatch (%v != %v)", r.Type, t)
return
}
buf := bytes.NewBuffer(r.Data)
return gob.NewDecoder(buf).Decode(data)
2024-02-20 13:25:31 +00:00
}
func (r *Req) Encode(conn net.Conn) (err error) {
return gob.NewEncoder(conn).Encode(r)
2024-02-20 13:25:31 +00:00
}
func (r *Req) Decode(conn net.Conn) (err error) {
return gob.NewDecoder(conn).Decode(r)
2024-02-20 13:25:31 +00:00
}
type Resp struct {
UUID string
Error string
Err error `json:"-"`
Type string
Data []byte
}
func NewResp() (resp Resp) {
resp.UUID = uuid.New().String()
return
}
func (r *Resp) SetData(data any) (err error) {
2024-02-20 13:25:31 +00:00
r.Type = fmt.Sprintf("%v", reflect.TypeOf(data))
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(data)
r.Data = buf.Bytes()
return
2024-02-20 13:25:31 +00:00
}
func (r *Resp) GetData(data any) (err error) {
if len(r.Data) == 0 {
return
}
t := fmt.Sprintf("%v", reflect.TypeOf(data))
if r.Type != t {
err = fmt.Errorf("type mismatch (%v != %v)", r.Type, t)
return
}
buf := bytes.NewBuffer(r.Data)
return gob.NewDecoder(buf).Decode(data)
2024-02-20 13:25:31 +00:00
}
func (r *Resp) Encode(conn net.Conn) (err error) {
2024-02-20 13:25:31 +00:00
if r.Err != nil && r.Err != ErrInvalid && r.Error == "" {
r.Error = fmt.Sprintf("%v", r.Err)
}
return gob.NewEncoder(conn).Encode(r)
2024-02-20 13:25:31 +00:00
}
func (r *Resp) Decode(conn net.Conn) (err error) {
err = gob.NewDecoder(conn).Decode(r)
2024-02-20 13:25:31 +00:00
r.Err = ErrInvalid
return
}