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

Build docker images when building packages #8898

Merged
merged 30 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c9ab6a4
Include docker image building in package release build
jsoriano Nov 1, 2018
cb332ea
Reorganize code, save docker as artifact, parametrize vendor
jsoriano Nov 3, 2018
4e07f55
Reuse data for templates
jsoriano Nov 3, 2018
fde2cf5
Fix permissions
jsoriano Nov 3, 2018
c42dc23
Better parametrize urls in Dockerfile
jsoriano Nov 4, 2018
ece0946
Use helper function for version
jsoriano Nov 4, 2018
c614a80
Set docker from and user from spec file
jsoriano Nov 4, 2018
c743ba8
Set linux capabilities on heartbeat and packetbeat
jsoriano Nov 4, 2018
691bf7b
Build docker for filebeat
jsoriano Nov 4, 2018
530bc94
Use beat name as default user name in docker
jsoriano Nov 5, 2018
c72feaf
Don't rely on current directory for files chmods
jsoriano Nov 5, 2018
ae4239a
Use proper license variable in Dockerfile
jsoriano Nov 5, 2018
1900d1f
Set capabilities after setting owner and permissions
jsoriano Nov 5, 2018
b15df4e
Add all kibana.generated directories to gitignore
jsoriano Nov 5, 2018
d72283c
Add specific configuration for docker images
jsoriano Nov 5, 2018
068f011
Parametrize base docker repository
jsoriano Nov 5, 2018
a0568cd
Remove unused Env
jsoriano Nov 6, 2018
67daa1a
Precalculate build and beat directories
jsoriano Nov 6, 2018
c8b015d
Add comment
jsoriano Nov 6, 2018
98c8f9b
Reorder commands in dockerfile so less layers are created
jsoriano Nov 6, 2018
7d6a294
Added changelog entry
jsoriano Nov 6, 2018
51ef1f6
Generate docker config for custom beats
jsoriano Nov 6, 2018
a30833c
Merge remote-tracking branch 'origin/master' into mage-package-docker
jsoriano Nov 6, 2018
e5ca02c
Add commit sha to docker labels
jsoriano Nov 6, 2018
e449166
Add some tests for docker packaging
jsoriano Nov 6, 2018
85ef192
Add expose_ports extra var to expose ports in dockers
jsoriano Nov 8, 2018
8e6a323
Set repository only for elastic dockers
jsoriano Nov 8, 2018
2b4b1af
Merge remote-tracking branch 'origin/master' into mage-package-docker
jsoriano Nov 13, 2018
db3987d
Revert "Add all kibana.generated directories to gitignore"
jsoriano Nov 13, 2018
39f1bc2
Fail if docker entry point is not an absolute path
jsoriano Nov 13, 2018
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
Prev Previous commit
Next Next commit
Reorganize code, save docker as artifact, parametrize vendor
  • Loading branch information
jsoriano committed Nov 5, 2018
commit cb332eac88e61e0cb6b144c52252461073f7699b
97 changes: 63 additions & 34 deletions dev-tools/mage/dockerbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ import (
"github.com/pkg/errors"
)

type dockerTemplateData struct {
BeatName string
Version string
License string
Env map[string]string
LinuxCapabilities string
User string
}

type dockerBuilder struct {
PackageSpec
}
Expand All @@ -48,65 +39,103 @@ func newDockerBuilder(spec PackageSpec) (*dockerBuilder, error) {
}

func (b *dockerBuilder) Build() error {
buildDir := filepath.Join(b.packageDir, "docker-build")
beatDir := filepath.Join(buildDir, "beat")
// TODO: defer removal of buildDir
buildDir := b.buildDir()
if err := os.RemoveAll(buildDir); err != nil {
return errors.Wrapf(err, "failed to clean existing build directory %s", buildDir)
}

elasticBeatsDir, err := ElasticBeatsDir()
if err != nil {
if err := b.copyFiles(); err != nil {
return err
}
templatesDir := filepath.Join(elasticBeatsDir, "dev-tools/packaging/templates/docker")

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) buildDir() string {
return filepath.Join(b.packageDir, "docker-build")
}

func (b *dockerBuilder) beatDir() string {
return filepath.Join(b.buildDir(), "beat")
}

func (b *dockerBuilder) copyFiles() error {
beatDir := b.beatDir()
for _, f := range b.Files {
target := filepath.Join(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
}

/* TODO:
data := dockerTemplateData{
BeatName: b.Name,
Version: b.Version,
License: b.License,
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{}{
"From": "centos:7", // TODO: Parametrize this
"BeatName": b.Name,
"Version": b.Version,
"Vendor": b.Vendor,
"License": b.License,
"Env": map[string]string{},
"LinuxCapabilities": "",
"User": b.Name,
}

// TODO: Expand templates from packages.yml?
err = filepath.Walk(templatesDir, func(path string, info os.FileInfo, err error) error {
/* TODO: Why there is an error here?
if err != nil {
return err
}
*/
buildDir := b.buildDir()
return filepath.Walk(templatesDir, func(path string, info os.FileInfo, _ error) error {
if !info.IsDir() {
target := strings.TrimSuffix(
filepath.Join(buildDir, filepath.Base(path)),
".tmpl",
)

err = expandFile(path, target, data)
if err != nil {
return errors.Wrapf(err, "expanding template '%s' to '%s'", path, target)
}
}
return nil
})
if err != nil {
}

func (b *dockerBuilder) dockerBuild() (string, error) {
repository := "docker.elastic.co/beats" // TODO: Parametrize this
tag := fmt.Sprintf("%s:%s", filepath.Join(repository, b.Name), b.Version) // TODO: What about OSS?
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")
if err != nil {
return err
}
outputFile = filepath.Join(distributionsDir, outputTar)
}
if err := sh.Run("docker", "save", "-o", outputFile, tag); err != nil {
return err
}

// TODO: Tag container on build
repository := "docker.elastic.co/beats" // TODO: Parametrize this
tag := fmt.Sprintf("%s/%s:%s", repository, b.Name, b.Version)
return sh.RunCmd("docker", "build", "-t", tag, buildDir)()
return errors.Wrap(CreateSHA512File(outputFile), "failed to create .sha512 file")
}
2 changes: 1 addition & 1 deletion dev-tools/packaging/templates/docker/Dockerfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM {{ .From }}

LABEL \
org.label-schema.schema-version="1.0" \
org.label-schema.vendor="Elastic" \
org.label-schema.vendor="{{ .Vendor }}" \
org.label-schema.name="{{ .BeatName }}" \
org.label-schema.version="{{ .Version }}" \
org.label-schema.url="https://www.elastic.co/products/beats/{{ .BeatName }}" \
Expand Down