Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker builds binary in case the binary doesn't match the worker arch #200

Merged
merged 52 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
265fb7d
Initial try to build binary by worker.
Jul 23, 2019
9557f27
A little bit of refactoring.
Jul 23, 2019
a56c272
Cleaning up the errorous binary.
Skarlso Jul 23, 2019
763462d
A different approach.
Skarlso Jul 23, 2019
fc756b9
Creating an api which the worker can call to get information about th…
Skarlso Jul 24, 2019
0e241f5
Skeleton of the api
Skarlso Jul 25, 2019
43fab8f
Added more logic.
Skarlso Jul 25, 2019
f355109
Using the name instead
Skarlso Jul 25, 2019
4bde3b2
Moar info
Skarlso Jul 25, 2019
8c346ad
Added correct error display.
Skarlso Jul 25, 2019
3ab6e20
RPC endpoint.
Jul 25, 2019
b19d052
Changed impl to gRPC
Jul 25, 2019
0b67ca3
Fixed wording and nil panic
Skarlso Jul 25, 2019
5b22447
Using the ID instead.
Skarlso Jul 25, 2019
52843de
Cannot save pipeline run
Skarlso Jul 25, 2019
0045758
GitPull the repo...
Skarlso Jul 25, 2019
73d8c18
Handling error
Skarlso Jul 25, 2019
23198f9
It works!
Skarlso Jul 25, 2019
d5acea1
Agent test build failure.
Skarlso Jul 25, 2019
9d19530
linter
Skarlso Jul 25, 2019
7c5648b
Trying out createpipeline again.
Skarlso Jul 26, 2019
c5ff190
Extracting rebuilding of binary.
Skarlso Jul 26, 2019
0e76372
Linting fix
Skarlso Jul 26, 2019
b625ee5
mergin...
Skarlso Jul 27, 2019
4e00b1f
Merge branch 'master' into issues_194_worker_builds_binary
Skarlso Jul 27, 2019
c1dbc53
Fixed version of protoc-gen-go
Skarlso Jul 27, 2019
ef7784d
Create the skeleton for the sha pair work
Aug 17, 2019
7768c37
The skeleton is there. Daughter woke up I will have to continue this …
Skarlso Aug 17, 2019
3cd8a84
Small adjustments.
Skarlso Aug 17, 2019
30444f5
Added metadata uniqueid to shapair.
Skarlso Aug 17, 2019
9ac6e38
Using pipelinID as string.
Skarlso Aug 18, 2019
0fc686b
Working on checking if there was a record or not.
Skarlso Aug 18, 2019
b9ac24b
Fixed condition.
Skarlso Aug 18, 2019
899b2bf
Added re-try of the failed operation after the binary has been rebuilt.
Skarlso Aug 19, 2019
315db17
Fixed the test.
Skarlso Aug 19, 2019
ba10c67
Added test and nil check for private key.
Skarlso Aug 19, 2019
3efb482
Deleting it for now... And then I will try to fix that.
Skarlso Aug 19, 2019
91b094d
Removed gosdk.
Skarlso Aug 19, 2019
1130104
Added a basic test case for the re-builder.
Skarlso Aug 19, 2019
c9e1bdf
Added test for the pipeline rebuilder.
Aug 22, 2019
820ff51
Some more coverage.
Aug 22, 2019
3c31dfb
TODO: Fix whatever is adding an extra run to the update workflow. It …
Aug 23, 2019
178a795
Fixed the test which was causing problems.
Aug 23, 2019
f773964
Passed test.
Aug 23, 2019
48ca7f6
Added moar coverage for the worker.
Skarlso Aug 25, 2019
47ea5c9
Added coverage to sha pair methods.
Skarlso Aug 25, 2019
f2c29d2
Merge branch 'master' into issues_194_worker_builds_binary
Sep 3, 2019
e4744b9
Shifted the sha storing process into bolt db.
Sep 3, 2019
e098b75
Added tests and fixed bucket names.
Sep 3, 2019
ca090e3
Changed to PipelineID instead of UniqueID which was unique per run in…
Sep 3, 2019
41b0432
Fixed comments.
Sep 4, 2019
5ca5149
Renamed uniqueID field to pipelineid so it does not get accidentally …
Sep 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions gaia.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ type Worker struct {
Tags []string `json:"tags"`
}

// SHAPair struct contains the original sha of a pipeline executable and the
// new sha which was created when the worker had to rebuild it.
type SHAPair struct {
Original []byte `json:"original"`
Worker []byte `json:"worker"`
UniqueID int `json:"pipelineid"`
}

// Cfg represents the global config instance
var Cfg = &Config{}

Expand Down
51 changes: 51 additions & 0 deletions store/sha_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package store

import (
"encoding/json"

bolt "github.com/coreos/bbolt"
"github.com/gaia-pipeline/gaia"
)

// UpsertSHAPair creates or updates a record for a SHA pair of the original SHA and the
// rebuilt Worker SHA for a pipeline.
func (s *BoltStore) UpsertSHAPair(pair gaia.SHAPair) error {
return s.db.Update(func(tx *bolt.Tx) error {
// Get bucket
b := tx.Bucket(shaPairBucket)

// Marshal user object
m, err := json.Marshal(pair)
if err != nil {
return err
}

// Put user
return b.Put(itob(pair.UniqueID), m)
})
}

// GetSHAPair returns a pair of shas for this pipeline run.
func (s *BoltStore) GetSHAPair(pipelineID int) (ok bool, pair gaia.SHAPair, err error) {
return ok, pair, s.db.View(func(tx *bolt.Tx) error {
// Get bucket
b := tx.Bucket(shaPairBucket)

// Get pipeline
v := b.Get(itob(pipelineID))

// Check if we found the pipeline
if v == nil {
ok = false
return nil
}

// Unmarshal pipeline object
err := json.Unmarshal(v, &pair)
if err != nil {
return err
}
ok = true
return nil
})
}
104 changes: 104 additions & 0 deletions store/sha_pair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package store

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/gaia-pipeline/gaia"
)

func TestGetSHAPair(t *testing.T) {
// Create tmp folder
tmp, err := ioutil.TempDir("", "TestGetSHAPAir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

store := NewBoltStore()
gaia.Cfg.Bolt.Mode = 0600
err = store.Init(tmp)
if err != nil {
t.Fatal(err)
}
defer store.Close()

pair := gaia.SHAPair{}
pair.UniqueID = 1
pair.Original = []byte("original")
pair.Worker = []byte("worker")
err = store.UpsertSHAPair(pair)
if err != nil {
t.Fatal(err)
}

ok, p, err := store.GetSHAPair(1)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatalf("sha pair not found")
}

if p.UniqueID != pair.UniqueID {
t.Fatalf("unique id match error. want %d got %d", pair.UniqueID, p.UniqueID)
}
if !bytes.Equal(p.Worker, pair.Worker) {
t.Fatalf("worker sha match error. want %s got %s", pair.Worker, p.Worker)
}
if !bytes.Equal(p.Original, pair.Original) {
t.Fatalf("original sha match error. want %s got %s", pair.Original, p.Original)
}
}

func TestUpsertSHAPair(t *testing.T) {
// Create tmp folder
tmp, err := ioutil.TempDir("", "TestUpsertSHAPair")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

store := NewBoltStore()
gaia.Cfg.Bolt.Mode = 0600
err = store.Init(tmp)
if err != nil {
t.Fatal(err)
}
defer store.Close()

pair := gaia.SHAPair{}
pair.UniqueID = 1
pair.Original = []byte("original")
pair.Worker = []byte("worker")
err = store.UpsertSHAPair(pair)
if err != nil {
t.Fatal(err)
}
// Test is upsert overwrites existing records.
pair.Original = []byte("original2")
err = store.UpsertSHAPair(pair)
if err != nil {
t.Fatal(err)
}

ok, p, err := store.GetSHAPair(1)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatalf("sha pair not found")
}

if p.UniqueID != pair.UniqueID {
t.Fatalf("unique id match error. want %d got %d", pair.UniqueID, p.UniqueID)
}
if !bytes.Equal(p.Worker, pair.Worker) {
t.Fatalf("worker sha match error. want %s got %s", pair.Worker, p.Worker)
}
if !bytes.Equal(p.Original, pair.Original) {
t.Fatalf("original sha match error. want %s got %s", pair.Original, p.Original)
}
}
80 changes: 41 additions & 39 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var (

// Name of the bucket where we store all worker.
workerBucket = []byte("Worker")

// SHA pair bucket.
shaPairBucket = []byte("SHAPair")
)

const (
Expand Down Expand Up @@ -85,6 +88,8 @@ type GaiaStore interface {
WorkerDelete(id string) error
WorkerDeleteAll() error
WorkerGet(id string) (*gaia.Worker, error)
UpsertSHAPair(pair gaia.SHAPair) error
GetSHAPair(pipelineID int) (bool, gaia.SHAPair, error)
}

// Compile time interface compliance check for BoltStore. If BoltStore
Expand Down Expand Up @@ -120,54 +125,48 @@ func (s *BoltStore) Close() error {
return s.db.Close()
}

// setupDatabase create all buckets in the db.
// Additionally, it makes sure that the admin user exists.
func (s *BoltStore) setupDatabase() error {
// Create bucket if not exists function
var bucketName []byte
type setup struct {
bs *BoltStore
err error
}

// Create bucket if not exists function
func (s *setup) update(bucketName []byte) {
// update is a no-op in case there was already an error
if s.err != nil {
return
}
c := func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(bucketName)
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
}
s.err = s.bs.db.Update(c)
}

// Make sure buckets exist
bucketName = userBucket
err := s.db.Update(c)
if err != nil {
return err
}
bucketName = userPermsBucket
err = s.db.Update(c)
if err != nil {
return err
}
bucketName = pipelineBucket
err = s.db.Update(c)
if err != nil {
return err
}
bucketName = createPipelineBucket
err = s.db.Update(c)
if err != nil {
return err
}
bucketName = pipelineRunBucket
err = s.db.Update(c)
if err != nil {
return err
}
bucketName = settingsBucket
err = s.db.Update(c)
if err != nil {
return err
// setupDatabase create all buckets in the db.
// Additionally, it makes sure that the admin user exists.
func (s *BoltStore) setupDatabase() error {
// Create bucket if not exists function
setP := &setup{
bs: s,
err: nil,
}
bucketName = workerBucket
err = s.db.Update(c)
if err != nil {
return err

// Make sure buckets exist
setP.update(userBucket)
Copy link
Member Author

@Skarlso Skarlso Sep 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michelvocks Sorry, I had to refactor this in this batch. It was too repetitive. :) Besides, you touch it you own it. :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 good job!

setP.update(userPermsBucket)
setP.update(pipelineBucket)
setP.update(createPipelineBucket)
setP.update(pipelineRunBucket)
setP.update(settingsBucket)
setP.update(workerBucket)
setP.update(shaPairBucket)

if setP.err != nil {
return setP.err
}

// Make sure that the user "admin" does exist
Expand Down Expand Up @@ -195,6 +194,9 @@ func (s *BoltStore) setupDatabase() error {
}

u, err := s.UserGet(autoUsername)
if err != nil {
return err
}

if u == nil {
triggerToken := security.GenerateRandomUUIDV5()
Expand Down
Loading