Skip to content

Commit

Permalink
WIP: mounting hackery in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Aug 28, 2020
1 parent bcbab81 commit 87a5fd8
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 33 deletions.
31 changes: 24 additions & 7 deletions internal/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compose

import (
"fmt"
"io"
"os"
"os/exec"

Expand All @@ -12,6 +13,9 @@ import (
type Project struct {
name string
paths []string

stdout io.Writer
stderr io.Writer
}

// NewProject creates a new Docker Compose project given a sequence of Docker Compose configuration files.
Expand All @@ -33,19 +37,32 @@ func NewProject(name string, paths ...string) (*Project, error) {
c := Project{
name,
paths,

os.Stdout,
os.Stderr,
}

return &c, nil
}

// SetStdout redirects the docker compose project's STDOUT stream to the given destination
func (p *Project) SetStdout(stdout io.Writer) {
p.stdout = stdout
}

// SetStderr redirects the docker compose project's STDERR stream to the given destination
func (p *Project) SetStderr(stderr io.Writer) {
p.stderr = stderr
}

// Up brings up a Docker Compose project.
func (p *Project) Up(extraArgs, env []string, services ...string) error {
args := p.baseArgs()
args = append(args, "up")
args = append(args, extraArgs...)
args = append(args, services...)

if err := runDockerComposeCmd(args, env); err != nil {
if err := p.runDockerComposeCmd(args, env); err != nil {
return errors.Wrap(err, "running docker-compose up command failed")
}

Expand All @@ -58,7 +75,7 @@ func (p *Project) Down(extraArgs, env []string) error {
args = append(args, "down")
args = append(args, extraArgs...)

if err := runDockerComposeCmd(args, env); err != nil {
if err := p.runDockerComposeCmd(args, env); err != nil {
return errors.Wrap(err, "running docker-compose down command failed")
}

Expand All @@ -72,7 +89,7 @@ func (p *Project) Build(extraArgs, env []string, services ...string) error {
args = append(args, extraArgs...)
args = append(args, services...)

if err := runDockerComposeCmd(args, env); err != nil {
if err := p.runDockerComposeCmd(args, env); err != nil {
return errors.Wrap(err, "running docker-compose build command failed")
}

Expand All @@ -86,7 +103,7 @@ func (p *Project) Pull(extraArgs, env []string, services ...string) error {
args = append(args, extraArgs...)
args = append(args, services...)

if err := runDockerComposeCmd(args, env); err != nil {
if err := p.runDockerComposeCmd(args, env); err != nil {
return errors.Wrap(err, "running docker-compose pull command failed")
}

Expand All @@ -103,11 +120,11 @@ func (p *Project) baseArgs() []string {
return args
}

func runDockerComposeCmd(args, env []string) error {
func (p *Project) runDockerComposeCmd(args, env []string) error {
cmd := exec.Command("docker-compose", args...)
cmd.Env = append(os.Environ(), env...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdout = p.stdout
cmd.Stderr = p.stderr

return cmd.Run()
}
23 changes: 23 additions & 0 deletions internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
elasticPackageDir = ".elastic-package"
stackDir = "stack"
packagesDir = "development"
tempDir = "tmp"
)

const versionFilename = "version"
Expand Down Expand Up @@ -44,6 +45,10 @@ func EnsureInstalled() error {
return errors.Wrap(err, "writing static resources failed")
}

if err := createTempDir(elasticPackagePath); err != nil {
return errors.Wrap(err, "creating temp dir failed")
}

fmt.Fprintln(os.Stderr, "elastic-package has been installed.")
return nil
}
Expand All @@ -66,6 +71,14 @@ func StackPackagesDir() (string, error) {
return filepath.Join(stackDir, packagesDir), nil
}

func TempDir() (string, error) {
configurationDir, err := configurationDir()
if err != nil {
return "", errors.Wrap(err, "locating configuration directory failed")
}
return filepath.Join(configurationDir, tempDir), nil
}

func configurationDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
Expand Down Expand Up @@ -127,3 +140,13 @@ func writeStaticResource(err error, path, content string) error {
}
return nil
}

func createTempDir(elasticPackagePath string) error {
tempPath := filepath.Join(elasticPackagePath, tempDir)
err := os.Mkdir(tempPath, 0755)
if err != nil {
return errors.Wrapf(err, "creating directory failed (path: %s)", tempPath)
}

return nil
}
11 changes: 6 additions & 5 deletions internal/stack/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"github.com/elastic/elastic-package/internal/install"
)

const dockerComposeProjectName = "elastic-package-stack"
// DockerComposeProjectName is the name of the docker compose project for spinning up the Elastic Stack.
const DockerComposeProjectName = "elastic-package-stack"

// BootUp method boots up the testing stack.
func BootUp(daemonMode bool, stackVersion string) error {
Expand Down Expand Up @@ -80,7 +81,7 @@ func dockerComposeBuild(stackVersion string) error {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(dockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}
Expand All @@ -99,7 +100,7 @@ func dockerComposePull(stackVersion string) error {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(dockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}
Expand All @@ -118,7 +119,7 @@ func dockerComposeUp(daemonMode bool, stackVersion string) error {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(dockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}
Expand All @@ -142,7 +143,7 @@ func dockerComposeDown() error {
return errors.Wrap(err, "locating stack directory failed")
}

c, err := compose.NewProject(dockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
c, err := compose.NewProject(DockerComposeProjectName, filepath.Join(stackDir, "snapshot.yml"))
if err != nil {
return errors.Wrap(err, "could not create docker compose project")
}
Expand Down
101 changes: 81 additions & 20 deletions internal/testrunner/runners/system/servicedeployer/compose.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package servicedeployer

import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/pkg/errors"

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

// DockerComposeRunner knows how to setup and teardown a service defined via
// a docker-compose.yml file.
type DockerComposeRunner struct {
ymlPath string
network string
project string

stdout io.WriteCloser
stderr io.WriteCloser
}

// NewDockerComposeRunner returns a new instance of a DockerComposeRunner.
Expand All @@ -22,30 +33,80 @@ func NewDockerComposeRunner(ymlPath string) (*DockerComposeRunner, error) {
// SetUp sets up the service and returns any relevant information.
func (r *DockerComposeRunner) SetUp(ctxt common.MapStr) (common.MapStr, error) {
logger.Infof("setting up service using docker compose runner")
//v, err := ctxt.GetValue("docker.compose.network")
//if err != nil {
// return ctxt, errors.Wrap(err, "could not determine docker compose network to join")
//}
//
//network, ok := v.(string)
//if !ok {
// return ctxt, fmt.Errorf("expected docker compose network name to be a string, got: %v", v)
//}
//r.network = network

// TODO: Write stdout and stderr streams to local filesystem files
// TODO: Return locations of stdout and stderr local files
newCtxt := ctxt
newCtxt.Put("stdoutFilePath", "/tmp/foo/bar/TODO-stdout")
newCtxt.Put("stderrFilePath", "/tmp/foo/bar/TODO-stderr")

// TODO
project, err := getStrFromCtxt(ctxt, "docker.compose.project")
if err != nil {
return ctxt, errors.Wrap(err, "could not get docker compose project")
}
r.project = project

c, err := compose.NewProject(project, r.ymlPath)
if err != nil {
return ctxt, errors.Wrap(err, "could not create docker compose project for service")
}

tempDirPath, err := getStrFromCtxt(ctxt, "tempdir")
if err != nil {
return ctxt, errors.Wrap(err, "could not get temporary folder path")
}

outFilePath := filepath.Join(tempDirPath, "stdout")
outFile, err := os.Create(outFilePath)
if err != nil {
return ctxt, errors.Wrap(err, "could not create STDOUT file")
}
r.stdout = outFile
c.SetStdout(r.stdout)
ctxt.Put("Service.STDOUT", outFilePath)

errFilePath := filepath.Join(tempDirPath, "stderr")
errFile, err := os.Create(errFilePath)
if err != nil {
return ctxt, errors.Wrap(err, "could not create STDERR file")
}
r.stderr = errFile
c.SetStderr(r.stderr)
ctxt.Put("Service.STDERR", errFilePath)

if err := c.Up(nil, nil); err != nil {
return ctxt, errors.Wrap(err, "could not boot up service using docker compose")
}

return ctxt, nil
}

// TearDown tears down the service.
func (r *DockerComposeRunner) TearDown(ctxt common.MapStr) error {
logger.Infof("tearing down service using docker compose runner")
// TODO
c, err := compose.NewProject(r.project, r.ymlPath)
if err != nil {
return errors.Wrap(err, "could not create docker compose project for service")
}

if err := c.Down(nil, nil); err != nil {
return errors.Wrap(err, "could not shut down service using docker compose")
}

if err := r.stderr.Close(); err != nil {
return errors.Wrap(err, "could not close STDERR file")
}

if err := r.stdout.Close(); err != nil {
return errors.Wrap(err, "could not close STDOUT file")
}

return nil
}

func getStrFromCtxt(ctxt common.MapStr, key string) (string, error) {
v, err := ctxt.GetValue(key)
if err != nil {
return "", errors.Wrapf(err, "could not get key %s from context", key)
}

val, ok := v.(string)
if !ok {
return "", fmt.Errorf("expected value for key %s be a string, got: %v", key, v)
}

return val, nil
}
12 changes: 11 additions & 1 deletion internal/testrunner/runners/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/common"
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/kibana/ingestmanager"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/stack"
"github.com/elastic/elastic-package/internal/testrunner"
"github.com/elastic/elastic-package/internal/testrunner/runners/system/servicedeployer"
)
Expand Down Expand Up @@ -84,7 +86,15 @@ func (r *runner) run() error {
return errors.Wrap(err, "could not create service runner")
}

tempDir, err := install.TempDir()
if err != nil {
return errors.Wrap(err, "could not get temporary folder")
}

ctxt := common.MapStr{}
ctxt.Put("docker.compose.project", stack.DockerComposeProjectName)
ctxt.Put("tempdir", tempDir)

ctxt, err = serviceRunner.SetUp(ctxt)
defer func() {
logger.Info("tearing down service...")
Expand All @@ -101,7 +111,7 @@ func (r *runner) run() error {
// to be deployed "close to" the service? So for docker-compose based services,
// we could use the Agent Docker image, for TF-based services, we use TF to
// deploy the agent "near" the service, etc.?
// TODO: mount ctxt.Get("stdoutFilePath") and ctxt.Get("stderrFilePath")
// TODO: mount tempdir

// Step 3. Configure package (single data stream) via Ingest Manager APIs.
im, err := ingestmanager.NewClient(r.stackSettings.kibana.host, r.stackSettings.elasticsearch.username, r.stackSettings.elasticsearch.password)
Expand Down

0 comments on commit 87a5fd8

Please sign in to comment.