Skip to content

Commit 37ec131

Browse files
committed
Added ticker as a periodic job to check pipelines folder for new pipelines
1 parent 6480c3a commit 37ec131

File tree

7 files changed

+100
-20
lines changed

7 files changed

+100
-20
lines changed

cmd/gaia/main.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/gaia-pipeline/gaia"
99
"github.com/gaia-pipeline/gaia/handlers"
10+
"github.com/gaia-pipeline/gaia/pipeline"
1011
"github.com/gaia-pipeline/gaia/store"
1112
hclog "github.com/hashicorp/go-hclog"
1213
"github.com/kataras/iris"
@@ -16,6 +17,11 @@ var (
1617
irisInstance *iris.Application
1718
)
1819

20+
const (
21+
dataFolder = "data"
22+
pipelinesFolder = "pipelines"
23+
)
24+
1925
func init() {
2026
gaia.Cfg = &gaia.Config{}
2127

@@ -51,12 +57,22 @@ func main() {
5157
gaia.Cfg.Logger.Debug("executeable path found", "path", execPath)
5258
}
5359

60+
// Set data path and pipeline path relative to home folder and create it
61+
// if not exist.
62+
gaia.Cfg.DataPath = gaia.Cfg.HomePath + string(os.PathSeparator) + dataFolder
63+
gaia.Cfg.PipelinePath = gaia.Cfg.DataPath + string(os.PathSeparator) + pipelinesFolder
64+
err := os.MkdirAll(gaia.Cfg.PipelinePath, 0700)
65+
if err != nil {
66+
gaia.Cfg.Logger.Error("cannot create data folder", "error", err.Error(), "path", gaia.Cfg.DataPath)
67+
os.Exit(1)
68+
}
69+
5470
// Initialize IRIS
5571
irisInstance = iris.New()
5672

5773
// Initialize store
5874
s := store.NewStore()
59-
err := s.Init()
75+
err = s.Init()
6076
if err != nil {
6177
gaia.Cfg.Logger.Error("cannot initialize store", "error", err.Error())
6278
os.Exit(1)
@@ -69,6 +85,9 @@ func main() {
6985
os.Exit(1)
7086
}
7187

88+
// Start ticker. Periodic job to check for new plugins.
89+
pipeline.InitTicker()
90+
7291
// Start listen
7392
irisInstance.Run(iris.Addr(":" + gaia.Cfg.ListenPort))
7493
}

frontend/client/views/pipelines/create.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ export default {
399399
}
400400
401401
.progress-bar-height {
402-
height: 45px;
402+
height: 50px;
403403
}
404404
405405
.table td {

gaia.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ var Cfg *Config
6565

6666
// Config holds all config options
6767
type Config struct {
68-
ListenPort string
69-
HomePath string
70-
Logger hclog.Logger
68+
ListenPort string
69+
HomePath string
70+
DataPath string
71+
PipelinePath string
72+
Logger hclog.Logger
7173

7274
Bolt struct {
7375
Path string

handlers/pipeline.go

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func CreatePipeline(ctx iris.Context) {
6060
ctx.WriteString(err.Error())
6161
return
6262
}
63-
gaia.Cfg.Logger.Debug("create pipeline", "pipeline", (*p))
6463

6564
// Set the creation date and unique id
6665
p.Created = time.Now()

pipeline/build_golang.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pipeline
22

33
import (
44
"context"
5+
"io"
56
"os"
67
"os/exec"
78
"time"
@@ -102,6 +103,33 @@ func executeCmd(path string, args []string, env []string, dir string) ([]byte, e
102103
// CopyBinary copies the final compiled archive to the
103104
// destination folder.
104105
func (b *BuildPipelineGolang) CopyBinary(p *gaia.CreatePipeline) error {
105-
// TODO
106-
return nil
106+
// Define src and destination
107+
src := p.Pipeline.Repo.LocalDest + string(os.PathSeparator) + p.Pipeline.Name
108+
dest := gaia.Cfg.PipelinePath + string(os.PathSeparator) + p.Pipeline.Name
109+
110+
return copyFileContents(src, dest)
111+
}
112+
113+
// copyFileContents copies the content from source to destination.
114+
func copyFileContents(src, dst string) (err error) {
115+
in, err := os.Open(src)
116+
if err != nil {
117+
return
118+
}
119+
defer in.Close()
120+
out, err := os.Create(dst)
121+
if err != nil {
122+
return
123+
}
124+
defer func() {
125+
cerr := out.Close()
126+
if err == nil {
127+
err = cerr
128+
}
129+
}()
130+
if _, err = io.Copy(out, in); err != nil {
131+
return
132+
}
133+
err = out.Sync()
134+
return
107135
}

pipeline/ticker.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package pipeline
2+
3+
import (
4+
"io/ioutil"
5+
"time"
6+
7+
"github.com/gaia-pipeline/gaia"
8+
)
9+
10+
const (
11+
tickerIntervalSeconds = 5
12+
)
13+
14+
// InitTicker inititates the pipeline ticker.
15+
// This periodic job will check for new pipelines.
16+
func InitTicker() {
17+
// Create ticker
18+
ticker := time.NewTicker(tickerIntervalSeconds * time.Second)
19+
quit := make(chan struct{})
20+
21+
// Actual ticker implementation
22+
go func() {
23+
for {
24+
select {
25+
case <-ticker.C:
26+
files, err := ioutil.ReadDir(gaia.Cfg.PipelinePath)
27+
if err != nil {
28+
gaia.Cfg.Logger.Error("cannot read pipelines folder", "error", err.Error(), "path", gaia.Cfg.PipelinePath)
29+
} else {
30+
// Iterate all found pipeline
31+
for _, file := range files {
32+
// TODO: Create for every file a pipeline object
33+
// and store it in a global pipeline array
34+
gaia.Cfg.Logger.Debug("pipeline found", "name", file.Name())
35+
}
36+
}
37+
case <-quit:
38+
ticker.Stop()
39+
return
40+
}
41+
}
42+
}()
43+
}

store/store.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import (
88
"github.com/gaia-pipeline/gaia"
99
)
1010

11-
const (
12-
dataFolder = "data"
13-
)
14-
1511
var (
1612
// Name of the bucket where we store user objects
1713
userBucket = []byte("Users")
@@ -45,15 +41,8 @@ func NewStore() *Store {
4541
// This should be called only once per database
4642
// because bolt holds a lock on the database file.
4743
func (s *Store) Init() error {
48-
// Make sure data folder exists
49-
folder := gaia.Cfg.HomePath + string(os.PathSeparator) + dataFolder
50-
err := os.MkdirAll(folder, 0700)
51-
if err != nil {
52-
return err
53-
}
54-
5544
// Open connection to bolt database
56-
path := folder + string(os.PathSeparator) + gaia.Cfg.Bolt.Path
45+
path := gaia.Cfg.DataPath + string(os.PathSeparator) + gaia.Cfg.Bolt.Path
5746
db, err := bolt.Open(path, gaia.Cfg.Bolt.Mode, nil)
5847
if err != nil {
5948
return err

0 commit comments

Comments
 (0)