Skip to content

Commit 48f9525

Browse files
committed
Repository checkout works now fully with the correct path. UI is automatically updated with progress.
1 parent 76a072a commit 48f9525

File tree

12 files changed

+234
-68
lines changed

12 files changed

+234
-68
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ selenium-debug.log
2525
test/unit/coverage
2626
test/e2e/reports
2727

28-
# data folder generated by gaia
29-
data/
28+
# home folder generated by gaia during local test
29+
tmp/

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ build: ./cmd/gaia/main.go
44
go install ./cmd/gaia/
55

66
run: build
7-
gaia
7+
gaia -homepath=${PWD}/tmp

cmd/gaia/main.go

+34-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"flag"
55
"os"
6+
"path/filepath"
67

78
"github.com/gaia-pipeline/gaia"
89
"github.com/gaia-pipeline/gaia/handlers"
@@ -12,51 +13,72 @@ import (
1213
)
1314

1415
var (
15-
cfg *gaia.Config
1616
irisInstance *iris.Application
1717
)
1818

1919
func init() {
20-
cfg = &gaia.Config{}
20+
gaia.Cfg = &gaia.Config{}
2121

2222
// command line arguments
23-
flag.StringVar(&cfg.ListenPort, "port", "8080", "Listen port for gaia")
24-
flag.StringVar(&cfg.DataPath, "datapath", "data", "Path to the data folder")
25-
flag.StringVar(&cfg.Bolt.Path, "dbpath", "gaia.db", "Path to gaia bolt db file")
23+
flag.StringVar(&gaia.Cfg.ListenPort, "port", "8080", "Listen port for gaia")
24+
flag.StringVar(&gaia.Cfg.HomePath, "homepath", "", "Path to the gaia home folder")
25+
flag.StringVar(&gaia.Cfg.Bolt.Path, "dbpath", "gaia.db", "Path to gaia bolt db file")
2626

2727
// Default values
28-
cfg.Bolt.Mode = 0600
28+
gaia.Cfg.Bolt.Mode = 0600
2929
}
3030

3131
func main() {
3232
// Parse command line flgs
3333
flag.Parse()
3434

3535
// Initialize shared logger
36-
cfg.Logger = hclog.New(&hclog.LoggerOptions{
36+
gaia.Cfg.Logger = hclog.New(&hclog.LoggerOptions{
3737
Level: hclog.Trace,
3838
Output: hclog.DefaultOutput,
3939
Name: "Gaia",
4040
})
4141

42+
// Find path for gaia home folder if not given by parameter
43+
if gaia.Cfg.HomePath == "" {
44+
// Find executeable path
45+
execPath, err := findExecuteablePath()
46+
if err != nil {
47+
gaia.Cfg.Logger.Error("cannot find executeable path", "error", err.Error())
48+
os.Exit(1)
49+
}
50+
gaia.Cfg.HomePath = execPath
51+
gaia.Cfg.Logger.Debug("executeable path found", "path", execPath)
52+
}
53+
4254
// Initialize IRIS
4355
irisInstance = iris.New()
4456

4557
// Initialize store
4658
s := store.NewStore()
47-
err := s.Init(cfg)
59+
err := s.Init()
4860
if err != nil {
49-
cfg.Logger.Error("cannot initialize store", "error", err.Error())
61+
gaia.Cfg.Logger.Error("cannot initialize store", "error", err.Error())
5062
os.Exit(1)
5163
}
5264

5365
// Initialize handlers
54-
err = handlers.InitHandlers(cfg, irisInstance, s)
66+
err = handlers.InitHandlers(irisInstance, s)
5567
if err != nil {
56-
cfg.Logger.Error("cannot initialize handlers", "error", err.Error())
68+
gaia.Cfg.Logger.Error("cannot initialize handlers", "error", err.Error())
5769
os.Exit(1)
5870
}
5971

6072
// Start listen
61-
irisInstance.Run(iris.Addr(":" + cfg.ListenPort))
73+
irisInstance.Run(iris.Addr(":" + gaia.Cfg.ListenPort))
74+
}
75+
76+
// findExecuteablePath returns the absolute path for the current
77+
// process.
78+
func findExecuteablePath() (string, error) {
79+
ex, err := os.Executable()
80+
if err != nil {
81+
return "", err
82+
}
83+
return filepath.Dir(ex), nil
6284
}

frontend/client/views/pipelines/create.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@
9898
</tr>
9999
</thead>
100100
<tbody>
101-
<tr class="blink" v-for="pipeline in createdPipelines" :key="pipeline">
101+
<tr v-bind:class="{ blink: pipeline.status < 100 }" v-for="(pipeline, index) in createdPipelines" :key="index">
102102
<td>
103103
{{ pipeline.pipelinename }}
104104
</td>
105-
<td class="th">
105+
<td>
106106
<div v-if="pipeline.status < 100">
107107
<progress-bar :type="'info'" :size="'small'" :value="pipeline.status" :max="100" :show-label="false"></progress-bar>
108108
</div>

gaia.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
hclog "github.com/hashicorp/go-hclog"
88
)
99

10-
// PluginType represents supported plugin types
11-
type PluginType string
10+
// PipelineType represents supported plugin types
11+
type PipelineType string
1212

1313
const (
1414
// GOLANG plugin type
15-
GOLANG PluginType = "golang"
15+
GOLANG PipelineType = "golang"
1616
)
1717

1818
// User is the user object
@@ -26,12 +26,12 @@ type User struct {
2626

2727
// Pipeline represents a single pipeline
2828
type Pipeline struct {
29-
Name string `json:"pipelinename"`
30-
Repo GitRepo `json:"gitrepo"`
31-
Type PluginType `json:"pipelinetype"`
32-
Status int `json:"status"`
33-
ErrMsg string `json:"errmsg"`
34-
CreationDate time.Time `json:"creationdate"`
29+
Name string `json:"pipelinename"`
30+
Repo GitRepo `json:"gitrepo"`
31+
Type PipelineType `json:"pipelinetype"`
32+
Status int `json:"status"`
33+
ErrMsg string `json:"errmsg"`
34+
CreationDate time.Time `json:"creationdate"`
3535
}
3636

3737
// GitRepo represents a single git repository
@@ -52,10 +52,13 @@ type PrivateKey struct {
5252
Password string `json:"password"`
5353
}
5454

55+
// Cfg represents the global config instance
56+
var Cfg *Config
57+
5558
// Config holds all config options
5659
type Config struct {
5760
ListenPort string
58-
DataPath string
61+
HomePath string
5962
Logger hclog.Logger
6063

6164
Bolt struct {

handlers/User.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ func UserLogin(ctx iris.Context) {
2424
if err := ctx.ReadJSON(u); err != nil {
2525
ctx.StatusCode(iris.StatusBadRequest)
2626
ctx.WriteString(err.Error())
27-
cfg.Logger.Debug("error reading json during UserLogin", "error", err.Error())
27+
gaia.Cfg.Logger.Debug("error reading json during UserLogin", "error", err.Error())
2828
return
2929
}
3030

3131
// Authenticate user
3232
user, err := storeService.UserAuth(u)
3333
if err != nil {
34-
cfg.Logger.Error("error during UserAuth", "error", err.Error())
34+
gaia.Cfg.Logger.Error("error during UserAuth", "error", err.Error())
3535
ctx.StatusCode(iris.StatusInternalServerError)
3636
return
3737
}
@@ -58,7 +58,7 @@ func UserLogin(ctx iris.Context) {
5858
tokenstring, err := token.SignedString(jwtKey)
5959
if err != nil {
6060
ctx.StatusCode(iris.StatusInternalServerError)
61-
cfg.Logger.Error("error signing jwt token", "error", err.Error())
61+
gaia.Cfg.Logger.Error("error signing jwt token", "error", err.Error())
6262
return
6363
}
6464
user.JwtExpiry = claims.ExpiresAt

handlers/handler.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package handlers
33
import (
44
"crypto/rand"
55

6-
"github.com/gaia-pipeline/gaia"
76
"github.com/gaia-pipeline/gaia/store"
87
"github.com/kataras/iris"
98
)
@@ -16,17 +15,11 @@ const (
1615
// Use this to talk to the store.
1716
var storeService *store.Store
1817

19-
// cfg is a pointer to the global config
20-
var cfg *gaia.Config
21-
2218
// jwtKey is a random generated key for jwt signing
2319
var jwtKey []byte
2420

2521
// InitHandlers initializes(registers) all handlers
26-
func InitHandlers(c *gaia.Config, i *iris.Application, s *store.Store) error {
27-
// Set config
28-
cfg = c
29-
22+
func InitHandlers(i *iris.Application, s *store.Store) error {
3023
// Set store instance
3124
storeService = s
3225

handlers/pipeline.go

+49-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const (
2121
// Percent of pipeline creation progress after git clone
2222
pipelineCloneStatus = 25
2323

24+
// Percent of pipeline creation progress after compile process done
25+
pipelineCompileStatus = 75
26+
2427
// Completed percent progress
2528
pipelineCompleteStatus = 100
2629
)
@@ -65,7 +68,7 @@ func PipelineBuildFromSource(ctx iris.Context) {
6568
if err != nil {
6669
ctx.StatusCode(iris.StatusInternalServerError)
6770
ctx.WriteString(err.Error())
68-
cfg.Logger.Debug("cannot put pipeline into store", "error", err.Error())
71+
gaia.Cfg.Logger.Debug("cannot put pipeline into store", "error", err.Error())
6972
return
7073
}
7174

@@ -77,37 +80,72 @@ func PipelineBuildFromSource(ctx iris.Context) {
7780
// the pipeline. After every step, the status will be store.
7881
// This method is designed to be called async.
7982
func createPipelineExecute(p *gaia.Pipeline) {
83+
// Define build process for the given type
84+
bP := pipeline.NewBuildPipeline(p.Type)
85+
if bP == nil {
86+
// Pipeline type is not supported
87+
gaia.Cfg.Logger.Debug("create pipeline failed. Pipeline type is not supported", "type", p.Type)
88+
return
89+
}
90+
91+
// Setup environment before cloning repo and command
92+
cmd, err := bP.PrepareBuild(p)
93+
if err != nil {
94+
gaia.Cfg.Logger.Error("cannot prepare build", "error", err.Error())
95+
return
96+
}
97+
8098
// Clone git repo
81-
err := pipeline.GitCloneRepo(&p.Repo)
99+
err = pipeline.GitCloneRepo(&p.Repo)
82100
if err != nil {
83101
// Add error message and store
84102
p.ErrMsg = err.Error()
85103
storeService.CreatePipelinePut(p)
86-
cfg.Logger.Debug("cannot clone repo", "error", err.Error())
104+
gaia.Cfg.Logger.Debug("cannot clone repo", "error", err.Error())
87105
return
88106
}
89107

90108
// Update status of our pipeline build
91109
p.Status = pipelineCloneStatus
92110
err = storeService.CreatePipelinePut(p)
93111
if err != nil {
94-
cfg.Logger.Error("cannot put create pipeline into store", "error", err.Error())
112+
gaia.Cfg.Logger.Error("cannot put create pipeline into store", "error", err.Error())
95113
return
96114
}
97115

98-
// Start compiling process for given plugin type
99-
switch p.Type {
100-
case gaia.GOLANG:
101-
// Start compile process for golang TODO
116+
// Run compile process
117+
err = bP.ExecuteBuild(cmd)
118+
if err != nil {
119+
// Add error message and store
120+
p.ErrMsg = err.Error()
121+
storeService.CreatePipelinePut(p)
122+
gaia.Cfg.Logger.Debug("cannot compile pipeline", "error", err.Error())
123+
return
102124
}
103125

104-
// copy compiled binary to plugins folder
126+
// Update status of our pipeline build
127+
p.Status = pipelineCompileStatus
128+
err = storeService.CreatePipelinePut(p)
129+
if err != nil {
130+
gaia.Cfg.Logger.Error("cannot put create pipeline into store", "error", err.Error())
131+
return
132+
}
133+
134+
// Copy compiled binary to plugins folder
135+
err = bP.CopyBinary(p)
136+
if err != nil {
137+
// Add error message and store
138+
p.ErrMsg = err.Error()
139+
storeService.CreatePipelinePut(p)
140+
gaia.Cfg.Logger.Debug("cannot copy compiled binary", "error", err.Error())
141+
return
142+
}
105143

106144
// Set create pipeline status to complete
107145
p.Status = pipelineCompleteStatus
108146
err = storeService.CreatePipelinePut(p)
109147
if err != nil {
110-
cfg.Logger.Error("cannot put create pipeline into store", "error", err.Error())
148+
gaia.Cfg.Logger.Error("cannot put create pipeline into store", "error", err.Error())
111149
return
112150
}
113151
}
@@ -121,7 +159,7 @@ func CreatePipelineGetAll(ctx iris.Context) {
121159
if err != nil {
122160
ctx.StatusCode(iris.StatusInternalServerError)
123161
ctx.WriteString(err.Error())
124-
cfg.Logger.Debug("cannot get create pipelines from store", "error", err.Error())
162+
gaia.Cfg.Logger.Debug("cannot get create pipelines from store", "error", err.Error())
125163
return
126164
}
127165

0 commit comments

Comments
 (0)