From 1ccaf2a8cbdb9667d39723f97f8637bfda51e256 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 15:06:41 +0100 Subject: [PATCH 01/10] [build][packaging] Add resilience when docker build --- dev-tools/mage/dockerbuilder.go | 9 ++++++++- x-pack/elastic-agent/magefile.go | 13 +++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/dev-tools/mage/dockerbuilder.go b/dev-tools/mage/dockerbuilder.go index 90a99434884..f47010f871b 100644 --- a/dev-tools/mage/dockerbuilder.go +++ b/dev-tools/mage/dockerbuilder.go @@ -26,6 +26,7 @@ import ( "os/exec" "path/filepath" "strings" + "time" "github.com/magefile/mage/sh" "github.com/pkg/errors" @@ -71,7 +72,13 @@ func (b *dockerBuilder) Build() error { tag, err := b.dockerBuild() if err != nil { - return errors.Wrap(err, "failed to build docker") + fmt.Println(">> Building docker images again (after 10 seconds)") + // This sleep is to avoid hitting the docker build issues when resources are not available. + time.Sleep(10) + tag, err := b.dockerBuild() + if err != nil { + return errors.Wrap(err, "failed to build docker") + } } if err := b.dockerSave(tag); err != nil { diff --git a/x-pack/elastic-agent/magefile.go b/x-pack/elastic-agent/magefile.go index a1aaba840fb..903954810a2 100644 --- a/x-pack/elastic-agent/magefile.go +++ b/x-pack/elastic-agent/magefile.go @@ -513,8 +513,13 @@ func runAgent(env map[string]string) error { } // build docker image - if err := sh.Run("docker", "build", "-t", tag, "."); err != nil { - return err + if err := dockerBuild(tag); err != nil { + fmt.Println(">> Building docker images again (after 10 seconds)") + // This sleep is to avoid hitting the docker build issues when resources are not available. + time.Sleep(10) + if err := dockerBuild(tag); err != nil { + return err + } } } @@ -625,6 +630,10 @@ func copyAll(from, to string) error { }) } +func dockerBuild(tag) string { + return sh.Run("docker", "build", "-t", tag, ".") +} + func dockerTag() string { const commitLen = 7 tagBase := "elastic-agent" From ba580ab63ac94dd19ff9528278839c7df1625fae Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 15:19:12 +0100 Subject: [PATCH 02/10] Update x-pack/elastic-agent/magefile.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Manuel de la Peña --- x-pack/elastic-agent/magefile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/elastic-agent/magefile.go b/x-pack/elastic-agent/magefile.go index 903954810a2..5118cda8c05 100644 --- a/x-pack/elastic-agent/magefile.go +++ b/x-pack/elastic-agent/magefile.go @@ -630,7 +630,7 @@ func copyAll(from, to string) error { }) } -func dockerBuild(tag) string { +func dockerBuild(tag string) string { return sh.Run("docker", "build", "-t", tag, ".") } From a1a6668bc4fe60bf1c8f4532f4f0b224327fcc63 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 15:23:15 +0100 Subject: [PATCH 03/10] [docker-compose] Add resilience when docker-compose up --build --- libbeat/tests/compose/wrapper.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libbeat/tests/compose/wrapper.go b/libbeat/tests/compose/wrapper.go index 87919c3fd9c..915f194ee64 100644 --- a/libbeat/tests/compose/wrapper.go +++ b/libbeat/tests/compose/wrapper.go @@ -195,6 +195,23 @@ func (d *wrapperDriver) Up(ctx context.Context, opts UpOptions, service string) return errors.Wrapf(err, "failed to pull images using docker-compose: %s", stderr.String()) } + // Try to build the docker images + var stderr bytes.Buffer + build := d.cmd(ctx, "build", "--pull", service) + build.Stdout = nil + build.Stderr = &stderr + if err := build.Run(); err != nil { + fmt.Println(">> Building docker images again (after 10 seconds)") + // This sleep is to avoid hitting the docker build issues when resources are not available. + time.Sleep(10) + build := d.cmd(ctx, "build", service) + build.Stdout = nil + build.Stderr = &stderr + if err := build.Run(); err != nil { + fmt.Println(">> Building docker images failed using docker-compose (let's try now with docker-compose up --build): %s", stderr.String()) + } + } + err := d.cmd(ctx, "up", args...).Run() if err != nil { return err From dba421c34789b9baa3ed89722377889b46827125 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 15:31:09 +0100 Subject: [PATCH 04/10] fix linting --- dev-tools/mage/dockerbuilder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-tools/mage/dockerbuilder.go b/dev-tools/mage/dockerbuilder.go index f47010f871b..503fcae9cfc 100644 --- a/dev-tools/mage/dockerbuilder.go +++ b/dev-tools/mage/dockerbuilder.go @@ -75,7 +75,7 @@ func (b *dockerBuilder) Build() error { fmt.Println(">> Building docker images again (after 10 seconds)") // This sleep is to avoid hitting the docker build issues when resources are not available. time.Sleep(10) - tag, err := b.dockerBuild() + tag, err = b.dockerBuild() if err != nil { return errors.Wrap(err, "failed to build docker") } From 81e447ad0c9a5947713c51335e4c63790280e237 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 15:40:47 +0100 Subject: [PATCH 05/10] fix linting --- dev-tools/mage/dockerbuilder.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dev-tools/mage/dockerbuilder.go b/dev-tools/mage/dockerbuilder.go index 503fcae9cfc..ae6b2622d8d 100644 --- a/dev-tools/mage/dockerbuilder.go +++ b/dev-tools/mage/dockerbuilder.go @@ -75,7 +75,8 @@ func (b *dockerBuilder) Build() error { fmt.Println(">> Building docker images again (after 10 seconds)") // This sleep is to avoid hitting the docker build issues when resources are not available. time.Sleep(10) - tag, err = b.dockerBuild() + tagSecondTry, err := b.dockerBuild() + tag = tagSecondTry if err != nil { return errors.Wrap(err, "failed to build docker") } From 1c2672089e5c69d13ffeb64565d8ff34dae0f590 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 15:45:56 +0100 Subject: [PATCH 06/10] Revert "fix linting" This reverts commit 81e447ad0c9a5947713c51335e4c63790280e237. --- dev-tools/mage/dockerbuilder.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dev-tools/mage/dockerbuilder.go b/dev-tools/mage/dockerbuilder.go index ae6b2622d8d..503fcae9cfc 100644 --- a/dev-tools/mage/dockerbuilder.go +++ b/dev-tools/mage/dockerbuilder.go @@ -75,8 +75,7 @@ func (b *dockerBuilder) Build() error { fmt.Println(">> Building docker images again (after 10 seconds)") // This sleep is to avoid hitting the docker build issues when resources are not available. time.Sleep(10) - tagSecondTry, err := b.dockerBuild() - tag = tagSecondTry + tag, err = b.dockerBuild() if err != nil { return errors.Wrap(err, "failed to build docker") } From 8211f250a04f0553abf9a63f5956bf54bf80550b Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 15:57:13 +0100 Subject: [PATCH 07/10] fix linting redeclared --- libbeat/tests/compose/wrapper.go | 1 - 1 file changed, 1 deletion(-) diff --git a/libbeat/tests/compose/wrapper.go b/libbeat/tests/compose/wrapper.go index 915f194ee64..8b5d1128e71 100644 --- a/libbeat/tests/compose/wrapper.go +++ b/libbeat/tests/compose/wrapper.go @@ -196,7 +196,6 @@ func (d *wrapperDriver) Up(ctx context.Context, opts UpOptions, service string) } // Try to build the docker images - var stderr bytes.Buffer build := d.cmd(ctx, "build", "--pull", service) build.Stdout = nil build.Stderr = &stderr From 1b94c0563e5820b8b880b14e3139672451009b2d Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 16:17:44 +0100 Subject: [PATCH 08/10] Use printf --- libbeat/tests/compose/wrapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libbeat/tests/compose/wrapper.go b/libbeat/tests/compose/wrapper.go index 8b5d1128e71..44ba297eed2 100644 --- a/libbeat/tests/compose/wrapper.go +++ b/libbeat/tests/compose/wrapper.go @@ -207,7 +207,7 @@ func (d *wrapperDriver) Up(ctx context.Context, opts UpOptions, service string) build.Stdout = nil build.Stderr = &stderr if err := build.Run(); err != nil { - fmt.Println(">> Building docker images failed using docker-compose (let's try now with docker-compose up --build): %s", stderr.String()) + fmt.Printf(">> Building docker images failed using docker-compose (let's try now with docker-compose up --build): %s", stderr.String()) } } From addadf2f692949a3b90c2825adb150acf952c7d0 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 21 Oct 2020 16:34:46 +0100 Subject: [PATCH 09/10] Fix type --- x-pack/elastic-agent/magefile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/elastic-agent/magefile.go b/x-pack/elastic-agent/magefile.go index 5118cda8c05..fad5ef935aa 100644 --- a/x-pack/elastic-agent/magefile.go +++ b/x-pack/elastic-agent/magefile.go @@ -630,7 +630,7 @@ func copyAll(from, to string) error { }) } -func dockerBuild(tag string) string { +func dockerBuild(tag string) error { return sh.Run("docker", "build", "-t", tag, ".") } From 109932add5e85130661b6168c53270e750218e85 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 22 Oct 2020 10:04:23 +0100 Subject: [PATCH 10/10] As stated in the review this change is not required anymore, since the retry happens in https://github.com/elastic/beats/blob/addadf2f692949a3b90c2825adb150acf952c7d0/libbeat/tests/compose/compose.go\#L99 --- libbeat/tests/compose/wrapper.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/libbeat/tests/compose/wrapper.go b/libbeat/tests/compose/wrapper.go index 44ba297eed2..87919c3fd9c 100644 --- a/libbeat/tests/compose/wrapper.go +++ b/libbeat/tests/compose/wrapper.go @@ -195,22 +195,6 @@ func (d *wrapperDriver) Up(ctx context.Context, opts UpOptions, service string) return errors.Wrapf(err, "failed to pull images using docker-compose: %s", stderr.String()) } - // Try to build the docker images - build := d.cmd(ctx, "build", "--pull", service) - build.Stdout = nil - build.Stderr = &stderr - if err := build.Run(); err != nil { - fmt.Println(">> Building docker images again (after 10 seconds)") - // This sleep is to avoid hitting the docker build issues when resources are not available. - time.Sleep(10) - build := d.cmd(ctx, "build", service) - build.Stdout = nil - build.Stderr = &stderr - if err := build.Run(); err != nil { - fmt.Printf(">> Building docker images failed using docker-compose (let's try now with docker-compose up --build): %s", stderr.String()) - } - } - err := d.cmd(ctx, "up", args...).Run() if err != nil { return err