Skip to content

Commit

Permalink
Update base docker image for EPR (elastic#125)
Browse files Browse the repository at this point in the history
* WIP

* Use docker command to fetch base image for EPR

* Fix: sort fields

* Remove TODO file

* Fix: comment
  • Loading branch information
mtojek authored Oct 6, 2020
1 parent 0bb695c commit 998d7e8
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 155 deletions.
4 changes: 2 additions & 2 deletions cmd/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func setupStackCommand() *cobra.Command {
return cobraext.FlagParsingError(err, cobraext.StackVersionFlagName)
}

err = stack.BootUp(stack.BootOptions{
err = stack.BootUp(stack.Options{
DaemonMode: daemonMode,
StackVersion: stackVersion,
Services: services,
Expand Down Expand Up @@ -93,7 +93,7 @@ func setupStackCommand() *cobra.Command {
return cobraext.FlagParsingError(err, cobraext.StackVersionFlagName)
}

err = stack.Update(stack.BootOptions{
err = stack.Update(stack.Options{
StackVersion: stackVersion,
})
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package docker

import (
"os"
"os/exec"

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/logger"
)

// Pull downloads the latest available revision of the image.
func Pull(image string) error {
cmd := exec.Command("docker", "pull", image)

if logger.IsDebugMode() {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}

logger.Debugf("running command: %s", cmd)
err := cmd.Run()
if err != nil {
return errors.Wrap(err, "running docker command failed")
}
return nil
}
5 changes: 4 additions & 1 deletion internal/install/static_package_registry_dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package install

const packageRegistryDockerfile = `FROM docker.elastic.co/package-registry/distribution:snapshot
// PackageRegistryBaseImage is the base Docker image of the Elastic Package Registry.
const PackageRegistryBaseImage = "docker.elastic.co/package-registry/distribution:snapshot"

const packageRegistryDockerfile = `FROM ` + PackageRegistryBaseImage + `
COPY ${STACK_DIR}/package-registry.config.yml /package-registry/config.yml
COPY ${STACK_DIR}/development/ /packages/development
Expand Down
137 changes: 1 addition & 136 deletions internal/stack/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,20 @@ package stack

import (
"fmt"
"path/filepath"

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/builder"
"github.com/elastic/elastic-package/internal/compose"
"github.com/elastic/elastic-package/internal/files"
"github.com/elastic/elastic-package/internal/install"
)

// BootOptions defines available image booting options.
type BootOptions struct {
DaemonMode bool
StackVersion string

Services []string
}

// DockerComposeProjectName is the name of the Docker Compose project used to boot up
// Elastic Stack containers.
const DockerComposeProjectName = "elastic-package-stack"

// BootUp method boots up the testing stack.
func BootUp(options BootOptions) error {
func BootUp(options Options) error {
buildPackagesPath, found, err := builder.FindBuildPackagesDirectory()
if err != nil {
return errors.Wrap(err, "finding build packages directory failed")
Expand Down Expand Up @@ -78,128 +68,3 @@ func TearDown() error {
}
return nil
}

// Update pulls down the most recent versions of the Docker images
func Update(options BootOptions) error {
err := dockerComposePull(options)
if err != nil {
return errors.Wrap(err, "updating docker images failed")
}
return nil
}

func dockerComposeBuild(options BootOptions) error {
stackDir, err := install.StackDir()
if err != nil {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}

opts := compose.CommandOptions{
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
Services: withIsReadyServices(withDependentServices(options.Services)),
}

if err := c.Build(opts); err != nil {
return errors.Wrap(err, "running command failed")
}
return nil
}

func dockerComposePull(options BootOptions) error {
stackDir, err := install.StackDir()
if err != nil {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}

opts := compose.CommandOptions{
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
Services: withIsReadyServices(withDependentServices(options.Services)),
}

if err := c.Pull(opts); err != nil {
return errors.Wrap(err, "running command failed")
}
return nil
}

func dockerComposeUp(options BootOptions) error {
stackDir, err := install.StackDir()
if err != nil {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}

var args []string
if options.DaemonMode {
args = append(args, "-d")
}

opts := compose.CommandOptions{
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
ExtraArgs: args,
Services: withIsReadyServices(withDependentServices(options.Services)),
}

if err := c.Up(opts); err != nil {
return errors.Wrap(err, "running command failed")
}
return nil
}

func dockerComposeDown() error {
stackDir, err := install.StackDir()
if err != nil {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}

opts := compose.CommandOptions{
// We set the STACK_VERSION env var here to avoid showing a warning to the user about
// it not being set.
Env: []string{fmt.Sprintf("STACK_VERSION=%s", DefaultVersion)},
}

if err := c.Down(opts); err != nil {
return errors.Wrap(err, "running command failed")
}
return nil
}

func withDependentServices(services []string) []string {
for _, aService := range services {
if aService == "elastic-agent" {
return []string{} // elastic-agent service requires to load all other services
}
}
return services
}

func withIsReadyServices(services []string) []string {
if len(services) == 0 {
return services // load all defined services
}

var allServices []string
for _, aService := range services {
allServices = append(allServices, aService, fmt.Sprintf("%s_is_ready", aService))
}
return allServices
}
133 changes: 133 additions & 0 deletions internal/stack/compose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package stack

import (
"fmt"
"path/filepath"

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/compose"
"github.com/elastic/elastic-package/internal/install"
)

const snapshotDefinitionFile = "snapshot.yml"

func dockerComposeBuild(options Options) error {
stackDir, err := install.StackDir()
if err != nil {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, snapshotDefinitionFile))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}

opts := compose.CommandOptions{
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
Services: withIsReadyServices(withDependentServices(options.Services)),
}

if err := c.Build(opts); err != nil {
return errors.Wrap(err, "running command failed")
}
return nil
}

func dockerComposePull(options Options) error {
stackDir, err := install.StackDir()
if err != nil {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, snapshotDefinitionFile))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}

opts := compose.CommandOptions{
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
Services: withIsReadyServices(withDependentServices(options.Services)),
}

if err := c.Pull(opts); err != nil {
return errors.Wrap(err, "running command failed")
}
return nil
}

func dockerComposeUp(options Options) error {
stackDir, err := install.StackDir()
if err != nil {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, snapshotDefinitionFile))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}

var args []string
if options.DaemonMode {
args = append(args, "-d")
}

opts := compose.CommandOptions{
Env: []string{fmt.Sprintf("STACK_VERSION=%s", options.StackVersion)},
ExtraArgs: args,
Services: withIsReadyServices(withDependentServices(options.Services)),
}

if err := c.Up(opts); err != nil {
return errors.Wrap(err, "running command failed")
}
return nil
}

func dockerComposeDown() error {
stackDir, err := install.StackDir()
if err != nil {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, snapshotDefinitionFile))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}

opts := compose.CommandOptions{
// We set the STACK_VERSION env var here to avoid showing a warning to the user about
// it not being set.
Env: []string{fmt.Sprintf("STACK_VERSION=%s", DefaultVersion)},
}

if err := c.Down(opts); err != nil {
return errors.Wrap(err, "running command failed")
}
return nil
}

func withDependentServices(services []string) []string {
for _, aService := range services {
if aService == "elastic-agent" {
return []string{} // elastic-agent service requires to load all other services
}
}
return services
}

func withIsReadyServices(services []string) []string {
if len(services) == 0 {
return services // load all defined services
}

var allServices []string
for _, aService := range services {
allServices = append(allServices, aService, fmt.Sprintf("%s_is_ready", aService))
}
return allServices
}
13 changes: 13 additions & 0 deletions internal/stack/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package stack

// Options defines available image booting options.
type Options struct {
DaemonMode bool
StackVersion string

Services []string
}
26 changes: 26 additions & 0 deletions internal/stack/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package stack

import (
"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/docker"
"github.com/elastic/elastic-package/internal/install"
)

// Update pulls down the most recent versions of the Docker images.
func Update(options Options) error {
err := docker.Pull(install.PackageRegistryBaseImage)
if err != nil {
return errors.Wrap(err, "pulling package-registry docker image failed")
}

err = dockerComposePull(options)
if err != nil {
return errors.Wrap(err, "updating docker images failed")
}
return nil
}
Loading

0 comments on commit 998d7e8

Please sign in to comment.