forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
… docker images when building packages (elastic#9433) Include generation of docker images in the process of building the rest of released packages by extending `mage package`. (elastic#8898) (cherry picked from commit 9294384) Tag container images with snapshot information (elastic#9063) (cherry picked from commit 1dd4fbf) * Compress docker artifacts with gzip (elastic#9223) (cherry picked from commit 675f2b0) Co-authored-by: Gil Raphaelli <[email protected]>
- Loading branch information
1 parent
c48d688
commit 97cb066
Showing
27 changed files
with
776 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
auditbeat.modules: | ||
|
||
- module: auditd | ||
audit_rules: | | ||
-w /etc/passwd -p wa -k identity | ||
-a always,exit -F arch=b32 -S open,creat,truncate,ftruncate,openat,open_by_handle_at -F exit=-EPERM -k access | ||
- module: file_integrity | ||
paths: | ||
- /bin | ||
- /usr/bin | ||
- /sbin | ||
- /usr/sbin | ||
- /etc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
auditbeat.modules: | ||
|
||
- module: auditd | ||
audit_rules: | | ||
-w /etc/passwd -p wa -k identity | ||
-a always,exit -F arch=b32 -S open,creat,truncate,ftruncate,openat,open_by_handle_at -F exit=-EPERM -k access | ||
- module: file_integrity | ||
paths: | ||
- /bin | ||
- /usr/bin | ||
- /sbin | ||
- /usr/sbin | ||
- /etc | ||
processors: | ||
- add_cloud_metadata: ~ | ||
|
||
output.elasticsearch: | ||
hosts: '${ELASTICSEARCH_HOSTS:elasticsearch:9200}' | ||
username: '${ELASTICSEARCH_USERNAME:}' | ||
password: '${ELASTICSEARCH_PASSWORD:}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package mage | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/magefile/mage/sh" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type dockerBuilder struct { | ||
PackageSpec | ||
|
||
buildDir string | ||
beatDir string | ||
} | ||
|
||
func newDockerBuilder(spec PackageSpec) (*dockerBuilder, error) { | ||
buildDir := filepath.Join(spec.packageDir, "docker-build") | ||
beatDir := filepath.Join(buildDir, "beat") | ||
|
||
return &dockerBuilder{ | ||
PackageSpec: spec, | ||
buildDir: buildDir, | ||
beatDir: beatDir, | ||
}, nil | ||
} | ||
|
||
func (b *dockerBuilder) Build() error { | ||
if err := os.RemoveAll(b.buildDir); err != nil { | ||
return errors.Wrapf(err, "failed to clean existing build directory %s", b.buildDir) | ||
} | ||
|
||
if err := b.copyFiles(); err != nil { | ||
return err | ||
} | ||
|
||
if err := b.prepareBuild(); err != nil { | ||
return errors.Wrap(err, "failed to prepare build") | ||
} | ||
|
||
tag, err := b.dockerBuild() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to build docker") | ||
} | ||
|
||
if err := b.dockerSave(tag); err != nil { | ||
return errors.Wrap(err, "failed to save docker as artifact") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b *dockerBuilder) modulesDirs() []string { | ||
var modulesd []string | ||
for _, f := range b.Files { | ||
if f.Modules { | ||
modulesd = append(modulesd, f.Target) | ||
} | ||
} | ||
return modulesd | ||
} | ||
|
||
func (b *dockerBuilder) exposePorts() []string { | ||
if ports, _ := b.ExtraVars["expose_ports"]; ports != "" { | ||
return strings.Split(ports, ",") | ||
} | ||
return nil | ||
} | ||
|
||
func (b *dockerBuilder) copyFiles() error { | ||
for _, f := range b.Files { | ||
target := filepath.Join(b.beatDir, f.Target) | ||
if err := Copy(f.Source, target); err != nil { | ||
return errors.Wrapf(err, "failed to copy from %s to %s", f.Source, target) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (b *dockerBuilder) prepareBuild() error { | ||
elasticBeatsDir, err := ElasticBeatsDir() | ||
if err != nil { | ||
return err | ||
} | ||
templatesDir := filepath.Join(elasticBeatsDir, "dev-tools/packaging/templates/docker") | ||
|
||
data := map[string]interface{}{ | ||
"ExposePorts": b.exposePorts(), | ||
"ModulesDirs": b.modulesDirs(), | ||
} | ||
|
||
return filepath.Walk(templatesDir, func(path string, info os.FileInfo, _ error) error { | ||
if !info.IsDir() { | ||
target := strings.TrimSuffix( | ||
filepath.Join(b.buildDir, filepath.Base(path)), | ||
".tmpl", | ||
) | ||
|
||
err = b.ExpandFile(path, target, data) | ||
if err != nil { | ||
return errors.Wrapf(err, "expanding template '%s' to '%s'", path, target) | ||
} | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func (b *dockerBuilder) dockerBuild() (string, error) { | ||
tag := fmt.Sprintf("%s:%s", b.Name, b.Version) | ||
if b.Snapshot { | ||
tag = tag + "-SNAPSHOT" | ||
} | ||
|
||
if repository, _ := b.ExtraVars["repository"]; repository != "" { | ||
tag = fmt.Sprintf("%s/%s", repository, tag) | ||
} | ||
return tag, sh.Run("docker", "build", "-t", tag, b.buildDir) | ||
} | ||
|
||
func (b *dockerBuilder) dockerSave(tag string) error { | ||
// Save the container as artifact | ||
outputFile := b.OutputFile | ||
if outputFile == "" { | ||
outputTar, err := b.Expand(defaultBinaryName + ".docker.tar.gz") | ||
if err != nil { | ||
return err | ||
} | ||
outputFile = filepath.Join(distributionsDir, outputTar) | ||
} | ||
var stderr bytes.Buffer | ||
cmd := exec.Command("docker", "save", tag) | ||
cmd.Stderr = &stderr | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
if err = cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
err = func() error { | ||
f, err := os.Create(outputFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
w := gzip.NewWriter(f) | ||
defer w.Close() | ||
|
||
_, err = io.Copy(w, stdout) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = cmd.Wait(); err != nil { | ||
if errmsg := strings.TrimSpace(stderr.String()); errmsg != "" { | ||
err = errors.Wrap(errors.New(errmsg), err.Error()) | ||
} | ||
return err | ||
} | ||
return errors.Wrap(CreateSHA512File(outputFile), "failed to create .sha512 file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.