forked from elastic/package-spec
-
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.
Manage the local cluster (elastic#18)
* Create stub for CLI * Run make check * Add README file * Fix: README * Fix make check * Stub for manage cluster * Install static resources * Remove empty line * Shellinit * Extract docker-compose config for volume * Use configDir * Write .env file * Boot up and tear down the cluster * Fix: write empty env file * Fix: go mod vendor
- Loading branch information
Showing
42 changed files
with
11,179 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,58 @@ | ||
package main | ||
|
||
import "github.com/spf13/cobra" | ||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/elastic/elastic-package/internal/cluster" | ||
) | ||
|
||
func setupClusterCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "cluster", | ||
Short: "Manage the testing environment", | ||
Long: "Use cluster command to boot up and take down the local testing cluster.", | ||
upCommand := &cobra.Command{ | ||
Use: "up", | ||
Short: "Boot up the testing cluster", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := cluster.BootUp() | ||
if err != nil { | ||
return errors.Wrap(err, "booting up the cluster failed") | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
downCommand := &cobra.Command{ | ||
Use: "down", | ||
Short: "Take down the testing cluster", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := cluster.TearDown() | ||
if err != nil { | ||
return errors.Wrap(err, "tearing down the cluster failed") | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
shellInitCommand := &cobra.Command{ | ||
Use: "shellinit", | ||
Short: "Initiate environment variables", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
shell, err := cluster.ShellInit() | ||
if err != nil { | ||
return errors.Wrap(err, "shellinit failed") | ||
} | ||
cmd.Println(shell) | ||
return nil | ||
}, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "cluster", | ||
Short: "Manage the testing environment", | ||
Long: "Use cluster command to boot up and take down the local testing cluster.", | ||
} | ||
cmd.AddCommand( | ||
upCommand, | ||
downCommand, | ||
shellInitCommand) | ||
return cmd | ||
} |
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
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
Empty 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/elastic-package/internal/install" | ||
) | ||
|
||
const envFile = ".env" | ||
|
||
func BootUp() error { | ||
buildPublicPath, found, err := findBuildPublicDirectory() | ||
if err != nil { | ||
return errors.Wrap(err, "finding build packages directory failed") | ||
} | ||
|
||
var envFileContent string | ||
if found { | ||
fmt.Printf("Custom build/public directory found: %s\n", buildPublicPath) | ||
envFileContent = fmt.Sprintf("PACKAGES_PATH=%s\n", buildPublicPath) | ||
} | ||
err = writeEnvFile(buildPublicPath, envFileContent) | ||
if err != nil { | ||
return errors.Wrapf(err, "writing .env file failed (packagesPath: %s)", buildPublicPath) | ||
} | ||
|
||
err = dockerComposeUp(found) | ||
if err != nil { | ||
return errors.Wrap(err, "running docker-compose failed") | ||
} | ||
return nil | ||
} | ||
|
||
func findBuildPublicDirectory() (string, bool, error) { | ||
workDir, err := os.Getwd() | ||
if err != nil { | ||
return "", false, errors.Wrap(err, "locating working directory failed") | ||
} | ||
|
||
dir := workDir | ||
for dir != "." { | ||
path := filepath.Join(dir, "build", "public") | ||
fileInfo, err := os.Stat(path) | ||
if err == nil && fileInfo.IsDir() { | ||
return path, true, nil | ||
} | ||
|
||
if dir == "/" { | ||
break | ||
} | ||
dir = filepath.Dir(dir) | ||
} | ||
return "", false, nil | ||
} | ||
|
||
func writeEnvFile(buildPublicPath, content string) error { | ||
clusterDir, err := install.ClusterDir() | ||
if err != nil { | ||
return errors.Wrap(err, "locating cluster directory failed") | ||
} | ||
envFilePath := filepath.Join(clusterDir, envFile) | ||
err = ioutil.WriteFile(envFilePath, []byte(content), 0644) | ||
if err != nil { | ||
return errors.Wrapf(err, "writing file failed (path: %s)", envFilePath) | ||
} | ||
return nil | ||
} | ||
|
||
func dockerComposeUp(useCustomPackagesPath bool) error { | ||
clusterDir, err := install.ClusterDir() | ||
if err != nil { | ||
return errors.Wrap(err, "locating cluster directory failed") | ||
} | ||
|
||
var args []string | ||
args = append(args, "-f", filepath.Join(clusterDir, "snapshot.yml"), | ||
"-f", filepath.Join(clusterDir, "local.yml")) | ||
|
||
if useCustomPackagesPath { | ||
args = append(args, "-f", filepath.Join(clusterDir, "package-registry-volume.yml")) | ||
} | ||
|
||
args = append(args, "--project-directory", clusterDir, | ||
"up", "-d") | ||
|
||
cmd := exec.Command("docker-compose", args...) | ||
cmd.Stderr = os.Stderr | ||
cmd.Stdout = os.Stdout | ||
err = cmd.Run() | ||
if err != nil { | ||
return errors.Wrap(err, "running command failed") | ||
} | ||
return nil | ||
} |
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,52 @@ | ||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
yaml "gopkg.in/yaml.v2" | ||
|
||
"github.com/elastic/elastic-package/internal/install" | ||
) | ||
|
||
const shellInitFormat = `ELASTIC_PACKAGE_ELASTICSEARCH_HOST=%s | ||
ELASTIC_PACKAGE_ELASTICSEARCH_USERNAME=%s | ||
ELASTIC_PACKAGE_ELASTICSEARCH_PASSWORD=%s | ||
ELASTIC_PACKAGE_KIBANA_HOST=%s` | ||
|
||
type kibanaConfiguration struct { | ||
ElasticsearchHosts []string `yaml:"elasticsearch.hosts"` | ||
ElasticsearchUsername string `yaml:"elasticsearch.username"` | ||
ElasticsearchPassword string `yaml:"elasticsearch.password"` | ||
KibanaHost string `yaml:"xpack.ingestManager.fleet.kibana.host"` | ||
} | ||
|
||
func ShellInit() (string, error) { | ||
clusterDir, err := install.ClusterDir() | ||
if err != nil { | ||
return "", errors.Wrap(err, "location cluster directory failed") | ||
} | ||
|
||
kibanaConfigurationPath := filepath.Join(clusterDir, "kibana.config.yml") | ||
body, err := ioutil.ReadFile(kibanaConfigurationPath) | ||
if err != nil { | ||
return "", errors.Wrap(err, "reading Kibana configuration file failed") | ||
} | ||
|
||
var kibanaCfg kibanaConfiguration | ||
err = yaml.Unmarshal(body, &kibanaCfg) | ||
if err != nil { | ||
return "", errors.Wrap(err, "unmarshalling Kibana configuration failed") | ||
} | ||
|
||
if len(kibanaCfg.ElasticsearchHosts) == 0 { | ||
return "", errors.New("expected at least one Elasticsearch defined") | ||
} | ||
return fmt.Sprintf(shellInitFormat, | ||
kibanaCfg.ElasticsearchHosts[0], | ||
kibanaCfg.ElasticsearchUsername, | ||
kibanaCfg.ElasticsearchPassword, | ||
kibanaCfg.KibanaHost), nil | ||
} |
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,30 @@ | ||
package cluster | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/elastic-package/internal/install" | ||
) | ||
|
||
func TearDown() error { | ||
clusterDir, err := install.ClusterDir() | ||
if err != nil { | ||
return errors.Wrap(err, "locating cluster directory failed") | ||
} | ||
|
||
cmd := exec.Command("docker-compose", | ||
"-f", filepath.Join(clusterDir, "snapshot.yml"), | ||
"--project-directory", clusterDir, | ||
"down") | ||
cmd.Stderr = os.Stderr | ||
cmd.Stdout = os.Stdout | ||
err = cmd.Run() | ||
if err != nil { | ||
return errors.Wrap(err, "running command failed") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.